3c629591d20f9288b065caa530f429db9c987148
[policy/drools-applications.git] / controlloop / common / controller-usecases / src / test / java / org / onap / policy / drools / apps / controller / usecases / step / AaiCqStep2Test.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.assertFalse;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.util.UUID;
33 import java.util.concurrent.CompletableFuture;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.policy.aai.AaiCqResponse;
40 import org.onap.policy.controlloop.VirtualControlLoopEvent;
41 import org.onap.policy.controlloop.actor.aai.AaiActor;
42 import org.onap.policy.controlloop.actor.aai.AaiCustomQueryOperation;
43 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
44 import org.onap.policy.controlloop.actorserviceprovider.Operation;
45 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
46 import org.onap.policy.controlloop.actorserviceprovider.Operator;
47 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
48 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
49 import org.onap.policy.controlloop.eventmanager.StepContext;
50
51 @RunWith(MockitoJUnitRunner.class)
52 public class AaiCqStep2Test {
53     private static final UUID REQ_ID = UUID.randomUUID();
54
55     @Mock
56     private Operator policyOperator;
57     @Mock
58     private Operation policyOperation;
59     @Mock
60     private Actor policyActor;
61     @Mock
62     private ActorService actors;
63     @Mock
64     private ControlLoopOperationParams params;
65     @Mock
66     private StepContext stepContext;
67     @Mock
68     private VirtualControlLoopEvent event;
69
70     private CompletableFuture<OperationOutcome> future;
71     private Step2 master;
72     private AaiCqStep2 step;
73
74     /**
75      * Sets up.
76      */
77     @Before
78     public void setUp() {
79         future = new CompletableFuture<>();
80
81         when(params.toBuilder()).thenReturn(ControlLoopOperationParams.builder().actorService(actors)
82                         .requestId(REQ_ID));
83
84         // configure policy operation
85         when(actors.getActor(AaiActor.NAME)).thenReturn(policyActor);
86         when(policyActor.getOperator(AaiCustomQueryOperation.NAME)).thenReturn(policyOperator);
87         when(policyOperator.buildOperation(any())).thenReturn(policyOperation);
88         when(policyOperation.start()).thenReturn(future);
89
90         master = new Step2(stepContext, params, event);
91         step = new AaiCqStep2(master);
92     }
93
94     @Test
95     public void testConstructor() {
96         assertEquals(AaiActor.NAME, step.getActorName());
97         assertEquals(AaiCustomQueryOperation.NAME, step.getOperationName());
98         assertSame(stepContext, step.stepContext);
99         assertSame(event, step.event);
100     }
101
102     @Test
103     public void testStart() {
104         step.init();
105         assertTrue(step.start(100));
106         verify(policyOperation).start();
107     }
108
109     /**
110      * Tests start() when the data has already been retrieved.
111      */
112     @Test
113     public void testStartAlreadyHaveData() {
114         when(stepContext.contains(AaiCqResponse.CONTEXT_KEY)).thenReturn(true);
115
116         step.init();
117         assertFalse(step.start(200));
118         verify(policyOperation, never()).start();
119     }
120
121     @Test
122     public void testSuccess() {
123         AaiCqResponse data = new AaiCqResponse("{}");
124         OperationOutcome outcome = new OperationOutcome();
125         outcome.setResponse(data);
126
127         step.success(outcome);
128         verify(stepContext).setProperty(AaiCqResponse.CONTEXT_KEY, data);
129     }
130 }