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.HBaseTestingUtil;
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.apache.hadoop.hbase.util.VersionInfo;
040import org.junit.AfterClass;
041import org.junit.BeforeClass;
042import org.junit.ClassRule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048import org.apache.hbase.thirdparty.com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
049import org.apache.hbase.thirdparty.javax.ws.rs.core.MediaType;
050
051@Category({ RestTests.class, MediumTests.class })
052public class TestVersionResource {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056    HBaseClassTestRule.forClass(TestVersionResource.class);
057
058  private static final Logger LOG = LoggerFactory.getLogger(TestVersionResource.class);
059
060  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
061  private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility();
062  private static Client client;
063  private static JAXBContext context;
064
065  @BeforeClass
066  public static void setUpBeforeClass() throws Exception {
067    TEST_UTIL.startMiniCluster();
068    REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
069    client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort()));
070    context = JAXBContext.newInstance(VersionModel.class, StorageClusterVersionModel.class);
071  }
072
073  @AfterClass
074  public static void tearDownAfterClass() throws Exception {
075    REST_TEST_UTIL.shutdownServletContainer();
076    TEST_UTIL.shutdownMiniCluster();
077  }
078
079  private static void validate(VersionModel model) {
080    assertNotNull(model);
081    String restVersion = model.getRESTVersion();
082    assertNotNull(restVersion);
083    assertEquals(RESTServlet.VERSION_STRING, restVersion);
084    String osVersion = model.getOSVersion();
085    assertNotNull(osVersion);
086    assertTrue(osVersion.contains(System.getProperty("os.name")));
087    assertTrue(osVersion.contains(System.getProperty("os.version")));
088    assertTrue(osVersion.contains(System.getProperty("os.arch")));
089    String jvmVersion = model.getJVMVersion();
090    assertNotNull(jvmVersion);
091    assertTrue(jvmVersion.contains(System.getProperty("java.vm.vendor")));
092    assertTrue(jvmVersion.contains(System.getProperty("java.version")));
093    assertTrue(jvmVersion.contains(System.getProperty("java.vm.version")));
094    assertNotNull(model.getServerVersion());
095    String jerseyVersion = model.getJerseyVersion();
096    assertNotNull(jerseyVersion);
097    // TODO: fix when we actually get a jersey version
098    // assertEquals(jerseyVersion, ServletContainer.class.getPackage().getImplementationVersion());
099
100    String version = model.getVersion();
101    assertNotNull(version);
102    assertEquals(VersionInfo.getVersion(), version);
103    String revision = model.getRevision();
104    assertNotNull(revision);
105    assertEquals(VersionInfo.getRevision(), revision);
106  }
107
108  @Test
109  public void testGetStargateVersionText() throws IOException {
110    Response response = client.get("/version", Constants.MIMETYPE_TEXT);
111    assertEquals(200, response.getCode());
112    assertEquals(Constants.MIMETYPE_TEXT, response.getHeader("content-type"));
113    String body = Bytes.toString(response.getBody());
114    assertTrue(body.length() > 0);
115    assertTrue(body.contains(RESTServlet.VERSION_STRING));
116    assertTrue(body.contains(System.getProperty("java.vm.vendor")));
117    assertTrue(body.contains(System.getProperty("java.version")));
118    assertTrue(body.contains(System.getProperty("java.vm.version")));
119    assertTrue(body.contains(System.getProperty("os.name")));
120    assertTrue(body.contains(System.getProperty("os.version")));
121    assertTrue(body.contains(System.getProperty("os.arch")));
122    // TODO: fix when we actually get a jersey version
123    // assertTrue(body.contains(ServletContainer.class.getPackage().getImplementationVersion()));
124  }
125
126  @Test
127  public void testGetStargateVersionXML() throws IOException, JAXBException {
128    Response response = client.get("/version", Constants.MIMETYPE_XML);
129    assertEquals(200, response.getCode());
130    assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
131    VersionModel model = (VersionModel) context.createUnmarshaller()
132      .unmarshal(new ByteArrayInputStream(response.getBody()));
133    validate(model);
134    LOG.info("success retrieving Stargate version as XML");
135  }
136
137  @Test
138  public void testGetStargateVersionJSON() throws IOException {
139    Response response = client.get("/version", Constants.MIMETYPE_JSON);
140    assertEquals(200, response.getCode());
141    assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
142    ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(VersionModel.class,
143      MediaType.APPLICATION_JSON_TYPE);
144    VersionModel model = mapper.readValue(response.getBody(), VersionModel.class);
145    validate(model);
146    LOG.info("success retrieving Stargate version as JSON");
147  }
148
149  @Test
150  public void testGetStargateVersionPB() throws IOException {
151    Response response = client.get("/version", Constants.MIMETYPE_PROTOBUF);
152    assertEquals(200, response.getCode());
153    assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
154    VersionModel model = new VersionModel();
155    model.getObjectFromMessage(response.getBody());
156    validate(model);
157    response = client.get("/version", Constants.MIMETYPE_PROTOBUF_IETF);
158    assertEquals(200, response.getCode());
159    assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
160    model = new VersionModel();
161    model.getObjectFromMessage(response.getBody());
162    validate(model);
163  }
164
165  @Test
166  public void testGetStorageClusterVersionText() throws IOException {
167    Response response = client.get("/version/cluster", Constants.MIMETYPE_TEXT);
168    assertEquals(200, response.getCode());
169    assertEquals(Constants.MIMETYPE_TEXT, response.getHeader("content-type"));
170  }
171
172  @Test
173  public void testGetStorageClusterVersionXML() throws IOException, JAXBException {
174    Response response = client.get("/version/cluster", Constants.MIMETYPE_XML);
175    assertEquals(200, response.getCode());
176    assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
177    StorageClusterVersionModel clusterVersionModel = (StorageClusterVersionModel) context
178      .createUnmarshaller().unmarshal(new ByteArrayInputStream(response.getBody()));
179    assertNotNull(clusterVersionModel);
180    assertNotNull(clusterVersionModel.getVersion());
181    LOG.info("success retrieving storage cluster version as XML");
182  }
183
184  @Test
185  public void testGetStorageClusterVersionJSON() throws IOException {
186    Response response = client.get("/version/cluster", Constants.MIMETYPE_JSON);
187    assertEquals(200, response.getCode());
188    assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
189    ObjectMapper mapper = new JacksonJaxbJsonProvider()
190      .locateMapper(StorageClusterVersionModel.class, MediaType.APPLICATION_JSON_TYPE);
191    StorageClusterVersionModel clusterVersionModel =
192      mapper.readValue(response.getBody(), StorageClusterVersionModel.class);
193    assertNotNull(clusterVersionModel);
194    assertNotNull(clusterVersionModel.getVersion());
195    LOG.info("success retrieving storage cluster version as JSON");
196  }
197}