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