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).targetEntity(TARGET_ENTITY).timeoutSec(TIMEOUT)
116 .startCallback(starter).preprocessed(true).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 assertEquals(ACTOR, outcome.getActor());
143 assertEquals(OPERATION, outcome.getOperation());
144 checkRemainingFields("with actor");
147 protected void checkRemainingFields(String testName) {
148 assertEquals(testName, TARGET_ENTITY, outcome.getTarget());
149 assertNull(testName, outcome.getStart());
150 assertNull(testName, outcome.getEnd());
151 assertNull(testName, outcome.getSubRequestId());
152 assertNotNull(testName, outcome.getResult());
153 assertNull(testName, outcome.getMessage());
157 public void testCallbackStarted() {
158 params.callbackStarted(outcome);
159 verify(starter).accept(outcome);
161 // modify starter to throw an exception
162 AtomicInteger count = new AtomicInteger();
164 count.incrementAndGet();
165 throw new IllegalStateException(EXPECTED_EXCEPTION);
166 }).when(starter).accept(outcome);
168 params.callbackStarted(outcome);
169 verify(starter, times(2)).accept(outcome);
170 assertEquals(1, count.get());
172 // repeat with no start-callback - no additional calls expected
173 params.toBuilder().startCallback(null).build().callbackStarted(outcome);
174 verify(starter, times(2)).accept(outcome);
175 assertEquals(1, count.get());
177 // should not call complete-callback
178 verify(completer, never()).accept(any());
182 public void testCallbackCompleted() {
183 params.callbackCompleted(outcome);
184 verify(completer).accept(outcome);
186 // modify completer to throw an exception
187 AtomicInteger count = new AtomicInteger();
189 count.incrementAndGet();
190 throw new IllegalStateException(EXPECTED_EXCEPTION);
191 }).when(completer).accept(outcome);
193 params.callbackCompleted(outcome);
194 verify(completer, times(2)).accept(outcome);
195 assertEquals(1, count.get());
197 // repeat with no complete-callback - no additional calls expected
198 params.toBuilder().completeCallback(null).build().callbackCompleted(outcome);
199 verify(completer, times(2)).accept(outcome);
200 assertEquals(1, count.get());
202 // should not call start-callback
203 verify(starter, never()).accept(any());
207 public void testValidateFields() {
208 testValidate("actor", NULL_MSG, bldr -> bldr.actor(null));
209 testValidate("actorService", NULL_MSG, bldr -> bldr.actorService(null));
210 testValidate("executor", NULL_MSG, bldr -> bldr.executor(null));
211 testValidate("operation", NULL_MSG, bldr -> bldr.operation(null));
212 testValidate("requestId", NULL_MSG, bldr -> bldr.requestId(null));
214 // has no target entity
215 BeanValidationResult result = params.toBuilder().targetEntity(null).build().validate();
216 assertTrue(result.isValid());
219 assertTrue(params.toBuilder().build().validate().isValid());
222 assertTrue(params.toBuilder().payload(null).retry(null).timeoutSec(null).startCallback(null)
223 .completeCallback(null).build().validate().isValid());
225 // test with minimal fields
226 assertTrue(ControlLoopOperationParams.builder().actorService(actorService).requestId(REQ_ID).actor(ACTOR)
227 .operation(OPERATION).targetEntity(TARGET_ENTITY).build().validate().isValid());
230 private void testValidate(String fieldName, String expected,
231 Function<ControlLoopOperationParamsBuilder, ControlLoopOperationParamsBuilder> makeInvalid) {
233 // original params should be valid
234 BeanValidationResult result = params.validate();
235 assertTrue(fieldName, result.isValid());
237 // make invalid params
238 result = makeInvalid.apply(params.toBuilder()).build().validate();
239 assertFalse(fieldName, result.isValid());
240 assertThat(result.getResult()).contains(fieldName).contains(expected);
244 public void testBuilder_testToBuilder() {
245 assertEquals(params, params.toBuilder().build());
249 public void testGetActor() {
250 assertSame(ACTOR, params.getActor());
254 public void testGetActorService() {
255 assertSame(actorService, params.getActorService());
259 public void testGetExecutor() {
260 assertSame(executor, params.getExecutor());
262 // should use default when unspecified
263 assertSame(ForkJoinPool.commonPool(), ControlLoopOperationParams.builder().build().getExecutor());
267 public void testGetOperation() {
268 assertSame(OPERATION, params.getOperation());
272 public void testGetPayload() {
273 assertSame(payload, params.getPayload());
275 // should be null when unspecified
276 assertNull(ControlLoopOperationParams.builder().build().getPayload());
281 assertTrue(params.isPreprocessed());
283 // should be false when unspecified
284 assertFalse(ControlLoopOperationParams.builder().build().isPreprocessed());
288 public void testGetRetry() {
289 assertSame(RETRY, params.getRetry());
291 // should be null when unspecified
292 assertNull(ControlLoopOperationParams.builder().build().getRetry());
296 public void testGetTimeoutSec() {
297 assertSame(TIMEOUT, params.getTimeoutSec());
299 // should be 300 when unspecified
300 assertEquals(Integer.valueOf(300), ControlLoopOperationParams.builder().build().getTimeoutSec());
302 // null should be ok too
303 assertNull(ControlLoopOperationParams.builder().timeoutSec(null).build().getTimeoutSec());
307 public void testGetStartCallback() {
308 assertSame(starter, params.getStartCallback());
312 public void testGetCompleteCallback() {
313 assertSame(completer, params.getCompleteCallback());
317 public void testGetTargetEntity() {
318 assertEquals(TARGET_ENTITY, params.getTargetEntity());