2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.apps.controller.usecases.step;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertSame;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.util.concurrent.CompletableFuture;
33 import java.util.function.Consumer;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.controlloop.VirtualControlLoopEvent;
37 import org.onap.policy.controlloop.actorserviceprovider.Operation;
38 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
41 import org.onap.policy.controlloop.eventmanager.ActorConstants;
42 import org.onap.policy.controlloop.eventmanager.StepContext;
45 private static final String MY_TARGET = "my-target";
47 private final StepContext stepContext = mock(StepContext.class);
48 private final Operation policyOper = mock(Operation.class);
49 private final VirtualControlLoopEvent event = mock(VirtualControlLoopEvent.class);
50 private final Consumer<OperationOutcome> callback = mock();
52 private ControlLoopOperationParams params;
53 private CompletableFuture<OperationOutcome> future;
55 private LockStep2 step;
62 future = new CompletableFuture<>();
64 when(stepContext.requestLock(MY_TARGET)).thenReturn(future);
65 when(stepContext.getProperty(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(MY_TARGET);
67 params = ControlLoopOperationParams.builder().completeCallback(callback).build();
69 master = new Step2(stepContext, params, event) {
71 protected Operation buildOperation() {
76 // force it to build the operation
79 step = new LockStep2(master);
83 void testConstructor() {
84 assertEquals(ActorConstants.LOCK_ACTOR, step.getActorName());
85 assertEquals(ActorConstants.LOCK_OPERATION, step.getOperationName());
86 assertSame(stepContext, step.stepContext);
87 assertSame(event, step.event);
91 void testAcceptsEvent() {
92 // it should always accept events
93 assertTrue(step.acceptsEvent());
98 // start the operation
100 step.setProperties();
101 assertTrue(step.start(100));
103 // complete the operation's future
104 var outcome = step.makeOutcome();
105 outcome.setTarget(MY_TARGET);
107 future.complete(outcome);
109 // ensure it invoked the callback
110 verify(callback).accept(outcome);
114 * Tests start when the operation throws an exception.
117 void testStartNoFuture() {
118 // start the operation
121 // force an exception by NOT invoking setProperties()
123 assertTrue(step.start(100));
125 // should have already invoked the callback
126 verify(callback).accept(any());