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 com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; 026import java.io.ByteArrayInputStream; 027import java.io.IOException; 028import javax.ws.rs.core.MediaType; 029import javax.xml.bind.JAXBContext; 030import javax.xml.bind.JAXBException; 031import org.apache.hadoop.hbase.HBaseClassTestRule; 032import org.apache.hadoop.hbase.HBaseTestingUtility; 033import org.apache.hadoop.hbase.rest.client.Client; 034import org.apache.hadoop.hbase.rest.client.Cluster; 035import org.apache.hadoop.hbase.rest.client.Response; 036import org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel; 037import org.apache.hadoop.hbase.rest.model.VersionModel; 038import org.apache.hadoop.hbase.testclassification.MediumTests; 039import org.apache.hadoop.hbase.testclassification.RestTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.junit.AfterClass; 042import org.junit.BeforeClass; 043import org.junit.ClassRule; 044import org.junit.Test; 045import org.junit.experimental.categories.Category; 046import org.slf4j.Logger; 047import org.slf4j.LoggerFactory; 048 049@Category({RestTests.class, MediumTests.class}) 050public class TestVersionResource { 051 052 @ClassRule 053 public static final HBaseClassTestRule CLASS_RULE = 054 HBaseClassTestRule.forClass(TestVersionResource.class); 055 056 private static final Logger LOG = LoggerFactory.getLogger(TestVersionResource.class); 057 058 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 059 private static final HBaseRESTTestingUtility REST_TEST_UTIL = 060 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", 069 REST_TEST_UTIL.getServletPort())); 070 context = JAXBContext.newInstance( 071 VersionModel.class, 072 StorageClusterVersionModel.class); 073 } 074 075 @AfterClass 076 public static void tearDownAfterClass() throws Exception { 077 REST_TEST_UTIL.shutdownServletContainer(); 078 TEST_UTIL.shutdownMiniCluster(); 079 } 080 081 private static void validate(VersionModel model) { 082 assertNotNull(model); 083 assertNotNull(model.getRESTVersion()); 084 assertEquals(RESTServlet.VERSION_STRING, model.getRESTVersion()); 085 String osVersion = model.getOSVersion(); 086 assertNotNull(osVersion); 087 assertTrue(osVersion.contains(System.getProperty("os.name"))); 088 assertTrue(osVersion.contains(System.getProperty("os.version"))); 089 assertTrue(osVersion.contains(System.getProperty("os.arch"))); 090 String jvmVersion = model.getJVMVersion(); 091 assertNotNull(jvmVersion); 092 assertTrue(jvmVersion.contains(System.getProperty("java.vm.vendor"))); 093 assertTrue(jvmVersion.contains(System.getProperty("java.version"))); 094 assertTrue(jvmVersion.contains(System.getProperty("java.vm.version"))); 095 assertNotNull(model.getServerVersion()); 096 String jerseyVersion = model.getJerseyVersion(); 097 assertNotNull(jerseyVersion); 098 // TODO: fix when we actually get a jersey version 099 // assertEquals(jerseyVersion, ServletContainer.class.getPackage().getImplementationVersion()); 100 } 101 102 @Test 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())); 118 } 119 120 @Test 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())); 128 validate(model); 129 LOG.info("success retrieving Stargate version as XML"); 130 } 131 132 @Test 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); 139 VersionModel model 140 = mapper.readValue(response.getBody(), VersionModel.class); 141 validate(model); 142 LOG.info("success retrieving Stargate version as JSON"); 143 } 144 145 @Test 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()); 152 validate(model); 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()); 158 validate(model); 159 } 160 161 @Test 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")); 166 } 167 168 @Test 169 public void testGetStorageClusterVersionXML() throws IOException, 170 JAXBException { 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"); 181 } 182 183 @Test 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"); 195 } 196} 197