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