2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.apps.controller.usecases;
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;
33 import java.util.List;
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;
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";
57 private StepContext stepContext;
59 private ControlLoopOperationParams params;
61 private VirtualControlLoopEvent event;
62 private Target target;
63 private GenericVnf vnf;
64 private GetTargetEntityOperation2 oper;
71 MockitoAnnotations.initMocks(this);
73 event = new VirtualControlLoopEvent();
74 event.setTarget("pnf.pnf-name");
75 event.setAai(Map.of("pnf.pnf-name", MY_PNF));
77 target = new Target();
78 target.setType(TargetType.PNF);
80 vnf = new GenericVnf();
83 when(params.getTarget()).thenReturn(target);
84 when(params.getActor()).thenReturn(MY_ACTOR);
85 when(params.getOperation()).thenReturn(MY_OPERATION);
87 oper = new GetTargetEntityOperation2(stepContext, event, params);
91 public void testGetPropertyNames() {
92 // already have the data - no properties needed
93 assertThat(oper.getPropertyNames()).isEmpty();
95 // try an operation that needs data
97 assertEquals(List.of(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF), oper.getPropertyNames());
99 // tell it the entity is available
100 when(stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(true);
101 assertThat(oper.getPropertyNames()).isEmpty();
105 public void testStart() {
106 assertThatThrownBy(() -> oper.start()).isInstanceOf(UnsupportedOperationException.class)
107 .hasMessage("cannot start get-target-entity operation");
111 * Tests detmTarget() when the target has already been determined.
114 public void testDetmTargetRepeat() {
116 assertFalse(oper.getPropertyNames().isEmpty());
118 // tell it the entity is available
119 when(stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(true);
120 assertThat(oper.getPropertyNames()).isEmpty();
123 assertThat(oper.getPropertyNames()).isEmpty();
127 * Tests detmTarget() when the target is {@code null}.
130 public void testDetmTargetNull() {
132 when(params.getTarget()).thenReturn(null);
134 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
135 .withMessage("The target is null");
139 * Tests detmTarget() when the target type is {@code null}.
142 public void testDetmTargetNullType() {
144 target.setType(null);
146 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
147 .withMessage("The target type is null");
151 * Tests detmTarget() when the target type is VM and enrichment data is provided.
154 public void testDetmTargetVm() {
155 target.setType(TargetType.VM);
156 enrichTarget(VSERVER_NAME);
157 assertThat(oper.getPropertyNames()).isEmpty();
158 verifyTarget(MY_VNF);
162 * Tests detmTarget() when the target type is VNF and enrichment data is provided.
165 public void testDetmTargetVnf() {
166 target.setType(TargetType.VNF);
167 enrichTarget(VSERVER_NAME);
168 assertThat(oper.getPropertyNames()).isEmpty();
169 verifyTarget(MY_VNF);
173 * Tests detmTarget() when the target type is VF Module and enrichment data is
177 public void testDetmTargetVfModule() {
178 target.setType(TargetType.VFMODULE);
179 enrichTarget(VSERVER_NAME);
180 assertThat(oper.getPropertyNames()).isEmpty();
181 verifyTarget(MY_VNF);
185 * Tests detmTarget() when the target type is unknown.
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");
196 public void testDetmPnfTargetPnf() {
197 assertThat(oper.getPropertyNames()).isEmpty();
198 verifyTarget(MY_PNF);
202 * Tests detmPnfTarget() when the target name is incorrect.
205 public void testDetmPnfTargetInvalidName() {
206 event.setTarget("unknown-pnf");
207 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
208 .withMessage("Target does not match target type");
212 * Tests detmPnfTarget() when the enrichment data is missing.
215 public void testDetmPnfTargetPnfNoEnrichment() {
216 event.setAai(Map.of());
217 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
218 .withMessage("AAI section is missing pnf.pnf-name");
222 * Tests detmVfModuleTarget() when the target is null.
225 public void testDetmVfModuleTargetNullTarget() {
226 target.setType(TargetType.VM);
227 event.setTarget(null);
228 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames()).withMessage("Target is null");
232 * Tests detmVfModuleTarget() when the target is the vserver name.
235 public void testDetmVfModuleTargetVserverName() {
236 target.setType(TargetType.VM);
237 enrichTarget(VSERVER_NAME);
238 assertThat(oper.getPropertyNames()).isEmpty();
239 verifyTarget(MY_VNF);
243 * Tests detmVfModuleTarget() when the target is the VNF ID.
246 public void testDetmVfModuleTargetVnfId() {
247 target.setType(TargetType.VM);
248 enrichTarget(GENERIC_VNF_ID);
249 assertThat(oper.getPropertyNames()).isEmpty();
250 verifyTarget(MY_VNF);
254 * Tests detmVfModuleTarget() when the target is the VNF name. Note: the enrichment
255 * data is actually stored in the VNF ID field.
258 public void testDetmVfModuleTargetVnfName() {
259 target.setType(TargetType.VM);
260 enrichTarget(GENERIC_VNF_ID);
262 // set this AFTER setting enrichment data
263 event.setTarget("generic-vnf.vnf-name");
265 assertThat(oper.getPropertyNames()).isEmpty();
266 verifyTarget(MY_VNF);
270 * Tests detmVfModuleTarget() when the target is unknown.
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");
282 * Tests detmVfModuleTarget() when the enrichment data is missing.
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);
293 * Tests detmVnfName() when enrichment data is available.
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);
305 * Tests detmVnfName() when enrichment data is missing.
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));
315 public void testSetProperty() {
316 target.setType(TargetType.VM);
317 event.setTarget(GENERIC_VNF_NAME);
319 // not a property of interest - should be ignored
320 oper.setProperty("unknown-property", vnf);
321 verify(stepContext, never()).setProperty(any(), any());
323 // now set the desired property and try again
324 oper.setProperty(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF, vnf);
325 verifyTarget(MY_VNF);
329 public void testGetActorName_testGetName() {
330 assertEquals(MY_ACTOR, oper.getActorName());
331 assertEquals(MY_OPERATION, oper.getName());
334 private void verifyTarget(String targetEntity) {
335 verify(stepContext).setProperty(OperationProperties.AAI_TARGET_ENTITY, targetEntity);
338 private void remakeWithoutData() {
339 target.setType(TargetType.VNF);
340 event.setTarget(GENERIC_VNF_NAME);
341 oper = new GetTargetEntityOperation2(stepContext, event, params);
344 private void enrichTarget(String enrichmentTargetType) {
345 event.setTarget(enrichmentTargetType);
346 event.setAai(Map.of(enrichmentTargetType, MY_VNF));