diff options
Diffstat (limited to 'services/services-engine')
2 files changed, 54 insertions, 62 deletions
diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java index 65c2acffa..3e9072dd4 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java @@ -71,7 +71,7 @@ public class ApexMain { * @param args the command line arguments * @throws ApexException the apex exception. */ - public ApexMain(final String[] args) throws ApexException { + public ApexMain(final String[] args) { LOGGER.entry("Starting Apex service with parameters " + Arrays.toString(args) + " . . ."); try { apexParameters = populateApexParameters(args); @@ -79,17 +79,18 @@ public class ApexMain { LOGGER.error(APEX_SERVICE_FAILED_MSG, e); return; } - aggregateParametersAndRegister(); + try { + aggregateParametersAndRegister(); - // Now, create the activator for the Apex service - activator = new ApexActivator(apexParameters); + // Now, create the activator for the Apex service + activator = new ApexActivator(apexParameters); - // Start the activator - try { + // Start the activator activator.initialize(); setAlive(true); - } catch (final ApexActivatorException e) { - throw new ApexException("start of Apex service failed, used parameters are " + Arrays.toString(args), e); + } catch (final ApexException e) { + LOGGER.error("start of Apex service failed, used parameters are {}", Arrays.toString(args), e); + return; } // Add a shutdown hook to shut everything down in an orderly manner @@ -246,7 +247,7 @@ public class ApexMain { * @param args the arguments * @throws ApexException the apex exception. */ - public static void main(final String[] args) throws ApexException { + public static void main(final String[] args) { new ApexMain(args); } } 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 } } |