2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.rest
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertNotNull
;
22 import static org
.junit
.Assert
.assertTrue
;
24 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
25 import com
.fasterxml
.jackson
.jaxrs
.json
.JacksonJaxbJsonProvider
;
26 import java
.io
.ByteArrayInputStream
;
27 import java
.io
.IOException
;
28 import javax
.ws
.rs
.core
.MediaType
;
29 import javax
.xml
.bind
.JAXBContext
;
30 import javax
.xml
.bind
.JAXBException
;
31 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
32 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
33 import org
.apache
.hadoop
.hbase
.rest
.client
.Client
;
34 import org
.apache
.hadoop
.hbase
.rest
.client
.Cluster
;
35 import org
.apache
.hadoop
.hbase
.rest
.client
.Response
;
36 import org
.apache
.hadoop
.hbase
.rest
.model
.StorageClusterVersionModel
;
37 import org
.apache
.hadoop
.hbase
.rest
.model
.VersionModel
;
38 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
39 import org
.apache
.hadoop
.hbase
.testclassification
.RestTests
;
40 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
41 import org
.junit
.AfterClass
;
42 import org
.junit
.BeforeClass
;
43 import org
.junit
.ClassRule
;
44 import org
.junit
.Test
;
45 import org
.junit
.experimental
.categories
.Category
;
46 import org
.slf4j
.Logger
;
47 import org
.slf4j
.LoggerFactory
;
49 @Category({RestTests
.class, MediumTests
.class})
50 public class TestVersionResource
{
53 public static final HBaseClassTestRule CLASS_RULE
=
54 HBaseClassTestRule
.forClass(TestVersionResource
.class);
56 private static final Logger LOG
= LoggerFactory
.getLogger(TestVersionResource
.class);
58 private static final HBaseTestingUtil TEST_UTIL
= new HBaseTestingUtil();
59 private static final HBaseRESTTestingUtility REST_TEST_UTIL
=
60 new HBaseRESTTestingUtility();
61 private static Client client
;
62 private static JAXBContext context
;
65 public static void setUpBeforeClass() throws Exception
{
66 TEST_UTIL
.startMiniCluster();
67 REST_TEST_UTIL
.startServletContainer(TEST_UTIL
.getConfiguration());
68 client
= new Client(new Cluster().add("localhost",
69 REST_TEST_UTIL
.getServletPort()));
70 context
= JAXBContext
.newInstance(
72 StorageClusterVersionModel
.class);
76 public static void tearDownAfterClass() throws Exception
{
77 REST_TEST_UTIL
.shutdownServletContainer();
78 TEST_UTIL
.shutdownMiniCluster();
81 private static void validate(VersionModel model
) {
83 assertNotNull(model
.getRESTVersion());
84 assertEquals(RESTServlet
.VERSION_STRING
, model
.getRESTVersion());
85 String osVersion
= model
.getOSVersion();
86 assertNotNull(osVersion
);
87 assertTrue(osVersion
.contains(System
.getProperty("os.name")));
88 assertTrue(osVersion
.contains(System
.getProperty("os.version")));
89 assertTrue(osVersion
.contains(System
.getProperty("os.arch")));
90 String jvmVersion
= model
.getJVMVersion();
91 assertNotNull(jvmVersion
);
92 assertTrue(jvmVersion
.contains(System
.getProperty("java.vm.vendor")));
93 assertTrue(jvmVersion
.contains(System
.getProperty("java.version")));
94 assertTrue(jvmVersion
.contains(System
.getProperty("java.vm.version")));
95 assertNotNull(model
.getServerVersion());
96 String jerseyVersion
= model
.getJerseyVersion();
97 assertNotNull(jerseyVersion
);
98 // TODO: fix when we actually get a jersey version
99 // assertEquals(jerseyVersion, ServletContainer.class.getPackage().getImplementationVersion());
103 public void testGetStargateVersionText() throws IOException
{
104 Response response
= client
.get("/version", Constants
.MIMETYPE_TEXT
);
105 assertEquals(200, response
.getCode());
106 assertEquals(Constants
.MIMETYPE_TEXT
, response
.getHeader("content-type"));
107 String body
= Bytes
.toString(response
.getBody());
108 assertTrue(body
.length() > 0);
109 assertTrue(body
.contains(RESTServlet
.VERSION_STRING
));
110 assertTrue(body
.contains(System
.getProperty("java.vm.vendor")));
111 assertTrue(body
.contains(System
.getProperty("java.version")));
112 assertTrue(body
.contains(System
.getProperty("java.vm.version")));
113 assertTrue(body
.contains(System
.getProperty("os.name")));
114 assertTrue(body
.contains(System
.getProperty("os.version")));
115 assertTrue(body
.contains(System
.getProperty("os.arch")));
116 // TODO: fix when we actually get a jersey version
117 // assertTrue(body.contains(ServletContainer.class.getPackage().getImplementationVersion()));
121 public void testGetStargateVersionXML() throws IOException
, JAXBException
{
122 Response response
= client
.get("/version", Constants
.MIMETYPE_XML
);
123 assertEquals(200, response
.getCode());
124 assertEquals(Constants
.MIMETYPE_XML
, response
.getHeader("content-type"));
125 VersionModel model
= (VersionModel
)
126 context
.createUnmarshaller().unmarshal(
127 new ByteArrayInputStream(response
.getBody()));
129 LOG
.info("success retrieving Stargate version as XML");
133 public void testGetStargateVersionJSON() throws IOException
{
134 Response response
= client
.get("/version", Constants
.MIMETYPE_JSON
);
135 assertEquals(200, response
.getCode());
136 assertEquals(Constants
.MIMETYPE_JSON
, response
.getHeader("content-type"));
137 ObjectMapper mapper
= new JacksonJaxbJsonProvider()
138 .locateMapper(VersionModel
.class, MediaType
.APPLICATION_JSON_TYPE
);
140 = mapper
.readValue(response
.getBody(), VersionModel
.class);
142 LOG
.info("success retrieving Stargate version as JSON");
146 public void testGetStargateVersionPB() throws IOException
{
147 Response response
= client
.get("/version", Constants
.MIMETYPE_PROTOBUF
);
148 assertEquals(200, response
.getCode());
149 assertEquals(Constants
.MIMETYPE_PROTOBUF
, response
.getHeader("content-type"));
150 VersionModel model
= new VersionModel();
151 model
.getObjectFromMessage(response
.getBody());
153 response
= client
.get("/version", Constants
.MIMETYPE_PROTOBUF_IETF
);
154 assertEquals(200, response
.getCode());
155 assertEquals(Constants
.MIMETYPE_PROTOBUF_IETF
, response
.getHeader("content-type"));
156 model
= new VersionModel();
157 model
.getObjectFromMessage(response
.getBody());
162 public void testGetStorageClusterVersionText() throws IOException
{
163 Response response
= client
.get("/version/cluster", Constants
.MIMETYPE_TEXT
);
164 assertEquals(200, response
.getCode());
165 assertEquals(Constants
.MIMETYPE_TEXT
, response
.getHeader("content-type"));
169 public void testGetStorageClusterVersionXML() throws IOException
,
171 Response response
= client
.get("/version/cluster",Constants
.MIMETYPE_XML
);
172 assertEquals(200, response
.getCode());
173 assertEquals(Constants
.MIMETYPE_XML
, response
.getHeader("content-type"));
174 StorageClusterVersionModel clusterVersionModel
=
175 (StorageClusterVersionModel
)
176 context
.createUnmarshaller().unmarshal(
177 new ByteArrayInputStream(response
.getBody()));
178 assertNotNull(clusterVersionModel
);
179 assertNotNull(clusterVersionModel
.getVersion());
180 LOG
.info("success retrieving storage cluster version as XML");
184 public void testGetStorageClusterVersionJSON() throws IOException
{
185 Response response
= client
.get("/version/cluster", Constants
.MIMETYPE_JSON
);
186 assertEquals(200, response
.getCode());
187 assertEquals(Constants
.MIMETYPE_JSON
, response
.getHeader("content-type"));
188 ObjectMapper mapper
= new JacksonJaxbJsonProvider()
189 .locateMapper(StorageClusterVersionModel
.class, MediaType
.APPLICATION_JSON_TYPE
);
190 StorageClusterVersionModel clusterVersionModel
191 = mapper
.readValue(response
.getBody(), StorageClusterVersionModel
.class);
192 assertNotNull(clusterVersionModel
);
193 assertNotNull(clusterVersionModel
.getVersion());
194 LOG
.info("success retrieving storage cluster version as JSON");