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.step;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.when;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.UUID;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.aai.domain.yang.CloudRegion;
40 import org.onap.aai.domain.yang.GenericVnf;
41 import org.onap.aai.domain.yang.RelatedToProperty;
42 import org.onap.aai.domain.yang.Relationship;
43 import org.onap.aai.domain.yang.RelationshipData;
44 import org.onap.aai.domain.yang.RelationshipList;
45 import org.onap.aai.domain.yang.Vserver;
46 import org.onap.policy.aai.AaiCqResponse;
47 import org.onap.policy.controlloop.VirtualControlLoopEvent;
48 import org.onap.policy.controlloop.actor.so.VfModuleCreate;
49 import org.onap.policy.controlloop.actor.xacml.GuardOperation;
50 import org.onap.policy.controlloop.actor.xacml.XacmlActor;
51 import org.onap.policy.controlloop.actorserviceprovider.Operation;
52 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
53 import org.onap.policy.controlloop.actorserviceprovider.TargetType;
54 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
55 import org.onap.policy.controlloop.eventmanager.StepContext;
56 import org.onap.policy.drools.apps.controller.usecases.UsecasesConstants;
58 @RunWith(MockitoJUnitRunner.class)
59 public class GuardStep2Test {
60 private static final String SOME_OTHER_VALUE = "some-other-value";
61 private static final String SOME_OTHER_KEY = "some-other-key";
62 private static final String CL_NAME = "my-closed-loop";
63 private static final String MASTER_ACTOR = "master-actor";
64 private static final String MASTER_OPERATION = "master-operation";
65 private static final String MY_TARGET = "my-target";
66 private static final String MY_NAME = "my-name";
67 private static final String MY_TYPE = "my-type";
68 private static final String MY_CODE = "my-code";
69 private static final String MY_SERVER2 = "my-server-2";
70 private static final String MY_SERVERNAME = "my-server-name";
71 private static final String MY_REGION = "my-region";
72 private static final UUID REQ_ID = UUID.randomUUID();
73 private static final int VF_COUNT = 10;
76 private StepContext stepContext;
78 private VirtualControlLoopEvent event;
80 private Operation policyOper;
82 private AaiCqResponse customQuery;
84 private GenericVnf genericVnf;
86 private CloudRegion cloudRegion;
87 @Mock Vserver theVserver;
90 Map<String, String> aai = new HashMap<>();
92 private ControlLoopOperationParams params;
94 private GuardStep2 step;
100 public void setUp() {
101 aai.put("vserver.vserver-name", MY_SERVERNAME);
102 when(event.getAai()).thenReturn(aai);
104 when(genericVnf.getVnfName()).thenReturn(MY_NAME);
105 when(genericVnf.getVnfType()).thenReturn(MY_TYPE);
106 when(genericVnf.getNfNamingCode()).thenReturn(MY_CODE);
108 RelationshipList relList = new RelationshipList();
109 when(genericVnf.getRelationshipList()).thenReturn(relList);
111 relList.getRelationship().add(new Relationship());
113 Relationship relationship = new Relationship();
114 relList.getRelationship().add(relationship);
115 relationship.setRelatedTo("vserver");
117 relationship.getRelatedToProperty().add(new RelatedToProperty());
119 // property key mismatch
120 RelatedToProperty relProp = new RelatedToProperty();
121 relationship.getRelatedToProperty().add(relProp);
122 relProp.setPropertyKey(SOME_OTHER_KEY);
123 relProp.setPropertyValue(MY_NAME);
125 // property value mismatch
126 relProp = new RelatedToProperty();
127 relationship.getRelatedToProperty().add(relProp);
128 relProp.setPropertyKey("vserver.vserver-name");
129 relProp.setPropertyValue(SOME_OTHER_VALUE);
132 relProp = new RelatedToProperty();
133 relationship.getRelatedToProperty().add(relProp);
134 relProp.setPropertyKey("vserver.vserver-name");
135 relProp.setPropertyValue(MY_SERVERNAME);
138 RelationshipData relData = new RelationshipData();
139 relationship.getRelationshipData().add(relData);
140 relData.setRelationshipKey(SOME_OTHER_KEY);
141 relData.setRelationshipValue(SOME_OTHER_VALUE);
144 relData = new RelationshipData();
145 relationship.getRelationshipData().add(relData);
146 relData.setRelationshipKey(GuardStep2.PAYLOAD_KEY_VSERVER_ID);
147 relData.setRelationshipValue(MY_SERVER2);
149 when(customQuery.getGenericVnfByVnfId(MY_TARGET)).thenReturn(genericVnf);
151 when(cloudRegion.getCloudRegionId()).thenReturn(MY_REGION);
152 when(customQuery.getDefaultCloudRegion()).thenReturn(cloudRegion);
154 when(stepContext.getProperty(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(MY_TARGET);
155 when(stepContext.getProperty(AaiCqResponse.CONTEXT_KEY)).thenReturn(customQuery);
156 //when(stepContext.getProperty(VSERVER_VSERVER_NAME)).thenReturn()
158 when(stepContext.contains(OperationProperties.DATA_VF_COUNT)).thenReturn(true);
159 when(stepContext.getProperty(OperationProperties.DATA_VF_COUNT)).thenReturn(VF_COUNT);
162 params = ControlLoopOperationParams.builder()
164 .operation(MASTER_OPERATION)
170 master = new Step2(stepContext, params, event) {
172 protected Operation buildOperation() {
177 // force it to build the operation
180 step = new GuardStep2(master, CL_NAME);
184 public void testConstructor() {
185 assertEquals(XacmlActor.NAME, step.getActorName());
186 assertEquals(GuardOperation.NAME, step.getOperationName());
187 assertSame(stepContext, step.stepContext);
188 assertSame(event, step.event);
190 // test when master is uninitialized
191 master = new Step2(stepContext, params, event);
192 assertThatIllegalStateException().isThrownBy(() -> new GuardStep2(master, CL_NAME));
194 ControlLoopOperationParams params2 = step.getParams();
197 assertThat(params2.getPayload()).isEqualTo(Map.of(
198 "actor", MASTER_ACTOR,
199 "operation", MASTER_OPERATION,
206 public void testAcceptsEvent() {
207 // it should always accept events
208 assertTrue(step.acceptsEvent());
212 public void testGetPropertyNames() {
213 // unmatching property names
214 when(policyOper.getPropertyNames()).thenReturn(List.of("propA", "propB"));
215 assertThat(step.getPropertyNames())
216 .containsAll(List.of(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF,
217 OperationProperties.AAI_DEFAULT_CLOUD_REGION));
219 // matching property names
220 when(policyOper.getPropertyNames())
221 .thenReturn(List.of("propA", OperationProperties.DATA_VF_COUNT, "propB"));
222 assertThat(step.getPropertyNames()).containsAll(List.of(OperationProperties.DATA_VF_COUNT,
223 UsecasesConstants.AAI_DEFAULT_GENERIC_VNF,
224 OperationProperties.AAI_DEFAULT_CLOUD_REGION));
228 public void testLoadTargetEntity() {
229 step.loadTargetEntity(OperationProperties.AAI_TARGET_ENTITY);
230 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_TARGET_ENTITY, MY_TARGET);
234 public void testLoadDefaultGenericVnf() {
235 step.loadDefaultGenericVnf(OperationProperties.AAI_VNF);
236 assertThat(step.getParams().getPayload())
237 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_ID, MY_TARGET)
238 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_NAME, MY_NAME)
239 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_TYPE, MY_TYPE)
240 .containsEntry(GuardStep2.PAYLOAD_KEY_NF_NAMING_CODE, MY_CODE)
241 .containsEntry(GuardStep2.PAYLOAD_KEY_VSERVER_ID, MY_SERVER2);
245 public void testLoadCloudRegion() {
246 step.loadCloudRegion(OperationProperties.AAI_DEFAULT_CLOUD_REGION);
247 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_CLOUD_REGION_ID, MY_REGION);
251 * Tests loadVfCount() when the policy operation is NOT "VF Module Create".
254 public void testLoadVfCountNotVfModuleCreate() {
255 // should decrement the count
256 step.loadVfCount("");
257 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT - 1);
261 * Tests loadVfCount() when the policy operation is "VF Module Create".
264 public void testLoadVfCountVfModuleCreate() {
265 when(policyOper.getName()).thenReturn(VfModuleCreate.NAME);
267 // should increment the count
268 step.loadVfCount("");
269 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT + 1);