Upgrade Java 17 in policy-drools-apps
[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  * 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.drools.apps.controller.usecases.step;
23
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;
31
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
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;
56
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;
72
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);
80
81     private TargetType target;
82     private Map<String, String> aai = new HashMap<>();
83
84     private ControlLoopOperationParams params;
85     private Step2 master;
86     private GuardStep2 step;
87
88     /**
89      * Sets up.
90      */
91     @BeforeEach
92     public void setUp() {
93         aai.put("vserver.vserver-name", MY_SERVERNAME);
94         when(event.getAai()).thenReturn(aai);
95
96         when(genericVnf.getVnfName()).thenReturn(MY_NAME);
97         when(genericVnf.getVnfType()).thenReturn(MY_TYPE);
98         when(genericVnf.getNfNamingCode()).thenReturn(MY_CODE);
99
100         var relList = new RelationshipList();
101         when(genericVnf.getRelationshipList()).thenReturn(relList);
102
103         relList.getRelationship().add(new Relationship());
104
105         var relationship = new Relationship();
106         relList.getRelationship().add(relationship);
107         relationship.setRelatedTo("vserver");
108
109         relationship.getRelatedToProperty().add(new RelatedToProperty());
110
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);
116
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);
122
123         // matching property
124         relProp = new RelatedToProperty();
125         relationship.getRelatedToProperty().add(relProp);
126         relProp.setPropertyKey("vserver.vserver-name");
127         relProp.setPropertyValue(MY_SERVERNAME);
128
129         // data key mismatch
130         var relData = new RelationshipData();
131         relationship.getRelationshipData().add(relData);
132         relData.setRelationshipKey(SOME_OTHER_KEY);
133         relData.setRelationshipValue(SOME_OTHER_VALUE);
134
135         // matching data
136         relData = new RelationshipData();
137         relationship.getRelationshipData().add(relData);
138         relData.setRelationshipKey(GuardStep2.PAYLOAD_KEY_VSERVER_ID);
139         relData.setRelationshipValue(MY_SERVER2);
140
141         when(customQuery.getGenericVnfByVnfId(MY_TARGET)).thenReturn(genericVnf);
142
143         when(cloudRegion.getCloudRegionId()).thenReturn(MY_REGION);
144         when(customQuery.getDefaultCloudRegion()).thenReturn(cloudRegion);
145
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()
149
150         when(stepContext.contains(OperationProperties.DATA_VF_COUNT)).thenReturn(true);
151         when(stepContext.getProperty(OperationProperties.DATA_VF_COUNT)).thenReturn(VF_COUNT);
152
153         // @formatter:off
154         params = ControlLoopOperationParams.builder()
155                     .actor(MASTER_ACTOR)
156                     .operation(MASTER_OPERATION)
157                     .requestId(REQ_ID)
158                     .targetType(target)
159                     .build();
160         // @formatter:on
161
162         master = new Step2(stepContext, params, event) {
163             @Override
164             protected Operation buildOperation() {
165                 return policyOper;
166             }
167         };
168
169         // force it to build the operation
170         master.init();
171
172         step = new GuardStep2(master, CL_NAME);
173     }
174
175     @Test
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);
181
182         // test when master is uninitialized
183         master = new Step2(stepContext, params, event);
184         assertThatIllegalStateException().isThrownBy(() -> new GuardStep2(master, CL_NAME));
185
186         var params2 = step.getParams();
187
188         // @formatter:off
189         assertThat(params2.getPayload()).isEqualTo(Map.of(
190                         "actor", MASTER_ACTOR,
191                         "operation", MASTER_OPERATION,
192                         "requestId", REQ_ID,
193                         "clname", CL_NAME));
194         // @formatter:on
195     }
196
197     @Test
198     void testAcceptsEvent() {
199         // it should always accept events
200         assertTrue(step.acceptsEvent());
201     }
202
203     @Test
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));
210
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));
217     }
218
219     @Test
220     void testLoadTargetEntity() {
221         step.loadTargetEntity(OperationProperties.AAI_TARGET_ENTITY);
222         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_TARGET_ENTITY, MY_TARGET);
223     }
224
225     @Test
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);
234     }
235
236     @Test
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);
240     }
241
242     /**
243      * Tests loadVfCount() when the policy operation is NOT "VF Module Create".
244      */
245     @Test
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);
250     }
251
252     /**
253      * Tests loadVfCount() when the policy operation is "VF Module Create".
254      */
255     @Test
256     void testLoadVfCountVfModuleCreate() {
257         when(policyOper.getName()).thenReturn(VfModuleCreate.NAME);
258
259         // should increment the count
260         step.loadVfCount("");
261         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT + 1);
262     }
263 }