diff options
4 files changed, 43 insertions, 82 deletions
diff --git a/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/models/sim/dmaap/rest/CommonRestServer.java b/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/models/sim/dmaap/rest/CommonRestServer.java index f0ce201c7..7b72e9250 100644 --- a/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/models/sim/dmaap/rest/CommonRestServer.java +++ b/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/models/sim/dmaap/rest/CommonRestServer.java @@ -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(); - } } /** @@ -135,28 +85,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. * * @param endpoint the target endpoint diff --git a/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/e2e/EndToEndTest.java b/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/e2e/EndToEndTest.java index 94fbc4fd3..066c38bb6 100644 --- a/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/e2e/EndToEndTest.java +++ b/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/e2e/EndToEndTest.java @@ -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); + } + } } diff --git a/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/startstop/DmaapSimActivatorTest.java b/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/startstop/DmaapSimActivatorTest.java index 30b65806f..810c16bc6 100644 --- a/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/startstop/DmaapSimActivatorTest.java +++ b/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/startstop/DmaapSimActivatorTest.java @@ -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); diff --git a/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/startstop/MainTest.java b/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/startstop/MainTest.java index b3f83ddde..cceeaf5af 100644 --- a/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/startstop/MainTest.java +++ b/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/sim/dmaap/startstop/MainTest.java @@ -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}; |