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