001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.rest; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNotNull; 022import static org.junit.Assert.assertTrue; 023 024import com.fasterxml.jackson.databind.ObjectMapper; 025import java.io.ByteArrayInputStream; 026import java.io.IOException; 027import javax.xml.bind.JAXBContext; 028import javax.xml.bind.JAXBException; 029import org.apache.hadoop.hbase.HBaseClassTestRule; 030import org.apache.hadoop.hbase.HBaseTestingUtility; 031import org.apache.hadoop.hbase.rest.client.Client; 032import org.apache.hadoop.hbase.rest.client.Cluster; 033import org.apache.hadoop.hbase.rest.client.Response; 034import org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel; 035import org.apache.hadoop.hbase.rest.model.VersionModel; 036import org.apache.hadoop.hbase.testclassification.MediumTests; 037import org.apache.hadoop.hbase.testclassification.RestTests; 038import org.apache.hadoop.hbase.util.Bytes; 039import org.junit.AfterClass; 040import org.junit.BeforeClass; 041import org.junit.ClassRule; 042import org.junit.Test; 043import org.junit.experimental.categories.Category; 044import org.slf4j.Logger; 045import org.slf4j.LoggerFactory; 046 047import org.apache.hbase.thirdparty.com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; 048import org.apache.hbase.thirdparty.javax.ws.rs.core.MediaType; 049 050@Category({ RestTests.class, MediumTests.class }) 051public class TestVersionResource { 052 053 @ClassRule 054 public static final HBaseClassTestRule CLASS_RULE = 055 HBaseClassTestRule.forClass(TestVersionResource.class); 056 057 private static final Logger LOG = LoggerFactory.getLogger(TestVersionResource.class); 058 059 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 060 private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility(); 061 private static Client client; 062 private static JAXBContext context; 063 064 @BeforeClass 065 public static void setUpBeforeClass() throws Exception { 066 TEST_UTIL.startMiniCluster(); 067 REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration()); 068 client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort())); 069 context = JAXBContext.newInstance(VersionModel.class, StorageClusterVersionModel.class); 070 } 071 072 @AfterClass 073 public static void tearDownAfterClass() throws Exception { 074 REST_TEST_UTIL.shutdownServletContainer(); 075 TEST_UTIL.shutdownMiniCluster(); 076 } 077 078 private static void validate(VersionModel model) { 079 assertNotNull(model); 080 assertNotNull(model.getRESTVersion()); 081 assertEquals(RESTServlet.VERSION_STRING, model.getRESTVersion()); 082 String osVersion = model.getOSVersion(); 083 assertNotNull(osVersion); 084 assertTrue(osVersion.contains(System.getProperty("os.name"))); 085 assertTrue(osVersion.contains(System.getProperty("os.version"))); 086 assertTrue(osVersion.contains(System.getProperty("os.arch"))); 087 String jvmVersion = model.getJVMVersion(); 088 assertNotNull(jvmVersion); 089 assertTrue(jvmVersion.contains(System.getProperty("java.vm.vendor"))); 090 assertTrue(jvmVersion.contains(System.getProperty("java.version"))); 091 assertTrue(jvmVersion.contains(System.getProperty("java.vm.version"))); 092 assertNotNull(model.getServerVersion()); 093 String jerseyVersion = model.getJerseyVersion(); 094 assertNotNull(jerseyVersion); 095 // TODO: fix when we actually get a jersey version 096 // assertEquals(jerseyVersion, ServletContainer.class.getPackage().getImplementationVersion()); 097 } 098 099 @Test 100 public void testGetStargateVersionText() throws IOException { 101 Response response = client.get("/version", Constants.MIMETYPE_TEXT); 102 assertEquals(200, response.getCode()); 103 assertEquals(Constants.MIMETYPE_TEXT, response.getHeader("content-type")); 104 String body = Bytes.toString(response.getBody()); 105 assertTrue(body.length() > 0); 106 assertTrue(body.contains(RESTServlet.VERSION_STRING)); 107 assertTrue(body.contains(System.getProperty("java.vm.vendor"))); 108 assertTrue(body.contains(System.getProperty("java.version"))); 109 assertTrue(body.contains(System.getProperty("java.vm.version"))); 110 assertTrue(body.contains(System.getProperty("os.name"))); 111 assertTrue(body.contains(System.getProperty("os.version"))); 112 assertTrue(body.contains(System.getProperty("os.arch"))); 113 // TODO: fix when we actually get a jersey version 114 // assertTrue(body.contains(ServletContainer.class.getPackage().getImplementationVersion())); 115 } 116 117 @Test 118 public void testGetStargateVersionXML() throws IOException, JAXBException { 119 Response response = client.get("/version", Constants.MIMETYPE_XML); 120 assertEquals(200, response.getCode()); 121 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type")); 122 VersionModel model = (VersionModel) context.createUnmarshaller() 123 .unmarshal(new ByteArrayInputStream(response.getBody())); 124 validate(model); 125 LOG.info("success retrieving Stargate version as XML"); 126 } 127 128 @Test 129 public void testGetStargateVersionJSON() throws IOException { 130 Response response = client.get("/version", Constants.MIMETYPE_JSON); 131 assertEquals(200, response.getCode()); 132 assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type")); 133 ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(VersionModel.class, 134 MediaType.APPLICATION_JSON_TYPE); 135 VersionModel model = mapper.readValue(response.getBody(), VersionModel.class); 136 validate(model); 137 LOG.info("success retrieving Stargate version as JSON"); 138 } 139 140 @Test 141 public void testGetStargateVersionPB() throws IOException { 142 Response response = client.get("/version", Constants.MIMETYPE_PROTOBUF); 143 assertEquals(200, response.getCode()); 144 assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type")); 145 VersionModel model = new VersionModel(); 146 model.getObjectFromMessage(response.getBody()); 147 validate(model); 148 response = client.get("/version", Constants.MIMETYPE_PROTOBUF_IETF); 149 assertEquals(200, response.getCode()); 150 assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type")); 151 model = new VersionModel(); 152 model.getObjectFromMessage(response.getBody()); 153 validate(model); 154 } 155 156 @Test 157 public void testGetStorageClusterVersionText() throws IOException { 158 Response response = client.get("/version/cluster", Constants.MIMETYPE_TEXT); 159 assertEquals(200, response.getCode()); 160 assertEquals(Constants.MIMETYPE_TEXT, response.getHeader("content-type")); 161 } 162 163 @Test 164 public void testGetStorageClusterVersionXML() throws IOException, JAXBException { 165 Response response = client.get("/version/cluster", Constants.MIMETYPE_XML); 166 assertEquals(200, response.getCode()); 167 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type")); 168 StorageClusterVersionModel clusterVersionModel = (StorageClusterVersionModel) context 169 .createUnmarshaller().unmarshal(new ByteArrayInputStream(response.getBody())); 170 assertNotNull(clusterVersionModel); 171 assertNotNull(clusterVersionModel.getVersion()); 172 LOG.info("success retrieving storage cluster version as XML"); 173 } 174 175 @Test 176 public void testGetStorageClusterVersionJSON() throws IOException { 177 Response response = client.get("/version/cluster", Constants.MIMETYPE_JSON); 178 assertEquals(200, response.getCode()); 179 assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type")); 180 ObjectMapper mapper = new JacksonJaxbJsonProvider() 181 .locateMapper(StorageClusterVersionModel.class, MediaType.APPLICATION_JSON_TYPE); 182 StorageClusterVersionModel clusterVersionModel = 183 mapper.readValue(response.getBody(), StorageClusterVersionModel.class); 184 assertNotNull(clusterVersionModel); 185 assertNotNull(clusterVersionModel.getVersion()); 186 LOG.info("success retrieving storage cluster version as JSON"); 187 } 188}