ab9507221b1330acfe3d1b100d1def59dc231358
[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                     .targetEntity(MY_TARGET)
172                     .requestId(REQ_ID)
173                     .targetType(target)
174                     .build();
175         // @formatter:on
176
177         master = new Step2(stepContext, params, event) {
178             @Override
179             protected Operation buildOperation() {
180                 return policyOper;
181             }
182         };
183
184         // force it to build the operation
185         master.init();
186
187         step = new GuardStep2(master, CL_NAME);
188     }
189
190     @Test
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);
196
197         // test when master is uninitialized
198         master = new Step2(stepContext, params, event);
199         assertThatIllegalStateException().isThrownBy(() -> new GuardStep2(master, CL_NAME));
200
201         ControlLoopOperationParams params2 = step.getParams();
202
203         // @formatter:off
204         assertThat(params2.getPayload()).isEqualTo(Map.of(
205                         "actor", MASTER_ACTOR,
206                         "operation", MASTER_OPERATION,
207                         "requestId", REQ_ID,
208                         "clname", CL_NAME));
209         // @formatter:on
210     }
211
212     @Test
213     public void testAcceptsEvent() {
214         // it should always accept events
215         assertTrue(step.acceptsEvent());
216     }
217
218     @Test
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));
225
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));
232     }
233
234     @Test
235     public void testLoadTargetEntity() {
236         step.loadTargetEntity(OperationProperties.AAI_TARGET_ENTITY);
237         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_TARGET_ENTITY, MY_TARGET);
238     }
239
240     @Test
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);
249     }
250
251     @Test
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);
255     }
256
257     /**
258      * Tests loadVfCount() when the policy operation is NOT "VF Module Create".
259      */
260     @Test
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);
265     }
266
267     /**
268      * Tests loadVfCount() when the policy operation is "VF Module Create".
269      */
270     @Test
271     public void testLoadVfCountVfModuleCreate() {
272         when(policyOper.getName()).thenReturn(VfModuleCreate.NAME);
273
274         // should increment the count
275         step.loadVfCount("");
276         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT + 1);
277     }
278 }