Handling apex-pdp multi policy deployment failure: 85/110985/6
authorputhuparambil.aditya <aditya.puthuparambil@bell.ca>
Thu, 6 Aug 2020 12:03:38 +0000 (13:03 +0100)
committeraditya puthuparambil <aditya.puthuparambil@bell.ca>
Fri, 7 Aug 2020 17:15:10 +0000 (17:15 +0000)
1.Include the list of duplicate parameters in the logs for easier troubleshooting
2.Also fix 4 Sonar blockers in ApexMainTest.java

Issue-ID: POLICY-2712
Signed-off-by: puthuparambil.aditya <aditya.puthuparambil@bell.ca>
Change-Id: I145a7e03f76f880f8e3cbde97284748468b354a5

services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexActivator.java
services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexMainTest.java

index 1e2447b..a928f20 100644 (file)
@@ -24,6 +24,7 @@ package org.onap.policy.apex.service.engine.main;
 
 import java.io.IOException;
 import java.util.AbstractMap;
+import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -133,16 +134,19 @@ public class ApexActivator {
             new LinkedHashSet<>(apexParametersMap.entrySet());
         apexParamsEntrySet.stream().forEach(apexParamsEntry -> {
             ApexParameters apexParams = apexParamsEntry.getValue();
-            boolean duplicateInputParameterExist =
-                apexParams.getEventInputParameters().keySet().stream().anyMatch(inputParametersMap::containsKey);
-            boolean duplicateOutputParameterExist =
-                apexParams.getEventOutputParameters().keySet().stream().anyMatch(outputParametersMap::containsKey);
-            if (duplicateInputParameterExist || duplicateOutputParameterExist) {
-                LOGGER.error("I/O Parameters for {}:{} has duplicates. So this policy is not executed.",
-                    apexParamsEntry.getKey().getName(), apexParamsEntry.getKey().getVersion());
+            List<String> duplicateInputParameters = new ArrayList<>(apexParams.getEventInputParameters().keySet());
+            duplicateInputParameters.retainAll(inputParametersMap.keySet());
+            List<String> duplicateOutputParameters = new ArrayList<>(apexParams.getEventOutputParameters().keySet());
+            duplicateOutputParameters.retainAll(outputParametersMap.keySet());
+
+            if (!(duplicateInputParameters.isEmpty() && duplicateOutputParameters.isEmpty())) {
+                LOGGER.error("I/O Parameters {}/{} for {}:{} are duplicates. So this policy is not executed.",
+                    duplicateInputParameters, duplicateOutputParameters, apexParamsEntry.getKey().getName(),
+                    apexParamsEntry.getKey().getVersion());
                 apexParametersMap.remove(apexParamsEntry.getKey());
                 return;
             }
+
             inputParametersMap.putAll(apexParams.getEventInputParameters());
             outputParametersMap.putAll(apexParams.getEventOutputParameters());
             // Check if a policy model file has been specified
index 731baf5..f38dcaa 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
  *  Modifications Copyright (C) 2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2020 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.
 
 package org.onap.policy.apex.service.engine.main;
 
+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 static org.junit.Assert.assertNotNull;
 
 import java.io.ByteArrayOutputStream;
 import java.io.OutputStream;
@@ -61,6 +63,8 @@ public class ApexMainTest {
         ApexMain.main(null);
         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
                 .contains("Apex configuration file was not specified as an argument"));
+        assertThat(outContent.toString())
+            .contains("Apex configuration file was not specified as an argument");
     }
 
     @Test
@@ -73,6 +77,7 @@ public class ApexMainTest {
         final ApexMain apexMain = new ApexMain(args);
         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
                 .contains("invalid command line arguments specified : Unrecognized option: -whee"));
+        assertNotNull(apexMain);
         apexMain.shutdown();
     }
 
@@ -86,6 +91,7 @@ public class ApexMainTest {
         final ApexMain apexMain = 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();
     }
 
@@ -99,6 +105,7 @@ public class ApexMainTest {
         final ApexMain apexMain = new ApexMain(args);
         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
                 .contains("parameter group has status INVALID"));
+        assertNotNull(apexMain);
         apexMain.shutdown();
     }
 
@@ -148,7 +155,7 @@ public class ApexMainTest {
         assertEquals("MyApexEngine", apexParam.getEngineServiceParameters().getName());
         apexMain.shutdown();
         final String outString = outContent.toString();
-        assertTrue(outString.contains("Added the action listener to the engine"));
+        assertThat(outString).contains("Added the action listener to the engine");
     }
 
     @Test
@@ -165,7 +172,8 @@ public class ApexMainTest {
         assertEquals("MyApexEngine", apexParam.getEngineServiceParameters().getName());
         apexMain.shutdown();
         final String outString = outContent.toString();
-        assertTrue(outString.contains("I/O Parameters for id2:v2 has duplicates. So this policy is not executed"));
+        assertThat(outString).contains("I/O Parameters [TheFileConsumer1]/[FirstProducer] for id2:v2 are duplicates. "
+            + "So this policy is not executed");
         assertEquals(1, apexMain.getApexParametersMap().size()); // only id1:v1 is kept in the map, id2:v2 failed
     }
 
@@ -179,8 +187,7 @@ public class ApexMainTest {
         final ApexMain apexMain = new ApexMain(argsMap);
         final String outString = outContent.toString();
         apexMain.shutdown();
-        assertTrue(
-            outString.contains("Arguments validation failed") && outString.contains("start of Apex service failed"));
-        assertTrue(apexMain.getApexParametersMap().isEmpty()); // No policy is running in the engine
+        assertThat(outString).contains("Arguments validation failed", "start of Apex service failed");
+        assertThat(apexMain.getApexParametersMap()).isEmpty(); // No policy is running in the engine
     }
 }