efb7e74d3a3e2b6a1261ae8d9d35d7583724be3a
[policy/drools-applications.git] / controlloop / common / controller-usecases / src / test / java / org / onap / policy / drools / apps / controller / usecases / GetTargetEntityOperation2Test.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;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import java.util.List;
34 import java.util.Map;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.aai.domain.yang.GenericVnf;
41 import org.onap.policy.controlloop.VirtualControlLoopEvent;
42 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
43 import org.onap.policy.controlloop.actorserviceprovider.TargetType;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
45 import org.onap.policy.controlloop.eventmanager.StepContext;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class GetTargetEntityOperation2Test {
49     private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
50     private static final String VSERVER_NAME = "vserver.vserver-name";
51     private static final String GENERIC_VNF_NAME = "generic-vnf.vnf-name";
52     private static final String MY_PNF = "my-pnf";
53     private static final String MY_VNF = "my-vnf";
54     private static final String MY_ACTOR = "my-actor";
55     private static final String MY_OPERATION = "my-operation";
56
57     @Mock
58     private StepContext stepContext;
59     @Mock
60     private ControlLoopOperationParams params;
61
62     private VirtualControlLoopEvent event;
63     private GenericVnf vnf;
64     private GetTargetEntityOperation2 oper;
65
66     /**
67      * Sets up.
68      */
69     @Before
70     public void setUp() {
71         event = new VirtualControlLoopEvent();
72         event.setTarget("pnf.pnf-name");
73         event.setAai(Map.of("pnf.pnf-name", MY_PNF));
74
75         vnf = new GenericVnf();
76         vnf.setVnfId(MY_VNF);
77
78         when(params.getTargetType()).thenReturn(TargetType.PNF);
79         when(params.getActor()).thenReturn(MY_ACTOR);
80         when(params.getOperation()).thenReturn(MY_OPERATION);
81
82         oper = new GetTargetEntityOperation2(stepContext, event, params);
83     }
84
85     @Test
86     public void testGetPropertyNames() {
87         // already have the data - no properties needed
88         assertThat(oper.getPropertyNames()).isEmpty();
89
90         // try an operation that needs data
91         remakeWithoutData();
92         assertEquals(List.of(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF), oper.getPropertyNames());
93
94         // tell it the entity is available
95         when(stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(true);
96         assertThat(oper.getPropertyNames()).isEmpty();
97     }
98
99     @Test
100     public void testStart() {
101         assertThatThrownBy(() -> oper.start()).isInstanceOf(UnsupportedOperationException.class)
102                         .hasMessage("cannot start get-target-entity operation");
103     }
104
105     /**
106      * Tests detmTarget() when the target has already been determined.
107      */
108     @Test
109     public void testDetmTargetRepeat() {
110         remakeWithoutData();
111         assertFalse(oper.getPropertyNames().isEmpty());
112
113         // tell it the entity is available
114         when(stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(true);
115         assertThat(oper.getPropertyNames()).isEmpty();
116
117         // repeat
118         assertThat(oper.getPropertyNames()).isEmpty();
119     }
120
121     /**
122      * Tests detmTarget() when the target type is {@code null}.
123      */
124     @Test
125     public void testDetmTargetNullType() {
126         remakeWithoutData();
127         when(params.getTargetType()).thenReturn(null);
128
129         assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
130                         .withMessage("The target type is null");
131     }
132
133     /**
134      * Tests detmTarget() when the target type is VM and enrichment data is provided.
135      */
136     @Test
137     public void testDetmTargetVm() {
138         when(params.getTargetType()).thenReturn(TargetType.VM);
139         enrichTarget(VSERVER_NAME);
140         assertThat(oper.getPropertyNames()).isEmpty();
141         verifyTarget(MY_VNF);
142     }
143
144     /**
145      * Tests detmTarget() when the target type is VNF and enrichment data is provided.
146      */
147     @Test
148     public void testDetmTargetVnf() {
149         when(params.getTargetType()).thenReturn(TargetType.VNF);
150         enrichTarget(VSERVER_NAME);
151         assertThat(oper.getPropertyNames()).isEmpty();
152         verifyTarget(MY_VNF);
153     }
154
155     /**
156      * Tests detmTarget() when the target type is VF Module and enrichment data is
157      * provided.
158      */
159     @Test
160     public void testDetmTargetVfModule() {
161         when(params.getTargetType()).thenReturn(TargetType.VFMODULE);
162         enrichTarget(VSERVER_NAME);
163         assertThat(oper.getPropertyNames()).isEmpty();
164         verifyTarget(MY_VNF);
165     }
166
167     /**
168      * Tests detmTarget() when the target type is unknown.
169      */
170     @Test
171     public void testDetmTargetUnknownType() {
172         when(params.getTargetType()).thenReturn(TargetType.VFC);
173         enrichTarget(VSERVER_NAME);
174         assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
175                         .withMessage("The target type is not supported");
176     }
177
178     @Test
179     public void testDetmPnfTargetPnf() {
180         assertThat(oper.getPropertyNames()).isEmpty();
181         verifyTarget(MY_PNF);
182     }
183
184     /**
185      * Tests detmPnfTarget() when the target name is incorrect.
186      */
187     @Test
188     public void testDetmPnfTargetInvalidName() {
189         event.setTarget("unknown-pnf");
190         assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
191                         .withMessage("Target does not match target type");
192     }
193
194     /**
195      * Tests detmPnfTarget() when the enrichment data is missing.
196      */
197     @Test
198     public void testDetmPnfTargetPnfNoEnrichment() {
199         event.setAai(Map.of());
200         assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
201                         .withMessage("AAI section is missing pnf.pnf-name");
202     }
203
204     /**
205      * Tests detmVfModuleTarget() when the target is null.
206      */
207     @Test
208     public void testDetmVfModuleTargetNullTarget() {
209         when(params.getTargetType()).thenReturn(TargetType.VM);
210         event.setTarget(null);
211         assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames()).withMessage("Target is null");
212     }
213
214     /**
215      * Tests detmVfModuleTarget() when the target is the vserver name.
216      */
217     @Test
218     public void testDetmVfModuleTargetVserverName() {
219         when(params.getTargetType()).thenReturn(TargetType.VM);
220         enrichTarget(VSERVER_NAME);
221         assertThat(oper.getPropertyNames()).isEmpty();
222         verifyTarget(MY_VNF);
223     }
224
225     /**
226      * Tests detmVfModuleTarget() when the target is the VNF ID.
227      */
228     @Test
229     public void testDetmVfModuleTargetVnfId() {
230         when(params.getTargetType()).thenReturn(TargetType.VM);
231         enrichTarget(GENERIC_VNF_ID);
232         assertThat(oper.getPropertyNames()).isEmpty();
233         verifyTarget(MY_VNF);
234     }
235
236     /**
237      * Tests detmVfModuleTarget() when the target is the VNF name. Note: the enrichment
238      * data is actually stored in the VNF ID field.
239      */
240     @Test
241     public void testDetmVfModuleTargetVnfName() {
242         when(params.getTargetType()).thenReturn(TargetType.VM);
243         enrichTarget(GENERIC_VNF_ID);
244
245         // set this AFTER setting enrichment data
246         event.setTarget("generic-vnf.vnf-name");
247
248         assertThat(oper.getPropertyNames()).isEmpty();
249         verifyTarget(MY_VNF);
250     }
251
252     /**
253      * Tests detmVfModuleTarget() when the target is unknown.
254      */
255     @Test
256     public void testDetmVfModuleTargetUnknown() {
257         when(params.getTargetType()).thenReturn(TargetType.VM);
258         enrichTarget(VSERVER_NAME);
259         event.setTarget("unknown-vnf");
260         assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
261                         .withMessage("Target does not match target type");
262     }
263
264     /**
265      * Tests detmVfModuleTarget() when the enrichment data is missing.
266      */
267     @Test
268     public void testDetmVfModuleTargetMissingEnrichment() {
269         when(params.getTargetType()).thenReturn(TargetType.VM);
270         event.setTarget(VSERVER_NAME);
271         assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
272                         .withMessage("Enrichment data is missing " + VSERVER_NAME);
273     }
274
275     /**
276      * Tests detmVnfName() when enrichment data is available.
277      */
278     @Test
279     public void testDetmVnfNameHaveData() {
280         when(params.getTargetType()).thenReturn(TargetType.VM);
281         event.setTarget(GENERIC_VNF_NAME);
282         event.setAai(Map.of(GENERIC_VNF_ID, MY_VNF));
283         assertThat(oper.getPropertyNames()).isEmpty();
284         verifyTarget(MY_VNF);
285     }
286
287     /**
288      * Tests detmVnfName() when enrichment data is missing.
289      */
290     @Test
291     public void testDetmVnfNameNoData() {
292         when(params.getTargetType()).thenReturn(TargetType.VM);
293         event.setTarget(GENERIC_VNF_NAME);
294         assertThat(oper.getPropertyNames()).isEqualTo(List.of(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF));
295     }
296
297     @Test
298     public void testSetProperty() {
299         event.setTarget(GENERIC_VNF_NAME);
300
301         // not a property of interest - should be ignored
302         oper.setProperty("unknown-property", vnf);
303         verify(stepContext, never()).setProperty(any(), any());
304
305         // now set the desired property and try again
306         oper.setProperty(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF, vnf);
307         verifyTarget(MY_VNF);
308     }
309
310     @Test
311     public void testGetActorName_testGetName() {
312         assertEquals(MY_ACTOR, oper.getActorName());
313         assertEquals(MY_OPERATION, oper.getName());
314     }
315
316     private void verifyTarget(String targetEntity) {
317         verify(stepContext).setProperty(OperationProperties.AAI_TARGET_ENTITY, targetEntity);
318     }
319
320     private void remakeWithoutData() {
321         when(params.getTargetType()).thenReturn(TargetType.VNF);
322         event.setTarget(GENERIC_VNF_NAME);
323         oper = new GetTargetEntityOperation2(stepContext, event, params);
324     }
325
326     private void enrichTarget(String enrichmentTargetType) {
327         event.setTarget(enrichmentTargetType);
328         event.setAai(Map.of(enrichmentTargetType, MY_VNF));
329     }
330 }