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.rest.client.Client;
31  import org.apache.hadoop.hbase.rest.client.Cluster;
32  import org.apache.hadoop.hbase.rest.client.Response;
33  import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
34  import org.apache.hadoop.hbase.util.Bytes;
35  
36  import static org.junit.Assert.*;
37  import org.junit.AfterClass;
38  import org.junit.BeforeClass;
39  import org.junit.Test;
40  
41  public class TestStatusResource {
42    private static final byte[] ROOT_REGION_NAME = Bytes.toBytes("-ROOT-,,0");
43    private static final byte[] META_REGION_NAME = Bytes.toBytes(".META.,,1");
44  
45    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46    private static final HBaseRESTTestingUtility REST_TEST_UTIL = 
47      new HBaseRESTTestingUtility();
48    private static Client client;
49    private static JAXBContext context;
50    
51    private static void validate(StorageClusterStatusModel model) {
52      assertNotNull(model);
53      assertTrue(model.getRegions() >= 1);
54      assertTrue(model.getRequests() >= 0);
55      assertTrue(model.getAverageLoad() >= 0.0);
56      assertNotNull(model.getLiveNodes());
57      assertNotNull(model.getDeadNodes());
58      assertFalse(model.getLiveNodes().isEmpty());
59      boolean foundRoot = false, foundMeta = false;
60      for (StorageClusterStatusModel.Node node: model.getLiveNodes()) {
61        assertNotNull(node.getName());
62        assertTrue(node.getStartCode() > 0L);
63        assertTrue(node.getRequests() >= 0);
64        for (StorageClusterStatusModel.Node.Region region: node.getRegions()) {
65          if (Bytes.equals(region.getName(), ROOT_REGION_NAME)) {
66            foundRoot = true;
67          } else if (Bytes.equals(region.getName(), META_REGION_NAME)) {
68            foundMeta = true;
69          }
70        }
71      }
72      assertTrue(foundRoot);
73      assertTrue(foundMeta);
74    }
75  
76    @BeforeClass
77    public static void setUpBeforeClass() throws Exception {
78      TEST_UTIL.startMiniCluster(3);
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      StorageClusterStatusModel model = (StorageClusterStatusModel)
96        context.createUnmarshaller().unmarshal(
97          new ByteArrayInputStream(response.getBody()));
98      validate(model);
99    }
100 
101   @Test
102   public void testGetClusterStatusPB() throws IOException {
103     Response response = client.get("/status/cluster", 
104       Constants.MIMETYPE_PROTOBUF);
105     assertEquals(response.getCode(), 200);
106     StorageClusterStatusModel model = new StorageClusterStatusModel();
107     model.getObjectFromMessage(response.getBody());
108     validate(model);
109   }
110 }