From f86d08ca5598571410386372f3f06d5c8c686c74 Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Tue, 9 Feb 2021 18:21:49 +0000 Subject: Changes related to multi policy handling improvement in APEX This review fixes an issue identified during testing the changes done for improving multiple policy handling in APEX. Changes done to a few test files in the previous review are reverted as well. Change-Id: I98324da708239d314aadd4c45dc377137fd552ba Issue-ID: POLICY-2883 Signed-off-by: a.sreekumar --- .../apex/service/engine/main/ApexMainTest.java | 97 ++++++++++------------ 1 file changed, 44 insertions(+), 53 deletions(-) (limited to 'services/services-engine/src/test/java/org') diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexMainTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexMainTest.java index 2cb12c397..5764a5275 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexMainTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexMainTest.java @@ -23,7 +23,6 @@ package org.onap.policy.apex.service.engine.main; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -31,10 +30,10 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; -import java.io.OutputStream; import java.io.PrintStream; import java.util.concurrent.TimeUnit; import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.service.ModelService; @@ -44,7 +43,20 @@ import org.onap.policy.common.parameters.ParameterService; * Test the ApexMain class. */ public class ApexMainTest { - private PrintStream stdout = System.out; + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream stdout = System.out; + private ApexMain apexMain1; + private ApexMain apexMain2; + + /** + * Method for set up before each test. + * + * @throws Exception if an error occurs + */ + @Before + public void setUp() throws Exception { + System.setOut(new PrintStream(outContent)); + } /** * Method for cleanup after each test. @@ -53,14 +65,17 @@ public class ApexMainTest { */ @After public void teardown() throws Exception { + if (null != apexMain1) { + apexMain1.shutdown(); + } + if (null != apexMain2) { + apexMain2.shutdown(); + } System.setOut(stdout); } @Test public void testNullParameters() throws ApexException { - OutputStream outContent = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outContent)); - ApexMain.main(null); await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString() .contains("Tosca Policy file was not specified as an argument")); @@ -70,95 +85,71 @@ public class ApexMainTest { @Test public void testBadArguments() throws ApexException { - OutputStream outContent = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outContent)); - String[] args = { "-whee" }; - final ApexMain apexMain = new ApexMain(args); + apexMain1 = new ApexMain(args); await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString() .contains("invalid command line arguments specified : Unrecognized option: -whee")); - assertNotNull(apexMain); - apexMain.shutdown(); + assertNotNull(apexMain1); } @Test public void testHelp() throws ApexException { - OutputStream outContent = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outContent)); - String[] args = { "-h" }; - final ApexMain apexMain = new ApexMain(args); + apexMain1 = new ApexMain(args); await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString() .contains("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); - assertNotNull(apexMain); - apexMain.shutdown(); + assertNotNull(apexMain1); } @Test public void testBadParameters() throws ApexException { - OutputStream outContent = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outContent)); - String[] args = { "-p", "src/test/resources/parameters/badParams.json" }; - final ApexMain apexMain = new ApexMain(args); + apexMain1 = new ApexMain(args); await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString() .contains("parameter group has status INVALID")); - assertNotNull(apexMain); - apexMain.shutdown(); + assertNotNull(apexMain1); } @Test public void testCorrectParameters() throws ApexException { - OutputStream outContent = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outContent)); - String[] args = {"-p", "src/test/resources/parameters/correctParams.json"}; - final ApexMain apexMain = new ApexMain(args); - assertEquals("MyApexEngine", apexMain.getApexParameters().getEngineServiceParameters().getName()); + apexMain1 = new ApexMain(args); + assertEquals("MyApexEngine", apexMain1.getApexParameters().getEngineServiceParameters().getName()); await().atMost(200, TimeUnit.MILLISECONDS) .until(() -> outContent.toString().contains("Added the action listener to the engine")); - assertTrue(apexMain.isAlive()); - apexMain.shutdown(); + assertTrue(apexMain1.isAlive()); } @Test public void testJavaProperties() throws ApexException { - OutputStream outContent = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outContent)); - String[] args = {"-p", "src/test/resources/parameters/correctParamsJavaProperties.json"}; - final ApexMain apexMain = new ApexMain(args); - assertEquals("MyApexEngine", apexMain.getApexParameters().getEngineServiceParameters().getName()); + apexMain1 = new ApexMain(args); + assertEquals("MyApexEngine", apexMain1.getApexParameters().getEngineServiceParameters().getName()); assertEquals("trust-store-file", System.getProperty("javax.net.ssl.trustStore")); assertEquals("Pol1cy_0nap", System.getProperty("javax.net.ssl.trustStorePassword")); await().atMost(10000, TimeUnit.MILLISECONDS) .until(() -> outContent.toString().contains("Added the action listener to the engine")); - apexMain.shutdown(); } @Test public void testCorrectParametersWithMultiplePolicies() throws ApexException { - OutputStream outContent = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outContent)); String[] args1 = {"-p", "src/test/resources/parameters/correctParams.json"}; String[] args2 = {"-p", "src/test/resources/parameters/correctParams2.json"}; - final ApexMain apexMain1 = new ApexMain(args1); - final ApexMain apexMain2 = new ApexMain(args2); + apexMain1 = new ApexMain(args1); + apexMain2 = new ApexMain(args2); assertEquals("MyApexEngine", apexMain1.getApexParameters().getEngineServiceParameters().getName()); assertEquals("MyApexEngine2", apexMain2.getApexParameters().getEngineServiceParameters().getName()); + assertTrue(apexMain1.isAlive()); + assertTrue(apexMain2.isAlive()); final String outString = outContent.toString(); assertThat(outString).contains("Added the action listener to the engine") .contains("Created apex engine MyApexEngine").contains("Created apex engine MyApexEngine2"); - assertTrue(apexMain1.isAlive()); - assertTrue(apexMain2.isAlive()); - apexMain1.shutdown(); - apexMain2.shutdown(); ModelService.clear(); ParameterService.clear(); } @@ -166,21 +157,21 @@ public class ApexMainTest { @Test public void testInCorrectParametersWithMultiplePolicies() throws ApexException { String[] args = {"-p", "src/test/resources/parameters/correctParams.json"}; - final ApexMain apexMain1 = new ApexMain(args); - assertThatThrownBy(() -> new ApexMain(args)).hasMessage("start of Apex service failed because this" + apexMain1 = new ApexMain(args); + apexMain2 = new ApexMain(args); + assertTrue(apexMain1.isAlive()); + assertFalse(apexMain2.isAlive()); + final String outString = outContent.toString(); + assertThat(outString).contains("start of Apex service failed because this" + " policy has the following duplicate I/O parameters: [TheFileConsumer1]/[FirstProducer]"); - apexMain1.shutdown(); } @Test public void testInvalidArgsWithMultiplePolicies() throws ApexException { - OutputStream outContent = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outContent)); String[] args = {"-c", "file1", "-m", "file2"}; - final ApexMain apexMain = new ApexMain(args); + apexMain1 = new ApexMain(args); + assertFalse(apexMain1.isAlive()); final String outString = outContent.toString(); - apexMain.shutdown(); assertThat(outString).contains("Arguments validation failed", "start of Apex service failed"); - assertFalse(apexMain.isAlive()); // No policy is running in the engine } } -- cgit 1.2.3-korg