1   /*
2    * Copyright 2010 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  
21  package org.apache.hadoop.hbase.rest;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.IOException;
25  
26  import javax.xml.bind.JAXBContext;
27  import javax.xml.bind.JAXBException;
28  
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.MediumTests;
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[] ROOT_REGION_NAME = Bytes.toBytes("-ROOT-,,0");
47    private static final byte[] META_REGION_NAME = Bytes.toBytes(".META.,,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);
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 foundRoot = false, 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(), ROOT_REGION_NAME)) {
70            foundRoot = true;
71          } else if (Bytes.equals(region.getName(), META_REGION_NAME)) {
72            foundMeta = true;
73          }
74        }
75      }
76      assertTrue(foundRoot);
77      assertTrue(foundMeta);
78    }
79  
80    @BeforeClass
81    public static void setUpBeforeClass() throws Exception {
82      TEST_UTIL.startMiniCluster();
83      REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
84      client = new Client(new Cluster().add("localhost", 
85        REST_TEST_UTIL.getServletPort()));
86      context = JAXBContext.newInstance(StorageClusterStatusModel.class);
87    }
88  
89    @AfterClass
90    public static void tearDownAfterClass() throws Exception {
91      REST_TEST_UTIL.shutdownServletContainer();
92      TEST_UTIL.shutdownMiniCluster();
93    }
94  
95    @Test
96    public void testGetClusterStatusXML() throws IOException, JAXBException {
97      Response response = client.get("/status/cluster", Constants.MIMETYPE_XML);
98      assertEquals(response.getCode(), 200);
99      assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
100     StorageClusterStatusModel model = (StorageClusterStatusModel)
101       context.createUnmarshaller().unmarshal(
102         new ByteArrayInputStream(response.getBody()));
103     validate(model);
104   }
105 
106   @Test
107   public void testGetClusterStatusPB() throws IOException {
108     Response response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF);
109     assertEquals(response.getCode(), 200);
110     assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
111     StorageClusterStatusModel model = new StorageClusterStatusModel();
112     model.getObjectFromMessage(response.getBody());
113     validate(model);
114     response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF_IETF);
115     assertEquals(response.getCode(), 200);
116     assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
117     model = new StorageClusterStatusModel();
118     model.getObjectFromMessage(response.getBody());
119     validate(model);
120   }
121 
122   @org.junit.Rule
123   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
124     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
125 }