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 / AaiGetPnfStep2Test.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.common.utils.coder.StandardCoderObject;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.actor.aai.AaiActor;
41 import org.onap.policy.controlloop.actor.aai.AaiGetPnfOperation;
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.OperationProperties;
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 class AaiGetPnfStep2Test {
52     private static final String MY_TARGET = "my-target";
53     private static final UUID REQ_ID = UUID.randomUUID();
54
55     private final Operator policyOperator = mock(Operator.class);
56     private final Operation policyOperation = mock(Operation.class);
57     private final Actor policyActor = mock(Actor.class);
58     private final ActorService actors = mock(ActorService.class);
59     private final ControlLoopOperationParams params = mock(ControlLoopOperationParams.class);
60     private final StepContext stepContext = mock(StepContext.class);
61     private final VirtualControlLoopEvent event = mock(VirtualControlLoopEvent.class);
62
63     private CompletableFuture<OperationOutcome> future;
64     private Step2 master;
65     private AaiGetPnfStep2 step;
66
67     /**
68      * Sets up.
69      */
70     @BeforeEach
71     public void setUp() {
72         future = new CompletableFuture<>();
73
74         when(params.toBuilder()).thenReturn(ControlLoopOperationParams.builder().actorService(actors)
75                         .requestId(REQ_ID));
76
77         // configure policy operation
78         when(actors.getActor(AaiActor.NAME)).thenReturn(policyActor);
79         when(policyActor.getOperator(AaiGetPnfOperation.NAME)).thenReturn(policyOperator);
80         when(policyOperator.buildOperation(any())).thenReturn(policyOperation);
81         when(policyOperation.start()).thenReturn(future);
82         when(stepContext.getProperty(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(MY_TARGET);
83
84         master = new Step2(stepContext, params, event);
85         step = new AaiGetPnfStep2(master);
86     }
87
88     @Test
89     void testConstructor() {
90         assertEquals(AaiActor.NAME, step.getActorName());
91         assertEquals(AaiGetPnfOperation.NAME, step.getOperationName());
92         assertSame(stepContext, step.stepContext);
93         assertSame(event, step.event);
94     }
95
96     @Test
97     void testStart() {
98         step.init();
99         assertTrue(step.start(100));
100         verify(policyOperation).start();
101     }
102
103     /**
104      * Tests start() when the data has already been retrieved.
105      */
106     @Test
107     void testStartAlreadyHaveData() {
108         when(stepContext.contains(AaiGetPnfOperation.getKey(MY_TARGET))).thenReturn(true);
109
110         step.init();
111         assertFalse(step.start(200));
112         verify(policyOperation, never()).start();
113     }
114
115     @Test
116     void testSuccess() {
117         var data = new StandardCoderObject();
118         var outcome = new OperationOutcome();
119         outcome.setResponse(data);
120
121         step.success(outcome);
122         verify(stepContext).setProperty(AaiGetPnfOperation.getKey(MY_TARGET), data);
123     }
124 }