ffd88d54d05b97e9b145c800736e8876f73bf0e6
[policy/drools-applications.git] / controlloop / common / controller-usecases / src / test / java / org / onap / policy / drools / apps / controller / usecases / step / GuardStep2Test.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.apps.controller.usecases.step;
22
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;
29
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
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;
57
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;
74
75     @Mock
76     private StepContext stepContext;
77     @Mock
78     private VirtualControlLoopEvent event;
79     @Mock
80     private Operation policyOper;
81     @Mock
82     private AaiCqResponse customQuery;
83     @Mock
84     private GenericVnf genericVnf;
85     @Mock
86     private CloudRegion cloudRegion;
87     @Mock Vserver theVserver;
88
89     TargetType target;
90     Map<String, String> aai = new HashMap<>();
91
92     private ControlLoopOperationParams params;
93     private Step2 master;
94     private GuardStep2 step;
95
96     /**
97      * Sets up.
98      */
99     @Before
100     public void setUp() {
101         aai.put("vserver.vserver-name", MY_SERVERNAME);
102         when(event.getAai()).thenReturn(aai);
103
104         when(genericVnf.getVnfName()).thenReturn(MY_NAME);
105         when(genericVnf.getVnfType()).thenReturn(MY_TYPE);
106         when(genericVnf.getNfNamingCode()).thenReturn(MY_CODE);
107
108         RelationshipList relList = new RelationshipList();
109         when(genericVnf.getRelationshipList()).thenReturn(relList);
110
111         relList.getRelationship().add(new Relationship());
112
113         Relationship relationship = new Relationship();
114         relList.getRelationship().add(relationship);
115         relationship.setRelatedTo("vserver");
116
117         relationship.getRelatedToProperty().add(new RelatedToProperty());
118
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);
124
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);
130
131         // matching property
132         relProp = new RelatedToProperty();
133         relationship.getRelatedToProperty().add(relProp);
134         relProp.setPropertyKey("vserver.vserver-name");
135         relProp.setPropertyValue(MY_SERVERNAME);
136
137         // data key mismatch
138         RelationshipData relData = new RelationshipData();
139         relationship.getRelationshipData().add(relData);
140         relData.setRelationshipKey(SOME_OTHER_KEY);
141         relData.setRelationshipValue(SOME_OTHER_VALUE);
142
143         // matching data
144         relData = new RelationshipData();
145         relationship.getRelationshipData().add(relData);
146         relData.setRelationshipKey(GuardStep2.PAYLOAD_KEY_VSERVER_ID);
147         relData.setRelationshipValue(MY_SERVER2);
148
149         when(customQuery.getGenericVnfByVnfId(MY_TARGET)).thenReturn(genericVnf);
150
151         when(cloudRegion.getCloudRegionId()).thenReturn(MY_REGION);
152         when(customQuery.getDefaultCloudRegion()).thenReturn(cloudRegion);
153
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()
157
158         when(stepContext.contains(OperationProperties.DATA_VF_COUNT)).thenReturn(true);
159         when(stepContext.getProperty(OperationProperties.DATA_VF_COUNT)).thenReturn(VF_COUNT);
160
161         // @formatter:off
162         params = ControlLoopOperationParams.builder()
163                     .actor(MASTER_ACTOR)
164                     .operation(MASTER_OPERATION)
165                     .requestId(REQ_ID)
166                     .targetType(target)
167                     .build();
168         // @formatter:on
169
170         master = new Step2(stepContext, params, event) {
171             @Override
172             protected Operation buildOperation() {
173                 return policyOper;
174             }
175         };
176
177         // force it to build the operation
178         master.init();
179
180         step = new GuardStep2(master, CL_NAME);
181     }
182
183     @Test
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);
189
190         // test when master is uninitialized
191         master = new Step2(stepContext, params, event);
192         assertThatIllegalStateException().isThrownBy(() -> new GuardStep2(master, CL_NAME));
193
194         ControlLoopOperationParams params2 = step.getParams();
195
196         // @formatter:off
197         assertThat(params2.getPayload()).isEqualTo(Map.of(
198                         "actor", MASTER_ACTOR,
199                         "operation", MASTER_OPERATION,
200                         "requestId", REQ_ID,
201                         "clname", CL_NAME));
202         // @formatter:on
203     }
204
205     @Test
206     public void testAcceptsEvent() {
207         // it should always accept events
208         assertTrue(step.acceptsEvent());
209     }
210
211     @Test
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));
218
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));
225     }
226
227     @Test
228     public void testLoadTargetEntity() {
229         step.loadTargetEntity(OperationProperties.AAI_TARGET_ENTITY);
230         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_TARGET_ENTITY, MY_TARGET);
231     }
232
233     @Test
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);
242     }
243
244     @Test
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);
248     }
249
250     /**
251      * Tests loadVfCount() when the policy operation is NOT "VF Module Create".
252      */
253     @Test
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);
258     }
259
260     /**
261      * Tests loadVfCount() when the policy operation is "VF Module Create".
262      */
263     @Test
264     public void testLoadVfCountVfModuleCreate() {
265         when(policyOper.getName()).thenReturn(VfModuleCreate.NAME);
266
267         // should increment the count
268         step.loadVfCount("");
269         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT + 1);
270     }
271 }