fd7efe7bf9d95535538ed91dd34047586c94e93d
[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  * ================================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.apps.controller.usecases.step;
22
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;
29
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;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class LockStep2Test {
47     private static final String MY_TARGET = "my-target";
48
49     @Mock
50     private StepContext stepContext;
51     @Mock
52     private Operation policyOper;
53     @Mock
54     private VirtualControlLoopEvent event;
55     @Mock
56     private Consumer<OperationOutcome> callback;
57
58     private ControlLoopOperationParams params;
59     private CompletableFuture<OperationOutcome> future;
60     private Step2 master;
61     private LockStep2 step;
62
63     /**
64      * Sets up.
65      */
66     @Before
67     public void setUp() {
68         future = new CompletableFuture<>();
69
70         when(stepContext.requestLock(MY_TARGET)).thenReturn(future);
71         when(stepContext.getProperty(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(MY_TARGET);
72
73         params = ControlLoopOperationParams.builder().completeCallback(callback).build();
74
75         master = new Step2(stepContext, params, event) {
76             @Override
77             protected Operation buildOperation() {
78                 return policyOper;
79             }
80         };
81
82         // force it to build the operation
83         master.init();
84
85         step = new LockStep2(master);
86     }
87
88     @Test
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);
94     }
95
96     @Test
97     public void testAcceptsEvent() {
98         // it should always accept events
99         assertTrue(step.acceptsEvent());
100     }
101
102     @Test
103     public void testStart() {
104         // start the operation
105         step.init();
106         step.setProperties();
107         assertTrue(step.start(100));
108
109         // complete the operation's future
110         OperationOutcome outcome = step.makeOutcome();
111         outcome.setTarget(MY_TARGET);
112
113         future.complete(outcome);
114
115         // ensure it invoked the callback
116         verify(callback).accept(outcome);
117     }
118
119     /**
120      * Tests start when the operation throws an exception.
121      */
122     @Test
123     public void testStartNoFuture() {
124         // start the operation
125         step.init();
126
127         // force an exception by NOT invoking setProperties()
128
129         assertTrue(step.start(100));
130
131         // should have already invoked the callback
132         verify(callback).accept(any());
133     }
134 }