2ab647aa4ebd48f549802025189daf92be893fd0
[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.parameters.ControlLoopOperationParams;
54 import org.onap.policy.controlloop.eventmanager.StepContext;
55 import org.onap.policy.drools.apps.controller.usecases.UsecasesConstants;
56
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;
73
74     @Mock
75     private StepContext stepContext;
76     @Mock
77     private VirtualControlLoopEvent event;
78     @Mock
79     private Operation policyOper;
80     @Mock
81     private AaiCqResponse customQuery;
82     @Mock
83     private GenericVnf genericVnf;
84     @Mock
85     private CloudRegion cloudRegion;
86     @Mock Vserver theVserver;
87
88     TargetType target;
89     Map<String, String> aai = new HashMap<>();
90
91     private ControlLoopOperationParams params;
92     private Step2 master;
93     private GuardStep2 step;
94
95     /**
96      * Sets up.
97      */
98     @Before
99     public void setUp() {
100         MockitoAnnotations.initMocks(this);
101
102         aai.put("vserver.vserver-name", MY_SERVERNAME);
103         when(event.getRequestId()).thenReturn(REQ_ID);
104         when(event.getAai()).thenReturn(aai);
105
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);
110
111         RelationshipList relList = new RelationshipList();
112         when(genericVnf.getRelationshipList()).thenReturn(relList);
113
114         relList.getRelationship().add(new Relationship());
115
116         Relationship relationship = new Relationship();
117         relList.getRelationship().add(relationship);
118         relationship.setRelatedTo("vserver");
119
120         relationship.getRelatedToProperty().add(new RelatedToProperty());
121
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);
127
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);
133
134         // matching property
135         relProp = new RelatedToProperty();
136         relationship.getRelatedToProperty().add(relProp);
137         relProp.setPropertyKey("vserver.vserver-name");
138         relProp.setPropertyValue(MY_SERVERNAME);
139
140         // data key mismatch
141         RelationshipData relData = new RelationshipData();
142         relationship.getRelationshipData().add(relData);
143         relData.setRelationshipKey(SOME_OTHER_KEY);
144         relData.setRelationshipValue(SOME_OTHER_VALUE);
145
146         // matching data
147         relData = new RelationshipData();
148         relationship.getRelationshipData().add(relData);
149         relData.setRelationshipKey(GuardStep2.PAYLOAD_KEY_VSERVER_ID);
150         relData.setRelationshipValue(MY_SERVER2);
151
152         when(customQuery.getGenericVnfByVnfId(MY_TARGET)).thenReturn(genericVnf);
153
154         when(theVserver.getVserverId()).thenReturn(MY_SERVER);
155         when(customQuery.getVserver()).thenReturn(theVserver);
156
157         when(cloudRegion.getCloudRegionId()).thenReturn(MY_REGION);
158         when(customQuery.getDefaultCloudRegion()).thenReturn(cloudRegion);
159
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()
163
164         when(stepContext.contains(OperationProperties.DATA_VF_COUNT)).thenReturn(true);
165         when(stepContext.getProperty(OperationProperties.DATA_VF_COUNT)).thenReturn(VF_COUNT);
166
167         // @formatter:off
168         params = ControlLoopOperationParams.builder()
169                     .actor(MASTER_ACTOR)
170                     .operation(MASTER_OPERATION)
171                     .requestId(REQ_ID)
172                     .targetType(target)
173                     .build();
174         // @formatter:on
175
176         master = new Step2(stepContext, params, event) {
177             @Override
178             protected Operation buildOperation() {
179                 return policyOper;
180             }
181         };
182
183         // force it to build the operation
184         master.init();
185
186         step = new GuardStep2(master, CL_NAME);
187     }
188
189     @Test
190     public void testConstructor() {
191         assertEquals(GuardActor.NAME, step.getActorName());
192         assertEquals(DecisionOperation.NAME, step.getOperationName());
193         assertSame(stepContext, step.stepContext);
194         assertSame(event, step.event);
195
196         // test when master is uninitialized
197         master = new Step2(stepContext, params, event);
198         assertThatIllegalStateException().isThrownBy(() -> new GuardStep2(master, CL_NAME));
199
200         ControlLoopOperationParams params2 = step.getParams();
201
202         // @formatter:off
203         assertThat(params2.getPayload()).isEqualTo(Map.of(
204                         "actor", MASTER_ACTOR,
205                         "operation", MASTER_OPERATION,
206                         "requestId", REQ_ID,
207                         "clname", CL_NAME));
208         // @formatter:on
209     }
210
211     @Test
212     public void testAcceptsEvent() {
213         // it should always accept events
214         assertTrue(step.acceptsEvent());
215     }
216
217     @Test
218     public void testGetPropertyNames() {
219         // unmatching property names
220         when(policyOper.getPropertyNames()).thenReturn(List.of("propA", "propB"));
221         assertThat(step.getPropertyNames())
222                 .containsAll(List.of(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF,
223                         OperationProperties.AAI_DEFAULT_CLOUD_REGION));
224
225         // matching property names
226         when(policyOper.getPropertyNames())
227                 .thenReturn(List.of("propA", OperationProperties.DATA_VF_COUNT, "propB"));
228         assertThat(step.getPropertyNames()).containsAll(List.of(OperationProperties.DATA_VF_COUNT,
229                 UsecasesConstants.AAI_DEFAULT_GENERIC_VNF,
230                 OperationProperties.AAI_DEFAULT_CLOUD_REGION));
231     }
232
233     @Test
234     public void testLoadTargetEntity() {
235         step.loadTargetEntity(OperationProperties.AAI_TARGET_ENTITY);
236         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_TARGET_ENTITY, MY_TARGET);
237     }
238
239     @Test
240     public void testLoadDefaultGenericVnf() {
241         step.loadDefaultGenericVnf(OperationProperties.AAI_VNF);
242         assertThat(step.getParams().getPayload())
243                 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_ID, MY_TARGET)
244                 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_NAME, MY_NAME)
245                 .containsEntry(GuardStep2.PAYLOAD_KEY_VNF_TYPE, MY_TYPE)
246                 .containsEntry(GuardStep2.PAYLOAD_KEY_NF_NAMING_CODE, MY_CODE)
247                 .containsEntry(GuardStep2.PAYLOAD_KEY_VSERVER_ID, MY_SERVER2);
248     }
249
250     @Test
251     public void testLoadCloudRegion() {
252         step.loadCloudRegion(OperationProperties.AAI_DEFAULT_CLOUD_REGION);
253         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_CLOUD_REGION_ID, MY_REGION);
254     }
255
256     /**
257      * Tests loadVfCount() when the policy operation is NOT "VF Module Create".
258      */
259     @Test
260     public void testLoadVfCountNotVfModuleCreate() {
261         // should decrement the count
262         step.loadVfCount("");
263         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT - 1);
264     }
265
266     /**
267      * Tests loadVfCount() when the policy operation is "VF Module Create".
268      */
269     @Test
270     public void testLoadVfCountVfModuleCreate() {
271         when(policyOper.getName()).thenReturn(VfModuleCreate.NAME);
272
273         // should increment the count
274         step.loadVfCount("");
275         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT + 1);
276     }
277 }