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.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.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.aai.domain.yang.CloudRegion;
39 import org.onap.aai.domain.yang.GenericVnf;
40 import org.onap.aai.domain.yang.RelatedToProperty;
41 import org.onap.aai.domain.yang.Relationship;
42 import org.onap.aai.domain.yang.RelationshipData;
43 import org.onap.aai.domain.yang.RelationshipList;
44 import org.onap.aai.domain.yang.Vserver;
45 import org.onap.policy.aai.AaiCqResponse;
46 import org.onap.policy.controlloop.VirtualControlLoopEvent;
47 import org.onap.policy.controlloop.actor.guard.DecisionOperation;
48 import org.onap.policy.controlloop.actor.guard.GuardActor;
49 import org.onap.policy.controlloop.actor.so.VfModuleCreate;
50 import org.onap.policy.controlloop.actorserviceprovider.Operation;
51 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
52 import org.onap.policy.controlloop.actorserviceprovider.TargetType;
53 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
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 public class GuardStep2Test {
59 private static final String SOME_OTHER_VALUE = "some-other-value";
60 private static final String SOME_OTHER_KEY = "some-other-key";
61 private static final String CL_NAME = "my-closed-loop";
62 private static final String MASTER_ACTOR = "master-actor";
63 private static final String MASTER_OPERATION = "master-operation";
64 private static final String MY_TARGET = "my-target";
65 private static final String MY_NAME = "my-name";
66 private static final String MY_TYPE = "my-type";
67 private static final String MY_CODE = "my-code";
68 private static final String MY_SERVER = "my-server";
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 ControlLoopEventContext context;
78 private StepContext stepContext;
80 private VirtualControlLoopEvent event;
82 private Operation policyOper;
84 private AaiCqResponse customQuery;
86 private GenericVnf genericVnf;
88 private CloudRegion cloudRegion;
89 @Mock Vserver theVserver;
92 Map<String, String> aai = new HashMap<>();
94 private ControlLoopOperationParams params;
96 private GuardStep2 step;
102 public void setUp() {
103 MockitoAnnotations.initMocks(this);
105 aai.put("vserver.vserver-name", MY_SERVERNAME);
106 when(event.getRequestId()).thenReturn(REQ_ID);
107 when(event.getAai()).thenReturn(aai);
109 when(context.getEvent()).thenReturn(event);
111 when(genericVnf.getVnfId()).thenReturn(MY_TARGET);
112 when(genericVnf.getVnfName()).thenReturn(MY_NAME);
113 when(genericVnf.getVnfType()).thenReturn(MY_TYPE);
114 when(genericVnf.getNfNamingCode()).thenReturn(MY_CODE);
116 RelationshipList relList = new RelationshipList();
117 when(genericVnf.getRelationshipList()).thenReturn(relList);
119 relList.getRelationship().add(new Relationship());
121 Relationship relationship = new Relationship();
122 relList.getRelationship().add(relationship);
123 relationship.setRelatedTo("vserver");
125 relationship.getRelatedToProperty().add(new RelatedToProperty());
127 // property key mismatch
128 RelatedToProperty relProp = new RelatedToProperty();
129 relationship.getRelatedToProperty().add(relProp);
130 relProp.setPropertyKey(SOME_OTHER_KEY);
131 relProp.setPropertyValue(MY_NAME);
133 // property value mismatch
134 relProp = new RelatedToProperty();
135 relationship.getRelatedToProperty().add(relProp);
136 relProp.setPropertyKey("vserver.vserver-name");
137 relProp.setPropertyValue(SOME_OTHER_VALUE);
140 relProp = new RelatedToProperty();
141 relationship.getRelatedToProperty().add(relProp);
142 relProp.setPropertyKey("vserver.vserver-name");
143 relProp.setPropertyValue(MY_SERVERNAME);
146 RelationshipData relData = new RelationshipData();
147 relationship.getRelationshipData().add(relData);
148 relData.setRelationshipKey(SOME_OTHER_KEY);
149 relData.setRelationshipValue(SOME_OTHER_VALUE);
152 relData = new RelationshipData();
153 relationship.getRelationshipData().add(relData);
154 relData.setRelationshipKey(GuardStep2.PAYLOAD_KEY_VSERVER_ID);
155 relData.setRelationshipValue(MY_SERVER2);
157 when(customQuery.getGenericVnfByVnfId(MY_TARGET)).thenReturn(genericVnf);
159 when(theVserver.getVserverId()).thenReturn(MY_SERVER);
160 when(customQuery.getVserver()).thenReturn(theVserver);
162 when(cloudRegion.getCloudRegionId()).thenReturn(MY_REGION);
163 when(customQuery.getDefaultCloudRegion()).thenReturn(cloudRegion);
165 when(stepContext.getProperty(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(MY_TARGET);
166 when(stepContext.getProperty(AaiCqResponse.CONTEXT_KEY)).thenReturn(customQuery);
167 //when(stepContext.getProperty(VSERVER_VSERVER_NAME)).thenReturn()
169 when(stepContext.contains(OperationProperties.DATA_VF_COUNT)).thenReturn(true);
170 when(stepContext.getProperty(OperationProperties.DATA_VF_COUNT)).thenReturn(VF_COUNT);
173 params = ControlLoopOperationParams.builder()
175 .operation(MASTER_OPERATION)
176 .targetEntity(MY_TARGET)
182 master = new Step2(stepContext, params, event) {
184 protected Operation buildOperation() {
189 // force it to build the operation
192 step = new GuardStep2(master, CL_NAME);
196 public void testConstructor() {
197 assertEquals(GuardActor.NAME, step.getActorName());
198 assertEquals(DecisionOperation.NAME, step.getOperationName());
199 assertSame(stepContext, step.stepContext);
200 assertSame(event, step.event);
202 // test when master is uninitialized
203 master = new Step2(stepContext, params, event);
204 assertThatIllegalStateException().isThrownBy(() -> new GuardStep2(master, CL_NAME));
206 ControlLoopOperationParams params2 = step.getParams();
209 assertThat(params2.getPayload()).isEqualTo(Map.of(
210 "actor", MASTER_ACTOR,
211 "operation", MASTER_OPERATION,
218 public void testAcceptsEvent() {
219 // it should always accept events
220 assertTrue(step.acceptsEvent());
224 public void testGetPropertyNames() {
225 // unmatching property names
226 when(policyOper.getPropertyNames()).thenReturn(List.of("propA", "propB"));
227 assertThat(step.getPropertyNames())
228 .containsAll(List.of(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF,
229 OperationProperties.AAI_DEFAULT_CLOUD_REGION));
231 // matching property names
232 when(policyOper.getPropertyNames())
233 .thenReturn(List.of("propA", OperationProperties.DATA_VF_COUNT, "propB"));
234 assertThat(step.getPropertyNames()).containsAll(List.of(OperationProperties.DATA_VF_COUNT,
235 UsecasesConstants.AAI_DEFAULT_GENERIC_VNF,
236 OperationProperties.AAI_DEFAULT_CLOUD_REGION));
240 public void testLoadTargetEntity() {
241 step.loadTargetEntity(OperationProperties.AAI_TARGET_ENTITY);
242 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_TARGET_ENTITY, MY_TARGET);
246 public void testLoadDefaultGenericVnf() {
247 step.loadDefaultGenericVnf(OperationProperties.AAI_VNF);
248 assertThat(step.getParams().getPayload())
249 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_ID, MY_TARGET)
250 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_NAME, MY_NAME)
251 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_TYPE, MY_TYPE)
252 .containsEntry(GuardStep2.PAYLOAD_KEY_NF_NAMING_CODE, MY_CODE)
253 .containsEntry(GuardStep2.PAYLOAD_KEY_VSERVER_ID, MY_SERVER2);
257 public void testLoadCloudRegion() {
258 step.loadCloudRegion(OperationProperties.AAI_DEFAULT_CLOUD_REGION);
259 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_CLOUD_REGION_ID, MY_REGION);
263 * Tests loadVfCount() when the policy operation is NOT "VF Module Create".
266 public void testLoadVfCountNotVfModuleCreate() {
267 // should decrement the count
268 step.loadVfCount("");
269 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT - 1);
273 * Tests loadVfCount() when the policy operation is "VF Module Create".
276 public void testLoadVfCountVfModuleCreate() {
277 when(policyOper.getName()).thenReturn(VfModuleCreate.NAME);
279 // should increment the count
280 step.loadVfCount("");
281 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT + 1);