summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authora.sreekumar <ajith.sreekumar@bell.ca>2021-02-09 18:21:49 +0000
committerAjith Sreekumar <ajith.sreekumar@bell.ca>2021-02-11 10:01:27 +0000
commitf86d08ca5598571410386372f3f06d5c8c686c74 (patch)
tree644913448310e4ea5fe201c16554a2f739d49a43
parentc64320a45b77eb3474f1f3f7372f1cd1f85ddfef (diff)
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 <ajith.sreekumar@bell.ca>
-rw-r--r--examples/examples-servlet/src/main/java/org/onap/policy/apex/examples/servlet/ApexServletListener.java7
-rw-r--r--plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java49
-rw-r--r--services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java19
-rw-r--r--services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexMainTest.java97
-rw-r--r--services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java158
-rw-r--r--testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestFile2Rest.java93
-rw-r--r--testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestRest2File.java118
-rw-r--r--testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restserver/TestRestServer.java125
8 files changed, 343 insertions, 323 deletions
diff --git a/examples/examples-servlet/src/main/java/org/onap/policy/apex/examples/servlet/ApexServletListener.java b/examples/examples-servlet/src/main/java/org/onap/policy/apex/examples/servlet/ApexServletListener.java
index 3375ff869..80ef7354b 100644
--- a/examples/examples-servlet/src/main/java/org/onap/policy/apex/examples/servlet/ApexServletListener.java
+++ b/examples/examples-servlet/src/main/java/org/onap/policy/apex/examples/servlet/ApexServletListener.java
@@ -1,7 +1,6 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
- * Modifications Copyright (C) 2021 Bell Canada. 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.
@@ -76,11 +75,7 @@ public class ApexServletListener implements ServletContextListener {
}
// Initialize apex
- try {
- apexMain = new ApexMain(argsList.toArray(new String[argsList.size()]));
- } catch (ApexException e) {
- LOGGER.error("Apex Engine initialization failed", e);
- }
+ apexMain = new ApexMain(argsList.toArray(new String[argsList.size()]));
}
diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java
index 013046444..40e900413 100644
--- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java
+++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java
@@ -22,10 +22,9 @@
package org.onap.policy.apex.plugins.event.carrier.restrequestor;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
import com.google.gson.Gson;
import java.io.ByteArrayOutputStream;
@@ -36,6 +35,7 @@ import java.util.concurrent.TimeUnit;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
+import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -94,11 +94,22 @@ public class RestRequestorTest {
}
/**
- * Reset counters.
+ * Before test.
*/
@Before
- public void resetCounters() {
+ public void beforeTest() {
SupportRestRequestorEndpoint.resetCounters();
+ System.setOut(new PrintStream(outContent));
+ System.setErr(new PrintStream(errContent));
+ }
+
+ /**
+ * After test.
+ */
+ @After
+ public void afterTest() {
+ System.setOut(stdout);
+ System.setErr(stderr);
}
/**
@@ -269,14 +280,23 @@ public class RestRequestorTest {
/**
* Test REST requestor producer alone.
+ *
+ * @throws MessagingException the messaging exception
+ * @throws ApexException the apex exception
+ * @throws IOException Signals that an I/O exception has occurred.
*/
@Test
- public void testRestRequestorProducerAlone() {
+ public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException {
+
final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json"};
- assertThatThrownBy(() -> new ApexMain(args))
- .hasRootCauseMessage("REST Requestor producer (RestRequestorProducer) "
- + "must run in peered requestor mode with a REST Requestor consumer");
+ ApexMain apexMain = new ApexMain(args);
+ apexMain.shutdown();
+
+ final String outString = outContent.toString();
+
+ assertThat(outString).contains("REST Requestor producer (RestRequestorProducer) "
+ + "must run in peered requestor mode with a REST Requestor consumer");
}
/**
@@ -288,21 +308,12 @@ public class RestRequestorTest {
*/
@Test
public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
- System.setOut(new PrintStream(outContent));
- System.setErr(new PrintStream(errContent));
-
final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json"};
-
ApexMain apexMain = new ApexMain(args);
apexMain.shutdown();
-
final String outString = outContent.toString();
-
- System.setOut(stdout);
- System.setErr(stderr);
-
- assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
- + "does not exist or is not defined with the same peered mode"));
+ assertThat(outString).contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
+ + "does not exist or is not defined with the same peered mode");
}
private double getStatsFromServer(final Client client, final String statToGet) {
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
}
}
diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java
index 03e97215b..5a113a77d 100644
--- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java
+++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java
@@ -72,7 +72,7 @@ public class ApexEngineHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(ApexEngineHandler.class);
- private Map<ToscaConceptIdentifier, ApexMain> apexMainMap;
+ private Map<ToscaConceptIdentifier, ApexMain> apexMainMap = new LinkedHashMap<>();
/**
* Constructs the object. Extracts the config and model files from each policy and instantiates the apex engine.
@@ -82,12 +82,7 @@ public class ApexEngineHandler {
*/
public ApexEngineHandler(List<ToscaPolicy> policies) throws ApexStarterException {
LOGGER.debug("Starting apex engine.");
- apexMainMap = initiateApexEngineForPolicies(policies);
- if (apexMainMap.isEmpty()) {
- ModelService.clear();
- ParameterService.clear();
- throw new ApexStarterException("Apex Engine failed to start.");
- }
+ initiateApexEngineForPolicies(policies);
}
/**
@@ -118,11 +113,7 @@ public class ApexEngineHandler {
updateModelAndParameterServices(undeployedPoliciesMainMap);
}
if (!policiesToDeploy.isEmpty()) {
- Map<ToscaConceptIdentifier, ApexMain> mainMap = initiateApexEngineForPolicies(policiesToDeploy);
- if (mainMap.isEmpty()) {
- throw new ApexStarterException("Updating the APEX engine with new policies failed.");
- }
- apexMainMap.putAll(mainMap);
+ initiateApexEngineForPolicies(policiesToDeploy);
}
if (apexMainMap.isEmpty()) {
ModelService.clear();
@@ -168,65 +159,80 @@ public class ApexEngineHandler {
policyKeystoRetain.addAll(main.getActivator().getPolicyModel().getPolicies().getPolicyMap().keySet());
});
for (ApexMain main : undeployedPoliciesMainMap.values()) {
- ApexParameters existingParameters = ParameterService.get(ApexParameterConstants.MAIN_GROUP_NAME);
- List<String> eventInputParamKeysToRemove = main.getApexParameters().getEventInputParameters().keySet()
- .stream().filter(key -> !inputParamKeysToRetain.contains(key)).collect(Collectors.toList());
- List<String> eventOutputParamKeysToRemove = main.getApexParameters().getEventOutputParameters().keySet()
- .stream().filter(key -> !outputParamKeysToRetain.contains(key)).collect(Collectors.toList());
- eventInputParamKeysToRemove.forEach(existingParameters.getEventInputParameters()::remove);
- eventOutputParamKeysToRemove.forEach(existingParameters.getEventOutputParameters()::remove);
- EngineParameters engineParameters =
- main.getApexParameters().getEngineServiceParameters().getEngineParameters();
- final List<TaskParameters> taskParametersToRemove = engineParameters.getTaskParameters().stream()
- .filter(taskParameter -> !taskParametersToRetain.contains(taskParameter)).collect(Collectors.toList());
- final List<String> executorParamKeysToRemove = engineParameters.getExecutorParameterMap().keySet().stream()
- .filter(key -> !executorParamKeysToRetain.contains(key)).collect(Collectors.toList());
- final List<String> schemaParamKeysToRemove =
- engineParameters.getContextParameters().getSchemaParameters().getSchemaHelperParameterMap().keySet()
- .stream().filter(key -> !schemaParamKeysToRetain.contains(key)).collect(Collectors.toList());
- EngineParameters aggregatedEngineParameters =
- existingParameters.getEngineServiceParameters().getEngineParameters();
- aggregatedEngineParameters.getTaskParameters().removeAll(taskParametersToRemove);
- executorParamKeysToRemove.forEach(aggregatedEngineParameters.getExecutorParameterMap()::remove);
- schemaParamKeysToRemove.forEach(aggregatedEngineParameters.getContextParameters().getSchemaParameters()
- .getSchemaHelperParameterMap()::remove);
+ handleParametersRemoval(inputParamKeysToRetain, outputParamKeysToRetain, taskParametersToRetain,
+ executorParamKeysToRetain, schemaParamKeysToRetain, main);
+
+ if (null != main.getActivator() && null != main.getActivator().getPolicyModel()) {
+ handleAxConceptsRemoval(keyInfoKeystoRetain, schemaKeystoRetain, eventKeystoRetain, albumKeystoRetain,
+ taskKeystoRetain, policyKeystoRetain, main);
+ }
+ }
+ }
- final AxPolicyModel policyModel = main.getActivator().getPolicyModel();
- final List<AxArtifactKey> keyInfoKeystoRemove = policyModel.getKeyInformation().getKeyInfoMap().keySet()
- .stream().filter(key -> !keyInfoKeystoRetain.contains(key)).collect(Collectors.toList());
- final List<AxArtifactKey> schemaKeystoRemove = policyModel.getSchemas().getSchemasMap().keySet().stream()
- .filter(key -> !schemaKeystoRetain.contains(key)).collect(Collectors.toList());
- final List<AxArtifactKey> eventKeystoRemove = policyModel.getEvents().getEventMap().keySet().stream()
- .filter(key -> !eventKeystoRetain.contains(key)).collect(Collectors.toList());
- final List<AxArtifactKey> albumKeystoRemove = policyModel.getAlbums().getAlbumsMap().keySet().stream()
- .filter(key -> !albumKeystoRetain.contains(key)).collect(Collectors.toList());
- final List<AxArtifactKey> taskKeystoRemove = policyModel.getTasks().getTaskMap().keySet().stream()
- .filter(key -> !taskKeystoRetain.contains(key)).collect(Collectors.toList());
- final List<AxArtifactKey> policyKeystoRemove = policyModel.getPolicies().getPolicyMap().keySet().stream()
- .filter(key -> !policyKeystoRetain.contains(key)).collect(Collectors.toList());
+ private void handleParametersRemoval(Set<String> inputParamKeysToRetain, Set<String> outputParamKeysToRetain,
+ List<TaskParameters> taskParametersToRetain, List<String> executorParamKeysToRetain,
+ List<String> schemaParamKeysToRetain, ApexMain main) {
+ ApexParameters existingParameters = ParameterService.get(ApexParameterConstants.MAIN_GROUP_NAME);
+ List<String> eventInputParamKeysToRemove = main.getApexParameters().getEventInputParameters().keySet().stream()
+ .filter(key -> !inputParamKeysToRetain.contains(key)).collect(Collectors.toList());
+ List<String> eventOutputParamKeysToRemove = main.getApexParameters().getEventOutputParameters().keySet()
+ .stream().filter(key -> !outputParamKeysToRetain.contains(key)).collect(Collectors.toList());
+ eventInputParamKeysToRemove.forEach(existingParameters.getEventInputParameters()::remove);
+ eventOutputParamKeysToRemove.forEach(existingParameters.getEventOutputParameters()::remove);
+ EngineParameters engineParameters = main.getApexParameters().getEngineServiceParameters().getEngineParameters();
+ final List<TaskParameters> taskParametersToRemove = engineParameters.getTaskParameters().stream()
+ .filter(taskParameter -> !taskParametersToRetain.contains(taskParameter)).collect(Collectors.toList());
+ final List<String> executorParamKeysToRemove = engineParameters.getExecutorParameterMap().keySet().stream()
+ .filter(key -> !executorParamKeysToRetain.contains(key)).collect(Collectors.toList());
+ final List<String> schemaParamKeysToRemove =
+ engineParameters.getContextParameters().getSchemaParameters().getSchemaHelperParameterMap().keySet()
+ .stream().filter(key -> !schemaParamKeysToRetain.contains(key)).collect(Collectors.toList());
+ EngineParameters aggregatedEngineParameters =
+ existingParameters.getEngineServiceParameters().getEngineParameters();
+ aggregatedEngineParameters.getTaskParameters().removeAll(taskParametersToRemove);
+ executorParamKeysToRemove.forEach(aggregatedEngineParameters.getExecutorParameterMap()::remove);
+ schemaParamKeysToRemove.forEach(aggregatedEngineParameters.getContextParameters().getSchemaParameters()
+ .getSchemaHelperParameterMap()::remove);
+ }
- final Map<AxArtifactKey, AxKeyInfo> keyInfoMap =
- ModelService.getModel(AxKeyInformation.class).getKeyInfoMap();
- final Map<AxArtifactKey, AxContextSchema> schemasMap =
- ModelService.getModel(AxContextSchemas.class).getSchemasMap();
- final Map<AxArtifactKey, AxEvent> eventMap = ModelService.getModel(AxEvents.class).getEventMap();
- final Map<AxArtifactKey, AxContextAlbum> albumsMap =
- ModelService.getModel(AxContextAlbums.class).getAlbumsMap();
- final Map<AxArtifactKey, AxTask> taskMap = ModelService.getModel(AxTasks.class).getTaskMap();
- final Map<AxArtifactKey, AxPolicy> policyMap = ModelService.getModel(AxPolicies.class).getPolicyMap();
+ private void handleAxConceptsRemoval(List<AxArtifactKey> keyInfoKeystoRetain,
+ List<AxArtifactKey> schemaKeystoRetain, List<AxArtifactKey> eventKeystoRetain,
+ List<AxArtifactKey> albumKeystoRetain, List<AxArtifactKey> taskKeystoRetain,
+ List<AxArtifactKey> policyKeystoRetain, ApexMain main) {
+ final AxPolicyModel policyModel = main.getActivator().getPolicyModel();
+ final List<AxArtifactKey> keyInfoKeystoRemove = policyModel.getKeyInformation().getKeyInfoMap().keySet()
+ .stream().filter(key -> !keyInfoKeystoRetain.contains(key)).collect(Collectors.toList());
+ final List<AxArtifactKey> schemaKeystoRemove = policyModel.getSchemas().getSchemasMap().keySet().stream()
+ .filter(key -> !schemaKeystoRetain.contains(key)).collect(Collectors.toList());
+ final List<AxArtifactKey> eventKeystoRemove = policyModel.getEvents().getEventMap().keySet().stream()
+ .filter(key -> !eventKeystoRetain.contains(key)).collect(Collectors.toList());
+ final List<AxArtifactKey> albumKeystoRemove = policyModel.getAlbums().getAlbumsMap().keySet().stream()
+ .filter(key -> !albumKeystoRetain.contains(key)).collect(Collectors.toList());
+ final List<AxArtifactKey> taskKeystoRemove = policyModel.getTasks().getTaskMap().keySet().stream()
+ .filter(key -> !taskKeystoRetain.contains(key)).collect(Collectors.toList());
+ final List<AxArtifactKey> policyKeystoRemove = policyModel.getPolicies().getPolicyMap().keySet().stream()
+ .filter(key -> !policyKeystoRetain.contains(key)).collect(Collectors.toList());
- keyInfoKeystoRemove.forEach(keyInfoMap::remove);
- schemaKeystoRemove.forEach(schemasMap::remove);
- eventKeystoRemove.forEach(eventMap::remove);
- albumKeystoRemove.forEach(albumsMap::remove);
- taskKeystoRemove.forEach(taskMap::remove);
- policyKeystoRemove.forEach(policyMap::remove);
- }
+ final Map<AxArtifactKey, AxKeyInfo> keyInfoMap = ModelService.getModel(AxKeyInformation.class).getKeyInfoMap();
+ final Map<AxArtifactKey, AxContextSchema> schemasMap =
+ ModelService.getModel(AxContextSchemas.class).getSchemasMap();
+ final Map<AxArtifactKey, AxEvent> eventMap = ModelService.getModel(AxEvents.class).getEventMap();
+ final Map<AxArtifactKey, AxContextAlbum> albumsMap =
+ ModelService.getModel(AxContextAlbums.class).getAlbumsMap();
+ final Map<AxArtifactKey, AxTask> taskMap = ModelService.getModel(AxTasks.class).getTaskMap();
+ final Map<AxArtifactKey, AxPolicy> policyMap = ModelService.getModel(AxPolicies.class).getPolicyMap();
+
+ keyInfoKeystoRemove.forEach(keyInfoMap::remove);
+ schemaKeystoRemove.forEach(schemasMap::remove);
+ eventKeystoRemove.forEach(eventMap::remove);
+ albumKeystoRemove.forEach(albumsMap::remove);
+ taskKeystoRemove.forEach(taskMap::remove);
+ policyKeystoRemove.forEach(policyMap::remove);
}
- private Map<ToscaConceptIdentifier, ApexMain> initiateApexEngineForPolicies(List<ToscaPolicy> policies)
+ private void initiateApexEngineForPolicies(List<ToscaPolicy> policies)
throws ApexStarterException {
- Map<ToscaConceptIdentifier, ApexMain> mainMap = new LinkedHashMap<>();
+ Map<ToscaConceptIdentifier, ApexMain> failedPoliciesMainMap = new LinkedHashMap<>();
for (ToscaPolicy policy : policies) {
String policyName = policy.getIdentifier().getName();
final StandardCoder standardCoder = new StandardCoder();
@@ -243,14 +249,24 @@ public class ApexEngineHandler {
}
final String[] apexArgs = {"-p", file.getAbsolutePath()};
LOGGER.info("Starting apex engine for policy {}", policy.getIdentifier());
- try {
- ApexMain apexMain = new ApexMain(apexArgs);
- mainMap.put(policy.getIdentifier(), apexMain);
- } catch (Exception e) {
- LOGGER.error("Execution of policy {} failed", policy.getIdentifier(), e);
+ ApexMain apexMain = new ApexMain(apexArgs);
+ if (apexMain.isAlive()) {
+ apexMainMap.put(policy.getIdentifier(), apexMain);
+ } else {
+ failedPoliciesMainMap.put(policy.getIdentifier(), apexMain);
+ LOGGER.error("Execution of policy {} failed", policy.getIdentifier());
+ }
+ }
+ if (apexMainMap.isEmpty()) {
+ ModelService.clear();
+ ParameterService.clear();
+ throw new ApexStarterException("Apex Engine failed to start.");
+ } else if (failedPoliciesMainMap.size() > 0) {
+ updateModelAndParameterServices(failedPoliciesMainMap);
+ if (failedPoliciesMainMap.size() == policies.size()) {
+ throw new ApexStarterException("Updating the APEX engine with new policies failed.");
}
}
- return mainMap;
}
/**
diff --git a/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestFile2Rest.java b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestFile2Rest.java
index 568e9f3f2..04a1e6faf 100644
--- a/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestFile2Rest.java
+++ b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestFile2Rest.java
@@ -22,7 +22,8 @@
package org.onap.policy.apex.testsuites.integration.uservice.adapt.restclient;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -31,9 +32,11 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Map;
+import java.util.concurrent.TimeUnit;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
+import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -63,6 +66,7 @@ public class TestFile2Rest {
private final PrintStream stdout = System.out;
private final PrintStream stderr = System.err;
+ private ApexMain apexMain;
/**
* Sets the up.
@@ -97,11 +101,26 @@ public class TestFile2Rest {
}
/**
- * Clear relative file root environment variable.
+ * Before test.
*/
@Before
- public void clearRelativeFileRoot() {
+ public void beforeTest() {
System.clearProperty("APEX_RELATIVE_FILE_ROOT");
+ System.setOut(new PrintStream(outContent));
+ System.setErr(new PrintStream(errContent));
+ }
+
+ /**
+ * After test.
+ * @throws ApexException the exception.
+ */
+ @After
+ public void afterTest() throws ApexException {
+ if (null != apexMain) {
+ apexMain.shutdown();
+ }
+ System.setOut(stdout);
+ System.setErr(stderr);
}
/**
@@ -207,22 +226,12 @@ public class TestFile2Rest {
*/
@Test
public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
- System.setOut(new PrintStream(outContent));
- System.setErr(new PrintStream(errContent));
final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventNoURL.json"};
- final ApexMain apexMain = new ApexMain(args);
-
- ThreadUtilities.sleep(200);
- apexMain.shutdown();
-
+ apexMain = new ApexMain(args);
final String outString = outContent.toString();
-
- System.setOut(stdout);
- System.setErr(stderr);
-
- LOGGER.info("NoUrl-OUTSTRING=\n" + outString + "\nEnd-NoUrl");
- assertTrue(outString.contains(" no URL has been set for event sending on RESTCLIENT"));
+ LOGGER.info("NoUrl-OUTSTRING=\n {} \nEnd-NoUrl", outString);
+ assertThat(outString).contains(" no URL has been set for event sending on RESTCLIENT");
}
/**
@@ -234,23 +243,15 @@ public class TestFile2Rest {
*/
@Test
public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
- System.setOut(new PrintStream(outContent));
- System.setErr(new PrintStream(errContent));
final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventBadURL.json"};
- final ApexMain apexMain = new ApexMain(args);
-
- ThreadUtilities.sleep(2000);
- apexMain.shutdown();
-
- final String outString = outContent.toString();
-
- System.setOut(stdout);
- System.setErr(stderr);
-
- LOGGER.info("BadUrl-OUTSTRING=\n" + outString + "\nEnd-BadUrl");
- assertTrue(outString.contains(
- "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/Bad\" using HTTP \"POST\" failed with status code 404"));
+ apexMain = new ApexMain(args);
+ await().atMost(5, TimeUnit.SECONDS)
+ .until(() -> outContent.toString()
+ .contains("send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/Bad\" "
+ + "using HTTP \"POST\" failed with status code 404"));
+ assertTrue(apexMain.isAlive());
+ LOGGER.info("BadUrl-OUTSTRING=\n {} \nEnd-BadUrl", outContent.toString());
}
/**
@@ -262,10 +263,14 @@ public class TestFile2Rest {
*/
@Test
public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
+
final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventBadHTTPMethod.json"};
- assertThatThrownBy(() -> new ApexMain(args)).hasRootCauseMessage(
- "specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" "
- + "are supported for event sending on REST client producer (FirstProducer)");
+ apexMain = new ApexMain(args);
+ final String outString = outContent.toString();
+ LOGGER.info("BadHttpMethod-OUTSTRING=\n {} \nEnd-BadHttpMethod", outString);
+ assertThat(outString)
+ .contains("specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" "
+ + "are supported for event sending on REST client producer");
}
/**
@@ -277,23 +282,15 @@ public class TestFile2Rest {
*/
@Test
public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
- System.setOut(new PrintStream(outContent));
- System.setErr(new PrintStream(errContent));
final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventPostBadResponse.json"};
- final ApexMain apexMain = new ApexMain(args);
-
- ThreadUtilities.sleep(2000);
- apexMain.shutdown();
-
- final String outString = outContent.toString();
-
- System.setOut(stdout);
- System.setErr(stderr);
+ apexMain = new ApexMain(args);
- LOGGER.info("BadResponse-OUTSTRING=\n" + outString + "\nEnd-BadResponse");
- assertTrue(outString.contains(
+ await().atMost(5, TimeUnit.SECONDS)
+ .until(() -> outContent.toString().contains(
"send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/PostEventBadResponse\""
- + " using HTTP \"POST\" failed with status code 400"));
+ + " using HTTP \"POST\" failed with status code 400"));
+ assertTrue(apexMain.isAlive());
+ LOGGER.info("BadResponse-OUTSTRING=\n {} \nEnd-BadResponse", outContent.toString());
}
}
diff --git a/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestRest2File.java b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestRest2File.java
index 25ff12243..0aeb196f0 100644
--- a/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestRest2File.java
+++ b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestRest2File.java
@@ -22,24 +22,26 @@
package org.onap.policy.apex.testsuites.integration.uservice.adapt.restclient;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.awaitility.Awaitility.await;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
import org.onap.policy.apex.service.engine.main.ApexMain;
import org.onap.policy.common.endpoints.http.server.HttpServletServer;
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.resources.TextFileUtils;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
@@ -57,13 +59,16 @@ public class TestRest2File {
private final PrintStream stdout = System.out;
private final PrintStream stderr = System.err;
+ private ApexMain apexMain;
/**
- * Clear relative file root environment variable.
+ * Before Test.
*/
@Before
- public void clearRelativeFileRoot() {
+ public void beforeTest() {
System.clearProperty("APEX_RELATIVE_FILE_ROOT");
+ System.setOut(new PrintStream(outContent));
+ System.setErr(new PrintStream(errContent));
}
/**
@@ -93,9 +98,14 @@ public class TestRest2File {
*/
@After
public void tearDown() throws Exception {
+ if (null != apexMain) {
+ apexMain.shutdown();
+ }
if (server != null) {
server.stop();
}
+ System.setOut(stdout);
+ System.setErr(stderr);
}
/**
@@ -109,16 +119,11 @@ public class TestRest2File {
public void testRestEventsIn() throws MessagingException, ApexException, IOException {
final String[] args = {"-rfr", "target", "-p", "target/examples/config/SampleDomain/REST2FileJsonEvent.json"};
- final ApexMain apexMain = new ApexMain(args);
-
- ThreadUtilities.sleep(5000);
- apexMain.shutdown();
-
- final String outputEventText =
- TextFileUtils.getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
-
- checkRequiredString(outputEventText,
- "04\",\n" + " \"version\": \"0.0.1\",\n" + " \"nameSpace\": \"org.onap.policy.apex.sample.events\"");
+ apexMain = new ApexMain(args);
+ await().atMost(5, TimeUnit.SECONDS).until(
+ () -> Files.readString(Path.of("target/examples/events/SampleDomain/EventsOut.json")).contains(
+ "04\",\n" + " \"version\": \"0.0.1\",\n" + " \"nameSpace\": \"org.onap.policy.apex.sample.events\""));
+ assertTrue(apexMain.isAlive());
}
/**
@@ -130,22 +135,12 @@ public class TestRest2File {
*/
@Test
public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
- System.setOut(new PrintStream(outContent));
- System.setErr(new PrintStream(errContent));
final String[] args = {"src/test/resources/prodcons/REST2FileJsonEmptyEvents.json"};
- final ApexMain apexMain = new ApexMain(args);
-
- ThreadUtilities.sleep(5000);
- apexMain.shutdown();
-
- final String outString = outContent.toString();
-
- System.setOut(stdout);
- System.setErr(stderr);
-
- checkRequiredString(outString,
- "received an empty event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\"");
+ apexMain = new ApexMain(args);
+ await().atMost(5, TimeUnit.SECONDS).until(() -> outContent.toString().contains(
+ "received an empty event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\""));
+ assertTrue(apexMain.isAlive());
}
/**
@@ -157,20 +152,11 @@ public class TestRest2File {
*/
@Test
public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
- System.setOut(new PrintStream(outContent));
- System.setErr(new PrintStream(errContent));
final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventNoURL.json"};
- final ApexMain apexMain = new ApexMain(args);
-
- ThreadUtilities.sleep(5000);
- apexMain.shutdown();
-
+ apexMain = new ApexMain(args);
final String outString = outContent.toString();
- System.setOut(stdout);
- System.setErr(stderr);
-
checkRequiredString(outString, " no URL has been set for event sending on RESTCLIENT");
}
@@ -183,34 +169,32 @@ public class TestRest2File {
*/
@Test
public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
- System.setOut(new PrintStream(outContent));
- System.setErr(new PrintStream(errContent));
final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadURL.json"};
- final ApexMain apexMain = new ApexMain(args);
-
- ThreadUtilities.sleep(5000);
- apexMain.shutdown();
-
- final String outString = outContent.toString();
-
- System.setOut(stdout);
- System.setErr(stderr);
+ apexMain = new ApexMain(args);
- checkRequiredString(outString, "reception of event from URL "
- + "\"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404");
+ await().atMost(5, TimeUnit.SECONDS).until(() -> outContent.toString().contains("reception of event from URL "
+ + "\"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404"));
+ assertTrue(apexMain.isAlive());
}
/**
* Test file events bad http method.
+ *
+ * @throws MessagingException the messaging exception
+ * @throws ApexException the apex exception
+ * @throws IOException Signals that an I/O exception has occurred.
*/
@Test
- public void testFileEventsBadHttpMethod() {
+ public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
+
final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json"};
- assertThatThrownBy(() -> new ApexMain(args))
- .hasRootCauseMessage("specified HTTP method of \"POST\" is invalid, "
- + "only HTTP method \"GET\" is supported for event reception on REST client consumer (FirstConsumer)");
+ apexMain = new ApexMain(args);
+
+ final String outString = outContent.toString();
+ checkRequiredString(outString, "specified HTTP method of \"POST\" is invalid, "
+ + "only HTTP method \"GET\" is supported for event reception on REST client consumer");
}
/**
@@ -222,23 +206,15 @@ public class TestRest2File {
*/
@Test
public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
- System.setOut(new PrintStream(outContent));
- System.setErr(new PrintStream(errContent));
final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadResponse.json"};
- final ApexMain apexMain = new ApexMain(args);
-
- ThreadUtilities.sleep(5000);
- apexMain.shutdown();
-
- final String outString = outContent.toString();
-
- System.setOut(stdout);
- System.setErr(stderr);
-
- checkRequiredString(outString,
- "reception of event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" "
- + "failed with status code 400 and message \"");
+ apexMain = new ApexMain(args);
+ await().atMost(5, TimeUnit.SECONDS)
+ .until(() -> outContent.toString()
+ .contains("reception of event from URL "
+ + "\"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" "
+ + "failed with status code 400 and message \""));
+ assertTrue(apexMain.isAlive());
}
/**
diff --git a/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restserver/TestRestServer.java b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restserver/TestRestServer.java
index 39173bc72..0596a65e8 100644
--- a/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restserver/TestRestServer.java
+++ b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restserver/TestRestServer.java
@@ -22,7 +22,7 @@
package org.onap.policy.apex.testsuites.integration.uservice.adapt.restserver;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -38,10 +38,10 @@ import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
import org.onap.policy.apex.service.engine.main.ApexMain;
import org.onap.policy.common.utils.network.NetworkUtil;
@@ -63,11 +63,22 @@ public class TestRestServer {
private static int eventsSent = 0;
/**
- * Clear relative file root environment variable.
+ * Before Test.
*/
@Before
- public void clearRelativeFileRoot() {
+ public void beforeTest() {
System.clearProperty("APEX_RELATIVE_FILE_ROOT");
+ System.setOut(new PrintStream(outContent));
+ System.setErr(new PrintStream(errContent));
+ }
+
+ /**
+ * After test.
+ */
+ @After
+ public void afterTest() {
+ System.setOut(stdout);
+ System.setErr(stderr);
}
/**
@@ -286,14 +297,28 @@ public class TestRestServer {
/**
* Test rest server producer standalone.
+ *
+ * @throws MessagingException the messaging exception
+ * @throws ApexException the apex exception
+ * @throws IOException Signals that an I/O exception has occurred.
+ * @throws InterruptedException interrupted exception
*/
@Test
- public void testRestServerProducerStandalone() {
+ public void testRestServerProducerStandalone()
+ throws MessagingException, ApexException, IOException, InterruptedException {
LOGGER.debug("testRestServerProducerStandalone start");
final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerStandalone.json"};
- assertThatThrownBy(() -> new ApexMain(args))
- .hasRootCauseMessage("the parameters \"host\", \"port\", and \"standalone\" are illegal"
- + " on REST Server producer (FirstProducer)");
+
+ final ApexMain apexMain = new ApexMain(args);
+ apexMain.shutdown();
+
+ await().atMost(10L, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
+
+ final String outString = outContent.toString();
+
+ assertThat(outString)
+ .contains("the parameters \"host\", \"port\", and \"standalone\" are illegal on REST Server producer");
+ LOGGER.debug("testRestServerProducerStandalone end");
}
/**
@@ -308,23 +333,15 @@ public class TestRestServer {
public void testRestServerProducerHost()
throws MessagingException, ApexException, IOException, InterruptedException {
LOGGER.debug("testRestServerProducerHost start");
- System.setOut(new PrintStream(outContent));
- System.setErr(new PrintStream(errContent));
-
final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerHost.json"};
final ApexMain apexMain = new ApexMain(args);
- ThreadUtilities.sleep(200);
apexMain.shutdown();
await().atMost(10L, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
final String outString = outContent.toString();
-
- System.setOut(stdout);
- System.setErr(stderr);
-
- assertTrue(outString.contains(" host is specified only in standalone mode"));
+ assertThat(outString).contains(" host is specified only in standalone mode");
LOGGER.debug("testRestServerProducerHost end");
}
@@ -340,61 +357,84 @@ public class TestRestServer {
public void testRestServerProducerPort()
throws MessagingException, ApexException, IOException, InterruptedException {
LOGGER.debug("testRestServerProducerPort start");
- System.setOut(new PrintStream(outContent));
- System.setErr(new PrintStream(errContent));
-
final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerPort.json"};
final ApexMain apexMain = new ApexMain(args);
- ThreadUtilities.sleep(200);
apexMain.shutdown();
await().atMost(10L, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
final String outString = outContent.toString();
-
- System.setOut(stdout);
- System.setErr(stderr);
-
- assertTrue(outString.contains(" port is specified only in standalone mode"));
+ assertThat(outString).contains(" port is specified only in standalone mode");
LOGGER.debug("testRestServerProducerPort end");
}
/**
* Test rest server consumer standalone no host.
+ *
+ * @throws MessagingException the messaging exception
+ * @throws ApexException the apex exception
+ * @throws IOException Signals that an I/O exception has occurred.
*/
@Test
- public void testRestServerConsumerStandaloneNoHost() {
+ public void testRestServerConsumerStandaloneNoHost() throws MessagingException, ApexException, IOException {
LOGGER.debug("testRestServerConsumerStandaloneNoHost start");
final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoHost.json"};
- assertThatThrownBy(() -> new ApexMain(args))
- .hasRootCauseMessage("the parameters \"host\" and \"port\" must be defined for REST Server consumer "
- + "(FirstConsumer) in standalone mode");
+ final ApexMain apexMain = new ApexMain(args);
+ apexMain.shutdown();
+
+ await().atMost(10L, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
+
+ final String outString = outContent.toString();
+ assertThat(outString).contains("the parameters \"host\" and \"port\" must be defined for REST Server consumer "
+ + "(FirstConsumer) in standalone mode");
LOGGER.debug("testRestServerConsumerStandaloneNoHost end");
}
/**
* Test rest server consumer standalone no port.
+ *
+ * @throws MessagingException the messaging exception
+ * @throws ApexException the apex exception
+ * @throws IOException Signals that an I/O exception has occurred.
*/
@Test
- public void testRestServerConsumerStandaloneNoPort() {
+ public void testRestServerConsumerStandaloneNoPort() throws MessagingException, ApexException, IOException {
LOGGER.debug("testRestServerConsumerStandaloneNoPort start");
final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoPort.json"};
- assertThatThrownBy(() -> new ApexMain(args))
- .hasRootCauseMessage("the parameters \"host\" and \"port\" must be defined for REST Server consumer "
- + "(FirstConsumer) in standalone mode");
+
+ final ApexMain apexMain = new ApexMain(args);
+ apexMain.shutdown();
+
+ await().atMost(10L, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
+
+ final String outString = outContent.toString();
+ assertThat(outString).contains("the parameters \"host\" and \"port\" must be defined for REST Server consumer "
+ + "(FirstConsumer) in standalone mode");
LOGGER.debug("testRestServerConsumerStandaloneNoPort end");
}
/**
* Test rest server producer not sync.
+ *
+ * @throws MessagingException the messaging exception
+ * @throws ApexException the apex exception
+ * @throws IOException Signals that an I/O exception has occurred.
*/
@Test
- public void testRestServerProducerNotSync() {
+ public void testRestServerProducerNotSync() throws MessagingException, ApexException, IOException {
LOGGER.debug("testRestServerProducerNotSync start");
final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerNotSync.json"};
- assertThatThrownBy(() -> new ApexMain(args)).hasRootCauseMessage(
+
+ final ApexMain apexMain = new ApexMain(args);
+ apexMain.shutdown();
+
+ await().atMost(10L, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
+
+ final String outString = outContent.toString();
+
+ assertThat(outString).contains(
"REST Server producer (FirstProducer) must run in synchronous mode " + "with a REST Server consumer");
LOGGER.debug("testRestServerProducerNotSync end");
}
@@ -409,25 +449,18 @@ public class TestRestServer {
@Test
public void testRestServerConsumerNotSync() throws MessagingException, ApexException, IOException {
LOGGER.debug("testRestServerConsumerNotSync start");
- System.setOut(new PrintStream(outContent));
- System.setErr(new PrintStream(errContent));
-
final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerNotSync.json"};
final ApexMain apexMain = new ApexMain(args);
- ThreadUtilities.sleep(200);
apexMain.shutdown();
await().atMost(10L, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
final String outString = outContent.toString();
- System.setOut(stdout);
- System.setErr(stderr);
-
- assertTrue(
- outString.contains("peer \"FirstConsumer for peered mode SYNCHRONOUS does not exist or is not defined "
- + "with the same peered mode"));
+ assertThat(outString)
+ .contains("peer \"FirstConsumer for peered mode SYNCHRONOUS does not exist or is not defined "
+ + "with the same peered mode");
LOGGER.debug("testRestServerConsumerNotSync end");
}