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  
27  import javax.xml.bind.JAXBContext;
28  import javax.xml.bind.JAXBException;
29  
30  import org.apache.hadoop.hbase.SmallTests;
31  import org.apache.hadoop.hbase.util.Base64;
32  
33  import junit.framework.TestCase;
34  import org.junit.experimental.categories.Category;
35  
36  @Category(SmallTests.class)
37  public class TestVersionModel extends TestCase {
38    private static final String REST_VERSION = "0.0.1";
39    private static final String OS_VERSION = 
40      "Linux 2.6.18-128.1.6.el5.centos.plusxen amd64";
41    private static final String JVM_VERSION =
42      "Sun Microsystems Inc. 1.6.0_13-11.3-b02";
43    private static final String JETTY_VERSION = "6.1.14";
44    private static final String JERSEY_VERSION = "1.1.0-ea";
45    
46    private static final String AS_XML =
47      "<Version REST=\"" + REST_VERSION + "\"" +
48      " OS=\"" + OS_VERSION + "\"" +
49      " JVM=\"" + JVM_VERSION + "\"" +
50      " Server=\"" + JETTY_VERSION + "\"" +
51      " Jersey=\"" + JERSEY_VERSION + "\"/>";
52  
53    private static final String AS_PB = 
54      "CgUwLjAuMRInU3VuIE1pY3Jvc3lzdGVtcyBJbmMuIDEuNi4wXzEzLTExLjMtYjAyGi1MaW51eCAy" +
55      "LjYuMTgtMTI4LjEuNi5lbDUuY2VudG9zLnBsdXN4ZW4gYW1kNjQiBjYuMS4xNCoIMS4xLjAtZWE=";
56  
57    private JAXBContext context;
58  
59    public TestVersionModel() throws JAXBException {
60      super();
61      context = JAXBContext.newInstance(VersionModel.class);
62    }
63  
64    private VersionModel buildTestModel() {
65      VersionModel model = new VersionModel();
66      model.setRESTVersion(REST_VERSION);
67      model.setOSVersion(OS_VERSION);
68      model.setJVMVersion(JVM_VERSION);
69      model.setServerVersion(JETTY_VERSION);
70      model.setJerseyVersion(JERSEY_VERSION);
71      return model;
72    }
73  
74    @SuppressWarnings("unused")
75    private String toXML(VersionModel model) throws JAXBException {
76      StringWriter writer = new StringWriter();
77      context.createMarshaller().marshal(model, writer);
78      return writer.toString();
79    }
80  
81    private VersionModel fromXML(String xml) throws JAXBException {
82      return (VersionModel)
83        context.createUnmarshaller().unmarshal(new StringReader(xml));
84    }
85  
86    @SuppressWarnings("unused")
87    private byte[] toPB(VersionModel model) {
88      return model.createProtobufOutput();
89    }
90  
91    private VersionModel fromPB(String pb) throws IOException {
92      return (VersionModel) 
93        new VersionModel().getObjectFromMessage(Base64.decode(AS_PB));
94    }
95  
96    private void checkModel(VersionModel model) {
97      assertEquals(model.getRESTVersion(), REST_VERSION);
98      assertEquals(model.getOSVersion(), OS_VERSION);
99      assertEquals(model.getJVMVersion(), JVM_VERSION);
100     assertEquals(model.getServerVersion(), JETTY_VERSION);
101     assertEquals(model.getJerseyVersion(), JERSEY_VERSION);
102   }
103 
104   public void testBuildModel() throws Exception {
105     checkModel(buildTestModel());
106   }
107 
108   public void testFromXML() throws Exception {
109     checkModel(fromXML(AS_XML));
110   }
111 
112   public void testFromPB() throws Exception {
113     checkModel(fromPB(AS_PB));
114   }
115 
116   @org.junit.Rule
117   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
118     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
119 }
120