Upgrade Java 17 in policy-drools-apps
[policy/drools-applications.git] / controlloop / common / controller-usecases / src / test / java / org / onap / policy / drools / apps / controller / usecases / step / LockStep2Test.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.drools.apps.controller.usecases.step;
23
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;
31
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;
43
44 class LockStep2Test {
45     private static final String MY_TARGET = "my-target";
46
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();
51
52     private ControlLoopOperationParams params;
53     private CompletableFuture<OperationOutcome> future;
54     private Step2 master;
55     private LockStep2 step;
56
57     /**
58      * Sets up.
59      */
60     @BeforeEach
61     public void setUp() {
62         future = new CompletableFuture<>();
63
64         when(stepContext.requestLock(MY_TARGET)).thenReturn(future);
65         when(stepContext.getProperty(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(MY_TARGET);
66
67         params = ControlLoopOperationParams.builder().completeCallback(callback).build();
68
69         master = new Step2(stepContext, params, event) {
70             @Override
71             protected Operation buildOperation() {
72                 return policyOper;
73             }
74         };
75
76         // force it to build the operation
77         master.init();
78
79         step = new LockStep2(master);
80     }
81
82     @Test
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);
88     }
89
90     @Test
91     void testAcceptsEvent() {
92         // it should always accept events
93         assertTrue(step.acceptsEvent());
94     }
95
96     @Test
97     void testStart() {
98         // start the operation
99         step.init();
100         step.setProperties();
101         assertTrue(step.start(100));
102
103         // complete the operation's future
104         var outcome = step.makeOutcome();
105         outcome.setTarget(MY_TARGET);
106
107         future.complete(outcome);
108
109         // ensure it invoked the callback
110         verify(callback).accept(outcome);
111     }
112
113     /**
114      * Tests start when the operation throws an exception.
115      */
116     @Test
117     void testStartNoFuture() {
118         // start the operation
119         step.init();
120
121         // force an exception by NOT invoking setProperties()
122
123         assertTrue(step.start(100));
124
125         // should have already invoked the callback
126         verify(callback).accept(any());
127     }
128 }