Allocate unique port for dmaap simulator junits 08/110808/2
authorJim Hahn <jrh3@att.com>
Fri, 31 Jul 2020 11:51:33 +0000 (07:51 -0400)
committerJorge Hernandez <jorge.hernandez-herrero@att.com>
Fri, 31 Jul 2020 17:52:38 +0000 (17:52 +0000)
The junits for the dmaap simulator were randomly failing, because the
port was already in use.  Modified the junits to allocate their own,
individual ports.

Issue-ID: POLICY-2749
Change-Id: I3f3858091ee8f36ed00346ad9fa002a76d0348ae
Signed-off-by: Jim Hahn <jrh3@att.com>
models-sim/models-sim-dmaap/src/test/java/org/onap/policy/models/sim/dmaap/rest/CommonRestServer.java
models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/e2e/EndToEndTest.java
models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/startstop/DmaapSimActivatorTest.java
models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/startstop/MainTest.java

index 7e30c5a..f0ce201 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START=======================================================
- *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
+ *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -32,7 +32,6 @@ import lombok.Getter;
 import org.glassfish.jersey.client.ClientProperties;
 import org.junit.AfterClass;
 import org.junit.Before;
-import org.junit.BeforeClass;
 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
 import org.onap.policy.common.gson.GsonMessageBodyHandler;
 import org.onap.policy.common.utils.network.NetworkUtil;
@@ -56,6 +55,8 @@ public class CommonRestServer {
     public static final String NAME = "DMaaP Simulator";
     public static final String ENDPOINT_PREFIX = "events/";
 
+    protected static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json";
+
     @Getter
     private static int port;
 
@@ -66,20 +67,24 @@ public class CommonRestServer {
     /**
      * Allocates a port for the server, writes a config file, and then starts Main.
      *
+     * @param shouldStartMain {@code true} if Main should be started, {@code false}
+     *        otherwise
+     *
      * @throws Exception if an error occurs
      */
-    @BeforeClass
-    public static void setUpBeforeClass() throws Exception {
+    public static void reconfigure(boolean shouldStartMain) throws Exception {
         port = NetworkUtil.allocPort();
 
         httpPrefix = "http://localhost:" + port + "/";
 
         String json = new CommonTestData().getParameterGroupAsString(port);
-        makeConfigFile("src/test/resources/parameters/TestConfigParams.json", json);
+        makeConfigFile(CONFIG_FILE, json);
 
         HttpServletServerFactoryInstance.getServerFactory().destroy();
 
-        startMain();
+        if (shouldStartMain) {
+            startMain();
+        }
     }
 
     /**
index 50c3198..94fbc4f 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -78,7 +78,7 @@ public class EndToEndTest extends CommonRestServer {
     public static void setUpBeforeClass() throws Exception {
         TopicEndpointManager.getManager().shutdown();
 
-        CommonRestServer.setUpBeforeClass();
+        CommonRestServer.reconfigure(true);
 
         queue = new LinkedBlockingQueue<>();
         queue2 = new LinkedBlockingQueue<>();
index 380a724..30b6580 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 AT&T Intellectual Property.
+ *  Copyright (C) 2019-2020 AT&T Intellectual Property.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@ import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInst
 import org.onap.policy.common.utils.services.Registry;
 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterHandler;
+import org.onap.policy.models.sim.dmaap.rest.CommonRestServer;
 import org.onap.policy.models.sim.dmaap.startstop.DmaapSimActivator;
 import org.onap.policy.models.sim.dmaap.startstop.DmaapSimCommandLineArguments;
 
@@ -38,7 +39,7 @@ import org.onap.policy.models.sim.dmaap.startstop.DmaapSimCommandLineArguments;
 /**
  * Class to perform unit test of {@link DmaapSimActivator}}.
  */
-public class DmaapSimActivatorTest {
+public class DmaapSimActivatorTest extends CommonRestServer {
 
     private DmaapSimActivator activator;
 
@@ -52,7 +53,9 @@ public class DmaapSimActivatorTest {
         Registry.newRegistry();
         HttpServletServerFactoryInstance.getServerFactory().destroy();
 
-        final String[] papConfigParameters = {"-c", "parameters/NormalParameters.json"};
+        CommonRestServer.reconfigure(false);
+
+        final String[] papConfigParameters = {"-c", CONFIG_FILE};
         final DmaapSimCommandLineArguments arguments = new DmaapSimCommandLineArguments(papConfigParameters);
         final DmaapSimParameterGroup parGroup = new DmaapSimParameterHandler().getParameters(arguments);
 
index baa13a1..b3f83dd 100644 (file)
@@ -29,7 +29,7 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
-import org.onap.policy.models.sim.dmaap.DmaapSimException;
+import org.onap.policy.models.sim.dmaap.rest.CommonRestServer;
 import org.onap.policy.models.sim.dmaap.startstop.Main;
 import org.onap.policy.sim.dmaap.parameters.CommonTestData;
 
@@ -38,7 +38,7 @@ import org.onap.policy.sim.dmaap.parameters.CommonTestData;
  *
  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
  */
-public class MainTest {
+public class MainTest extends CommonRestServer {
     private Main main;
 
     /**
@@ -63,8 +63,9 @@ public class MainTest {
     }
 
     @Test
-    public void testMain() throws DmaapSimException {
-        final String[] NormalParameters = {"-c", "parameters/NormalParameters.json"};
+    public void testMain() throws Exception {
+        CommonRestServer.reconfigure(false);
+        final String[] NormalParameters = {"-c", CONFIG_FILE};
         main = new Main(NormalParameters);
         assertTrue(main.getParameters().isValid());
         assertEquals(CommonTestData.SIM_GROUP_NAME, main.getParameters().getName());
@@ -80,9 +81,11 @@ public class MainTest {
     }
 
     @Test
-    public void testMain_InvalidArguments() {
+    public void testMain_InvalidArguments() throws Exception {
+        CommonRestServer.reconfigure(false);
+
         // note: this is missing the "-c" argument, thus the ARGUMENTS are invalid
-        final String[] NormalParameters = {"parameters/NormalParameters.json"};
+        final String[] NormalParameters = {CONFIG_FILE};
         main = new Main(NormalParameters);
         assertNull(main.getParameters());
     }