2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.apps.controller.usecases.step;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertSame;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.UUID;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
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.so.VfModuleCreate;
48 import org.onap.policy.controlloop.actor.xacml.GuardOperation;
49 import org.onap.policy.controlloop.actor.xacml.XacmlActor;
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 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_SERVER2 = "my-server-2";
68 private static final String MY_SERVERNAME = "my-server-name";
69 private static final String MY_REGION = "my-region";
70 private static final UUID REQ_ID = UUID.randomUUID();
71 private static final int VF_COUNT = 10;
73 private final StepContext stepContext = mock(StepContext.class);
74 private final VirtualControlLoopEvent event = mock(VirtualControlLoopEvent.class);
75 private final Operation policyOper = mock(Operation.class);
76 private final AaiCqResponse customQuery = mock(AaiCqResponse.class);
77 private final GenericVnf genericVnf = mock(GenericVnf.class);
78 private final CloudRegion cloudRegion = mock(CloudRegion.class);
79 private final Vserver theVserver = mock(Vserver.class);
81 private TargetType target;
82 private Map<String, String> aai = new HashMap<>();
84 private ControlLoopOperationParams params;
86 private GuardStep2 step;
93 aai.put("vserver.vserver-name", MY_SERVERNAME);
94 when(event.getAai()).thenReturn(aai);
96 when(genericVnf.getVnfName()).thenReturn(MY_NAME);
97 when(genericVnf.getVnfType()).thenReturn(MY_TYPE);
98 when(genericVnf.getNfNamingCode()).thenReturn(MY_CODE);
100 var relList = new RelationshipList();
101 when(genericVnf.getRelationshipList()).thenReturn(relList);
103 relList.getRelationship().add(new Relationship());
105 var relationship = new Relationship();
106 relList.getRelationship().add(relationship);
107 relationship.setRelatedTo("vserver");
109 relationship.getRelatedToProperty().add(new RelatedToProperty());
111 // property key mismatch
112 var relProp = new RelatedToProperty();
113 relationship.getRelatedToProperty().add(relProp);
114 relProp.setPropertyKey(SOME_OTHER_KEY);
115 relProp.setPropertyValue(MY_NAME);
117 // property value mismatch
118 relProp = new RelatedToProperty();
119 relationship.getRelatedToProperty().add(relProp);
120 relProp.setPropertyKey("vserver.vserver-name");
121 relProp.setPropertyValue(SOME_OTHER_VALUE);
124 relProp = new RelatedToProperty();
125 relationship.getRelatedToProperty().add(relProp);
126 relProp.setPropertyKey("vserver.vserver-name");
127 relProp.setPropertyValue(MY_SERVERNAME);
130 var relData = new RelationshipData();
131 relationship.getRelationshipData().add(relData);
132 relData.setRelationshipKey(SOME_OTHER_KEY);
133 relData.setRelationshipValue(SOME_OTHER_VALUE);
136 relData = new RelationshipData();
137 relationship.getRelationshipData().add(relData);
138 relData.setRelationshipKey(GuardStep2.PAYLOAD_KEY_VSERVER_ID);
139 relData.setRelationshipValue(MY_SERVER2);
141 when(customQuery.getGenericVnfByVnfId(MY_TARGET)).thenReturn(genericVnf);
143 when(cloudRegion.getCloudRegionId()).thenReturn(MY_REGION);
144 when(customQuery.getDefaultCloudRegion()).thenReturn(cloudRegion);
146 when(stepContext.getProperty(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(MY_TARGET);
147 when(stepContext.getProperty(AaiCqResponse.CONTEXT_KEY)).thenReturn(customQuery);
148 //when(stepContext.getProperty(VSERVER_VSERVER_NAME)).thenReturn()
150 when(stepContext.contains(OperationProperties.DATA_VF_COUNT)).thenReturn(true);
151 when(stepContext.getProperty(OperationProperties.DATA_VF_COUNT)).thenReturn(VF_COUNT);
154 params = ControlLoopOperationParams.builder()
156 .operation(MASTER_OPERATION)
162 master = new Step2(stepContext, params, event) {
164 protected Operation buildOperation() {
169 // force it to build the operation
172 step = new GuardStep2(master, CL_NAME);
176 void testConstructor() {
177 assertEquals(XacmlActor.NAME, step.getActorName());
178 assertEquals(GuardOperation.NAME, step.getOperationName());
179 assertSame(stepContext, step.stepContext);
180 assertSame(event, step.event);
182 // test when master is uninitialized
183 master = new Step2(stepContext, params, event);
184 assertThatIllegalStateException().isThrownBy(() -> new GuardStep2(master, CL_NAME));
186 var params2 = step.getParams();
189 assertThat(params2.getPayload()).isEqualTo(Map.of(
190 "actor", MASTER_ACTOR,
191 "operation", MASTER_OPERATION,
198 void testAcceptsEvent() {
199 // it should always accept events
200 assertTrue(step.acceptsEvent());
204 void testGetPropertyNames() {
205 // unmatching property names
206 when(policyOper.getPropertyNames()).thenReturn(List.of("propA", "propB"));
207 assertThat(step.getPropertyNames())
208 .containsAll(List.of(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF,
209 OperationProperties.AAI_DEFAULT_CLOUD_REGION));
211 // matching property names
212 when(policyOper.getPropertyNames())
213 .thenReturn(List.of("propA", OperationProperties.DATA_VF_COUNT, "propB"));
214 assertThat(step.getPropertyNames()).containsAll(List.of(OperationProperties.DATA_VF_COUNT,
215 UsecasesConstants.AAI_DEFAULT_GENERIC_VNF,
216 OperationProperties.AAI_DEFAULT_CLOUD_REGION));
220 void testLoadTargetEntity() {
221 step.loadTargetEntity(OperationProperties.AAI_TARGET_ENTITY);
222 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_TARGET_ENTITY, MY_TARGET);
226 void testLoadDefaultGenericVnf() {
227 step.loadDefaultGenericVnf(OperationProperties.AAI_VNF);
228 assertThat(step.getParams().getPayload())
229 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_ID, MY_TARGET)
230 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_NAME, MY_NAME)
231 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_TYPE, MY_TYPE)
232 .containsEntry(GuardStep2.PAYLOAD_KEY_NF_NAMING_CODE, MY_CODE)
233 .containsEntry(GuardStep2.PAYLOAD_KEY_VSERVER_ID, MY_SERVER2);
237 void testLoadCloudRegion() {
238 step.loadCloudRegion(OperationProperties.AAI_DEFAULT_CLOUD_REGION);
239 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_CLOUD_REGION_ID, MY_REGION);
243 * Tests loadVfCount() when the policy operation is NOT "VF Module Create".
246 void testLoadVfCountNotVfModuleCreate() {
247 // should decrement the count
248 step.loadVfCount("");
249 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT - 1);
253 * Tests loadVfCount() when the policy operation is "VF Module Create".
256 void testLoadVfCountVfModuleCreate() {
257 when(policyOper.getName()).thenReturn(VfModuleCreate.NAME);
259 // should increment the count
260 step.loadVfCount("");
261 assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT + 1);