changeset 164:e84c133dfe3e

Added test cases for the jvm-memory service Reviewed-by: jkang, jmatsuok Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-May/023379.html Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-May/023259.html
author Christopher Koehler <chkoehle@redhat.com>
date Wed, 31 May 2017 10:51:36 -0400
parents 5c0df0f4ea4a
children 8ff65314fb0d
files tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/gc/JvmGcServiceIntegrationTest.java tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/memory/JvmMemoryServiceIntegrationTest.java tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvms/JvmsServiceIntegrationTest.java tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/IntegrationTest.java tests/test-utils/pom.xml tests/test-utils/src/main/java/com/redhat/thermostat/gateway/tests/utils/HttpTestUtil.java
diffstat 6 files changed, 322 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/gc/JvmGcServiceIntegrationTest.java	Tue May 30 15:49:18 2017 -0400
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/gc/JvmGcServiceIntegrationTest.java	Wed May 31 10:51:36 2017 -0400
@@ -52,11 +52,15 @@
 
 public class JvmGcServiceIntegrationTest extends IntegrationTest {
 
-    private final String gcUrl = baseUrl + "/jvm-gc/0.0.2";
-    private final String collectionName = "jvm-gc";
+    private static final String collectionName = "jvm-gc";
+    private static final String gcUrl = baseUrl + "/" + collectionName + "/0.0.2";
     private final String data = "[{ \"a\" : \"test\", \"b\" : \"test1\", \"c\" : \"test2\" }, { \"d\" : \"test3\"}," +
             "{\"e\" : \"test4\" }]";
 
+    public JvmGcServiceIntegrationTest() {
+        super(collectionName);
+    }
+
     @Before
     public void beforeIntegrationTest() {
         mongodTestUtil.dropCollection(collectionName);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/memory/JvmMemoryServiceIntegrationTest.java	Wed May 31 10:51:36 2017 -0400
@@ -0,0 +1,206 @@
+/*
+ * Copyright 2012-2017 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.gateway.service.jvm.memory;
+
+import com.redhat.thermostat.gateway.tests.integration.IntegrationTest;
+import com.redhat.thermostat.gateway.tests.utils.HttpTestUtil;
+import org.eclipse.jetty.http.HttpMethod;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
+
+public class JvmMemoryServiceIntegrationTest extends IntegrationTest {
+
+    private static final String collectionName = "jvm-memory";
+    private static final String versionNumber = "0.0.2";
+
+    public JvmMemoryServiceIntegrationTest() {
+        super(collectionName + "/" + versionNumber);
+    }
+
+    @Before
+    public void beforeIntegrationTest() {
+        mongodTestUtil.dropCollection(collectionName);
+    }
+
+    @Test
+    public void testGet() throws InterruptedException, TimeoutException, ExecutionException {
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl, 200, HttpTestUtil.EMPTY_RESPONSE);
+    }
+
+    @Test
+    public void testGetWithQuery() throws InterruptedException, TimeoutException, ExecutionException {
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?l=10", 200, HttpTestUtil.EMPTY_RESPONSE);
+    }
+
+    @Test
+    public void testGetWithMalformedQuery() throws InterruptedException, TimeoutException, ExecutionException {
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?xyz=5", 200, HttpTestUtil.EMPTY_RESPONSE);
+    }
+
+    @Test
+    public void testPostProperlyAddsData() throws InterruptedException, TimeoutException, ExecutionException {
+        String expectedResponse = "{ \"response\" : [{ \"fakedata\" : \"test\" }] }";
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, "[{ \"fakedata\" : \"test\" }]", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl, 200, expectedResponse);
+    }
+
+    @Test
+    public void testMultiplePosts() throws InterruptedException, TimeoutException, ExecutionException {
+        String expectedResponse = "{ \"response\" : [{ \"fakedata\" : \"test\" },{ \"new\" : \"data\" }] }";
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, "[{ \"fakedata\" : \"test\" }]", 200);
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, "[{ \"new\" : \"data\" }]", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?l=5", 200, expectedResponse);
+    }
+
+    @Test
+    public void testPostPutAddsData() throws InterruptedException, TimeoutException, ExecutionException {
+        String expectedDataBeforePut = "{ \"response\" : [{ \"a\" : \"b\" }] }";
+        String expectedDataAfterPut = "{ \"response\" : [{ \"a\" : \"b\", \"x\" : \"y\" }] }";
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, "[{ \"a\" : \"b\" }]", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl, 200, expectedDataBeforePut);
+        HttpTestUtil.testContentResponse(client, HttpMethod.PUT, resourceUrl + "?q=a==b", "{ \"set\" : { \"x\" : \"y\" }}", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl, 200, expectedDataAfterPut);
+    }
+
+    @Test
+    public void testPostPutModifiesData() throws InterruptedException, TimeoutException, ExecutionException {
+        String expectedDataBeforePut = "{ \"response\" : [{ \"a\" : \"b\" }] }";
+        String expectedDataAfterPut = "{ \"response\" : [{ \"a\" : \"c\" }] }";
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, "[{ \"a\" : \"b\" }]", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl, 200, expectedDataBeforePut);
+        HttpTestUtil.testContentResponse(client, HttpMethod.PUT, resourceUrl + "?q=a==b", "{ \"set\" : { \"a\" : \"c\" }}", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl, 200, expectedDataAfterPut);
+    }
+
+    @Test
+    public void testDeleteProperlyDeletesData() throws InterruptedException, TimeoutException, ExecutionException {
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, "[{ \"fakedata\" : \"test\" }]", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.DELETE, resourceUrl + "?q=fakedata==test", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl, 200, HttpTestUtil.EMPTY_RESPONSE);
+    }
+
+    @Test
+    public void testMalformedDeleteRequestDoesNotMutateData() throws InterruptedException, TimeoutException, ExecutionException {
+        String expectedDataResponse = "{ \"response\" : [{ \"fakedata\" : \"test\" }] }";
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, "[{ \"fakedata\" : \"test\" }]", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl, 200, expectedDataResponse);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.DELETE, resourceUrl + "?q=nosuchkey==", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl, 200, expectedDataResponse);
+    }
+
+    @Test
+    public void testPutDataWithoutUrlQuery() throws InterruptedException, TimeoutException, ExecutionException {
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, "[{ \"fakedata\" : \"test\" }]", 200);
+        HttpTestUtil.testContentResponse(client, HttpMethod.PUT, resourceUrl, "{ \"set\" : { \"fakedata\" : \"test\" }}", 400);
+    }
+
+    @Test
+    public void testPostAndPutWithInvalidData() throws InterruptedException, TimeoutException, ExecutionException {
+        String expectedDataResponse = "{ \"response\" : [{ \"fakedata\" : \"test\" }] }";
+        String urlQuery = resourceUrl + "?q=nosuchkey==nosuchvalue";
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, urlQuery, "[{ \"fakedata\" : \"test\" }]", 200);
+        HttpTestUtil.testContentResponse(client, HttpMethod.PUT, urlQuery, "{ \"set\" : { \"fakedata\" : \"somethingnew\" }}", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?l=5", 200, expectedDataResponse);
+    }
+
+    @Test
+    public void testPutWithIdenticalData() throws InterruptedException, TimeoutException, ExecutionException {
+        String expectedDataResponse = "{ \"response\" : [{ \"fakedata\" : \"test\" }] }";
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, "[{ \"fakedata\" : \"test\" }]", 200);
+        HttpTestUtil.testContentResponse(client, HttpMethod.PUT, resourceUrl, "{ \"set\" : { \"fakedata\" : \"test\" }}", 400);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?l=5", 200, expectedDataResponse);
+    }
+
+    @Test
+    public void testPutDifferentData() throws InterruptedException, TimeoutException, ExecutionException {
+        String expectedDataResponse = "{ \"response\" : [{ \"a\" : \"b\", \"c\" : \"d\" }] }";
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, "[{ \"a\" : \"b\" }]", 200);
+        HttpTestUtil.testContentResponse(client, HttpMethod.PUT, resourceUrl + "?q=a==b", "{ \"set\" : { \"c\" : \"d\" }}", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?l=5", 200, expectedDataResponse);
+    }
+
+    @Test
+    public void testChangeDataWithPutMultipleTimes() throws InterruptedException, TimeoutException, ExecutionException {
+        String expectedData = "{ \"response\" : [{ \"a\" : \"a2\", \"b\" : \"b2\", \"c\" : \"c2\" }] }";
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, "[{ \"a\" : \"a2\" }]", 200);
+        HttpTestUtil.testContentResponse(client, HttpMethod.PUT, resourceUrl + "?q=a==a2", "{ \"set\" : { \"b\" : \"b2\" }}", 200);
+        HttpTestUtil.testContentResponse(client, HttpMethod.PUT, resourceUrl + "?q=a==a2", "{ \"set\" : { \"c\" : \"c2\" }}", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?l=5", 200, expectedData);
+    }
+
+    @Test
+    public void testGetWithBadUrlQuery() throws InterruptedException, TimeoutException, ExecutionException {
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?q=2", 400);
+    }
+
+    @Test
+    public void testGetLimitWithQuery() throws InterruptedException, TimeoutException, ExecutionException {
+        String data = "[{ \"a\" : \"a2\" },{ \"b\" : \"b2\" }]";
+        String expectedDataOne = "{ \"response\" : [{ \"a\" : \"a2\" }] }";
+        String expectedDataAll = "{ \"response\" : " + data + " }";
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, data, 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl, 200, expectedDataOne);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?l=2", 200, expectedDataAll);
+    }
+
+    @Test
+    public void testQueryOffset() throws InterruptedException, TimeoutException, ExecutionException {
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, "[{ \"a\" : \"1\" } , {\"b\" : \"2\"}, {\"c\" : \"3\"}]", 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?o=1", 200, "{ \"response\" : [{ \"b\" : \"2\" }] }");
+    }
+
+    @Test
+    public void testQueryOrdering() throws InterruptedException, TimeoutException, ExecutionException {
+        String content = "[{ \"a\" : 1 },{ \"a\" : 2 }]";
+        String expectedGet = "{ \"response\" : " + content + " }";
+        String expectedGetReverse = "{ \"response\" : [{ \"a\" : 2 },{ \"a\" : 1 }] }";
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, content, 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?l=10&s=+a", 200, expectedGet);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?l=10&s=-a", 200, expectedGetReverse);
+    }
+
+    @Test
+    public void testQueryProjection() throws InterruptedException, TimeoutException, ExecutionException {
+        String content = "[{ \"a\" : \"1\", \"b\" : \"2\", \"c\" : \"3\" }]";
+        String expectedGet = "{ \"response\" : [{ \"b\" : \"2\", \"c\" : \"3\" }] }";
+        HttpTestUtil.testContentResponse(client, HttpMethod.POST, resourceUrl, content, 200);
+        HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, resourceUrl + "?p=b,c", 200, expectedGet);
+    }
+}
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvms/JvmsServiceIntegrationTest.java	Tue May 30 15:49:18 2017 -0400
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvms/JvmsServiceIntegrationTest.java	Wed May 31 10:51:36 2017 -0400
@@ -50,8 +50,8 @@
 import com.redhat.thermostat.gateway.tests.integration.IntegrationTest;
 
 public class JvmsServiceIntegrationTest extends IntegrationTest {
-    private final String collectionName = "jvm-info";
-    private final String jvmsUrl = baseUrl + "/jvms/0.0.1";
+    private static final String collectionName = "jvm-info";
+    private static final String jvmsUrl = baseUrl + "/jvms/0.0.1";
 
     private final String postData = "[{ \"agentId\" : \"aid\", \"jvmId\" : \"jid1\", \"jvmPid\" : 1, \"startTime\" :" +
             "{ \"$numberLong\" : \"1495727607481\" }, \"stopTime\" : { \"$numberLong\" : \"-9223372036854775808\" }," +
@@ -86,6 +86,10 @@
         private final String postDataWithSystemId = postData.substring(0, postData.length() - 2) +
                 ", \"systemId\" : \"invalid\"}]";
 
+    public JvmsServiceIntegrationTest() {
+        super(jvmsUrl);
+    }
+
     @Before
     public void beforeIntegrationTest() {
         mongodTestUtil.dropCollection(collectionName);
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/IntegrationTest.java	Tue May 30 15:49:18 2017 -0400
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/IntegrationTest.java	Wed May 31 10:51:36 2017 -0400
@@ -69,6 +69,12 @@
 
     private static Process serverProcess;
 
+    protected String resourceUrl;
+
+    public IntegrationTest(String serviceUrl) {
+        this.resourceUrl = baseUrl + "/" + serviceUrl;
+    }
+
     @BeforeClass
     public static void beforeClassIntegrationTest() throws Exception {
         mongodTestUtil.startMongod();
--- a/tests/test-utils/pom.xml	Tue May 30 15:49:18 2017 -0400
+++ b/tests/test-utils/pom.xml	Wed May 31 10:51:36 2017 -0400
@@ -63,5 +63,17 @@
             <artifactId>mongo-java-driver</artifactId>
             <version>${mongo-java-driver.version}</version>
         </dependency>
+        <!-- HTTP test dependencies -->
+        <dependency>
+            <groupId>org.eclipse.jetty</groupId>
+            <artifactId>jetty-client</artifactId>
+            <version>${jetty.version}</version>
+        </dependency>
+        <!-- Test dependencies -->
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>${junit.version}</version>
+        </dependency>
     </dependencies>
 </project>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-utils/src/main/java/com/redhat/thermostat/gateway/tests/utils/HttpTestUtil.java	Wed May 31 10:51:36 2017 -0400
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2012-2017 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.gateway.tests.utils;
+
+import static org.junit.Assert.assertEquals;
+
+import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.client.api.ContentResponse;
+import org.eclipse.jetty.client.util.StringContentProvider;
+import org.eclipse.jetty.http.HttpMethod;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
+
+public class HttpTestUtil {
+
+    public static final String EMPTY_RESPONSE = "{ \"response\" : [] }";
+
+    public static void testContentlessResponse(HttpClient client,
+                                               HttpMethod httpMethod,
+                                               String url,
+                                               int expectedResponseStatus)
+            throws InterruptedException, TimeoutException, ExecutionException {
+        ContentResponse response = client.newRequest(url).method(httpMethod).send();
+        assertEquals(expectedResponseStatus, response.getStatus());
+    }
+
+    public static void testContentlessResponse(HttpClient client,
+                                               HttpMethod httpMethod,
+                                               String url,
+                                               int expectedResponseStatus,
+                                               String expectedResponseContent)
+            throws InterruptedException, TimeoutException, ExecutionException {
+        ContentResponse response = client.newRequest(url).method(httpMethod).send();
+        assertEquals(expectedResponseStatus, response.getStatus());
+        assertEquals(expectedResponseContent, response.getContentAsString());
+    }
+
+    public static void testContentResponse(HttpClient client,
+                                           HttpMethod httpMethod,
+                                           String url,
+                                           String content,
+                                           int expectedResponseStatus)
+            throws InterruptedException, TimeoutException, ExecutionException {
+        StringContentProvider stringContentProvider = new StringContentProvider(content, "UTF-8");
+        ContentResponse response = client.newRequest(url)
+                                         .method(httpMethod)
+                                         .content(stringContentProvider, "application/json")
+                                         .send();
+        assertEquals(expectedResponseStatus, response.getStatus());
+    }
+}