2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider.parameters;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertSame;
30 import static org.junit.Assert.assertTrue;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.Mockito.doAnswer;
33 import static org.mockito.Mockito.never;
34 import static org.mockito.Mockito.times;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
39 import java.util.TreeMap;
40 import java.util.UUID;
41 import java.util.concurrent.CompletableFuture;
42 import java.util.concurrent.Executor;
43 import java.util.concurrent.ForkJoinPool;
44 import java.util.concurrent.atomic.AtomicInteger;
45 import java.util.function.Consumer;
46 import java.util.function.Function;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 import org.onap.policy.common.parameters.BeanValidationResult;
52 import org.onap.policy.controlloop.VirtualControlLoopEvent;
53 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
54 import org.onap.policy.controlloop.actorserviceprovider.Operation;
55 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
56 import org.onap.policy.controlloop.actorserviceprovider.Operator;
57 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
58 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams.ControlLoopOperationParamsBuilder;
59 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
60 import org.onap.policy.controlloop.policy.Target;
62 public class ControlLoopOperationParamsTest {
63 private static final String EXPECTED_EXCEPTION = "expected exception";
64 private static final String ACTOR = "my-actor";
65 private static final String OPERATION = "my-operation";
66 private static final Target TARGET = new Target();
67 private static final String TARGET_ENTITY = "my-target";
68 private static final Integer RETRY = 3;
69 private static final Integer TIMEOUT = 100;
70 private static final UUID REQ_ID = UUID.randomUUID();
76 private ActorService actorService;
79 private Consumer<OperationOutcome> completer;
82 private ControlLoopEventContext context;
85 private VirtualControlLoopEvent event;
88 private Executor executor;
91 private CompletableFuture<OperationOutcome> operFuture;
94 private Operator operator;
97 private Operation operation;
100 private Consumer<OperationOutcome> starter;
102 private Map<String, Object> payload;
104 private ControlLoopOperationParams params;
105 private OperationOutcome outcome;
109 * Initializes mocks and sets {@link #params} to a fully-loaded set of parameters.
112 public void setUp() {
113 MockitoAnnotations.initMocks(this);
115 when(actorService.getActor(ACTOR)).thenReturn(actor);
116 when(actor.getOperator(OPERATION)).thenReturn(operator);
117 when(operator.buildOperation(any())).thenReturn(operation);
118 when(operation.start()).thenReturn(operFuture);
120 when(event.getRequestId()).thenReturn(REQ_ID);
122 when(context.getEvent()).thenReturn(event);
124 payload = new TreeMap<>();
126 params = ControlLoopOperationParams.builder().actorService(actorService).completeCallback(completer)
127 .context(context).executor(executor).actor(ACTOR).operation(OPERATION).payload(payload)
128 .retry(RETRY).target(TARGET).targetEntity(TARGET_ENTITY).timeoutSec(TIMEOUT)
129 .startCallback(starter).build();
131 outcome = params.makeOutcome();
135 public void testStart() {
136 assertSame(operFuture, params.start());
138 assertThatIllegalArgumentException().isThrownBy(() -> params.toBuilder().context(null).build().start());
142 public void testGetRequestId() {
143 assertSame(REQ_ID, params.getRequestId());
145 // try with null context
146 assertNull(params.toBuilder().context(null).build().getRequestId());
148 // try with null event
149 when(context.getEvent()).thenReturn(null);
150 assertNull(params.getRequestId());
154 public void testMakeOutcome() {
155 assertEquals(ACTOR, outcome.getActor());
156 assertEquals(OPERATION, outcome.getOperation());
157 checkRemainingFields("with actor");
160 protected void checkRemainingFields(String testName) {
161 assertEquals(testName, TARGET_ENTITY, outcome.getTarget());
162 assertNull(testName, outcome.getStart());
163 assertNull(testName, outcome.getEnd());
164 assertNull(testName, outcome.getSubRequestId());
165 assertNotNull(testName, outcome.getResult());
166 assertNull(testName, outcome.getMessage());
170 public void testCallbackStarted() {
171 params.callbackStarted(outcome);
172 verify(starter).accept(outcome);
174 // modify starter to throw an exception
175 AtomicInteger count = new AtomicInteger();
177 count.incrementAndGet();
178 throw new IllegalStateException(EXPECTED_EXCEPTION);
179 }).when(starter).accept(outcome);
181 params.callbackStarted(outcome);
182 verify(starter, times(2)).accept(outcome);
183 assertEquals(1, count.get());
185 // repeat with no start-callback - no additional calls expected
186 params.toBuilder().startCallback(null).build().callbackStarted(outcome);
187 verify(starter, times(2)).accept(outcome);
188 assertEquals(1, count.get());
190 // should not call complete-callback
191 verify(completer, never()).accept(any());
195 public void testCallbackCompleted() {
196 params.callbackCompleted(outcome);
197 verify(completer).accept(outcome);
199 // modify completer to throw an exception
200 AtomicInteger count = new AtomicInteger();
202 count.incrementAndGet();
203 throw new IllegalStateException(EXPECTED_EXCEPTION);
204 }).when(completer).accept(outcome);
206 params.callbackCompleted(outcome);
207 verify(completer, times(2)).accept(outcome);
208 assertEquals(1, count.get());
210 // repeat with no complete-callback - no additional calls expected
211 params.toBuilder().completeCallback(null).build().callbackCompleted(outcome);
212 verify(completer, times(2)).accept(outcome);
213 assertEquals(1, count.get());
215 // should not call start-callback
216 verify(starter, never()).accept(any());
220 public void testValidateFields() {
221 testValidate("actor", "null", bldr -> bldr.actor(null));
222 testValidate("actorService", "null", bldr -> bldr.actorService(null));
223 testValidate("context", "null", bldr -> bldr.context(null));
224 testValidate("executor", "null", bldr -> bldr.executor(null));
225 testValidate("operation", "null", bldr -> bldr.operation(null));
226 testValidate("target", "null", bldr -> bldr.targetEntity(null));
229 assertTrue(params.toBuilder().build().validate().isValid());
232 assertTrue(params.toBuilder().payload(null).retry(null).target(null).timeoutSec(null).startCallback(null)
233 .completeCallback(null).build().validate().isValid());
235 // test with minimal fields
236 assertTrue(ControlLoopOperationParams.builder().actorService(actorService).context(context).actor(ACTOR)
237 .operation(OPERATION).targetEntity(TARGET_ENTITY).build().validate().isValid());
240 private void testValidate(String fieldName, String expected,
241 Function<ControlLoopOperationParamsBuilder, ControlLoopOperationParamsBuilder> makeInvalid) {
243 // original params should be valid
244 BeanValidationResult result = params.validate();
245 assertTrue(fieldName, result.isValid());
247 // make invalid params
248 result = makeInvalid.apply(params.toBuilder()).build().validate();
249 assertFalse(fieldName, result.isValid());
250 assertThat(result.getResult()).contains(fieldName).contains(expected);
254 public void testBuilder_testToBuilder() {
255 assertEquals(params, params.toBuilder().build());
259 public void testGetActor() {
260 assertSame(ACTOR, params.getActor());
264 public void testGetActorService() {
265 assertSame(actorService, params.getActorService());
269 public void testGetContext() {
270 assertSame(context, params.getContext());
274 public void testGetExecutor() {
275 assertSame(executor, params.getExecutor());
277 // should use default when unspecified
278 assertSame(ForkJoinPool.commonPool(), ControlLoopOperationParams.builder().build().getExecutor());
282 public void testGetOperation() {
283 assertSame(OPERATION, params.getOperation());
287 public void testGetPayload() {
288 assertSame(payload, params.getPayload());
290 // should be null when unspecified
291 assertNull(ControlLoopOperationParams.builder().build().getPayload());
295 public void testGetRetry() {
296 assertSame(RETRY, params.getRetry());
298 // should be null when unspecified
299 assertNull(ControlLoopOperationParams.builder().build().getRetry());
303 public void testTarget() {
304 assertSame(TARGET, params.getTarget());
306 // should be null when unspecified
307 assertNull(ControlLoopOperationParams.builder().build().getTarget());
311 public void testGetTimeoutSec() {
312 assertSame(TIMEOUT, params.getTimeoutSec());
314 // should be 300 when unspecified
315 assertEquals(Integer.valueOf(300), ControlLoopOperationParams.builder().build().getTimeoutSec());
317 // null should be ok too
318 assertNull(ControlLoopOperationParams.builder().timeoutSec(null).build().getTimeoutSec());
322 public void testGetStartCallback() {
323 assertSame(starter, params.getStartCallback());
327 public void testGetCompleteCallback() {
328 assertSame(completer, params.getCompleteCallback());
332 public void testGetTargetEntity() {
333 assertEquals(TARGET_ENTITY, params.getTargetEntity());