2 * ============LICENSE_START=======================================================
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
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.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;
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";
58 private StepContext stepContext;
60 private ControlLoopOperationParams params;
62 private VirtualControlLoopEvent event;
63 private GenericVnf vnf;
64 private GetTargetEntityOperation2 oper;
71 event = new VirtualControlLoopEvent();
72 event.setTarget("pnf.pnf-name");
73 event.setAai(Map.of("pnf.pnf-name", MY_PNF));
75 vnf = new GenericVnf();
78 when(params.getTargetType()).thenReturn(TargetType.PNF);
79 when(params.getActor()).thenReturn(MY_ACTOR);
80 when(params.getOperation()).thenReturn(MY_OPERATION);
82 oper = new GetTargetEntityOperation2(stepContext, event, params);
86 public void testGetPropertyNames() {
87 // already have the data - no properties needed
88 assertThat(oper.getPropertyNames()).isEmpty();
90 // try an operation that needs data
92 assertEquals(List.of(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF), oper.getPropertyNames());
94 // tell it the entity is available
95 when(stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(true);
96 assertThat(oper.getPropertyNames()).isEmpty();
100 public void testStart() {
101 assertThatThrownBy(() -> oper.start()).isInstanceOf(UnsupportedOperationException.class)
102 .hasMessage("cannot start get-target-entity operation");
106 * Tests detmTarget() when the target has already been determined.
109 public void testDetmTargetRepeat() {
111 assertFalse(oper.getPropertyNames().isEmpty());
113 // tell it the entity is available
114 when(stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(true);
115 assertThat(oper.getPropertyNames()).isEmpty();
118 assertThat(oper.getPropertyNames()).isEmpty();
122 * Tests detmTarget() when the target type is {@code null}.
125 public void testDetmTargetNullType() {
127 when(params.getTargetType()).thenReturn(null);
129 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
130 .withMessage("The target type is null");
134 * Tests detmTarget() when the target type is VM and enrichment data is provided.
137 public void testDetmTargetVm() {
138 when(params.getTargetType()).thenReturn(TargetType.VM);
139 enrichTarget(VSERVER_NAME);
140 assertThat(oper.getPropertyNames()).isEmpty();
141 verifyTarget(MY_VNF);
145 * Tests detmTarget() when the target type is VNF and enrichment data is provided.
148 public void testDetmTargetVnf() {
149 when(params.getTargetType()).thenReturn(TargetType.VNF);
150 enrichTarget(VSERVER_NAME);
151 assertThat(oper.getPropertyNames()).isEmpty();
152 verifyTarget(MY_VNF);
156 * Tests detmTarget() when the target type is VF Module and enrichment data is
160 public void testDetmTargetVfModule() {
161 when(params.getTargetType()).thenReturn(TargetType.VFMODULE);
162 enrichTarget(VSERVER_NAME);
163 assertThat(oper.getPropertyNames()).isEmpty();
164 verifyTarget(MY_VNF);
168 * Tests detmTarget() when the target type is unknown.
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");
179 public void testDetmPnfTargetPnf() {
180 assertThat(oper.getPropertyNames()).isEmpty();
181 verifyTarget(MY_PNF);
185 * Tests detmPnfTarget() when the target name is incorrect.
188 public void testDetmPnfTargetInvalidName() {
189 event.setTarget("unknown-pnf");
190 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
191 .withMessage("Target does not match target type");
195 * Tests detmPnfTarget() when the enrichment data is missing.
198 public void testDetmPnfTargetPnfNoEnrichment() {
199 event.setAai(Map.of());
200 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
201 .withMessage("AAI section is missing pnf.pnf-name");
205 * Tests detmVfModuleTarget() when the target is null.
208 public void testDetmVfModuleTargetNullTarget() {
209 when(params.getTargetType()).thenReturn(TargetType.VM);
210 event.setTarget(null);
211 assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames()).withMessage("Target is null");
215 * Tests detmVfModuleTarget() when the target is the vserver name.
218 public void testDetmVfModuleTargetVserverName() {
219 when(params.getTargetType()).thenReturn(TargetType.VM);
220 enrichTarget(VSERVER_NAME);
221 assertThat(oper.getPropertyNames()).isEmpty();
222 verifyTarget(MY_VNF);
226 * Tests detmVfModuleTarget() when the target is the VNF ID.
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);
237 * Tests detmVfModuleTarget() when the target is the VNF name. Note: the enrichment
238 * data is actually stored in the VNF ID field.
241 public void testDetmVfModuleTargetVnfName() {
242 when(params.getTargetType()).thenReturn(TargetType.VM);
243 enrichTarget(GENERIC_VNF_ID);
245 // set this AFTER setting enrichment data
246 event.setTarget("generic-vnf.vnf-name");
248 assertThat(oper.getPropertyNames()).isEmpty();
249 verifyTarget(MY_VNF);
253 * Tests detmVfModuleTarget() when the target is unknown.
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");
265 * Tests detmVfModuleTarget() when the enrichment data is missing.
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);
276 * Tests detmVnfName() when enrichment data is available.
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);
288 * Tests detmVnfName() when enrichment data is missing.
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));
298 public void testSetProperty() {
299 event.setTarget(GENERIC_VNF_NAME);
301 // not a property of interest - should be ignored
302 oper.setProperty("unknown-property", vnf);
303 verify(stepContext, never()).setProperty(any(), any());
305 // now set the desired property and try again
306 oper.setProperty(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF, vnf);
307 verifyTarget(MY_VNF);
311 public void testGetActorName_testGetName() {
312 assertEquals(MY_ACTOR, oper.getActorName());
313 assertEquals(MY_OPERATION, oper.getName());
316 private void verifyTarget(String targetEntity) {
317 verify(stepContext).setProperty(OperationProperties.AAI_TARGET_ENTITY, targetEntity);
320 private void remakeWithoutData() {
321 when(params.getTargetType()).thenReturn(TargetType.VNF);
322 event.setTarget(GENERIC_VNF_NAME);
323 oper = new GetTargetEntityOperation2(stepContext, event, params);
326 private void enrichTarget(String enrichmentTargetType) {
327 event.setTarget(enrichmentTargetType);
328 event.setAai(Map.of(enrichmentTargetType, MY_VNF));