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.rest.client.Client;
32  import org.apache.hadoop.hbase.rest.client.Cluster;
33  import org.apache.hadoop.hbase.rest.client.Response;
34  import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
35  import org.apache.hadoop.hbase.util.Bytes;
36  
37  import static org.junit.Assert.*;
38  
39  import org.junit.AfterClass;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  @Category(MediumTests.class)
45  public class TestStatusResource {
46    private static final byte[] META_REGION_NAME = Bytes.toBytes(TableName.META_TABLE_NAME+",,1");
47  
48    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
49    private static final HBaseRESTTestingUtility REST_TEST_UTIL = 
50      new HBaseRESTTestingUtility();
51    private static Client client;
52    private static JAXBContext context;
53    
54    private static void validate(StorageClusterStatusModel model) {
55      assertNotNull(model);
56      assertTrue(model.getRegions() + ">= 1", model.getRegions() >= 1);
57      assertTrue(model.getRequests() >= 0);
58      assertTrue(model.getAverageLoad() >= 0.0);
59      assertNotNull(model.getLiveNodes());
60      assertNotNull(model.getDeadNodes());
61      assertFalse(model.getLiveNodes().isEmpty());
62      boolean foundMeta = false;
63      for (StorageClusterStatusModel.Node node: model.getLiveNodes()) {
64        assertNotNull(node.getName());
65        assertTrue(node.getStartCode() > 0L);
66        assertTrue(node.getRequests() >= 0);
67        for (StorageClusterStatusModel.Node.Region region: node.getRegions()) {
68          if (Bytes.equals(region.getName(), META_REGION_NAME)) {
69            foundMeta = true;
70          }
71        }
72      }
73      assertTrue(foundMeta);
74    }
75  
76    @BeforeClass
77    public static void setUpBeforeClass() throws Exception {
78      TEST_UTIL.startMiniCluster();
79      REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
80      client = new Client(new Cluster().add("localhost", 
81        REST_TEST_UTIL.getServletPort()));
82      context = JAXBContext.newInstance(StorageClusterStatusModel.class);
83    }
84  
85    @AfterClass
86    public static void tearDownAfterClass() throws Exception {
87      REST_TEST_UTIL.shutdownServletContainer();
88      TEST_UTIL.shutdownMiniCluster();
89    }
90  
91    @Test
92    public void testGetClusterStatusXML() throws IOException, JAXBException {
93      Response response = client.get("/status/cluster", Constants.MIMETYPE_XML);
94      assertEquals(response.getCode(), 200);
95      assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
96      StorageClusterStatusModel model = (StorageClusterStatusModel)
97        context.createUnmarshaller().unmarshal(
98          new ByteArrayInputStream(response.getBody()));
99      validate(model);
100   }
101 
102   @Test
103   public void testGetClusterStatusPB() throws IOException {
104     Response response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF);
105     assertEquals(response.getCode(), 200);
106     assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
107     StorageClusterStatusModel model = new StorageClusterStatusModel();
108     model.getObjectFromMessage(response.getBody());
109     validate(model);
110     response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF_IETF);
111     assertEquals(response.getCode(), 200);
112     assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
113     model = new StorageClusterStatusModel();
114     model.getObjectFromMessage(response.getBody());
115     validate(model);
116   }
117 }