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