Remove annotations from dmaap common test class 89/111289/3
authorJim Hahn <jrh3@att.com>
Fri, 14 Aug 2020 19:25:35 +0000 (15:25 -0400)
committerJim Hahn <jrh3@att.com>
Fri, 14 Aug 2020 19:45:05 +0000 (15:45 -0400)
Jenkins builds are randomly failing in the junits for the dmaap simulator.
From the console output, it appears that the simulator is being shut down
before all of the test methods have executed.  Moved the code for
managing the "Main" object from the common superclass to the only
subclass that needed it.

Issue-ID: POLICY-2749
Change-Id: I2615bbef9b790dbb633ed7f123aeb3f880826862
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 f0ce201..7b72e92 100644 (file)
@@ -30,25 +30,15 @@ import javax.ws.rs.client.WebTarget;
 import javax.ws.rs.core.MediaType;
 import lombok.Getter;
 import org.glassfish.jersey.client.ClientProperties;
-import org.junit.AfterClass;
-import org.junit.Before;
 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
 import org.onap.policy.common.gson.GsonMessageBodyHandler;
 import org.onap.policy.common.utils.network.NetworkUtil;
-import org.onap.policy.common.utils.services.Registry;
-import org.onap.policy.models.sim.dmaap.DmaapSimException;
-import org.onap.policy.models.sim.dmaap.startstop.Main;
 import org.onap.policy.sim.dmaap.parameters.CommonTestData;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Common base class for rest server tests.
  */
 public class CommonRestServer {
-
-    private static final Logger LOGGER = LoggerFactory.getLogger(CommonRestServer.class);
-
     public static final String NOT_ALIVE = "not alive";
     public static final String ALIVE = "alive";
     public static final String SELF = "self";
@@ -62,17 +52,12 @@ public class CommonRestServer {
 
     protected static String httpPrefix;
 
-    private static Main main;
-
     /**
-     * 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
+     * Allocates a port for the server, writes a config file.
      *
      * @throws Exception if an error occurs
      */
-    public static void reconfigure(boolean shouldStartMain) throws Exception {
+    public static void reconfigure() throws Exception {
         port = NetworkUtil.allocPort();
 
         httpPrefix = "http://localhost:" + port + "/";
@@ -81,41 +66,6 @@ public class CommonRestServer {
         makeConfigFile(CONFIG_FILE, json);
 
         HttpServletServerFactoryInstance.getServerFactory().destroy();
-
-        if (shouldStartMain) {
-            startMain();
-        }
-    }
-
-    /**
-     * Stops Main.
-     */
-    @AfterClass
-    public static void teardownAfterClass() {
-        try {
-            if (main != null) {
-                Main main2 = main;
-                main = null;
-
-                main2.shutdown();
-            }
-
-        } catch (DmaapSimException exp) {
-            LOGGER.error("cannot stop main", exp);
-        }
-    }
-
-    /**
-     * Set up.
-     *
-     * @throws Exception if an error occurs
-     */
-    @Before
-    public void setUp() throws Exception {
-        // restart, if not currently running
-        if (main == null) {
-            startMain();
-        }
     }
 
     /**
@@ -134,28 +84,6 @@ public class CommonRestServer {
         }
     }
 
-    /**
-     * Starts the "Main".
-     *
-     * @throws Exception if an error occurs
-     */
-    private static void startMain() throws Exception {
-        Registry.newRegistry();
-
-        // make sure port is available
-        if (NetworkUtil.isTcpPortOpen("localhost", port, 1, 1L)) {
-            throw new IllegalStateException("port " + port + " is still in use");
-        }
-
-        final String[] simConfigParameters = {"-c", "src/test/resources/parameters/TestConfigParams.json"};
-
-        main = new Main(simConfigParameters);
-
-        if (!NetworkUtil.isTcpPortOpen("localhost", port, 60, 1000L)) {
-            throw new IllegalStateException("server is not listening on port " + port);
-        }
-    }
-
     /**
      * Sends a request to an endpoint.
      *
index 94fbc4f..066c38b 100644 (file)
@@ -42,7 +42,11 @@ import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
 import org.onap.policy.common.endpoints.parameters.TopicParameters;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.network.NetworkUtil;
+import org.onap.policy.common.utils.services.Registry;
+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;
 
 /**
  * This tests the simulator using dmaap endpoints to verify that it works from publisher
@@ -54,6 +58,8 @@ public class EndToEndTest extends CommonRestServer {
     private static final String TOPIC2 = "MY-TOPIC-B";
     private static final int MAX_MSG = 200;
 
+    private static Main main;
+
     /**
      * Messages from the topic are placed here by the endpoint.
      */
@@ -78,7 +84,9 @@ public class EndToEndTest extends CommonRestServer {
     public static void setUpBeforeClass() throws Exception {
         TopicEndpointManager.getManager().shutdown();
 
-        CommonRestServer.reconfigure(true);
+        CommonRestServer.reconfigure();
+
+        startMain();
 
         queue = new LinkedBlockingQueue<>();
         queue2 = new LinkedBlockingQueue<>();
@@ -103,11 +111,13 @@ public class EndToEndTest extends CommonRestServer {
      * Stops the topics and clears the queues.
      */
     @AfterClass
-    public static void tearDownAfterClass() {
+    public static void tearDownAfterClass() throws DmaapSimException {
         TopicEndpointManager.getManager().shutdown();
 
         queue = null;
         queue2 = null;
+
+        main.shutdown();
     }
 
     /**
@@ -116,8 +126,7 @@ public class EndToEndTest extends CommonRestServer {
      * @throws CoderException if the parameters cannot be decoded
      */
     @Before
-    @Override
-    public void setUp() throws CoderException {
+    public void setUp() {
         queue.clear();
         queue2.clear();
     }
@@ -189,4 +198,28 @@ public class EndToEndTest extends CommonRestServer {
         assertEquals(testName + " message 1", msg1, queue.poll(MAX_WAIT_SEC, TimeUnit.SECONDS));
         assertEquals(testName + " message 2", msg2, queue.poll(MAX_WAIT_SEC, TimeUnit.SECONDS));
     }
+
+    /**
+     * Starts the "Main".
+     *
+     * @throws Exception if an error occurs
+     */
+    private static void startMain() throws Exception {
+        Registry.newRegistry();
+
+        int port = CommonRestServer.getPort();
+
+        // make sure port is available
+        if (NetworkUtil.isTcpPortOpen("localhost", port, 1, 1L)) {
+            throw new IllegalStateException("port " + port + " is still in use");
+        }
+
+        final String[] simConfigParameters = {"-c", "src/test/resources/parameters/TestConfigParams.json"};
+
+        main = new Main(simConfigParameters);
+
+        if (!NetworkUtil.isTcpPortOpen("localhost", port, 300, 200L)) {
+            throw new IllegalStateException("server is not listening on port " + port);
+        }
+    }
 }
index 30b6580..810c16b 100644 (file)
@@ -53,7 +53,7 @@ public class DmaapSimActivatorTest extends CommonRestServer {
         Registry.newRegistry();
         HttpServletServerFactoryInstance.getServerFactory().destroy();
 
-        CommonRestServer.reconfigure(false);
+        CommonRestServer.reconfigure();
 
         final String[] papConfigParameters = {"-c", CONFIG_FILE};
         final DmaapSimCommandLineArguments arguments = new DmaapSimCommandLineArguments(papConfigParameters);
index b3f83dd..cceeaf5 100644 (file)
@@ -42,7 +42,7 @@ public class MainTest extends CommonRestServer {
     private Main main;
 
     /**
-     * Set up.
+     * Sets up.
      */
     @Before
     public void setUp() {
@@ -64,7 +64,7 @@ public class MainTest extends CommonRestServer {
 
     @Test
     public void testMain() throws Exception {
-        CommonRestServer.reconfigure(false);
+        CommonRestServer.reconfigure();
         final String[] NormalParameters = {"-c", CONFIG_FILE};
         main = new Main(NormalParameters);
         assertTrue(main.getParameters().isValid());
@@ -82,7 +82,7 @@ public class MainTest extends CommonRestServer {
 
     @Test
     public void testMain_InvalidArguments() throws Exception {
-        CommonRestServer.reconfigure(false);
+        CommonRestServer.reconfigure();
 
         // note: this is missing the "-c" argument, thus the ARGUMENTS are invalid
         final String[] NormalParameters = {CONFIG_FILE};