changeset 297:8ecf97835acd

[commands] Bring version in line with others. Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025543.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Thu, 26 Oct 2017 16:07:31 +0200
parents d28b7994e59e
children 36a5f2fed6d3
files services/commands/src/main/java/com/redhat/thermostat/gateway/service/commands/http/handlers/StaticResourcesHandler.java services/commands/src/main/resources/agent.html services/commands/src/main/resources/agent2.html services/commands/src/main/resources/index.html services/commands/src/main/webapp/WEB-INF/web.xml services/commands/src/test/java/com/redhat/thermostat/gateway/service/commands/channel/endpoints/AuthBasicCoreServerTest.java tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/commands/CommandsServiceIntegrationTest.java tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/schema/SchemaIntegrationTest.java tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/ServiceIntegrationTest.java
diffstat 9 files changed, 114 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/services/commands/src/main/java/com/redhat/thermostat/gateway/service/commands/http/handlers/StaticResourcesHandler.java	Thu Oct 26 12:20:51 2017 +0200
+++ b/services/commands/src/main/java/com/redhat/thermostat/gateway/service/commands/http/handlers/StaticResourcesHandler.java	Thu Oct 26 16:07:31 2017 +0200
@@ -38,20 +38,24 @@
 
 import java.io.IOException;
 
+import javax.servlet.ServletContext;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
 import com.redhat.thermostat.gateway.common.core.servlet.BasicResourceHandler;
+import com.redhat.thermostat.gateway.common.core.servlet.GlobalConstants;
 
-@Path("{fileName: .+(\\.(yaml|html))}")
-@Produces({MediaType.TEXT_PLAIN, MediaType.TEXT_HTML})
+@Path("/")
 public class StaticResourcesHandler extends BasicResourceHandler {
 
+    @Path("{fileName: .+(\\.(yaml|html))}")
     @GET
+    @Produces({MediaType.TEXT_PLAIN, MediaType.TEXT_HTML})
     public Response getFileAsTextOrHtml(@PathParam("fileName") String fileName) throws IOException {
         MediaType type = MediaType.TEXT_PLAIN_TYPE;
         if (fileName.endsWith(".html")) {
@@ -59,4 +63,14 @@
         }
         return getFileAsResponse(StaticResourcesHandler.class.getClassLoader(), fileName, type);
     }
+
+    // "version" is mapped to this servlet and comes here as '/'
+    @Path("/")
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response getVersion(@Context ServletContext context) throws IOException {
+        String version = context.getInitParameter(GlobalConstants.SERVICE_VERSION_KEY);
+        String response = "{ \"version\": \"" + version + "\" }";
+        return Response.ok(response).build();
+    }
 }
--- a/services/commands/src/main/resources/agent.html	Thu Oct 26 12:20:51 2017 +0200
+++ b/services/commands/src/main/resources/agent.html	Thu Oct 26 16:07:31 2017 +0200
@@ -117,9 +117,9 @@
                 query_string = "?access_token=" + token;
             }
             if (window.location.protocol == 'http:') {
-                CmdChan.connect('ws://' + window.location.host + '/commands/v1/systems/ignored_system/agents/testAgent' + query_string);
+                CmdChan.connect('ws://' + window.location.host + '/commands/0.0.1/systems/ignored_system/agents/testAgent' + query_string);
             } else {
-                CmdChan.connect('wss://' + window.location.host + '/commands/v1/systems/ignored_system/agents/testAgent' + query_string);
+                CmdChan.connect('wss://' + window.location.host + '/commands/0.0.1/systems/ignored_system/agents/testAgent' + query_string);
             }
         };
 
--- a/services/commands/src/main/resources/agent2.html	Thu Oct 26 12:20:51 2017 +0200
+++ b/services/commands/src/main/resources/agent2.html	Thu Oct 26 16:07:31 2017 +0200
@@ -117,9 +117,9 @@
                 query_string = "?access_token=" + token;
             }
             if (window.location.protocol == 'http:') {
-                CmdChan.connect('ws://' + window.location.host + '/commands/v1/systems/ignored_system/agents/otherAgent' + query_string);
+                CmdChan.connect('ws://' + window.location.host + '/commands/0.0.1/systems/ignored_system/agents/otherAgent' + query_string);
             } else {
-                CmdChan.connect('wss://' + window.location.host + '/commands/v1/systems/ignored_system/agents/otherAgent' + query_string);
+                CmdChan.connect('wss://' + window.location.host + '/commands/0.0.1/systems/ignored_system/agents/otherAgent' + query_string);
             }
         };
 
--- a/services/commands/src/main/resources/index.html	Thu Oct 26 12:20:51 2017 +0200
+++ b/services/commands/src/main/resources/index.html	Thu Oct 26 16:07:31 2017 +0200
@@ -122,7 +122,7 @@
                 if (token != null) {
                     query_string = "?access_token=" + token;
                 }
-                url = url + '/commands/v1/actions/' + action + '/systems/' + systemId + '/agents/' + agent + '/jvms/' + jvmId +'/sequence/' + CmdChan.sequence++ + query_string;
+                url = url + '/commands/0.0.1/actions/' + action + '/systems/' + systemId + '/agents/' + agent + '/jvms/' + jvmId +'/sequence/' + CmdChan.sequence++ + query_string;
 
                 CmdChan.connect( url, clientRequest );
                 document.getElementById('cmd-chan').value = '';
--- a/services/commands/src/main/webapp/WEB-INF/web.xml	Thu Oct 26 12:20:51 2017 +0200
+++ b/services/commands/src/main/webapp/WEB-INF/web.xml	Thu Oct 26 16:07:31 2017 +0200
@@ -54,11 +54,15 @@
     </servlet>
     <servlet-mapping>
       <servlet-name>StaticResourcesServlet</servlet-name>
-      <url-pattern>/v1/static/*</url-pattern>
+      <url-pattern>/0.0.1/static/*</url-pattern>
     </servlet-mapping>
     <servlet-mapping>
       <servlet-name>StaticResourcesServlet</servlet-name>
-      <url-pattern>/v1/doc/*</url-pattern>
+      <url-pattern>/0.0.1/doc/*</url-pattern>
+    </servlet-mapping>
+    <servlet-mapping>
+      <servlet-name>StaticResourcesServlet</servlet-name>
+      <url-pattern>/version</url-pattern>
     </servlet-mapping>
     <!-- Listener for setting up the websocket handlers -->
     <listener>
@@ -66,15 +70,15 @@
     </listener>
     <!-- Context configuration -->
     <context-param>
-        <param-name>com.redhat.thermostat.gateway.SERVICE_VERSION</param-name>
-        <param-value>v1</param-value>
+      <param-name>com.redhat.thermostat.gateway.SERVICE_VERSION</param-name>
+      <param-value>0.0.1</param-value>
     </context-param>
 
     <!-- allow querying API version without authentication -->
     <security-constraint>
-        <web-resource-collection>
-            <web-resource-name>Version</web-resource-name>
-            <url-pattern>/version</url-pattern>
+      <web-resource-collection>
+        <web-resource-name>Version</web-resource-name>
+          <url-pattern>/version</url-pattern>
         </web-resource-collection>
     </security-constraint>
 
@@ -82,7 +86,7 @@
     <security-constraint>
       <web-resource-collection>
         <web-resource-name>Swagger API SPEC YAML File</web-resource-name>
-        <url-pattern>/v1/doc/@com.redhat.thermostat.gateway.SERVICE_NAME@-swagger.yaml</url-pattern>
+        <url-pattern>/0.0.1/doc/@com.redhat.thermostat.gateway.SERVICE_NAME@-swagger.yaml</url-pattern>
       </web-resource-collection>
       <!-- Explicitly no auth constraint for this file -->
     </security-constraint>
--- a/services/commands/src/test/java/com/redhat/thermostat/gateway/service/commands/channel/endpoints/AuthBasicCoreServerTest.java	Thu Oct 26 12:20:51 2017 +0200
+++ b/services/commands/src/test/java/com/redhat/thermostat/gateway/service/commands/channel/endpoints/AuthBasicCoreServerTest.java	Thu Oct 26 16:07:31 2017 +0200
@@ -80,7 +80,7 @@
     private static final int TEST_PORT = 32039;
     private static final String TEST_ADDRESS = "127.0.0.1";
     protected WebSocketClient client;
-    protected final String baseUrl = "ws://" + TEST_ADDRESS + ":" + TEST_PORT + CONTEXT_NAME +"/v1/";
+    protected final String baseUrl = "ws://" + TEST_ADDRESS + ":" + TEST_PORT + CONTEXT_NAME +"/0.0.1/";
 
     private static Thread thread;
     private static Server server;
@@ -208,10 +208,10 @@
                 Configuration serviceConfig = mock(Configuration.class);
                 when(serviceConfig.asMap()).thenReturn(serviceConfigMap);
                 ServerEndpointConfig agentConf = configFactory.createEndpointConfig(CommandChannelAgentEndpointHandler.class,
-                                                                                    "/v1" + CommandChannelAgentEndpointHandler.PATH,
+                                                                                    "/0.0.1" + CommandChannelAgentEndpointHandler.PATH,
                                                                                     serviceConfig);
                 ServerEndpointConfig clientConf = configFactory.createEndpointConfig(CommandChannelClientEndpointHandler.class,
-                                                                                     "/v1" + CommandChannelClientEndpointHandler.PATH,
+                                                                                     "/0.0.1" + CommandChannelClientEndpointHandler.PATH,
                                                                                      serviceConfig);
                 container.addEndpoint(agentConf);
                 container.addEndpoint(clientConf);
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/commands/CommandsServiceIntegrationTest.java	Thu Oct 26 12:20:51 2017 +0200
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/commands/CommandsServiceIntegrationTest.java	Thu Oct 26 16:07:31 2017 +0200
@@ -41,7 +41,7 @@
 public class CommandsServiceIntegrationTest extends ServiceIntegrationTest {
 
     private static final String SERVICE_NAME = "commands";
-    private static final String SERVICE_VERSION = "v1";
+    private static final String SERVICE_VERSION = "0.0.1";
 
     public CommandsServiceIntegrationTest() {
         super("ignore-me");
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/schema/SchemaIntegrationTest.java	Thu Oct 26 12:20:51 2017 +0200
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/schema/SchemaIntegrationTest.java	Thu Oct 26 16:07:31 2017 +0200
@@ -36,31 +36,29 @@
 
 package com.redhat.thermostat.gateway.service.schema;
 
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-import com.redhat.thermostat.common.json.JsonUtil;
-import com.redhat.thermostat.common.yaml.YamlToJson;
-import com.redhat.thermostat.gateway.tests.integration.ServiceIntegrationTest;
-import org.eclipse.jetty.http.HttpMethod;
-import org.eclipse.jetty.client.api.ContentResponse;
-import org.junit.Assert;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeoutException;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import org.eclipse.jetty.client.api.ContentResponse;
+import org.eclipse.jetty.http.HttpMethod;
+import org.eclipse.jetty.http.HttpStatus;
+import org.junit.Test;
 
-public class SchemaIntegrationTest extends ServiceIntegrationTest {
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.redhat.thermostat.common.json.JsonUtil;
+import com.redhat.thermostat.common.yaml.YamlToJson;
+import com.redhat.thermostat.gateway.tests.integration.IntegrationTest;
+
+public class SchemaIntegrationTest extends IntegrationTest {
 
     private static final String serviceName = "schema";
-    private static final String versionString = "";
-    private static final int HTTP_200_OK = 200;
-    private static final int HTTP_404_NOTFOUND = 404;
 
     public SchemaIntegrationTest() {
         super(serviceName);
@@ -92,33 +90,18 @@
 
     @Test
     public void testBad() throws InterruptedException, ExecutionException, TimeoutException {
-        final ContentResponse resp = getBad("/fullapi-swagge.yaml");
+        getBad("/fullapi-swagge.yaml");
     }
 
     protected ContentResponse get(final String path) throws InterruptedException, ExecutionException, TimeoutException {
         ContentResponse response = client.newRequest(resourceUrl + path).method(HttpMethod.GET).send();
-        assertEquals(HTTP_200_OK, response.getStatus());
+        assertEquals(HttpStatus.OK_200, response.getStatus());
         return response;
     }
 
     protected ContentResponse getBad(final String path) throws InterruptedException, ExecutionException, TimeoutException {
         ContentResponse response = client.newRequest(resourceUrl + path).method(HttpMethod.GET).send();
-        assertEquals(HTTP_404_NOTFOUND, response.getStatus());
+        assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus());
         return response;
     }
-
-    @Override
-    public String getServiceVersion() {
-        return versionString;
-    }
-
-    @Override
-    public String getServiceName() {
-        return serviceName;
-    }
-
-    @Override
-    protected String getDocUrl() {
-        return baseUrl + "/" + getServiceName() + "/doc/" + getServiceName() + "-swagger.yaml";
-    }
 }
\ No newline at end of file
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/ServiceIntegrationTest.java	Thu Oct 26 12:20:51 2017 +0200
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/ServiceIntegrationTest.java	Thu Oct 26 16:07:31 2017 +0200
@@ -41,21 +41,25 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeoutException;
-
 import org.eclipse.jetty.client.api.ContentResponse;
 import org.eclipse.jetty.client.api.Request;
 import org.eclipse.jetty.http.HttpMethod;
 import org.eclipse.jetty.http.HttpStatus;
 import org.junit.Test;
 
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+
 /**
  * Base class for integration tests with swagger API docs.
  *
  */
 public abstract class ServiceIntegrationTest extends IntegrationTest {
 
+    private static final JsonParser jsonParser = new JsonParser();
+    private static final String VERSION_ELEM = "version";
+
     public ServiceIntegrationTest(String serviceUrl) {
         super(serviceUrl);
     }
@@ -65,24 +69,66 @@
     public abstract String getServiceName();
 
     @Test(timeout = 5000)
-    public void verifySwaggerYamlAccessible() throws ExecutionException, InterruptedException, TimeoutException {
+    public void verifySwaggerYamlAccessible() throws Exception {
+        TestCallback test = new TestCallback() {
+            @Override
+            public void performTest() throws Exception {
+                final String swaggerDocsUrl = getDocUrl();
+                final Request request = client.newRequest(swaggerDocsUrl);
+                final ContentResponse response = request.method(HttpMethod.GET).send();
+                assertEquals("Expected OK response (no auth should be required). URL was: " + swaggerDocsUrl, HttpStatus.OK_200, response.getStatus());
+                final String actual = response.getContentAsString();
+                assertNotNull(actual);
+                assertFalse(actual.isEmpty());
+                assertTrue("service name expected to be part of swagger spec", actual.contains(getServiceName()));
+                assertTrue("service version expected to be part of swagger spec", actual.contains(getServiceVersion()));
+            }
+        };
+        wrapUnauthenticated(test);
+    }
+
+    @Test(timeout = 5000)
+    public void verifyVersionEndpoint() throws Exception {
+        final String serviceVersion = getServiceVersion();
+        TestCallback test = new TestCallback() {
+            @Override
+            public void performTest() throws Exception {
+                final String versionUrl = getVersionUrl();
+                final Request request = client.newRequest(versionUrl);
+                final ContentResponse response = request.method(HttpMethod.GET).send();
+                assertEquals("Expected OK response (no auth should be required). URL was: " + versionUrl, HttpStatus.OK_200, response.getStatus());
+                final String versionJson = response.getContentAsString();
+                assertNotNull(versionJson);
+                assertEquals(serviceVersion, getVersionFromJson(versionJson));
+            }
+        };
+        wrapUnauthenticated(test);
+    }
+
+    private void wrapUnauthenticated(TestCallback actualTest) throws Exception {
         try {
             removeAuthentication(client); // be sure to perform get with no authentication
-            final String swaggerDocsUrl = getDocUrl();
-            final Request request = client.newRequest(swaggerDocsUrl);
-            final ContentResponse response = request.method(HttpMethod.GET).send();
-            assertEquals("Expected OK response (no auth should be required). URL was: " + swaggerDocsUrl, HttpStatus.OK_200, response.getStatus());
-            final String actual = response.getContentAsString();
-            assertNotNull(actual);
-            assertFalse(actual.isEmpty());
-            assertTrue("service name expected to be part of swagger spec", actual.contains(getServiceName()));
-            assertTrue("service version expected to be part of swagger spec", actual.contains(getServiceVersion()));
+            actualTest.performTest();
         } finally {
             addAuthentication(client);
         }
     }
 
+    private String getVersionFromJson(String json) {
+        JsonObject versionObj = (JsonObject)jsonParser.parse(json);
+        JsonElement versionElem = versionObj.get(VERSION_ELEM);
+        return versionElem.getAsString();
+    }
+
+    protected String getVersionUrl() {
+        return baseUrl + "/" + getServiceName() + "/version";
+    }
+
     protected String getDocUrl() {
         return baseUrl + "/" + getServiceName() + "/" + getServiceVersion() + "/doc/" + getServiceName() + "-swagger.yaml";
     }
+
+    static interface TestCallback {
+        void performTest() throws Exception;
+    }
 }