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.parameters.ControlLoopOperationParams;
54 import org.onap.policy.controlloop.eventmanager.StepContext;
55 import org.onap.policy.drools.apps.controller.usecases.UsecasesConstants;
57 public class GuardStep2Test {
58 private static final String SOME_OTHER_VALUE = "some-other-value";
59 private static final String SOME_OTHER_KEY = "some-other-key";
60 private static final String CL_NAME = "my-closed-loop";
61 private static final String MASTER_ACTOR = "master-actor";
62 private static final String MASTER_OPERATION = "master-operation";
63 private static final String MY_TARGET = "my-target";
64 private static final String MY_NAME = "my-name";
65 private static final String MY_TYPE = "my-type";
66 private static final String MY_CODE = "my-code";
67 private static final String MY_SERVER = "my-server";
68 private static final String MY_SERVER2 = "my-server-2";
69 private static final String MY_SERVERNAME = "my-server-name";
70 private static final String MY_REGION = "my-region";
71 private static final UUID REQ_ID = UUID.randomUUID();
72 private static final int VF_COUNT = 10;
75 private StepContext stepContext;
77 private VirtualControlLoopEvent event;
79 private Operation policyOper;
81 private AaiCqResponse customQuery;
83 private GenericVnf genericVnf;
85 private CloudRegion cloudRegion;
86 @Mock Vserver theVserver;
89 Map<String, String> aai = new HashMap<>();
91 private ControlLoopOperationParams params;
93 private GuardStep2 step;
100 MockitoAnnotations.initMocks(this);
102 aai.put("vserver.vserver-name", MY_SERVERNAME);
103 when(event.getRequestId()).thenReturn(REQ_ID);
104 when(event.getAai()).thenReturn(aai);
106 when(genericVnf.getVnfId()).thenReturn(MY_TARGET);
107 when(genericVnf.getVnfName()).thenReturn(MY_NAME);
108 when(genericVnf.getVnfType()).thenReturn(MY_TYPE);
109 when(genericVnf.getNfNamingCode()).thenReturn(MY_CODE);
111 RelationshipList relList = new RelationshipList();
112 when(genericVnf.getRelationshipList()).thenReturn(relList);
114 relList.getRelationship().add(new Relationship());
116 Relationship relationship = new Relationship();
117 relList.getRelationship().add(relationship);
118 relationship.setRelatedTo("vserver");
120 relationship.getRelatedToProperty().add(new RelatedToProperty());
122 // property key mismatch
123 RelatedToProperty relProp = new RelatedToProperty();
124 relationship.getRelatedToProperty().add(relProp);
125 relProp.setPropertyKey(SOME_OTHER_KEY);
126 relProp.setPropertyValue(MY_NAME);
128 // property value mismatch
129 relProp = new RelatedToProperty();
130 relationship.getRelatedToProperty().add(relProp);
131 relProp.setPropertyKey("vserver.vserver-name");
132 relProp.setPropertyValue(SOME_OTHER_VALUE);
135 relProp = new RelatedToProperty();
136 relationship.getRelatedToProperty().add(relProp);
137 relProp.setPropertyKey("vserver.vserver-name");
138 relProp.setPropertyValue(MY_SERVERNAME);
141 RelationshipData relData = new RelationshipData();
142 relationship.getRelationshipData().add(relData);
143 relData.setRelationshipKey(SOME_OTHER_KEY);
144 relData.setRelationshipValue(SOME_OTHER_VALUE);
147 relData = new RelationshipData();
148 relationship.getRelationshipData().add(relData);
149 relData.setRelationshipKey(GuardStep2.PAYLOAD_KEY_VSERVER_ID);
150 relData.setRelationshipValue(MY_SERVER2);
152 when(customQuery.getGenericVnfByVnfId(MY_TARGET)).thenReturn(genericVnf);
154 when(theVserver.getVserverId()).thenReturn(MY_SERVER);
155 when(customQuery.getVserver()).thenReturn(theVserver);
157 when(cloudRegion.getCloudRegionId()).thenReturn(MY_REGION);
158 when(customQuery.getDefaultCloudRegion()).thenReturn(cloudRegion);
160 when(stepContext.getProperty(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(MY_TARGET);
161 when(stepContext.getProperty(AaiCqResponse.CONTEXT_KEY)).thenReturn(customQuery);
162 //when(stepContext.getProperty(VSERVER_VSERVER_NAME)).thenReturn()
164 when(stepContext.contains(OperationProperties.DATA_VF_COUNT)).thenReturn(true);
165 when(stepContext.getProperty(OperationProperties.DATA_VF_COUNT)).thenReturn(VF_COUNT);
168 params = ControlLoopOperationParams.builder()
170 .operation(MASTER_OPERATION)
171 .targetEntity(MY_TARGET)
177 master = new Step2(stepContext, params, event) {
179 protected Operation buildOperation() {
184 // force it to build the operation
187 step = new GuardStep2(master, CL_NAME);
191 public void testConstructor() {
192 assertEquals(GuardActor.NAME, step.getActorName());
193 assertEquals(DecisionOperation.NAME, step.getOperationName());
194 assertSame(stepContext, step.stepContext);
195 assertSame(event, step.event);
197 // test when master is uninitialized
198 master = new Step2(stepContext, params, event);
199 assertThatIllegalStateException().isThrownBy(() -> new GuardStep2(master, CL_NAME));
201 ControlLoopOperationParams params2 = step.getParams();
204 assertThat(params2.getPayload()).isEqualTo(Map.of(
205 "actor", MASTER_ACTOR,
206 "operation", MASTER_OPERATION,
213 public void testAcceptsEvent() {
214 // it should always accept events
215 assertTrue(step.acceptsEvent());
219 public void testGetPropertyNames() {
220 // unmatching property names
221 when(policyOper.getPropertyNames()).thenReturn(List.of("propA", "propB"));
222 assertThat(step.getPropertyNames())
223 .containsAll(List.of(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF,
224 OperationProperties.AAI_DEFAULT_CLOUD_REGION));
226 // matching property names
227 when(policyOper.getPropertyNames())
228 .thenReturn(List.of("propA", OperationProperties.DATA_VF_COUNT, "propB"));
229 assertThat(step.getPropertyNames()).containsAll(List.of(OperationProperties.DATA_VF_COUNT,
230 UsecasesConstants.AAI_DEFAULT_GENERIC_VNF,
231 OperationProperties.AAI_DEFAULT_CLOUD_REGION));
235 public void testLoadTargetEntity() {
236 step.loadTargetEntity(OperationProperties.AAI_TARGET_ENTITY);
237 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_TARGET_ENTITY, MY_TARGET);
241 public void testLoadDefaultGenericVnf() {
242 step.loadDefaultGenericVnf(OperationProperties.AAI_VNF);
243 assertThat(step.getParams().getPayload())
244 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_ID, MY_TARGET)
245 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_NAME, MY_NAME)
246 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_TYPE, MY_TYPE)
247 .containsEntry(GuardStep2.PAYLOAD_KEY_NF_NAMING_CODE, MY_CODE)
248 .containsEntry(GuardStep2.PAYLOAD_KEY_VSERVER_ID, MY_SERVER2);
252 public void testLoadCloudRegion() {
253 step.loadCloudRegion(OperationProperties.AAI_DEFAULT_CLOUD_REGION);
254 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_CLOUD_REGION_ID, MY_REGION);
258 * Tests loadVfCount() when the policy operation is NOT "VF Module Create".
261 public void testLoadVfCountNotVfModuleCreate() {
262 // should decrement the count
263 step.loadVfCount("");
264 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT - 1);
268 * Tests loadVfCount() when the policy operation is "VF Module Create".
271 public void testLoadVfCountVfModuleCreate() {
272 when(policyOper.getName()).thenReturn(VfModuleCreate.NAME);
274 // should increment the count
275 step.loadVfCount("");
276 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT + 1);