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.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
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.StorageClusterVersionModel;
35  import org.apache.hadoop.hbase.rest.model.VersionModel;
36  import org.apache.hadoop.hbase.util.Bytes;
37  
38  import com.sun.jersey.spi.container.servlet.ServletContainer;
39  
40  public class TestVersionResource extends HBaseRESTClusterTestBase {
41    static final Log LOG = LogFactory.getLog(TestVersionResource.class);
42  
43    Client client;
44    JAXBContext context;
45  
46    public TestVersionResource() throws JAXBException {
47      super();
48      context = JAXBContext.newInstance(
49        VersionModel.class,
50        StorageClusterVersionModel.class);
51    }
52  
53    @Override
54    protected void setUp() throws Exception {
55      super.setUp();
56      client = new Client(new Cluster().add("localhost", testServletPort));
57    }
58  
59    @Override
60    protected void tearDown() throws Exception {
61      client.shutdown();
62      super.tearDown();
63    }
64  
65    void validate(VersionModel model) {
66      assertNotNull(model);
67      assertNotNull(model.getRESTVersion());
68      assertEquals(model.getRESTVersion(), RESTServlet.VERSION_STRING);
69      String osVersion = model.getOSVersion(); 
70      assertNotNull(osVersion);
71      assertTrue(osVersion.contains(System.getProperty("os.name")));
72      assertTrue(osVersion.contains(System.getProperty("os.version")));
73      assertTrue(osVersion.contains(System.getProperty("os.arch")));
74      String jvmVersion = model.getJVMVersion();
75      assertNotNull(jvmVersion);
76      assertTrue(jvmVersion.contains(System.getProperty("java.vm.vendor")));
77      assertTrue(jvmVersion.contains(System.getProperty("java.version")));
78      assertTrue(jvmVersion.contains(System.getProperty("java.vm.version")));
79      assertNotNull(model.getServerVersion());
80      String jerseyVersion = model.getJerseyVersion();
81      assertNotNull(jerseyVersion);
82      assertEquals(jerseyVersion, ServletContainer.class.getPackage()
83        .getImplementationVersion());
84    }
85  
86    void doTestGetStargateVersionText() throws IOException {
87      Response response = client.get("/version", MIMETYPE_TEXT);
88      assertTrue(response.getCode() == 200);
89      String body = Bytes.toString(response.getBody());
90      assertTrue(body.length() > 0);
91      assertTrue(body.contains(RESTServlet.VERSION_STRING));
92      assertTrue(body.contains(System.getProperty("java.vm.vendor")));
93      assertTrue(body.contains(System.getProperty("java.version")));
94      assertTrue(body.contains(System.getProperty("java.vm.version")));
95      assertTrue(body.contains(System.getProperty("os.name")));
96      assertTrue(body.contains(System.getProperty("os.version")));
97      assertTrue(body.contains(System.getProperty("os.arch")));
98      assertTrue(body.contains(ServletContainer.class.getPackage()
99        .getImplementationVersion()));
100   }
101 
102   void doTestGetStargateVersionXML() throws IOException, JAXBException {
103     Response response = client.get("/version", MIMETYPE_XML);
104     assertTrue(response.getCode() == 200);
105     VersionModel model = (VersionModel)
106       context.createUnmarshaller().unmarshal(
107         new ByteArrayInputStream(response.getBody()));
108     validate(model);
109     LOG.info("success retrieving Stargate version as XML");
110   }
111 
112   void doTestGetStargateVersionJSON() throws IOException {
113     Response response = client.get("/version", MIMETYPE_JSON);
114     assertTrue(response.getCode() == 200);
115   }
116 
117   void doTestGetStargateVersionPB() throws IOException {
118     Response response = client.get("/version", MIMETYPE_PROTOBUF);
119     assertTrue(response.getCode() == 200);
120     VersionModel model = new VersionModel();
121     model.getObjectFromMessage(response.getBody());
122     validate(model);
123     LOG.info("success retrieving Stargate version as protobuf");
124   }
125 
126   void doTestGetStorageClusterVersionText() throws IOException {
127     Response response = client.get("/version/cluster", MIMETYPE_TEXT);
128     assertTrue(response.getCode() == 200);
129   }
130 
131   void doTestGetStorageClusterVersionXML() throws IOException,
132       JAXBException {
133     Response response = client.get("/version/cluster", MIMETYPE_XML);
134     assertTrue(response.getCode() == 200);
135     StorageClusterVersionModel clusterVersionModel = 
136       (StorageClusterVersionModel)
137         context.createUnmarshaller().unmarshal(
138           new ByteArrayInputStream(response.getBody()));
139     assertNotNull(clusterVersionModel);
140     assertNotNull(clusterVersionModel.getVersion());
141     LOG.info("success retrieving storage cluster version as XML");
142   }
143 
144   void doTestGetStorageClusterVersionJSON() throws IOException {
145     Response response = client.get("/version/cluster", MIMETYPE_JSON);
146     assertTrue(response.getCode() == 200);
147   }
148 
149   public void testVersionResource() throws Exception {
150     doTestGetStargateVersionText();
151     doTestGetStargateVersionXML();
152     doTestGetStargateVersionJSON();
153     doTestGetStargateVersionPB();
154     doTestGetStorageClusterVersionText();
155     doTestGetStorageClusterVersionXML();
156     doTestGetStorageClusterVersionJSON();
157   }
158 }