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