131e5d3c876eea0046a66300c61b3160f6ad99d8
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.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;
57
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;
74
75     @Mock
76     private ControlLoopEventContext context;
77     @Mock
78     private StepContext stepContext;
79     @Mock
80     private VirtualControlLoopEvent event;
81     @Mock
82     private Operation policyOper;
83     @Mock
84     private AaiCqResponse customQuery;
85     @Mock
86     private GenericVnf genericVnf;
87     @Mock
88     private CloudRegion cloudRegion;
89     @Mock Vserver theVserver;
90
91     TargetType target;
92     Map<String, String> aai = new HashMap<>();
93
94     private ControlLoopOperationParams params;
95     private Step2 master;
96     private GuardStep2 step;
97
98     /**
99      * Sets up.
100      */
101     @Before
102     public void setUp() {
103         MockitoAnnotations.initMocks(this);
104
105         aai.put("vserver.vserver-name", MY_SERVERNAME);
106         when(event.getRequestId()).thenReturn(REQ_ID);
107         when(event.getAai()).thenReturn(aai);
108
109         when(context.getEvent()).thenReturn(event);
110
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);
115
116         RelationshipList relList = new RelationshipList();
117         when(genericVnf.getRelationshipList()).thenReturn(relList);
118
119         relList.getRelationship().add(new Relationship());
120
121         Relationship relationship = new Relationship();
122         relList.getRelationship().add(relationship);
123         relationship.setRelatedTo("vserver");
124
125         relationship.getRelatedToProperty().add(new RelatedToProperty());
126
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);
132
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);
138
139         // matching property
140         relProp = new RelatedToProperty();
141         relationship.getRelatedToProperty().add(relProp);
142         relProp.setPropertyKey("vserver.vserver-name");
143         relProp.setPropertyValue(MY_SERVERNAME);
144
145         // data key mismatch
146         RelationshipData relData = new RelationshipData();
147         relationship.getRelationshipData().add(relData);
148         relData.setRelationshipKey(SOME_OTHER_KEY);
149         relData.setRelationshipValue(SOME_OTHER_VALUE);
150
151         // matching data
152         relData = new RelationshipData();
153         relationship.getRelationshipData().add(relData);
154         relData.setRelationshipKey(GuardStep2.PAYLOAD_KEY_VSERVER_ID);
155         relData.setRelationshipValue(MY_SERVER2);
156
157         when(customQuery.getGenericVnfByVnfId(MY_TARGET)).thenReturn(genericVnf);
158
159         when(theVserver.getVserverId()).thenReturn(MY_SERVER);
160         when(customQuery.getVserver()).thenReturn(theVserver);
161
162         when(cloudRegion.getCloudRegionId()).thenReturn(MY_REGION);
163         when(customQuery.getDefaultCloudRegion()).thenReturn(cloudRegion);
164
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()
168
169         when(stepContext.contains(OperationProperties.DATA_VF_COUNT)).thenReturn(true);
170         when(stepContext.getProperty(OperationProperties.DATA_VF_COUNT)).thenReturn(VF_COUNT);
171
172         // @formatter:off
173         params = ControlLoopOperationParams.builder()
174                     .actor(MASTER_ACTOR)
175                     .operation(MASTER_OPERATION)
176                     .targetEntity(MY_TARGET)
177                     .context(context)
178                     .targetType(target)
179                     .build();
180         // @formatter:on
181
182         master = new Step2(stepContext, params, event) {
183             @Override
184             protected Operation buildOperation() {
185                 return policyOper;
186             }
187         };
188
189         // force it to build the operation
190         master.init();
191
192         step = new GuardStep2(master, CL_NAME);
193     }
194
195     @Test
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);
201
202         // test when master is uninitialized
203         master = new Step2(stepContext, params, event);
204         assertThatIllegalStateException().isThrownBy(() -> new GuardStep2(master, CL_NAME));
205
206         ControlLoopOperationParams params2 = step.getParams();
207
208         // @formatter:off
209         assertThat(params2.getPayload()).isEqualTo(Map.of(
210                         "actor", MASTER_ACTOR,
211                         "operation", MASTER_OPERATION,
212                         "requestId", REQ_ID,
213                         "clname", CL_NAME));
214         // @formatter:on
215     }
216
217     @Test
218     public void testAcceptsEvent() {
219         // it should always accept events
220         assertTrue(step.acceptsEvent());
221     }
222
223     @Test
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));
230
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));
237     }
238
239     @Test
240     public void testLoadTargetEntity() {
241         step.loadTargetEntity(OperationProperties.AAI_TARGET_ENTITY);
242         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_TARGET_ENTITY, MY_TARGET);
243     }
244
245     @Test
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);
254     }
255
256     @Test
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);
260     }
261
262     /**
263      * Tests loadVfCount() when the policy operation is NOT "VF Module Create".
264      */
265     @Test
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);
270     }
271
272     /**
273      * Tests loadVfCount() when the policy operation is "VF Module Create".
274      */
275     @Test
276     public void testLoadVfCountVfModuleCreate() {
277         when(policyOper.getName()).thenReturn(VfModuleCreate.NAME);
278
279         // should increment the count
280         step.loadVfCount("");
281         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT + 1);
282     }
283 }