2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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.drools.apps.controller.usecases.step;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertSame;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import java.util.concurrent.CompletableFuture;
31 import java.util.function.Consumer;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.policy.controlloop.VirtualControlLoopEvent;
38 import org.onap.policy.controlloop.actorserviceprovider.Operation;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
42 import org.onap.policy.controlloop.eventmanager.ActorConstants;
43 import org.onap.policy.controlloop.eventmanager.StepContext;
45 @RunWith(MockitoJUnitRunner.class)
46 public class LockStep2Test {
47 private static final String MY_TARGET = "my-target";
50 private StepContext stepContext;
52 private Operation policyOper;
54 private VirtualControlLoopEvent event;
56 private Consumer<OperationOutcome> callback;
58 private ControlLoopOperationParams params;
59 private CompletableFuture<OperationOutcome> future;
61 private LockStep2 step;
68 future = new CompletableFuture<>();
70 when(stepContext.requestLock(MY_TARGET)).thenReturn(future);
71 when(stepContext.getProperty(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(MY_TARGET);
73 params = ControlLoopOperationParams.builder().completeCallback(callback).build();
75 master = new Step2(stepContext, params, event) {
77 protected Operation buildOperation() {
82 // force it to build the operation
85 step = new LockStep2(master);
89 public void testConstructor() {
90 assertEquals(ActorConstants.LOCK_ACTOR, step.getActorName());
91 assertEquals(ActorConstants.LOCK_OPERATION, step.getOperationName());
92 assertSame(stepContext, step.stepContext);
93 assertSame(event, step.event);
97 public void testAcceptsEvent() {
98 // it should always accept events
99 assertTrue(step.acceptsEvent());
103 public void testStart() {
104 // start the operation
106 step.setProperties();
107 assertTrue(step.start(100));
109 // complete the operation's future
110 OperationOutcome outcome = step.makeOutcome();
111 outcome.setTarget(MY_TARGET);
113 future.complete(outcome);
115 // ensure it invoked the callback
116 verify(callback).accept(outcome);
120 * Tests start when the operation throws an exception.
123 public void testStartNoFuture() {
124 // start the operation
127 // force an exception by NOT invoking setProperties()
129 assertTrue(step.start(100));
131 // should have already invoked the callback
132 verify(callback).accept(any());