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