View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.IOException;
24  
25  import javax.xml.bind.JAXBContext;
26  import javax.xml.bind.JAXBException;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.MediumTests;
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  
41  import org.junit.AfterClass;
42  import org.junit.BeforeClass;
43  import org.junit.Test;
44  
45  import com.sun.jersey.spi.container.servlet.ServletContainer;
46  import org.junit.experimental.categories.Category;
47  
48  @Category(MediumTests.class)
49  public class TestVersionResource {
50    private static final Log LOG = LogFactory.getLog(TestVersionResource.class);
51  
52    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
53    private static final HBaseRESTTestingUtility REST_TEST_UTIL = 
54      new HBaseRESTTestingUtility();
55    private static Client client;
56    private static JAXBContext context;
57  
58    @BeforeClass
59    public static void setUpBeforeClass() throws Exception {
60      TEST_UTIL.startMiniCluster();
61      REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
62      client = new Client(new Cluster().add("localhost", 
63        REST_TEST_UTIL.getServletPort()));
64      context = JAXBContext.newInstance(
65        VersionModel.class,
66        StorageClusterVersionModel.class);
67    }
68  
69    @AfterClass
70    public static void tearDownAfterClass() throws Exception {
71      REST_TEST_UTIL.shutdownServletContainer();
72      TEST_UTIL.shutdownMiniCluster();
73    }
74  
75    private static void validate(VersionModel model) {
76      assertNotNull(model);
77      assertNotNull(model.getRESTVersion());
78      assertEquals(model.getRESTVersion(), RESTServlet.VERSION_STRING);
79      String osVersion = model.getOSVersion(); 
80      assertNotNull(osVersion);
81      assertTrue(osVersion.contains(System.getProperty("os.name")));
82      assertTrue(osVersion.contains(System.getProperty("os.version")));
83      assertTrue(osVersion.contains(System.getProperty("os.arch")));
84      String jvmVersion = model.getJVMVersion();
85      assertNotNull(jvmVersion);
86      assertTrue(jvmVersion.contains(System.getProperty("java.vm.vendor")));
87      assertTrue(jvmVersion.contains(System.getProperty("java.version")));
88      assertTrue(jvmVersion.contains(System.getProperty("java.vm.version")));
89      assertNotNull(model.getServerVersion());
90      String jerseyVersion = model.getJerseyVersion();
91      assertNotNull(jerseyVersion);
92      assertEquals(jerseyVersion, ServletContainer.class.getPackage()
93        .getImplementationVersion());
94    }
95  
96    @Test
97    public void testGetStargateVersionText() throws IOException {
98      Response response = client.get("/version", Constants.MIMETYPE_TEXT);
99      assertTrue(response.getCode() == 200);
100     assertEquals(Constants.MIMETYPE_TEXT, response.getHeader("content-type"));
101     String body = Bytes.toString(response.getBody());
102     assertTrue(body.length() > 0);
103     assertTrue(body.contains(RESTServlet.VERSION_STRING));
104     assertTrue(body.contains(System.getProperty("java.vm.vendor")));
105     assertTrue(body.contains(System.getProperty("java.version")));
106     assertTrue(body.contains(System.getProperty("java.vm.version")));
107     assertTrue(body.contains(System.getProperty("os.name")));
108     assertTrue(body.contains(System.getProperty("os.version")));
109     assertTrue(body.contains(System.getProperty("os.arch")));
110     assertTrue(body.contains(ServletContainer.class.getPackage()
111       .getImplementationVersion()));
112   }
113 
114   @Test
115   public void testGetStargateVersionXML() throws IOException, JAXBException {
116     Response response = client.get("/version", Constants.MIMETYPE_XML);
117     assertTrue(response.getCode() == 200);
118     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
119     VersionModel model = (VersionModel)
120       context.createUnmarshaller().unmarshal(
121         new ByteArrayInputStream(response.getBody()));
122     validate(model);
123     LOG.info("success retrieving Stargate version as XML");
124   }
125 
126   @Test
127   public void testGetStargateVersionJSON() throws IOException {
128     Response response = client.get("/version", Constants.MIMETYPE_JSON);
129     assertTrue(response.getCode() == 200);
130     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
131   }
132 
133   @Test
134   public void testGetStargateVersionPB() throws IOException {
135     Response response = client.get("/version", Constants.MIMETYPE_PROTOBUF);
136     assertTrue(response.getCode() == 200);
137     assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
138     VersionModel model = new VersionModel();
139     model.getObjectFromMessage(response.getBody());
140     validate(model);
141     response = client.get("/version", Constants.MIMETYPE_PROTOBUF_IETF);
142     assertTrue(response.getCode() == 200);
143     assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
144     model = new VersionModel();
145     model.getObjectFromMessage(response.getBody());
146     validate(model);
147   }
148 
149   @Test
150   public void testGetStorageClusterVersionText() throws IOException {
151     Response response = client.get("/version/cluster", Constants.MIMETYPE_TEXT);
152     assertTrue(response.getCode() == 200);
153     assertEquals(Constants.MIMETYPE_TEXT, response.getHeader("content-type"));
154   }
155 
156   @Test
157   public void testGetStorageClusterVersionXML() throws IOException,
158       JAXBException {
159     Response response = client.get("/version/cluster",Constants.MIMETYPE_XML);
160     assertTrue(response.getCode() == 200);
161     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
162     StorageClusterVersionModel clusterVersionModel = 
163       (StorageClusterVersionModel)
164         context.createUnmarshaller().unmarshal(
165           new ByteArrayInputStream(response.getBody()));
166     assertNotNull(clusterVersionModel);
167     assertNotNull(clusterVersionModel.getVersion());
168     LOG.info("success retrieving storage cluster version as XML");
169   }
170 
171   @Test
172   public void doTestGetStorageClusterVersionJSON() throws IOException {
173     Response response = client.get("/version/cluster", Constants.MIMETYPE_JSON);
174     assertTrue(response.getCode() == 200);
175     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
176   }
177 
178 }
179