changeset 173:81771b842d01

Extract mongo code from integration tests into extendable class Reviewed-by: jerboaa, jkang Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-May/023388.html Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023401.html
author Christopher Koehler <chkoehle@redhat.com>
date Mon, 12 Jun 2017 11:36:02 -0400
parents e51637bcfdf9
children 1ce424035c41
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/service/systems/SystemInfoIntegrationTest.java tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/IntegrationTest.java tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/MongoIntegrationTest.java
diffstat 6 files changed, 106 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/gc/JvmGcServiceIntegrationTest.java	Thu Jun 08 16:55:46 2017 +0200
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/gc/JvmGcServiceIntegrationTest.java	Mon Jun 12 11:36:02 2017 -0400
@@ -42,28 +42,22 @@
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeoutException;
 
+import com.redhat.thermostat.gateway.tests.integration.MongoIntegrationTest;
 import org.eclipse.jetty.client.api.ContentResponse;
 import org.eclipse.jetty.client.util.StringContentProvider;
 import org.eclipse.jetty.http.HttpMethod;
-import org.junit.Before;
 import org.junit.Test;
 
-import com.redhat.thermostat.gateway.tests.integration.IntegrationTest;
+public class JvmGcServiceIntegrationTest extends MongoIntegrationTest {
 
-public class JvmGcServiceIntegrationTest extends IntegrationTest {
-
-    private static final String collectionName = "jvm-gc";
-    private static final String gcUrl = baseUrl + "/" + collectionName + "/0.0.2";
+    private static final String serviceName = "jvm-gc";
+    private static final String versionNumber = "0.0.2";
+    private static final String gcUrl = baseUrl + "/" + serviceName + "/" + versionNumber;
     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);
+        super(serviceName + "/" + versionNumber, serviceName);
     }
 
     private void makeHttpGetRequest(String url, String expectedResponse, int expectedStatus)
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/memory/JvmMemoryServiceIntegrationTest.java	Thu Jun 08 16:55:46 2017 +0200
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/memory/JvmMemoryServiceIntegrationTest.java	Mon Jun 12 11:36:02 2017 -0400
@@ -36,27 +36,21 @@
 
 package com.redhat.thermostat.gateway.service.jvm.memory;
 
-import com.redhat.thermostat.gateway.tests.integration.IntegrationTest;
+import com.redhat.thermostat.gateway.tests.integration.MongoIntegrationTest;
 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 {
+public class JvmMemoryServiceIntegrationTest extends MongoIntegrationTest {
 
-    private static final String collectionName = "jvm-memory";
+    private static final String serviceName = "jvm-memory";
     private static final String versionNumber = "0.0.2";
 
     public JvmMemoryServiceIntegrationTest() {
-        super(collectionName + "/" + versionNumber);
-    }
-
-    @Before
-    public void beforeIntegrationTest() {
-        mongodTestUtil.dropCollection(collectionName);
+        super(serviceName + "/" + versionNumber, serviceName);
     }
 
     @Test
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvms/JvmsServiceIntegrationTest.java	Thu Jun 08 16:55:46 2017 +0200
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvms/JvmsServiceIntegrationTest.java	Mon Jun 12 11:36:02 2017 -0400
@@ -41,15 +41,13 @@
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeoutException;
 
+import com.redhat.thermostat.gateway.tests.integration.MongoIntegrationTest;
 import org.eclipse.jetty.client.api.ContentResponse;
 import org.eclipse.jetty.client.util.StringContentProvider;
 import org.eclipse.jetty.http.HttpMethod;
-import org.junit.Before;
 import org.junit.Test;
 
-import com.redhat.thermostat.gateway.tests.integration.IntegrationTest;
-
-public class JvmsServiceIntegrationTest extends IntegrationTest {
+public class JvmsServiceIntegrationTest extends MongoIntegrationTest {
     private static final String collectionName = "jvm-info";
     private static final String jvmsUrl = baseUrl + "/jvms/0.0.1";
 
@@ -87,12 +85,7 @@
                 ", \"systemId\" : \"invalid\"}]";
 
     public JvmsServiceIntegrationTest() {
-        super(jvmsUrl);
-    }
-
-    @Before
-    public void beforeIntegrationTest() {
-        mongodTestUtil.dropCollection(collectionName);
+        super(jvmsUrl, "jvm-info");
     }
 
     @Test
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/systems/SystemInfoIntegrationTest.java	Thu Jun 08 16:55:46 2017 +0200
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/systems/SystemInfoIntegrationTest.java	Mon Jun 12 11:36:02 2017 -0400
@@ -40,7 +40,7 @@
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
-import com.redhat.thermostat.gateway.tests.integration.IntegrationTest;
+import com.redhat.thermostat.gateway.tests.integration.MongoIntegrationTest;
 import org.eclipse.jetty.client.api.ContentResponse;
 import org.eclipse.jetty.client.api.Request;
 import org.eclipse.jetty.client.util.StringContentProvider;
@@ -57,10 +57,10 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
-public class SystemInfoIntegrationTest extends IntegrationTest {
+public class SystemInfoIntegrationTest extends MongoIntegrationTest {
 
-    private static final String collectionName = "system-info";
-    private static final String serviceURL = baseUrl + "/systems/0.0.1";
+    private static final String serviceName = "system-info";
+    private static final String versionNumber = "0.0.1";
     private static final int HTTP_200_OK = 200;
     private static final String CPU_STRING1 = "Intel";
     private static final String CPU_STRING2 = "AMD";
@@ -93,7 +93,7 @@
     }
 
     public SystemInfoIntegrationTest() {
-        super(serviceURL);
+        super("systems/" + versionNumber, serviceName);
     }
 
     @Before
@@ -109,17 +109,17 @@
         final String systemid2 = getRandomSystemId();
         postSystemInfo(systemid2);
 
-        ContentResponse response = client.newRequest(serviceURL).method(HttpMethod.GET).send();
+        ContentResponse response = client.newRequest(resourceUrl).method(HttpMethod.GET).send();
         assertEquals(HTTP_200_OK, response.getStatus());
         final List<TinyHostInfo> list = parseHostInfo(response, null);
         assertEquals(1, list.size());
 
-        ContentResponse response2 = client.newRequest(serviceURL + "?limit=99").method(HttpMethod.GET).send();
+        ContentResponse response2 = client.newRequest(resourceUrl + "?limit=99").method(HttpMethod.GET).send();
         assertEquals(HTTP_200_OK, response2.getStatus());
         final List<TinyHostInfo> list2 = parseHostInfo(response2, null);
         assertEquals(2, list2.size());
 
-        ContentResponse response3 = client.newRequest(serviceURL + "?limit=0").method(HttpMethod.GET).send();
+        ContentResponse response3 = client.newRequest(resourceUrl + "?limit=0").method(HttpMethod.GET).send();
         assertEquals(HTTP_200_OK, response3.getStatus());
         final List<TinyHostInfo> list3 = parseHostInfo(response3, null);
         assertEquals(2, list3.size());
@@ -127,7 +127,7 @@
 
     @Test
     public void testGetAllEmpty() throws InterruptedException, TimeoutException, ExecutionException {
-        ContentResponse response = client.newRequest(serviceURL).method(HttpMethod.GET).send();
+        ContentResponse response = client.newRequest(resourceUrl).method(HttpMethod.GET).send();
         assertEquals(HTTP_200_OK, response.getStatus());
         final List<TinyHostInfo> list = parseHostInfo(response, null);
         assertTrue(list.isEmpty());
@@ -197,7 +197,7 @@
     }
 
     private ContentResponse postSystemInfo(final String systemid) throws InterruptedException, ExecutionException, TimeoutException {
-        final Request request = client.newRequest(serviceURL + "/systems/" + systemid);
+        final Request request = client.newRequest(resourceUrl + "/systems/" + systemid);
         request.header("Content-Type", "application/json");
         request.content(new StringContentProvider("[" + createSystemInfoJSON(systemid) + "]"));
         ContentResponse response = request.method(HttpMethod.POST).send();
@@ -244,7 +244,7 @@
     }
 
     private ContentResponse putSystemInfo(final String systemid, final String cpuid) throws InterruptedException, ExecutionException, TimeoutException {
-        final Request request = client.newRequest(serviceURL + "/systems/" + systemid);
+        final Request request = client.newRequest(resourceUrl + "/systems/" + systemid);
         request.header("Content-Type", "application/json");
         final String contentStr = createSystemInfoJSON(systemid).replace(CPU_STRING1, cpuid);
         request.content(new StringContentProvider("{ \"set\" : " +contentStr + "}"));
@@ -268,13 +268,13 @@
     }
 
     private ContentResponse getSystemInfo(final String systemid) throws InterruptedException, ExecutionException, TimeoutException {
-        ContentResponse response = client.newRequest(serviceURL + "/systems/" + systemid).method(HttpMethod.GET).send();
+        ContentResponse response = client.newRequest(resourceUrl + "/systems/" + systemid).method(HttpMethod.GET).send();
         assertEquals(HTTP_200_OK, response.getStatus());
         return response;
     }
 
     private ContentResponse deleteSystemInfo(final String systemid) throws InterruptedException, ExecutionException, TimeoutException {
-        ContentResponse response = client.newRequest(serviceURL + "/systems/" + systemid).method(HttpMethod.DELETE).send();
+        ContentResponse response = client.newRequest(resourceUrl + "/systems/" + systemid).method(HttpMethod.DELETE).send();
         assertEquals(HTTP_200_OK, response.getStatus());
         final String expected = "";
         assertEquals(expected, response.getContentAsString());
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/IntegrationTest.java	Thu Jun 08 16:55:46 2017 +0200
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/IntegrationTest.java	Mon Jun 12 11:36:02 2017 -0400
@@ -57,7 +57,7 @@
     protected static String baseUrl = "http://127.0.0.1:30000";
 
     protected static final MongodTestUtil mongodTestUtil = new MongodTestUtil();
-    private static final Path distributionImage;
+    protected static final Path distributionImage;
 
     static {
         final String distDir = System.getProperty(GlobalConstants.GATEWAY_HOME_ENV, System.getenv(GlobalConstants.GATEWAY_HOME_ENV));
@@ -75,23 +75,12 @@
 
     @BeforeClass
     public static void beforeClassIntegrationTest() throws Exception {
-        mongodTestUtil.startMongod();
-        setupMongoCredentials();
-
         client = new HttpClient();
         client.start();
 
         startServer();
     }
 
-    private static void setupMongoCredentials() throws IOException, InterruptedException {
-        Path mongoSetup = distributionImage.resolve("etc/mongo-dev-setup.js");
-
-        ProcessBuilder processBuilder = new ProcessBuilder().command("mongo", mongodTestUtil.listenAddress, mongoSetup.toAbsolutePath().toString()).inheritIO();
-        Process mongoProcess = processBuilder.start();
-        mongoProcess.waitFor();
-    }
-
     private static Thread serverThread = null;
     private static Start serverObject = null;
 
@@ -161,7 +150,6 @@
 
     @AfterClass
     public static void afterClassIntegrationTest() throws Exception {
-        mongodTestUtil.stopMongod();
         client.stop();
         stopServer();
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/MongoIntegrationTest.java	Mon Jun 12 11:36:02 2017 -0400
@@ -0,0 +1,79 @@
+/*
+ * 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.integration;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.Objects;
+
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+
+public class MongoIntegrationTest extends IntegrationTest {
+
+    protected String collectionName;
+
+    public MongoIntegrationTest(String serviceUrl, String collectionName) {
+        super(serviceUrl);
+        this.collectionName = Objects.requireNonNull(collectionName);
+    }
+
+    @BeforeClass
+    public static void beforeClassMongoIntegrationTest() throws Exception {
+        mongodTestUtil.startMongod();
+        setupMongoCredentials();
+    }
+
+    @Before
+    public void beforeIntegrationTest() {
+        mongodTestUtil.dropCollection(collectionName);
+    }
+
+    private static void setupMongoCredentials() throws IOException, InterruptedException {
+        Path mongoSetup = distributionImage.resolve("etc/mongo-dev-setup.js");
+
+        ProcessBuilder processBuilder = new ProcessBuilder().command("mongo", mongodTestUtil.listenAddress, mongoSetup.toAbsolutePath().toString()).inheritIO();
+        Process mongoProcess = processBuilder.start();
+        mongoProcess.waitFor();
+    }
+
+    @AfterClass
+    public static void afterClassMongoIntegrationTest() throws Exception {
+        mongodTestUtil.stopMongod();
+    }
+}