diff options
author | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2020-02-24 19:01:23 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-02-24 19:01:23 +0000 |
commit | 4782f3267518d66d01b3d3cbec9d98ba7e1e92bd (patch) | |
tree | c046dd6cb1cf376ecac6a000fbaa0b51a3572ca7 /models-interactions/model-actors/actorServiceProvider/src/test | |
parent | 7f6e3f7b55a80fb18b92991f958b1d5d1499b1a0 (diff) | |
parent | 7f69c5ca0a6f6018166f8fee3e811edf4dee1eb8 (diff) |
Merge "Change payload to Map<String,Object> so it's more versatile"
Diffstat (limited to 'models-interactions/model-actors/actorServiceProvider/src/test')
9 files changed, 114 insertions, 62 deletions
diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/controlloop/ControlLoopEventContextTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/controlloop/ControlLoopEventContextTest.java index cf2426214..0f44f4f36 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/controlloop/ControlLoopEventContextTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/controlloop/ControlLoopEventContextTest.java @@ -41,6 +41,7 @@ import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; public class ControlLoopEventContextTest { + private static final String MY_KEY = "def"; private static final UUID REQ_ID = UUID.randomUUID(); private static final String ITEM_KEY = "obtain-C"; @@ -53,7 +54,7 @@ public class ControlLoopEventContextTest { */ @Before public void setUp() { - enrichment = Map.of("abc", "one", "def", "two"); + enrichment = Map.of("abc", "one", MY_KEY, "two"); event = new VirtualControlLoopEvent(); event.setRequestId(REQ_ID); @@ -81,17 +82,21 @@ public class ControlLoopEventContextTest { } @Test - public void testContains_testGetProperty_testSetProperty() { + public void testContains_testGetProperty_testSetProperty_testRemoveProperty() { context.setProperty("abc", "a string"); - context.setProperty("def", 100); + context.setProperty(MY_KEY, 100); + assertTrue(context.contains(MY_KEY)); assertFalse(context.contains("ghi")); String strValue = context.getProperty("abc"); assertEquals("a string", strValue); - int intValue = context.getProperty("def"); + int intValue = context.getProperty(MY_KEY); assertEquals(100, intValue); + + context.removeProperty(MY_KEY); + assertFalse(context.contains(MY_KEY)); } @Test diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicActorTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicActorTest.java index 4a4354195..dfd3f8bc6 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicActorTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicActorTest.java @@ -172,7 +172,7 @@ public class BidirectionalTopicActorTest { new TreeMap<>(maker.apply("operB")).toString()); // with invalid actor parameters - params.setOperation(null); + params.setOperations(null); assertThatThrownBy(() -> prov.makeOperatorParameters(Util.translateToMap(prov.getName(), params))) .isInstanceOf(ParameterValidationRuntimeException.class); } @@ -212,7 +212,7 @@ public class BidirectionalTopicActorTest { params.setTimeoutSec(TIMEOUT); // @formatter:off - params.setOperation(Map.of( + params.setOperations(Map.of( "operA", Map.of(), "operB", Map.of("sourceTopic", "topicB"))); // @formatter:on diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpActorTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpActorTest.java index dacd3a529..a28b98c81 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpActorTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpActorTest.java @@ -54,7 +54,7 @@ public class HttpActorTest { params.setTimeoutSec(TIMEOUT); // @formatter:off - params.setOperation(Map.of( + params.setOperations(Map.of( "operA", Map.of("path", "urlA"), "operB", Map.of("path", "urlB"))); // @formatter:on @@ -73,7 +73,7 @@ public class HttpActorTest { new TreeMap<>(maker.apply("operB")).toString()); // with invalid actor parameters - params.setOperation(null); + params.setOperations(null); assertThatThrownBy(() -> prov.makeOperatorParameters(Util.translateToMap(prov.getName(), params))) .isInstanceOf(ParameterValidationRuntimeException.class); } diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartialTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartialTest.java index 39564a443..2893cb627 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartialTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartialTest.java @@ -28,6 +28,9 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import ch.qos.logback.classic.Logger; import java.time.Instant; @@ -44,7 +47,6 @@ import java.util.concurrent.ForkJoinPool; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import java.util.function.Supplier; @@ -55,6 +57,9 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; import org.onap.policy.common.utils.coder.Coder; @@ -64,11 +69,14 @@ import org.onap.policy.common.utils.test.log.logback.ExtractAppender; import org.onap.policy.common.utils.time.PseudoExecutor; import org.onap.policy.controlloop.ControlLoopOperation; 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.Operator; import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.OperatorConfig; +import org.onap.policy.controlloop.actorserviceprovider.spi.Actor; import org.onap.policy.controlloop.policy.PolicyResult; import org.slf4j.LoggerFactory; @@ -82,6 +90,7 @@ public class OperationPartialTest { private static final String OPERATION = "my-operation"; private static final String MY_SINK = "my-sink"; private static final String MY_SOURCE = "my-source"; + private static final String MY_TARGET_ENTITY = "my-entity"; private static final String TEXT = "my-text"; private static final int TIMEOUT = 1000; private static final UUID REQ_ID = UUID.randomUUID(); @@ -95,6 +104,15 @@ public class OperationPartialTest { private static final Logger logger = (Logger) LoggerFactory.getLogger(OperationPartial.class); private static final ExtractAppender appender = new ExtractAppender(); + @Mock + private ActorService service; + @Mock + private Actor guardActor; + @Mock + private Operator guardOperator; + @Mock + private Operation guardOperation; + private VirtualControlLoopEvent event; private ControlLoopEventContext context; private PseudoExecutor executor; @@ -139,6 +157,8 @@ public class OperationPartialTest { */ @Before public void setUp() { + MockitoAnnotations.initMocks(this); + event = new VirtualControlLoopEvent(); event.setRequestId(REQ_ID); @@ -146,8 +166,13 @@ public class OperationPartialTest { executor = new PseudoExecutor(); params = ControlLoopOperationParams.builder().completeCallback(this::completer).context(context) - .executor(executor).actor(ACTOR).operation(OPERATION).timeoutSec(TIMEOUT) - .startCallback(this::starter).targetEntity(MY_SINK).build(); + .executor(executor).actorService(service).actor(ACTOR).operation(OPERATION).timeoutSec(TIMEOUT) + .startCallback(this::starter).targetEntity(MY_TARGET_ENTITY).build(); + + when(service.getActor(OperationPartial.GUARD_ACTOR_NAME)).thenReturn(guardActor); + when(guardActor.getOperator(OperationPartial.GUARD_OPERATION_NAME)).thenReturn(guardOperator); + when(guardOperator.buildOperation(any())).thenReturn(guardOperation); + when(guardOperation.start()).thenReturn(CompletableFuture.completedFuture(makeSuccess())); config = new OperatorConfig(executor); @@ -189,25 +214,6 @@ public class OperationPartialTest { } /** - * Tests startOperation() when the operation has a preprocessor. - */ - @Test - public void testStartWithPreprocessor() { - AtomicInteger count = new AtomicInteger(); - - CompletableFuture<OperationOutcome> preproc = CompletableFuture.supplyAsync(() -> { - count.incrementAndGet(); - return makeSuccess(); - }, executor); - - oper.setGuard(preproc); - - verifyRun("testStartWithPreprocessor_testStartPreprocessor", 1, 1, PolicyResult.SUCCESS); - - assertEquals(1, count.get()); - } - - /** * Tests start() with multiple running requests. */ @Test @@ -232,7 +238,7 @@ public class OperationPartialTest { */ @Test public void testStartPreprocessorFailure() { - oper.setGuard(CompletableFuture.completedFuture(makeFailure())); + oper.setPreProc(CompletableFuture.completedFuture(makeFailure())); verifyRun("testStartPreprocessorFailure", 1, 0, PolicyResult.FAILURE_GUARD); } @@ -243,7 +249,7 @@ public class OperationPartialTest { @Test public void testStartPreprocessorException() { // arrange for the preprocessor to throw an exception - oper.setGuard(CompletableFuture.failedFuture(new IllegalStateException(EXPECTED_EXCEPTION))); + oper.setPreProc(CompletableFuture.failedFuture(new IllegalStateException(EXPECTED_EXCEPTION))); verifyRun("testStartPreprocessorException", 1, 0, PolicyResult.FAILURE_GUARD); } @@ -254,7 +260,7 @@ public class OperationPartialTest { @Test public void testStartPreprocessorNotRunning() { // arrange for the preprocessor to return success, which will be ignored - oper.setGuard(CompletableFuture.completedFuture(makeSuccess())); + // oper.setGuard(CompletableFuture.completedFuture(makeSuccess())); oper.start().cancel(false); assertTrue(executor.runAll(MAX_REQUESTS)); @@ -291,8 +297,49 @@ public class OperationPartialTest { } @Test - public void testStartGuardAsync() { - assertNull(oper.startGuardAsync()); + public void testStartGuardAsync() throws Exception { + CompletableFuture<OperationOutcome> future = oper.startGuardAsync(); + assertTrue(future.isDone()); + assertEquals(PolicyResult.SUCCESS, future.get().getResult()); + + // verify the parameters that were passed + ArgumentCaptor<ControlLoopOperationParams> paramsCaptor = + ArgumentCaptor.forClass(ControlLoopOperationParams.class); + verify(guardOperator).buildOperation(paramsCaptor.capture()); + + params = paramsCaptor.getValue(); + assertEquals(OperationPartial.GUARD_ACTOR_NAME, params.getActor()); + assertEquals(OperationPartial.GUARD_OPERATION_NAME, params.getOperation()); + assertNull(params.getRetry()); + assertNull(params.getTimeoutSec()); + + Map<String, Object> payload = params.getPayload(); + assertNotNull(payload); + + @SuppressWarnings("unchecked") + Map<String, Object> resource = (Map<String, Object>) payload.get("resource"); + assertNotNull(resource); + + @SuppressWarnings("unchecked") + Map<String, Object> guard = (Map<String, Object>) resource.get("guard"); + assertEquals(oper.makeGuardPayload(), guard); + } + + @Test + public void testMakeGuardPayload() { + Map<String, Object> payload = oper.makeGuardPayload(); + assertSame(REQ_ID, payload.get("requestId")); + + // request id changes, so remove it + payload.remove("requestId"); + + assertEquals("{actor=my-actor, recipe=my-operation, target=my-entity}", payload.toString()); + + // repeat, but with closed loop name + event.setClosedLoopControlName("my-loop"); + payload = oper.makeGuardPayload(); + payload.remove("requestId"); + assertEquals("{actor=my-actor, recipe=my-operation, target=my-entity, clname=my-loop}", payload.toString()); } @Test @@ -541,7 +588,7 @@ public class OperationPartialTest { */ @Test public void testHandlePreprocessorFailureTrue() { - oper.setGuard(CompletableFuture.completedFuture(makeSuccess())); + oper.setPreProc(CompletableFuture.completedFuture(makeSuccess())); verifyRun("testHandlePreprocessorFailureTrue", 1, 1, PolicyResult.SUCCESS); } @@ -550,7 +597,7 @@ public class OperationPartialTest { */ @Test public void testHandlePreprocessorFailureFalse() throws Exception { - oper.setGuard(CompletableFuture.completedFuture(makeFailure())); + oper.setPreProc(CompletableFuture.completedFuture(makeFailure())); verifyRun("testHandlePreprocessorFailureFalse", 1, 0, PolicyResult.FAILURE_GUARD); } @@ -559,9 +606,8 @@ public class OperationPartialTest { */ @Test public void testHandlePreprocessorFailureNull() throws Exception { - // arrange to return null from the preprocessor - oper.setGuard(CompletableFuture.completedFuture(null)); - + // arrange to return a null outcome from the preprocessor + oper.setPreProc(CompletableFuture.completedFuture(null)); verifyRun("testHandlePreprocessorFailureNull", 1, 0, PolicyResult.FAILURE_GUARD); } @@ -1211,12 +1257,10 @@ public class OperationPartialTest { @Setter private boolean genException; - @Setter private int maxFailures = 0; - @Setter - private CompletableFuture<OperationOutcome> guard; + private CompletableFuture<OperationOutcome> preProc; public MyOper() { @@ -1242,11 +1286,6 @@ public class OperationPartialTest { } @Override - protected CompletableFuture<OperationOutcome> startGuardAsync() { - return (guard != null ? guard : super.startGuardAsync()); - } - - @Override protected long getRetryWaitMs() { /* * Sleep timers run in the background, but we want to control things via the @@ -1254,5 +1293,10 @@ public class OperationPartialTest { */ return 0L; } + + @Override + protected CompletableFuture<OperationOutcome> startPreprocessorAsync() { + return (preProc != null ? preProc : super.startPreprocessorAsync()); + } } } diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicActorParamsTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicActorParamsTest.java index 1f38ad371..1276950a5 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicActorParamsTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicActorParamsTest.java @@ -76,11 +76,12 @@ public class BidirectionalTopicActorParamsTest { assertTrue(params.validate(CONTAINER).isValid()); // only a few fields are required - BidirectionalTopicActorParams sparse = Util.translate(CONTAINER, Map.of("operation", operMap, "timeoutSec", 1), - BidirectionalTopicActorParams.class); + BidirectionalTopicActorParams sparse = + Util.translate(CONTAINER, Map.of(CommonActorParams.OPERATIONS_FIELD, operMap, "timeoutSec", 1), + BidirectionalTopicActorParams.class); assertTrue(sparse.validate(CONTAINER).isValid()); - testValidateField("operation", "null", params2 -> params2.setOperation(null)); + testValidateField(CommonActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1)); // check edge cases @@ -111,7 +112,7 @@ public class BidirectionalTopicActorParamsTest { params2.setSinkTopic(DFLT_SINK); params2.setSourceTopic(DFLT_SOURCE); params2.setTimeoutSec(DFLT_TIMEOUT); - params2.setOperation(operMap); + params2.setOperations(operMap); return params2; } diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParamsTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParamsTest.java index 901420346..99b441064 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParamsTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParamsTest.java @@ -86,7 +86,7 @@ public class CommonActorParamsTest { assertThatCode(() -> params.doValidation(CONTAINER)).doesNotThrowAnyException(); // invalid param - params.setOperation(null); + params.setOperations(null); assertThatThrownBy(() -> params.doValidation(CONTAINER)) .isInstanceOf(ParameterValidationRuntimeException.class); } @@ -96,11 +96,12 @@ public class CommonActorParamsTest { assertTrue(params.validate(CONTAINER).isValid()); // only a few fields are required - CommonActorParams sparse = Util.translate(CONTAINER, Map.of("operation", operations, "timeoutSec", 1), + CommonActorParams sparse = Util.translate(CONTAINER, + Map.of(CommonActorParams.OPERATIONS_FIELD, operations, "timeoutSec", 1), CommonActorParams.class); assertTrue(sparse.validate(CONTAINER).isValid()); - testValidateField("operation", "null", params2 -> params2.setOperation(null)); + testValidateField(CommonActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); } private void testValidateField(String fieldName, String expected, Consumer<CommonActorParams> makeInvalid) { @@ -119,7 +120,7 @@ public class CommonActorParamsTest { private CommonActorParams makeCommonActorParams() { MyParams params2 = new MyParams(); - params2.setOperation(operations); + params2.setOperations(operations); params2.setText1(TEXT1); params2.setText2(TEXT2); diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ControlLoopOperationParamsTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ControlLoopOperationParamsTest.java index a5215a48f..e4c83049b 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ControlLoopOperationParamsTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ControlLoopOperationParamsTest.java @@ -99,7 +99,7 @@ public class ControlLoopOperationParamsTest { @Mock private Consumer<OperationOutcome> starter; - private Map<String, String> payload; + private Map<String, Object> payload; private ControlLoopOperationParams params; private OperationOutcome outcome; diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpActorParamsTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpActorParamsTest.java index 9e708535f..99bf63305 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpActorParamsTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpActorParamsTest.java @@ -64,11 +64,11 @@ public class HttpActorParamsTest { assertTrue(params.validate(CONTAINER).isValid()); // only a few fields are required - HttpActorParams sparse = Util.translate(CONTAINER, Map.of("operation", operations, "timeoutSec", 1), - HttpActorParams.class); + HttpActorParams sparse = Util.translate(CONTAINER, + Map.of(CommonActorParams.OPERATIONS_FIELD, operations, "timeoutSec", 1), HttpActorParams.class); assertTrue(sparse.validate(CONTAINER).isValid()); - testValidateField("operation", "null", params2 -> params2.setOperation(null)); + testValidateField(CommonActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1)); // check edge cases @@ -97,7 +97,7 @@ public class HttpActorParamsTest { HttpActorParams params2 = new HttpActorParams(); params2.setClientName(CLIENT); params2.setTimeoutSec(TIMEOUT); - params2.setOperation(operations); + params2.setOperations(operations); return params2; } diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/resources/META-INF/services/org.onap.policy.controlloop.actorserviceprovider.spi.Actor b/models-interactions/model-actors/actorServiceProvider/src/test/resources/META-INF/services/org.onap.policy.controlloop.actorserviceprovider.spi.Actor index 2a4bb5749..5f28cf4f2 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/resources/META-INF/services/org.onap.policy.controlloop.actorserviceprovider.spi.Actor +++ b/models-interactions/model-actors/actorServiceProvider/src/test/resources/META-INF/services/org.onap.policy.controlloop.actorserviceprovider.spi.Actor @@ -1 +1,2 @@ -org.onap.policy.controlloop.actorserviceprovider.DummyActor
\ No newline at end of file +org.onap.policy.controlloop.actorserviceprovider.DummyActor +org.onap.policy.controlloop.actorserviceprovider.InvalidActor
\ No newline at end of file |