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.rest.client.Client;
30  import org.apache.hadoop.hbase.rest.client.Cluster;
31  import org.apache.hadoop.hbase.rest.client.Response;
32  import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
33  import org.apache.hadoop.hbase.util.Bytes;
34  
35  public class TestStatusResource extends HBaseRESTClusterTestBase {
36    static final byte[] ROOT_REGION_NAME = Bytes.toBytes("-ROOT-,,0");
37    static final byte[] META_REGION_NAME = Bytes.toBytes(".META.,,1");
38  
39    Client client;
40    JAXBContext context;
41    
42    @Override
43    protected void setUp() throws Exception {
44      super.setUp();
45      context = JAXBContext.newInstance(
46        StorageClusterStatusModel.class);
47      client = new Client(new Cluster().add("localhost", testServletPort));
48    }
49  
50    @Override
51    protected void tearDown() throws Exception {
52      client.shutdown();
53      super.tearDown();
54    }
55  
56    void validate(StorageClusterStatusModel model) {
57      assertNotNull(model);
58      assertTrue(model.getRegions() >= 2);
59      assertTrue(model.getRequests() >= 0);
60      // assumes minicluster with two regionservers
61      assertTrue(model.getAverageLoad() >= 1.0);
62      assertNotNull(model.getLiveNodes());
63      assertNotNull(model.getDeadNodes());
64      assertFalse(model.getLiveNodes().isEmpty());
65      boolean foundRoot = false, foundMeta = false;
66      for (StorageClusterStatusModel.Node node: model.getLiveNodes()) {
67        assertNotNull(node.getName());
68        assertTrue(node.getStartCode() > 0L);
69        assertTrue(node.getRequests() >= 0);
70        assertFalse(node.getRegions().isEmpty());
71        for (StorageClusterStatusModel.Node.Region region: node.getRegions()) {
72          if (Bytes.equals(region.getName(), ROOT_REGION_NAME)) {
73            foundRoot = true;
74          } else if (Bytes.equals(region.getName(), META_REGION_NAME)) {
75            foundMeta = true;
76          }
77        }
78      }
79      assertTrue(foundRoot);
80      assertTrue(foundMeta);
81    }
82  
83    void doTestGetClusterStatusXML() throws IOException, JAXBException {
84      Response response = client.get("/status/cluster", MIMETYPE_XML);
85      assertEquals(response.getCode(), 200);
86      StorageClusterStatusModel model = (StorageClusterStatusModel)
87        context.createUnmarshaller().unmarshal(
88          new ByteArrayInputStream(response.getBody()));
89      validate(model);
90    }
91    
92    void doTestGetClusterStatusPB() throws IOException {
93      Response response = client.get("/status/cluster", MIMETYPE_PROTOBUF);
94      assertEquals(response.getCode(), 200);
95      StorageClusterStatusModel model = new StorageClusterStatusModel();
96      model.getObjectFromMessage(response.getBody());
97      validate(model);
98    }
99  
100   public void testStatusResource() throws Exception {
101     doTestGetClusterStatusXML();
102     doTestGetClusterStatusPB();
103   }
104 }