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.actorserviceprovider.ActorService;
53 import org.onap.policy.controlloop.actorserviceprovider.Operation;
54 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
55 import org.onap.policy.controlloop.actorserviceprovider.Operator;
56 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams.ControlLoopOperationParamsBuilder;
57 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
59 public class ControlLoopOperationParamsTest {
60 private static final String NULL_MSG = "null";
61 private static final String EXPECTED_EXCEPTION = "expected exception";
62 private static final String ACTOR = "my-actor";
63 private static final String OPERATION = "my-operation";
64 private static final String TARGET_ENTITY = "my-target";
65 private static final Integer RETRY = 3;
66 private static final Integer TIMEOUT = 100;
67 private static final UUID REQ_ID = UUID.randomUUID();
73 private ActorService actorService;
76 private Consumer<OperationOutcome> completer;
79 private Executor executor;
82 private CompletableFuture<OperationOutcome> operFuture;
85 private Operator operator;
88 private Operation operation;
91 private Consumer<OperationOutcome> starter;
93 private Map<String, Object> payload;
95 private ControlLoopOperationParams params;
96 private OperationOutcome outcome;
100 * Initializes mocks and sets {@link #params} to a fully-loaded set of parameters.
103 public void setUp() {
104 MockitoAnnotations.initMocks(this);
106 when(actorService.getActor(ACTOR)).thenReturn(actor);
107 when(actor.getOperator(OPERATION)).thenReturn(operator);
108 when(operator.buildOperation(any())).thenReturn(operation);
109 when(operation.start()).thenReturn(operFuture);
111 payload = new TreeMap<>();
113 params = ControlLoopOperationParams.builder().actorService(actorService).completeCallback(completer)
114 .requestId(REQ_ID).executor(executor).actor(ACTOR).operation(OPERATION).payload(payload)
115 .retry(RETRY).timeoutSec(TIMEOUT)
116 .startCallback(starter).build();
118 outcome = params.makeOutcome(TARGET_ENTITY);
122 public void testStart() {
123 assertThatIllegalArgumentException().isThrownBy(() -> params.toBuilder().requestId(null).build().start());
125 assertSame(operFuture, params.start());
129 public void testBuild() {
130 assertThatIllegalArgumentException().isThrownBy(() -> params.toBuilder().requestId(null).build().build());
132 assertSame(operation, params.build());
136 public void testGetRequestId() {
137 assertSame(REQ_ID, params.getRequestId());
141 public void testMakeOutcome() {
142 outcome = params.makeOutcome();
143 assertNull(outcome.getTarget());
144 checkRemainingFields("with actor");
148 public void testMakeOutcomeString() {
149 assertEquals(TARGET_ENTITY, outcome.getTarget());
150 checkRemainingFields("with actor");
153 protected void checkRemainingFields(String testName) {
154 assertEquals(ACTOR, outcome.getActor());
155 assertEquals(OPERATION, outcome.getOperation());
156 assertNull(testName, outcome.getStart());
157 assertNull(testName, outcome.getEnd());
158 assertNull(testName, outcome.getSubRequestId());
159 assertNotNull(testName, outcome.getResult());
160 assertNull(testName, outcome.getMessage());
164 public void testCallbackStarted() {
165 params.callbackStarted(outcome);
166 verify(starter).accept(outcome);
168 // modify starter to throw an exception
169 AtomicInteger count = new AtomicInteger();
171 count.incrementAndGet();
172 throw new IllegalStateException(EXPECTED_EXCEPTION);
173 }).when(starter).accept(outcome);
175 params.callbackStarted(outcome);
176 verify(starter, times(2)).accept(outcome);
177 assertEquals(1, count.get());
179 // repeat with no start-callback - no additional calls expected
180 params.toBuilder().startCallback(null).build().callbackStarted(outcome);
181 verify(starter, times(2)).accept(outcome);
182 assertEquals(1, count.get());
184 // should not call complete-callback
185 verify(completer, never()).accept(any());
189 public void testCallbackCompleted() {
190 params.callbackCompleted(outcome);
191 verify(completer).accept(outcome);
193 // modify completer to throw an exception
194 AtomicInteger count = new AtomicInteger();
196 count.incrementAndGet();
197 throw new IllegalStateException(EXPECTED_EXCEPTION);
198 }).when(completer).accept(outcome);
200 params.callbackCompleted(outcome);
201 verify(completer, times(2)).accept(outcome);
202 assertEquals(1, count.get());
204 // repeat with no complete-callback - no additional calls expected
205 params.toBuilder().completeCallback(null).build().callbackCompleted(outcome);
206 verify(completer, times(2)).accept(outcome);
207 assertEquals(1, count.get());
209 // should not call start-callback
210 verify(starter, never()).accept(any());
214 public void testValidateFields() {
215 testValidate("actor", NULL_MSG, bldr -> bldr.actor(null));
216 testValidate("actorService", NULL_MSG, bldr -> bldr.actorService(null));
217 testValidate("executor", NULL_MSG, bldr -> bldr.executor(null));
218 testValidate("operation", NULL_MSG, bldr -> bldr.operation(null));
219 testValidate("requestId", NULL_MSG, bldr -> bldr.requestId(null));
221 // has no target entity
222 BeanValidationResult result = params.toBuilder().build().validate();
223 assertTrue(result.isValid());
226 assertTrue(params.toBuilder().build().validate().isValid());
229 assertTrue(params.toBuilder().payload(null).retry(null).timeoutSec(null).startCallback(null)
230 .completeCallback(null).build().validate().isValid());
232 // test with minimal fields
233 assertTrue(ControlLoopOperationParams.builder().actorService(actorService).requestId(REQ_ID).actor(ACTOR)
234 .operation(OPERATION).build().validate().isValid());
237 private void testValidate(String fieldName, String expected,
238 Function<ControlLoopOperationParamsBuilder, ControlLoopOperationParamsBuilder> makeInvalid) {
240 // original params should be valid
241 BeanValidationResult result = params.validate();
242 assertTrue(fieldName, result.isValid());
244 // make invalid params
245 result = makeInvalid.apply(params.toBuilder()).build().validate();
246 assertFalse(fieldName, result.isValid());
247 assertThat(result.getResult()).contains(fieldName).contains(expected);
251 public void testBuilder_testToBuilder() {
252 assertEquals(params, params.toBuilder().build());
256 public void testGetActor() {
257 assertSame(ACTOR, params.getActor());
261 public void testGetActorService() {
262 assertSame(actorService, params.getActorService());
266 public void testGetExecutor() {
267 assertSame(executor, params.getExecutor());
269 // should use default when unspecified
270 assertSame(ForkJoinPool.commonPool(), ControlLoopOperationParams.builder().build().getExecutor());
274 public void testGetOperation() {
275 assertSame(OPERATION, params.getOperation());
279 public void testGetPayload() {
280 assertSame(payload, params.getPayload());
282 // should be null when unspecified
283 assertNull(ControlLoopOperationParams.builder().build().getPayload());
287 public void testGetRetry() {
288 assertSame(RETRY, params.getRetry());
290 // should be null when unspecified
291 assertNull(ControlLoopOperationParams.builder().build().getRetry());
295 public void testGetTimeoutSec() {
296 assertSame(TIMEOUT, params.getTimeoutSec());
298 // should be 300 when unspecified
299 assertEquals(Integer.valueOf(300), ControlLoopOperationParams.builder().build().getTimeoutSec());
301 // null should be ok too
302 assertNull(ControlLoopOperationParams.builder().timeoutSec(null).build().getTimeoutSec());
306 public void testGetStartCallback() {
307 assertSame(starter, params.getStartCallback());
311 public void testGetCompleteCallback() {
312 assertSame(completer, params.getCompleteCallback());