2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021, 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.main.comm.msgdata;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertSame;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.Mockito.never;
33 import static org.mockito.Mockito.verify;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.LinkedList;
39 import java.util.List;
41 import java.util.TreeSet;
42 import java.util.stream.Collectors;
43 import org.junit.jupiter.api.BeforeEach;
44 import org.junit.jupiter.api.Test;
45 import org.onap.policy.models.pdp.concepts.PdpStateChange;
46 import org.onap.policy.models.pdp.concepts.PdpStatus;
47 import org.onap.policy.models.pdp.concepts.PdpUpdate;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
50 import org.onap.policy.pap.main.comm.CommonRequestBase;
52 class UpdateReqTest extends CommonRequestBase {
54 private UpdateReq data;
55 private PdpUpdate update;
56 private PdpStatus response;
61 * @throws Exception if an error occurs
65 public void setUp() throws Exception {
68 response = new PdpStatus();
70 update = makeUpdate();
72 response.setName(MY_NAME);
73 response.setPdpGroup(update.getPdpGroup());
74 response.setPdpSubgroup(update.getPdpSubgroup());
76 update.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
77 .collect(Collectors.toList()));
79 data = new UpdateReq(reqParams, MY_REQ_NAME, update);
80 data.setNotifier(notifier);
84 void testGetMessage() {
85 assertEquals(MY_REQ_NAME, data.getName());
86 assertSame(update, data.getMessage());
90 void testCheckResponse() {
91 assertNull(data.checkResponse(response));
92 assertTrue(data.getUndeployPolicies().isEmpty());
95 // both policy lists null
96 update.setPoliciesToBeDeployed(null);
97 response.setPolicies(null);
98 assertNull(data.checkResponse(response));
99 assertTrue(data.getUndeployPolicies().isEmpty());
103 void testCheckResponse_NullName() {
104 response.setName(null);
106 assertEquals("null PDP name", data.checkResponse(response));
107 assertTrue(data.getUndeployPolicies().isEmpty());
112 void testCheckResponse_NullMsgName() {
113 update.setName(null);
115 assertNull(data.checkResponse(response));
116 assertTrue(data.getUndeployPolicies().isEmpty());
121 void testUpdateReqCheckResponse_MismatchedGroup() {
122 response.setPdpGroup(DIFFERENT);
124 assertEquals("group does not match", data.checkResponse(response));
125 assertTrue(data.getUndeployPolicies().isEmpty());
130 void testUpdateReqCheckResponse_MismatchedSubGroup() {
131 response.setPdpSubgroup(DIFFERENT);
133 assertEquals("subgroup does not match", data.checkResponse(response));
134 assertTrue(data.getUndeployPolicies().isEmpty());
139 void testCheckResponse_NullSubGroup() {
140 update.setPdpSubgroup(null);
141 response.setPdpSubgroup(null);
143 // different policy list - should have no impact
144 response.setPolicies(Collections.emptyList());
146 assertNull(data.checkResponse(response));
147 assertTrue(data.getUndeployPolicies().isEmpty());
152 void testUpdateReqCheckResponse_MismatchedPolicies() {
153 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
154 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
156 response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
158 assertEquals("policies do not match", data.checkResponse(response));
161 // the first policy from the original update is all that should be undeployed
162 assertEquals(Collections.singleton(update.getPoliciesToBeDeployed().get(0).getIdentifier()).toString(),
163 data.getUndeployPolicies().toString());
167 void testUpdateReqCheckResponse_MismatchedPolicies_Null_NotNull() {
168 update.setPoliciesToBeDeployed(null);
170 assertNull(data.checkResponse(response));
171 assertTrue(data.getUndeployPolicies().isEmpty());
176 void testUpdateReqCheckResponse_MismatchedPolicies_NotNull_Null() {
177 response.setPolicies(null);
179 assertEquals("policies do not match", data.checkResponse(response));
182 // all policies in the update should be undeployed
183 assertEquals(update.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
184 .toList().toString(), new TreeSet<>(data.getUndeployPolicies()).toString());
188 void testReconfigure() {
189 // different message type should fail and leave message unchanged
190 assertFalse(data.reconfigure(new PdpStateChange()));
191 assertSame(update, data.getMessage());
193 // same content - should succeed, but leave message unchanged
194 PdpUpdate msg2 = new PdpUpdate(update);
195 assertTrue(data.reconfigure(msg2));
196 assertSame(update, data.getMessage());
198 // different content - should succeed and install NEW message
199 msg2.setPdpGroup(DIFFERENT);
200 assertTrue(data.reconfigure(msg2));
201 assertSame(msg2, data.getMessage());
203 // both lists in the update are null - should not throw an exception
204 PdpUpdate msg3 = new PdpUpdate(update);
205 msg3.setPdpGroup(DIFFERENT);
206 msg3.setPoliciesToBeDeployed(null);
207 msg3.setPoliciesToBeUndeployed(null);
208 assertThatCode(() -> data.reconfigure(msg3)).doesNotThrowAnyException();
210 // both lists in the current msg (i.e., msg3) are null - should not throw an exception
211 assertThatCode(() -> data.reconfigure(update)).doesNotThrowAnyException();
215 void testReconfigureIsFullSameAsDeployList() {
216 PdpUpdate msg2 = new PdpUpdate(update);
217 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
219 msg2.setPoliciesToBeDeployed(policies);
220 assertTrue(data.reconfigure(msg2));
221 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
225 void testListsNewVsResult() {
226 PdpUpdate msg2 = new PdpUpdate(update);
227 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
229 // some items in both deploy and newMessage.deploy
230 msg2.setPoliciesToBeDeployed(policies);
232 data.getMessage().setPoliciesToBeDeployed(policies);
233 assertTrue(data.reconfigure(msg2));
234 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
236 // some items in both deploy and newMessage.undeploy
237 policies = new ArrayList<>();
238 policies.add(makePolicy("policy-z-1", "1.0.0"));
239 policies.add(makePolicy("policy-y-1", "1.0.0"));
240 data.getMessage().setPoliciesToBeDeployed(policies);
243 policies = new ArrayList<>(update.getPoliciesToBeDeployed());
244 List<ToscaConceptIdentifier> polsToUndep = policies.parallelStream()
245 .map(ToscaPolicy::getIdentifier)
246 .collect(Collectors.toList());
247 msg2.setPoliciesToBeUndeployed(polsToUndep);
249 assertTrue(data.reconfigure(msg2));
251 // some items only in deploy
252 policies = new ArrayList<>(update.getPoliciesToBeDeployed());
253 msg2.setPoliciesToBeDeployed(policies);
254 data.getMessage().setPoliciesToBeDeployed(new LinkedList<>());
255 assertTrue(data.reconfigure(msg2));
256 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
258 // some items in both undeploy and newMessage.undeploy
259 List<ToscaConceptIdentifier> pols = policies.stream().map(ToscaPolicy::getIdentifier)
260 .collect(Collectors.toList());
261 msg2.setPoliciesToBeUndeployed(pols);
262 pols.add(makePolicy("policy-zz-1", "1.1.0").getIdentifier());
263 data.getMessage().setPoliciesToBeUndeployed(pols);
264 assertTrue(data.reconfigure(msg2));
265 assertThat(data.getMessage().getPoliciesToBeUndeployed()).containsAll(msg2.getPoliciesToBeUndeployed());
267 // some items in both undeploy and newMessage.deploy
268 policies = new ArrayList<>(update.getPoliciesToBeDeployed());
269 List<ToscaConceptIdentifier> polsToUndep2 = policies.parallelStream()
270 .map(ToscaPolicy::getIdentifier)
271 .collect(Collectors.toList());
272 data.getMessage().setPoliciesToBeUndeployed(polsToUndep2);
274 List<ToscaPolicy> polsToDep2 = new LinkedList<>();
275 polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
276 polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
277 msg2.setPoliciesToBeDeployed(polsToDep2);
279 assertTrue(data.reconfigure(msg2));
281 List<ToscaConceptIdentifier> dataPols2 = data.getMessage().getPoliciesToBeDeployed().stream()
282 .map(ToscaPolicy::getIdentifier)
283 .collect(Collectors.toList());
284 assertThat(data.getMessage().getPoliciesToBeUndeployed())
285 .doesNotContainAnyElementsOf(dataPols2);
287 // some items only in undeploy
288 pols = policies.stream().map(ToscaPolicy::getIdentifier)
289 .collect(Collectors.toList());
290 msg2.setPoliciesToBeUndeployed(pols);
291 data.getMessage().setPoliciesToBeUndeployed(new LinkedList<>());
292 assertTrue(data.reconfigure(msg2));
293 assertThat(data.getMessage().getPoliciesToBeUndeployed()).containsAll(msg2.getPoliciesToBeUndeployed());
297 void testIsSameContent() {
298 data = new UpdateReq(reqParams, MY_REQ_NAME, update);
299 data.setNotifier(notifier);
301 PdpUpdate msg2 = new PdpUpdate(update);
302 msg2.setName("world");
303 update.setPoliciesToBeDeployed(null);
304 List<ToscaPolicy> polsToDep2 = new LinkedList<>();
305 polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
306 polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
307 msg2.setPoliciesToBeDeployed(polsToDep2);
308 assertThat(data.isSameContent(msg2)).isFalse();
310 // both policy lists null
311 msg2.setPoliciesToBeDeployed(null);
312 assertEquals(data.getMessage().getPoliciesToBeDeployed(), msg2.getPoliciesToBeDeployed());
316 void testIsSameContent_BothGroupNamesNull() {
317 PdpUpdate msg2 = new PdpUpdate(update);
318 msg2.setPdpGroup(null);
319 update.setPdpGroup(null);
320 assertEquals(data.getMessage().getPdpGroup(), msg2.getPdpGroup());
324 void testIsSameContent_BothSubGroupNamesNull() {
325 PdpUpdate msg2 = new PdpUpdate(update);
326 msg2.setPdpSubgroup(null);
327 update.setPdpSubgroup(null);
328 assertEquals(data.getMessage().getPdpSubgroup(), msg2.getPdpSubgroup());
332 void testIsSameContent_DiffGroup() {
333 PdpUpdate msg2 = new PdpUpdate(update);
334 msg2.setPdpGroup(null);
335 assertFalse(data.isSameContent(msg2));
337 msg2.setPdpGroup(DIFFERENT);
338 assertFalse(data.isSameContent(msg2));
340 update.setPdpGroup(null);
341 assertFalse(data.isSameContent(msg2));
345 void testIsSameContent_DiffSubGroup() {
346 PdpUpdate msg2 = new PdpUpdate(update);
347 msg2.setPdpSubgroup(null);
348 assertFalse(data.isSameContent(msg2));
350 msg2.setPdpSubgroup(DIFFERENT);
351 assertFalse(data.isSameContent(msg2));
353 update.setPdpSubgroup(null);
354 assertFalse(data.isSameContent(msg2));
358 void testIsSameContent_DiffPolicies() {
359 PdpUpdate msg2 = new PdpUpdate(update);
361 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
362 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
363 msg2.setPoliciesToBeDeployed(policies);
365 assertFalse(data.isSameContent(msg2));
369 void testIsSameContent_DiffPolicies_NotNull_Null() {
370 PdpUpdate msg2 = new PdpUpdate(update);
371 msg2.setPoliciesToBeDeployed(null);
373 assertFalse(data.isSameContent(msg2));
377 void testIsSameContent_DiffPolicies_Null_NotNull() {
378 final PdpUpdate msg2 = new PdpUpdate(update);
380 update.setPoliciesToBeDeployed(null);
381 List<ToscaPolicy> polsToDep2 = new LinkedList<>();
382 polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
383 polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
384 msg2.setPoliciesToBeDeployed(polsToDep2);
386 assertThat(data.isSameContent(msg2)).isFalse();
389 @SuppressWarnings("unchecked")
390 private void verifyResponse() {
391 verify(notifier).processResponse(any(), any(), any(Set.class), any(Set.class));
394 @SuppressWarnings("unchecked")
395 private void verifyNoResponse() {
396 verify(notifier, never()).processResponse(any(), any(), any(Set.class), any(Set.class));
400 * Makes an update message.
402 * @return a new update message
404 private PdpUpdate makeUpdate() {
405 PdpUpdate upd = new PdpUpdate();
407 upd.setDescription("update-description");
408 upd.setName(MY_NAME);
409 upd.setPdpGroup(MY_GROUP);
410 upd.setPdpSubgroup(MY_SUBGROUP);
412 ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
413 ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
415 upd.setPoliciesToBeDeployed(Arrays.asList(policy1, policy2));
416 upd.setPoliciesToBeUndeployed(Collections.emptyList());