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.model;
22  
23  import java.io.IOException;
24  import java.io.StringReader;
25  import java.io.StringWriter;
26  import java.util.Iterator;
27  
28  import javax.xml.bind.JAXBContext;
29  import javax.xml.bind.JAXBException;
30  
31  import org.apache.hadoop.hbase.util.Base64;
32  import org.apache.hadoop.hbase.util.Bytes;
33  
34  import junit.framework.TestCase;
35  
36  public class TestStorageClusterStatusModel extends TestCase {
37  
38    private static final String AS_XML =
39      "<ClusterStatus requests=\"0\" regions=\"2\" averageLoad=\"1.0\">" +
40      "<DeadNodes/>" + 
41      "<LiveNodes><Node startCode=\"1245219839331\" requests=\"0\"" + 
42        " name=\"test1\" maxHeapSizeMB=\"1024\" heapSizeMB=\"128\">" + 
43          "<Region stores=\"1\" storefiles=\"1\" storefileSizeMB=\"0\"" + 
44          " storefileIndexSizeMB=\"0\" name=\"LVJPT1QtLCww\"" + 
45          " memstoreSizeMB=\"0\"/></Node>" + 
46        "<Node startCode=\"1245239331198\" requests=\"0\" name=\"test2\"" + 
47          " maxHeapSizeMB=\"1024\" heapSizeMB=\"512\">" + 
48          "<Region stores=\"1\" storefiles=\"1\" storefileSizeMB=\"0\"" +
49          " storefileIndexSizeMB=\"0\" name=\"Lk1FVEEuLCwxMjQ2MDAwMDQzNzI0\"" +
50          " memstoreSizeMB=\"0\"/></Node>"+
51      "</LiveNodes></ClusterStatus>";
52  
53    private static final String AS_PB = 
54  "Ci0KBXRlc3QxEOO6i+eeJBgAIIABKIAIMhUKCS1ST09ULSwsMBABGAEgACgAMAAKOQoFdGVzdDIQ"+
55  "/pKx8J4kGAAggAQogAgyIQoVLk1FVEEuLCwxMjQ2MDAwMDQzNzI0EAEYASAAKAAwABgCIAApAAAA"+
56  "AAAA8D8=";
57  
58    private JAXBContext context;
59  
60    public TestStorageClusterStatusModel() throws JAXBException {
61      super();
62      context = JAXBContext.newInstance(StorageClusterStatusModel.class);
63    }
64  
65    private StorageClusterStatusModel buildTestModel() {
66      StorageClusterStatusModel model = new StorageClusterStatusModel();
67      model.setRegions(2);
68      model.setRequests(0);
69      model.setAverageLoad(1.0);
70      model.addLiveNode("test1", 1245219839331L, 128, 1024)
71        .addRegion(Bytes.toBytes("-ROOT-,,0"), 1, 1, 0, 0, 0);
72      model.addLiveNode("test2", 1245239331198L, 512, 1024)
73        .addRegion(Bytes.toBytes(".META.,,1246000043724"),1, 1, 0, 0, 0);
74      return model;
75    }
76  
77    @SuppressWarnings("unused")
78    private String toXML(StorageClusterStatusModel model) throws JAXBException {
79      StringWriter writer = new StringWriter();
80      context.createMarshaller().marshal(model, writer);
81      return writer.toString();
82    }
83  
84    private StorageClusterStatusModel fromXML(String xml) throws JAXBException {
85      return (StorageClusterStatusModel)
86        context.createUnmarshaller().unmarshal(new StringReader(xml));
87    }
88  
89    @SuppressWarnings("unused")
90    private byte[] toPB(StorageClusterStatusModel model) {
91      return model.createProtobufOutput();
92    }
93  
94    private StorageClusterStatusModel fromPB(String pb) throws IOException {
95      return (StorageClusterStatusModel) 
96        new StorageClusterStatusModel().getObjectFromMessage(Base64.decode(AS_PB));
97    }
98  
99    private void checkModel(StorageClusterStatusModel model) {
100     assertEquals(model.getRegions(), 2);
101     assertEquals(model.getRequests(), 0);
102     assertEquals(model.getAverageLoad(), 1.0);
103     Iterator<StorageClusterStatusModel.Node> nodes =
104       model.getLiveNodes().iterator();
105     StorageClusterStatusModel.Node node = nodes.next();
106     assertEquals(node.getName(), "test1");
107     assertEquals(node.getStartCode(), 1245219839331L);
108     assertEquals(node.getHeapSizeMB(), 128);
109     assertEquals(node.getMaxHeapSizeMB(), 1024);
110     Iterator<StorageClusterStatusModel.Node.Region> regions = 
111       node.getRegions().iterator();
112     StorageClusterStatusModel.Node.Region region = regions.next();
113     assertTrue(Bytes.toString(region.getName()).equals("-ROOT-,,0"));
114     assertEquals(region.getStores(), 1);
115     assertEquals(region.getStorefiles(), 1);
116     assertEquals(region.getStorefileSizeMB(), 0);
117     assertEquals(region.getMemstoreSizeMB(), 0);
118     assertEquals(region.getStorefileIndexSizeMB(), 0);
119     assertFalse(regions.hasNext());
120     node = nodes.next();
121     assertEquals(node.getName(), "test2");
122     assertEquals(node.getStartCode(), 1245239331198L);
123     assertEquals(node.getHeapSizeMB(), 512);
124     assertEquals(node.getMaxHeapSizeMB(), 1024);
125     regions = node.getRegions().iterator();
126     region = regions.next();
127     assertEquals(Bytes.toString(region.getName()), ".META.,,1246000043724");
128     assertEquals(region.getStores(), 1);
129     assertEquals(region.getStorefiles(), 1);
130     assertEquals(region.getStorefileSizeMB(), 0);
131     assertEquals(region.getMemstoreSizeMB(), 0);
132     assertEquals(region.getStorefileIndexSizeMB(), 0);
133     assertFalse(regions.hasNext());
134     assertFalse(nodes.hasNext());
135   }
136 
137   public void testBuildModel() throws Exception {
138     checkModel(buildTestModel());
139   }
140 
141   public void testFromXML() throws Exception {
142     checkModel(fromXML(AS_XML));
143   }
144 
145   public void testFromPB() throws Exception {
146     checkModel(fromPB(AS_PB));
147   }
148 }