1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/*-
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
* Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.policy.controlloop.actorserviceprovider.parameters;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
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.doAnswer;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.policy.common.parameters.BeanValidationResult;
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.ControlLoopOperationParamsBuilder;
import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
import org.onap.policy.controlloop.policy.Target;
public class ControlLoopOperationParamsTest {
private static final String EXPECTED_EXCEPTION = "expected exception";
private static final String ACTOR = "my-actor";
private static final String OPERATION = "my-operation";
private static final Target TARGET = new Target();
private static final String TARGET_ENTITY = "my-target";
private static final Integer RETRY = 3;
private static final Integer TIMEOUT = 100;
private static final UUID REQ_ID = UUID.randomUUID();
@Mock
private Actor actor;
@Mock
private ActorService actorService;
@Mock
private Consumer<OperationOutcome> completer;
@Mock
private ControlLoopEventContext context;
@Mock
private VirtualControlLoopEvent event;
@Mock
private Executor executor;
@Mock
private CompletableFuture<OperationOutcome> operFuture;
@Mock
private Operator operator;
@Mock
private Operation operation;
@Mock
private Consumer<OperationOutcome> starter;
private Map<String, Object> payload;
private ControlLoopOperationParams params;
private OperationOutcome outcome;
/**
* Initializes mocks and sets {@link #params} to a fully-loaded set of parameters.
*/
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(actorService.getActor(ACTOR)).thenReturn(actor);
when(actor.getOperator(OPERATION)).thenReturn(operator);
when(operator.buildOperation(any())).thenReturn(operation);
when(operation.start()).thenReturn(operFuture);
when(event.getRequestId()).thenReturn(REQ_ID);
when(context.getEvent()).thenReturn(event);
payload = new TreeMap<>();
params = ControlLoopOperationParams.builder().actorService(actorService).completeCallback(completer)
.context(context).executor(executor).actor(ACTOR).operation(OPERATION).payload(payload)
.retry(RETRY).target(TARGET).targetEntity(TARGET_ENTITY).timeoutSec(TIMEOUT)
.startCallback(starter).preprocessed(true).build();
outcome = params.makeOutcome();
}
@Test
public void testStart() {
assertSame(operFuture, params.start());
assertThatIllegalArgumentException().isThrownBy(() -> params.toBuilder().context(null).build().start());
}
@Test
public void testBuild() {
assertSame(operation, params.build());
assertThatIllegalArgumentException().isThrownBy(() -> params.toBuilder().context(null).build().build());
}
@Test
public void testGetRequestId() {
assertSame(REQ_ID, params.getRequestId());
// try with null context
assertNull(params.toBuilder().context(null).build().getRequestId());
// try with null event
when(context.getEvent()).thenReturn(null);
assertNull(params.getRequestId());
}
@Test
public void testMakeOutcome() {
assertEquals(ACTOR, outcome.getActor());
assertEquals(OPERATION, outcome.getOperation());
checkRemainingFields("with actor");
}
protected void checkRemainingFields(String testName) {
assertEquals(testName, TARGET_ENTITY, outcome.getTarget());
assertNull(testName, outcome.getStart());
assertNull(testName, outcome.getEnd());
assertNull(testName, outcome.getSubRequestId());
assertNotNull(testName, outcome.getResult());
assertNull(testName, outcome.getMessage());
}
@Test
public void testCallbackStarted() {
params.callbackStarted(outcome);
verify(starter).accept(outcome);
// modify starter to throw an exception
AtomicInteger count = new AtomicInteger();
doAnswer(args -> {
count.incrementAndGet();
throw new IllegalStateException(EXPECTED_EXCEPTION);
}).when(starter).accept(outcome);
params.callbackStarted(outcome);
verify(starter, times(2)).accept(outcome);
assertEquals(1, count.get());
// repeat with no start-callback - no additional calls expected
params.toBuilder().startCallback(null).build().callbackStarted(outcome);
verify(starter, times(2)).accept(outcome);
assertEquals(1, count.get());
// should not call complete-callback
verify(completer, never()).accept(any());
}
@Test
public void testCallbackCompleted() {
params.callbackCompleted(outcome);
verify(completer).accept(outcome);
// modify completer to throw an exception
AtomicInteger count = new AtomicInteger();
doAnswer(args -> {
count.incrementAndGet();
throw new IllegalStateException(EXPECTED_EXCEPTION);
}).when(completer).accept(outcome);
params.callbackCompleted(outcome);
verify(completer, times(2)).accept(outcome);
assertEquals(1, count.get());
// repeat with no complete-callback - no additional calls expected
params.toBuilder().completeCallback(null).build().callbackCompleted(outcome);
verify(completer, times(2)).accept(outcome);
assertEquals(1, count.get());
// should not call start-callback
verify(starter, never()).accept(any());
}
@Test
public void testValidateFields() {
testValidate("actor", "null", bldr -> bldr.actor(null));
testValidate("actorService", "null", bldr -> bldr.actorService(null));
testValidate("context", "null", bldr -> bldr.context(null));
testValidate("executor", "null", bldr -> bldr.executor(null));
testValidate("operation", "null", bldr -> bldr.operation(null));
testValidate("target", "null", bldr -> bldr.targetEntity(null));
// check edge cases
assertTrue(params.toBuilder().build().validate().isValid());
// these can be null
assertTrue(params.toBuilder().payload(null).retry(null).target(null).timeoutSec(null).startCallback(null)
.completeCallback(null).build().validate().isValid());
// test with minimal fields
assertTrue(ControlLoopOperationParams.builder().actorService(actorService).context(context).actor(ACTOR)
.operation(OPERATION).targetEntity(TARGET_ENTITY).build().validate().isValid());
}
private void testValidate(String fieldName, String expected,
Function<ControlLoopOperationParamsBuilder, ControlLoopOperationParamsBuilder> makeInvalid) {
// original params should be valid
BeanValidationResult result = params.validate();
assertTrue(fieldName, result.isValid());
// make invalid params
result = makeInvalid.apply(params.toBuilder()).build().validate();
assertFalse(fieldName, result.isValid());
assertThat(result.getResult()).contains(fieldName).contains(expected);
}
@Test
public void testBuilder_testToBuilder() {
assertEquals(params, params.toBuilder().build());
}
@Test
public void testGetActor() {
assertSame(ACTOR, params.getActor());
}
@Test
public void testGetActorService() {
assertSame(actorService, params.getActorService());
}
@Test
public void testGetContext() {
assertSame(context, params.getContext());
}
@Test
public void testGetExecutor() {
assertSame(executor, params.getExecutor());
// should use default when unspecified
assertSame(ForkJoinPool.commonPool(), ControlLoopOperationParams.builder().build().getExecutor());
}
@Test
public void testGetOperation() {
assertSame(OPERATION, params.getOperation());
}
@Test
public void testGetPayload() {
assertSame(payload, params.getPayload());
// should be null when unspecified
assertNull(ControlLoopOperationParams.builder().build().getPayload());
}
@Test
public void test() {
assertTrue(params.isPreprocessed());
// should be false when unspecified
assertFalse(ControlLoopOperationParams.builder().build().isPreprocessed());
}
@Test
public void testGetRetry() {
assertSame(RETRY, params.getRetry());
// should be null when unspecified
assertNull(ControlLoopOperationParams.builder().build().getRetry());
}
@Test
public void testTarget() {
assertSame(TARGET, params.getTarget());
// should be null when unspecified
assertNull(ControlLoopOperationParams.builder().build().getTarget());
}
@Test
public void testGetTimeoutSec() {
assertSame(TIMEOUT, params.getTimeoutSec());
// should be 300 when unspecified
assertEquals(Integer.valueOf(300), ControlLoopOperationParams.builder().build().getTimeoutSec());
// null should be ok too
assertNull(ControlLoopOperationParams.builder().timeoutSec(null).build().getTimeoutSec());
}
@Test
public void testGetStartCallback() {
assertSame(starter, params.getStartCallback());
}
@Test
public void testGetCompleteCallback() {
assertSame(completer, params.getCompleteCallback());
}
@Test
public void testGetTargetEntity() {
assertEquals(TARGET_ENTITY, params.getTargetEntity());
}
}
|