2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.drools.apps.controller.usecases;
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;
35 import java.util.List;
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;
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";
55 private final StepContext stepContext = mock(StepContext.class);
56 private final ControlLoopOperationParams params = mock(ControlLoopOperationParams.class);
58 private VirtualControlLoopEvent event;
59 private GenericVnf vnf;
60 private GetTargetEntityOperation2 oper;
67 event = new VirtualControlLoopEvent();
68 event.setTarget("pnf.pnf-name");
69 event.setAai(Map.of("pnf.pnf-name", MY_PNF));
71 vnf = new GenericVnf();
74 when(params.getTargetType()).thenReturn(TargetType.PNF);
75 when(params.getActor()).thenReturn(MY_ACTOR);
76 when(params.getOperation()).thenReturn(MY_OPERATION);
78 oper = new GetTargetEntityOperation2(stepContext, event, params);
82 void testGetPropertyNames() {
83 // already have the data - no properties needed
84 assertThat(oper.getPropertyNames()).isEmpty();
86 // try an operation that needs data
88 assertEquals(List.of(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF), oper.getPropertyNames());
90 // tell it the entity is available
91 when(stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(true);
92 assertThat(oper.getPropertyNames()).isEmpty();
97 assertThatThrownBy(() -> oper.start()).isInstanceOf(UnsupportedOperationException.class)
98 .hasMessage("cannot start get-target-entity operation");
102 * Tests detmTarget() when the target has already been determined.
105 void testDetmTargetRepeat() {
107 assertFalse(oper.getPropertyNames().isEmpty());
109 // tell it the entity is available
110 when(stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(true);
111 assertThat(oper.getPropertyNames()).isEmpty();
114 assertThat(oper.getPropertyNames()).isEmpty();
118 * Tests detmTarget() when the target type is {@code null}.
121 void testDetmTargetNullType() {
123 when(params.getTargetType()).thenReturn(null);
125 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
126 .withMessage("The target type is null");
130 * Tests detmTarget() when the target type is VM and enrichment data is provided.
133 void testDetmTargetVm() {
134 when(params.getTargetType()).thenReturn(TargetType.VM);
135 enrichTarget(VSERVER_NAME);
136 assertThat(oper.getPropertyNames()).isEmpty();
137 verifyTarget(MY_VNF);
141 * Tests detmTarget() when the target type is VNF and enrichment data is provided.
144 void testDetmTargetVnf() {
145 when(params.getTargetType()).thenReturn(TargetType.VNF);
146 enrichTarget(VSERVER_NAME);
147 assertThat(oper.getPropertyNames()).isEmpty();
148 verifyTarget(MY_VNF);
152 * Tests detmTarget() when the target type is VF Module and enrichment data is
156 void testDetmTargetVfModule() {
157 when(params.getTargetType()).thenReturn(TargetType.VFMODULE);
158 enrichTarget(VSERVER_NAME);
159 assertThat(oper.getPropertyNames()).isEmpty();
160 verifyTarget(MY_VNF);
164 * Tests detmTarget() when the target type is unknown.
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");
175 void testDetmPnfTargetPnf() {
176 assertThat(oper.getPropertyNames()).isEmpty();
177 verifyTarget(MY_PNF);
181 * Tests detmPnfTarget() when the target name is incorrect.
184 void testDetmPnfTargetInvalidName() {
185 event.setTarget("unknown-pnf");
186 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
187 .withMessage("Target does not match target type");
191 * Tests detmPnfTarget() when the enrichment data is missing.
194 void testDetmPnfTargetPnfNoEnrichment() {
195 event.setAai(Map.of());
196 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
197 .withMessage("AAI section is missing pnf.pnf-name");
201 * Tests detmVfModuleTarget() when the target is null.
204 void testDetmVfModuleTargetNullTarget() {
205 when(params.getTargetType()).thenReturn(TargetType.VM);
206 event.setTarget(null);
207 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames()).withMessage("Target is null");
211 * Tests detmVfModuleTarget() when the target is the vserver name.
214 void testDetmVfModuleTargetVserverName() {
215 when(params.getTargetType()).thenReturn(TargetType.VM);
216 enrichTarget(VSERVER_NAME);
217 assertThat(oper.getPropertyNames()).isEmpty();
218 verifyTarget(MY_VNF);
222 * Tests detmVfModuleTarget() when the target is the VNF ID.
225 void testDetmVfModuleTargetVnfId() {
226 when(params.getTargetType()).thenReturn(TargetType.VM);
227 enrichTarget(GENERIC_VNF_ID);
228 assertThat(oper.getPropertyNames()).isEmpty();
229 verifyTarget(MY_VNF);
233 * Tests detmVfModuleTarget() when the target is the VNF name. Note: the enrichment
234 * data is actually stored in the VNF ID field.
237 void testDetmVfModuleTargetVnfName() {
238 when(params.getTargetType()).thenReturn(TargetType.VM);
239 enrichTarget(GENERIC_VNF_ID);
241 // set this AFTER setting enrichment data
242 event.setTarget("generic-vnf.vnf-name");
244 assertThat(oper.getPropertyNames()).isEmpty();
245 verifyTarget(MY_VNF);
249 * Tests detmVfModuleTarget() when the target is unknown.
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");
261 * Tests detmVfModuleTarget() when the enrichment data is missing.
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);
272 * Tests detmVnfName() when enrichment data is available.
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);
284 * Tests detmVnfName() when enrichment data is missing.
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));
294 void testSetProperty() {
295 event.setTarget(GENERIC_VNF_NAME);
297 // not a property of interest - should be ignored
298 oper.setProperty("unknown-property", vnf);
299 verify(stepContext, never()).setProperty(any(), any());
301 // now set the desired property and try again
302 oper.setProperty(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF, vnf);
303 verifyTarget(MY_VNF);
307 void testGetActorName_testGetName() {
308 assertEquals(MY_ACTOR, oper.getActorName());
309 assertEquals(MY_OPERATION, oper.getName());
312 private void verifyTarget(String targetEntity) {
313 verify(stepContext).setProperty(OperationProperties.AAI_TARGET_ENTITY, targetEntity);
316 private void remakeWithoutData() {
317 when(params.getTargetType()).thenReturn(TargetType.VNF);
318 event.setTarget(GENERIC_VNF_NAME);
319 oper = new GetTargetEntityOperation2(stepContext, event, params);
322 private void enrichTarget(String enrichmentTargetType) {
323 event.setTarget(enrichmentTargetType);
324 event.setAai(Map.of(enrichmentTargetType, MY_VNF));