View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.IOException;
24  
25  import javax.xml.bind.JAXBContext;
26  import javax.xml.bind.JAXBException;
27  
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.testclassification.MediumTests;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.Waiter;
32  import org.apache.hadoop.hbase.rest.client.Client;
33  import org.apache.hadoop.hbase.rest.client.Cluster;
34  import org.apache.hadoop.hbase.rest.client.Response;
35  import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
36  import org.apache.hadoop.hbase.util.Bytes;
37  
38  import static org.junit.Assert.*;
39  
40  import org.junit.AfterClass;
41  import org.junit.BeforeClass;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  
45  @Category(MediumTests.class)
46  public class TestStatusResource {
47    private static final byte[] META_REGION_NAME = Bytes.toBytes(TableName.META_TABLE_NAME+",,1");
48  
49    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
50    private static final HBaseRESTTestingUtility REST_TEST_UTIL = 
51      new HBaseRESTTestingUtility();
52    private static Client client;
53    private static JAXBContext context;
54    
55    private static void validate(StorageClusterStatusModel model) {
56      assertNotNull(model);
57      assertTrue(model.getRegions() + ">= 1", model.getRegions() >= 1);
58      assertTrue(model.getRequests() >= 0);
59      assertTrue(model.getAverageLoad() >= 0.0);
60      assertNotNull(model.getLiveNodes());
61      assertNotNull(model.getDeadNodes());
62      assertFalse(model.getLiveNodes().isEmpty());
63      boolean foundMeta = false;
64      for (StorageClusterStatusModel.Node node: model.getLiveNodes()) {
65        assertNotNull(node.getName());
66        assertTrue(node.getStartCode() > 0L);
67        assertTrue(node.getRequests() >= 0);
68        for (StorageClusterStatusModel.Node.Region region: node.getRegions()) {
69          if (Bytes.equals(region.getName(), META_REGION_NAME)) {
70            foundMeta = true;
71          }
72        }
73      }
74      assertTrue(foundMeta);
75    }
76  
77    @BeforeClass
78    public static void setUpBeforeClass() throws Exception {
79      TEST_UTIL.startMiniCluster();
80      REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
81      client = new Client(new Cluster().add("localhost", 
82        REST_TEST_UTIL.getServletPort()));
83      context = JAXBContext.newInstance(StorageClusterStatusModel.class);
84    }
85  
86    @AfterClass
87    public static void tearDownAfterClass() throws Exception {
88      REST_TEST_UTIL.shutdownServletContainer();
89      TEST_UTIL.shutdownMiniCluster();
90    }
91  
92    @Test
93    public void testGetClusterStatusXML() throws IOException, JAXBException {
94      Response response = client.get("/status/cluster", Constants.MIMETYPE_XML);
95      assertEquals(response.getCode(), 200);
96      assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
97      StorageClusterStatusModel model = (StorageClusterStatusModel)
98        context.createUnmarshaller().unmarshal(
99          new ByteArrayInputStream(response.getBody()));
100     validate(model);
101   }
102 
103   @Test
104   public void testGetClusterStatusPB() throws IOException {
105     Response response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF);
106     assertEquals(response.getCode(), 200);
107     assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
108     StorageClusterStatusModel model = new StorageClusterStatusModel();
109     model.getObjectFromMessage(response.getBody());
110     validate(model);
111     response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF_IETF);
112     assertEquals(response.getCode(), 200);
113     assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
114     model = new StorageClusterStatusModel();
115     model.getObjectFromMessage(response.getBody());
116     validate(model);
117   }
118 }