Make targetEntity a property
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / OperationPartialTest.java
index 7b8eed5..6db824f 100644 (file)
@@ -36,6 +36,7 @@ import ch.qos.logback.classic.Logger;
 import java.time.Instant;
 import java.util.ArrayDeque;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Deque;
 import java.util.LinkedList;
 import java.util.List;
@@ -74,6 +75,7 @@ import org.onap.policy.controlloop.VirtualControlLoopEvent;
 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
 import org.onap.policy.controlloop.actorserviceprovider.Operation;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
 import org.onap.policy.controlloop.actorserviceprovider.Operator;
 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
@@ -106,6 +108,8 @@ public class OperationPartialTest {
     private static final Logger logger = (Logger) LoggerFactory.getLogger(OperationPartial.class);
     private static final ExtractAppender appender = new ExtractAppender();
 
+    private static final List<String> PROP_NAMES = List.of("hello", "world");
+
     @Mock
     private ActorService service;
     @Mock
@@ -140,7 +144,7 @@ public class OperationPartialTest {
      */
     @BeforeClass
     public static void setUpBeforeClass() throws Exception {
-        /**
+        /*
          * Attach appender to the logger.
          */
         appender.setContext(logger.getLoggerContext());
@@ -216,6 +220,22 @@ public class OperationPartialTest {
         assertNull(future.get(5, TimeUnit.SECONDS));
     }
 
+    @Test
+    public void testGetPropertyNames() {
+        assertThat(oper.getPropertyNames()).isEqualTo(PROP_NAMES);
+    }
+
+    @Test
+    public void testGetProperty_testSetProperty() {
+        oper.setProperty("propertyA", "valueA");
+        oper.setProperty("propertyB", "valueB");
+        oper.setProperty("propertyC", 20);
+
+        assertEquals("valueA", oper.getProperty("propertyA"));
+        assertEquals("valueB", oper.getProperty("propertyB"));
+        assertEquals(Integer.valueOf(20), oper.getProperty("propertyC"));
+    }
+
     @Test
     public void testStart() {
         verifyRun("testStart", 1, 1, PolicyResult.SUCCESS);
@@ -327,6 +347,15 @@ public class OperationPartialTest {
         assertEquals(oper.makeGuardPayload(), payload);
     }
 
+    /**
+     * Tests startGuardAsync() when preprocessing is disabled.
+     */
+    @Test
+    public void testStartGuardAsyncDisabled() {
+        params = params.toBuilder().preprocessed(true).build();
+        assertNull(new MyOper().startGuardAsync());
+    }
+
     @Test
     public void testMakeGuardPayload() {
         Map<String, Object> payload = oper.makeGuardPayload();
@@ -371,7 +400,7 @@ public class OperationPartialTest {
     public void testIsActorFailed() {
         assertFalse(oper.isActorFailed(null));
 
-        OperationOutcome outcome = params.makeOutcome();
+        OperationOutcome outcome = params.makeOutcome(null);
 
         // incorrect outcome
         outcome.setResult(PolicyResult.SUCCESS);
@@ -406,7 +435,7 @@ public class OperationPartialTest {
         /*
          * Use an operation that doesn't override doOperation().
          */
-        OperationPartial oper2 = new OperationPartial(params, config) {};
+        OperationPartial oper2 = new OperationPartial(params, config, Collections.emptyList()) {};
 
         oper2.start();
         assertTrue(executor.runAll(MAX_REQUESTS));
@@ -431,7 +460,7 @@ public class OperationPartialTest {
             @Override
             protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
 
-                OperationOutcome outcome2 = params.makeOutcome();
+                OperationOutcome outcome2 = params.makeOutcome(null);
                 outcome2.setResult(PolicyResult.SUCCESS);
 
                 /*
@@ -531,7 +560,7 @@ public class OperationPartialTest {
             }
         };
 
-        verifyRun("testSetRetryFlag_testRetryOnFailure_NullOutcome", 1, 1, PolicyResult.FAILURE, null, noop());
+        verifyRun("testSetRetryFlag_testRetryOnFailure_NullOutcome", 1, 1, PolicyResult.FAILURE, noop());
     }
 
     @Test
@@ -568,7 +597,7 @@ public class OperationPartialTest {
     public void testIsSameOperation() {
         assertFalse(oper.isSameOperation(null));
 
-        OperationOutcome outcome = params.makeOutcome();
+        OperationOutcome outcome = params.makeOutcome(null);
 
         // wrong actor - should be false
         outcome.setActor(null);
@@ -639,7 +668,7 @@ public class OperationPartialTest {
         // first task completes, others do not
         List<Supplier<CompletableFuture<OperationOutcome>>> tasks = new LinkedList<>();
 
-        final OperationOutcome outcome = params.makeOutcome();
+        final OperationOutcome outcome = params.makeOutcome(null);
 
         tasks.add(() -> CompletableFuture.completedFuture(outcome));
         tasks.add(() -> new CompletableFuture<>());
@@ -704,7 +733,7 @@ public class OperationPartialTest {
 
     @Test
     public void testAllOfArray() throws Exception {
-        final OperationOutcome outcome = params.makeOutcome();
+        final OperationOutcome outcome = params.makeOutcome(null);
 
         CompletableFuture<OperationOutcome> future1 = new CompletableFuture<>();
         CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
@@ -735,7 +764,7 @@ public class OperationPartialTest {
 
     @Test
     public void testAllOfList() throws Exception {
-        final OperationOutcome outcome = params.makeOutcome();
+        final OperationOutcome outcome = params.makeOutcome(null);
 
         CompletableFuture<OperationOutcome> future1 = new CompletableFuture<>();
         CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
@@ -824,9 +853,9 @@ public class OperationPartialTest {
 
         // null outcome - takes precedence over a success
         List<Supplier<CompletableFuture<OperationOutcome>>> tasks = new LinkedList<>();
-        tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome()));
+        tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome(null)));
         tasks.add(() -> CompletableFuture.completedFuture(null));
-        tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome()));
+        tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome(null)));
         CompletableFuture<OperationOutcome> result = oper.allOf(tasks);
 
         assertTrue(executor.runAll(MAX_REQUESTS));
@@ -837,9 +866,9 @@ public class OperationPartialTest {
         IllegalStateException except = new IllegalStateException(EXPECTED_EXCEPTION);
 
         tasks.clear();
-        tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome()));
+        tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome(null)));
         tasks.add(() -> CompletableFuture.failedFuture(except));
-        tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome()));
+        tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome(null)));
         result = oper.allOf(tasks);
 
         assertTrue(executor.runAll(MAX_REQUESTS));
@@ -852,7 +881,7 @@ public class OperationPartialTest {
      */
     @Test
     public void testSequence() throws Exception {
-        final OperationOutcome outcome = params.makeOutcome();
+        final OperationOutcome outcome = params.makeOutcome(null);
 
         List<Supplier<CompletableFuture<OperationOutcome>>> tasks = new LinkedList<>();
         tasks.add(() -> CompletableFuture.completedFuture(outcome));
@@ -874,7 +903,7 @@ public class OperationPartialTest {
         assertSame(outcome, result.get());
 
         // second task fails, third should not run
-        OperationOutcome failure = params.makeOutcome();
+        OperationOutcome failure = params.makeOutcome(null);
         failure.setResult(PolicyResult.FAILURE);
         tasks.clear();
         tasks.add(() -> CompletableFuture.completedFuture(outcome));
@@ -913,7 +942,7 @@ public class OperationPartialTest {
         OperationOutcome expectedOutcome = null;
 
         for (int count = 0; count < results.length; ++count) {
-            OperationOutcome outcome = params.makeOutcome();
+            OperationOutcome outcome = params.makeOutcome(null);
             outcome.setResult(results[count]);
             tasks.add(() -> CompletableFuture.completedFuture(outcome));
 
@@ -933,7 +962,7 @@ public class OperationPartialTest {
     public void testDetmPriority() throws CoderException {
         assertEquals(1, oper.detmPriority(null));
 
-        OperationOutcome outcome = params.makeOutcome();
+        OperationOutcome outcome = params.makeOutcome(null);
 
         Map<PolicyResult, Integer> map = Map.of(PolicyResult.SUCCESS, 0, PolicyResult.FAILURE_GUARD, 2,
                         PolicyResult.FAILURE_RETRIES, 3, PolicyResult.FAILURE, 4, PolicyResult.FAILURE_TIMEOUT, 5,
@@ -1027,6 +1056,10 @@ public class OperationPartialTest {
         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
 
+        oper.setOutcome(outcome, PolicyResult.SUCCESS);
+        assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
+        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+
         for (PolicyResult result : FAILURE_RESULTS) {
             outcome = new OperationOutcome();
             oper.setOutcome(outcome, result);
@@ -1114,10 +1147,20 @@ public class OperationPartialTest {
     @Test
     public void testGetRetryWait() {
         // need an operator that doesn't override the retry time
-        OperationPartial oper2 = new OperationPartial(params, config) {};
+        OperationPartial oper2 = new OperationPartial(params, config, Collections.emptyList()) {};
         assertEquals(OperationPartial.DEFAULT_RETRY_WAIT_MS, oper2.getRetryWaitMs());
     }
 
+    @Test
+    public void testGetTargetEntity() {
+        // get it from the params
+        assertEquals(MY_TARGET_ENTITY, oper.getTargetEntity());
+
+        // now get it from the properties
+        oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "entityX");
+        assertEquals("entityX", oper.getTargetEntity());
+    }
+
     @Test
     public void testGetTimeOutMs() {
         assertEquals(TIMEOUT * 1000, oper.getTimeoutMs(params.getTimeoutSec()));
@@ -1155,14 +1198,14 @@ public class OperationPartialTest {
     }
 
     private OperationOutcome makeSuccess() {
-        OperationOutcome outcome = params.makeOutcome();
+        OperationOutcome outcome = params.makeOutcome(null);
         outcome.setResult(PolicyResult.SUCCESS);
 
         return outcome;
     }
 
     private OperationOutcome makeFailure() {
-        OperationOutcome outcome = params.makeOutcome();
+        OperationOutcome outcome = params.makeOutcome(null);
         outcome.setResult(PolicyResult.FAILURE);
 
         return outcome;
@@ -1179,10 +1222,7 @@ public class OperationPartialTest {
     private void verifyRun(String testName, int expectedCallbacks, int expectedOperations,
                     PolicyResult expectedResult) {
 
-        String expectedSubRequestId =
-                        (expectedResult == PolicyResult.FAILURE_EXCEPTION ? null : String.valueOf(expectedOperations));
-
-        verifyRun(testName, expectedCallbacks, expectedOperations, expectedResult, expectedSubRequestId, noop());
+        verifyRun(testName, expectedCallbacks, expectedOperations, expectedResult, noop());
     }
 
     /**
@@ -1192,13 +1232,12 @@ public class OperationPartialTest {
      * @param expectedCallbacks number of callbacks expected
      * @param expectedOperations number of operation invocations expected
      * @param expectedResult expected outcome
-     * @param expectedSubRequestId expected sub request ID
      * @param manipulator function to modify the future returned by
      *        {@link OperationPartial#start(ControlLoopOperationParams)} before the tasks
      *        in the executor are run
      */
     private void verifyRun(String testName, int expectedCallbacks, int expectedOperations, PolicyResult expectedResult,
-                    String expectedSubRequestId, Consumer<CompletableFuture<OperationOutcome>> manipulator) {
+                    Consumer<CompletableFuture<OperationOutcome>> manipulator) {
 
         tstart = null;
         opstart = null;
@@ -1244,7 +1283,9 @@ public class OperationPartialTest {
             }
 
             if (expectedOperations > 0) {
-                assertEquals(testName, expectedSubRequestId, opend.getSubRequestId());
+                assertNotNull(testName, oper.getSubRequestId());
+                assertEquals(testName + " op start", oper.getSubRequestId(), opstart.getSubRequestId());
+                assertEquals(testName + " op end", oper.getSubRequestId(), opend.getSubRequestId());
             }
         }
 
@@ -1257,7 +1298,7 @@ public class OperationPartialTest {
     private void setOperCoderException() {
         oper = new MyOper() {
             @Override
-            protected Coder makeCoder() {
+            protected Coder getCoder() {
                 return new StandardCoder() {
                     @Override
                     public String encode(Object object, boolean pretty) throws CoderException {
@@ -1288,7 +1329,7 @@ public class OperationPartialTest {
 
 
         public MyOper() {
-            super(OperationPartialTest.this.params, config);
+            super(OperationPartialTest.this.params, config, PROP_NAMES);
         }
 
         @Override