1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import static org.junit.Assert.*;
23  
24  import java.io.IOException;
25  import java.io.StringWriter;
26  import java.util.List;
27  
28  import org.apache.hadoop.hbase.HBaseConfiguration;
29  import org.apache.hadoop.hbase.HRegionInfo;
30  import org.apache.hadoop.hbase.HServerAddress;
31  import org.apache.hadoop.hbase.HServerInfo;
32  import org.apache.hadoop.hbase.HTableDescriptor;
33  import org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetrics;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
36  import org.apache.hbase.tmpl.regionserver.RSStatusTmpl;
37  import org.junit.Before;
38  import org.junit.Test;
39  import org.mockito.Mockito;
40  
41  import com.google.common.collect.Lists;
42  
43  /**
44   * Tests for the region server status page and its template.
45   */
46  public class TestRSStatusServlet {
47    private HRegionServer rs;
48    
49    static final int FAKE_IPC_PORT = 1585;
50    static final int FAKE_WEB_PORT = 1586;
51    
52    @SuppressWarnings("deprecation")
53    private HServerAddress fakeAddress =
54      new HServerAddress("localhost", FAKE_IPC_PORT);
55    @SuppressWarnings("deprecation")
56    private HServerInfo fakeInfo =
57      new HServerInfo(fakeAddress, FAKE_WEB_PORT);
58    private RegionServerMetrics metrics =
59      new RegionServerMetrics();
60    
61    @SuppressWarnings("deprecation")
62    @Before
63    public void setupBasicMocks() throws IOException {
64      rs = Mockito.mock(HRegionServer.class);
65      Mockito.doReturn(HBaseConfiguration.create())
66        .when(rs).getConfiguration();
67      Mockito.doReturn(fakeInfo).when(rs).getHServerInfo();
68      Mockito.doReturn(metrics).when(rs).getMetrics();
69  
70      // Fake ZKW
71      ZooKeeperWatcher zkw = Mockito.mock(ZooKeeperWatcher.class);
72      Mockito.doReturn("fakequorum").when(zkw).getQuorum();
73      Mockito.doReturn(zkw).when(rs).getZooKeeper();
74    }
75    
76    @Test
77    public void testBasic() throws IOException {
78      new RSStatusTmpl().render(new StringWriter(), rs);
79    }
80    
81    @Test
82    public void testWithRegions() throws IOException {
83      HTableDescriptor htd = new HTableDescriptor("mytable");
84      List<HRegionInfo> regions = Lists.newArrayList(
85          new HRegionInfo(htd.getName(), Bytes.toBytes("a"), Bytes.toBytes("d")),
86          new HRegionInfo(htd.getName(), Bytes.toBytes("d"), Bytes.toBytes("z"))
87          );
88      Mockito.doReturn(regions).when(rs).getOnlineRegions();
89      
90      new RSStatusTmpl().render(new StringWriter(), rs);    
91    }
92    
93    
94  }