bccfbb3be42855ebc6f233112b6c49655804085e
[policy/drools-applications.git] / controlloop / common / controller-usecases / src / test / java / org / onap / policy / drools / apps / controller / usecases / step / AaiGetTenantStep2Test.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.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
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.Map;
35 import java.util.UUID;
36 import java.util.concurrent.CompletableFuture;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.policy.common.utils.coder.StandardCoderObject;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.actor.aai.AaiActor;
45 import org.onap.policy.controlloop.actor.aai.AaiGetTenantOperation;
46 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
47 import org.onap.policy.controlloop.actorserviceprovider.Operation;
48 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
49 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
50 import org.onap.policy.controlloop.actorserviceprovider.Operator;
51 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
52 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
53 import org.onap.policy.controlloop.eventmanager.StepContext;
54
55 @RunWith(MockitoJUnitRunner.class)
56 public class AaiGetTenantStep2Test {
57     private static final String MY_VSERVER = "my-vserver";
58     private static final UUID REQ_ID = UUID.randomUUID();
59
60     @Mock
61     private Operator policyOperator;
62     @Mock
63     private Operation policyOperation;
64     @Mock
65     private Actor policyActor;
66     @Mock
67     private ActorService actors;
68     @Mock
69     private ControlLoopOperationParams params;
70     @Mock
71     private StepContext stepContext;
72     @Mock
73     private VirtualControlLoopEvent event;
74
75     private CompletableFuture<OperationOutcome> future;
76     private Step2 master;
77     private AaiGetTenantStep2 step;
78
79     /**
80      * Sets up.
81      */
82     @Before
83     public void setUp() {
84         future = new CompletableFuture<>();
85
86         when(params.toBuilder()).thenReturn(ControlLoopOperationParams.builder().actorService(actors)
87                         .requestId(REQ_ID));
88
89         // configure policy operation
90         when(actors.getActor(AaiActor.NAME)).thenReturn(policyActor);
91         when(policyActor.getOperator(AaiGetTenantOperation.NAME)).thenReturn(policyOperator);
92         when(policyOperator.buildOperation(any())).thenReturn(policyOperation);
93         when(policyOperation.start()).thenReturn(future);
94
95         when(event.getAai()).thenReturn(Map.of(Step2.VSERVER_VSERVER_NAME, MY_VSERVER));
96
97         master = new Step2(stepContext, params, event);
98         step = new AaiGetTenantStep2(master);
99     }
100
101     @Test
102     public void testConstructor() {
103         assertEquals(AaiActor.NAME, step.getActorName());
104         assertEquals(AaiGetTenantOperation.NAME, step.getOperationName());
105         assertSame(stepContext, step.stepContext);
106         assertSame(event, step.event);
107
108         // empty vserver name
109         when(event.getAai()).thenReturn(Map.of(Step2.VSERVER_VSERVER_NAME, ""));
110         assertThatIllegalArgumentException().isThrownBy(() -> new AaiGetTenantStep2(master))
111                         .withMessage("missing " + Step2.VSERVER_VSERVER_NAME + " in enrichment data");
112
113         // missing vserver name
114         when(event.getAai()).thenReturn(Map.of());
115         assertThatIllegalArgumentException().isThrownBy(() -> new AaiGetTenantStep2(master))
116                         .withMessage("missing " + Step2.VSERVER_VSERVER_NAME + " in enrichment data");
117     }
118
119     @Test
120     public void testGetPropertyNames() {
121         assertThat(step.getPropertyNames()).isEmpty();
122     }
123
124     @Test
125     public void testSetProperties() {
126         step.init();
127
128         step.setProperties();
129
130         verify(policyOperation).setProperty(OperationProperties.AAI_TARGET_ENTITY, MY_VSERVER);
131     }
132
133     @Test
134     public void testStart() {
135         step.init();
136         assertTrue(step.start(100));
137         verify(policyOperation).start();
138     }
139
140     /**
141      * Tests start() when the data has already been retrieved.
142      */
143     @Test
144     public void testStartAlreadyHaveData() {
145         when(stepContext.contains(AaiGetTenantOperation.getKey(MY_VSERVER))).thenReturn(true);
146
147         step.init();
148         assertFalse(step.start(200));
149         verify(policyOperation, never()).start();
150     }
151
152     @Test
153     public void testSuccess() {
154         StandardCoderObject data = new StandardCoderObject();
155         OperationOutcome outcome = new OperationOutcome();
156         outcome.setResponse(data);
157
158         step.success(outcome);
159         verify(stepContext).setProperty(AaiGetTenantOperation.getKey(MY_VSERVER), data);
160     }
161 }