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.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertSame;
30 import static org.junit.Assert.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.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.junit.MockitoJUnitRunner;
47 import org.onap.policy.models.pdp.concepts.PdpStateChange;
48 import org.onap.policy.models.pdp.concepts.PdpStatus;
49 import org.onap.policy.models.pdp.concepts.PdpUpdate;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
52 import org.onap.policy.pap.main.comm.CommonRequestBase;
54 @RunWith(MockitoJUnitRunner.class)
55 public class UpdateReqTest extends CommonRequestBase {
57 private UpdateReq data;
58 private PdpUpdate update;
59 private PdpStatus response;
64 * @throws Exception if an error occurs
68 public void setUp() throws Exception {
71 response = new PdpStatus();
73 update = makeUpdate();
75 response.setName(MY_NAME);
76 response.setPdpGroup(update.getPdpGroup());
77 response.setPdpSubgroup(update.getPdpSubgroup());
79 update.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
80 .collect(Collectors.toList()));
82 data = new UpdateReq(reqParams, MY_REQ_NAME, update);
83 data.setNotifier(notifier);
87 public void testGetMessage() {
88 assertEquals(MY_REQ_NAME, data.getName());
89 assertSame(update, data.getMessage());
93 public void testCheckResponse() {
94 assertNull(data.checkResponse(response));
95 assertTrue(data.getUndeployPolicies().isEmpty());
98 // both policy lists null
99 update.setPoliciesToBeDeployed(null);
100 response.setPolicies(null);
101 assertNull(data.checkResponse(response));
102 assertTrue(data.getUndeployPolicies().isEmpty());
106 public void testCheckResponse_NullName() {
107 response.setName(null);
109 assertEquals("null PDP name", data.checkResponse(response));
110 assertTrue(data.getUndeployPolicies().isEmpty());
115 public void testCheckResponse_NullMsgName() {
116 update.setName(null);
118 assertEquals(null, data.checkResponse(response));
119 assertTrue(data.getUndeployPolicies().isEmpty());
124 public void testUpdateReqCheckResponse_MismatchedGroup() {
125 response.setPdpGroup(DIFFERENT);
127 assertEquals("group does not match", data.checkResponse(response));
128 assertTrue(data.getUndeployPolicies().isEmpty());
133 public void testUpdateReqCheckResponse_MismatchedSubGroup() {
134 response.setPdpSubgroup(DIFFERENT);
136 assertEquals("subgroup does not match", data.checkResponse(response));
137 assertTrue(data.getUndeployPolicies().isEmpty());
142 public void testCheckResponse_NullSubGroup() {
143 update.setPdpSubgroup(null);
144 response.setPdpSubgroup(null);
146 // different policy list - should have no impact
147 response.setPolicies(Collections.emptyList());
149 assertEquals(null, data.checkResponse(response));
150 assertTrue(data.getUndeployPolicies().isEmpty());
155 public void testUpdateReqCheckResponse_MismatchedPolicies() {
156 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
157 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
159 response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
161 assertEquals("policies do not match", data.checkResponse(response));
164 // the first policy from the original update is all that should be undeployed
165 assertEquals(Collections.singleton(update.getPoliciesToBeDeployed().get(0).getIdentifier()).toString(),
166 data.getUndeployPolicies().toString());
170 public void testUpdateReqCheckResponse_MismatchedPolicies_Null_NotNull() {
171 update.setPoliciesToBeDeployed(null);
173 assertEquals(null, data.checkResponse(response));
174 assertTrue(data.getUndeployPolicies().isEmpty());
179 public void testUpdateReqCheckResponse_MismatchedPolicies_NotNull_Null() {
180 response.setPolicies(null);
182 assertEquals("policies do not match", data.checkResponse(response));
185 // all policies in the update should be undeployed
186 assertEquals(update.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
187 .collect(Collectors.toList())
188 .toString(), new TreeSet<>(data.getUndeployPolicies()).toString());
192 public void testReconfigure() {
193 // different message type should fail and leave message unchanged
194 assertFalse(data.reconfigure(new PdpStateChange()));
195 assertSame(update, data.getMessage());
197 // same content - should succeed, but leave message unchanged
198 PdpUpdate msg2 = new PdpUpdate(update);
199 assertTrue(data.reconfigure(msg2));
200 assertSame(update, data.getMessage());
202 // different content - should succeed and install NEW message
203 msg2.setPdpGroup(DIFFERENT);
204 assertTrue(data.reconfigure(msg2));
205 assertSame(msg2, data.getMessage());
207 // both lists in the update are null - should not throw an exception
208 PdpUpdate msg3 = new PdpUpdate(update);
209 msg3.setPdpGroup(DIFFERENT);
210 msg3.setPoliciesToBeDeployed(null);
211 msg3.setPoliciesToBeUndeployed(null);
212 assertThatCode(() -> data.reconfigure(msg3)).doesNotThrowAnyException();
214 // both lists in the current msg (i.e., msg3) are null - should not throw an exception
215 assertThatCode(() -> data.reconfigure(update)).doesNotThrowAnyException();
219 public void testReconfigureIsFullSameAsDeployList() {
220 PdpUpdate msg2 = new PdpUpdate(update);
221 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
223 msg2.setPoliciesToBeDeployed(policies);
224 assertTrue(data.reconfigure(msg2));
225 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
229 public void testListsNewVsResult() {
230 PdpUpdate msg2 = new PdpUpdate(update);
231 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
233 // some items in both deploy and newMessage.deploy
234 msg2.setPoliciesToBeDeployed(policies);
236 data.getMessage().setPoliciesToBeDeployed(policies);
237 assertTrue(data.reconfigure(msg2));
238 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
240 // some items in both deploy and newMessage.undeploy
241 policies = new ArrayList<>();
242 policies.add(makePolicy("policy-z-1", "1.0.0"));
243 policies.add(makePolicy("policy-y-1", "1.0.0"));
244 data.getMessage().setPoliciesToBeDeployed(policies);
247 policies = new ArrayList<>(update.getPoliciesToBeDeployed());
248 List<ToscaConceptIdentifier> polsToUndep = policies.parallelStream()
249 .map(ToscaPolicy::getIdentifier)
250 .collect(Collectors.toList());
251 msg2.setPoliciesToBeUndeployed(polsToUndep);
253 assertTrue(data.reconfigure(msg2));
255 // some items only in deploy
256 policies = new ArrayList<>(update.getPoliciesToBeDeployed());
257 msg2.setPoliciesToBeDeployed(policies);
258 data.getMessage().setPoliciesToBeDeployed(new LinkedList<>());
259 assertTrue(data.reconfigure(msg2));
260 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
262 // some items in both undeploy and newMessage.undeploy
263 List<ToscaConceptIdentifier> pols = policies.stream().map(ToscaPolicy::getIdentifier)
264 .collect(Collectors.toList());
265 msg2.setPoliciesToBeUndeployed(pols);
266 pols.add(makePolicy("policy-zz-1", "1.1.0").getIdentifier());
267 data.getMessage().setPoliciesToBeUndeployed(pols);
268 assertTrue(data.reconfigure(msg2));
269 assertThat(data.getMessage().getPoliciesToBeUndeployed()).containsAll(msg2.getPoliciesToBeUndeployed());
271 // some items in both undeploy and newMessage.deploy
272 policies = new ArrayList<>(update.getPoliciesToBeDeployed());
273 List<ToscaConceptIdentifier> polsToUndep2 = policies.parallelStream()
274 .map(ToscaPolicy::getIdentifier)
275 .collect(Collectors.toList());
276 data.getMessage().setPoliciesToBeUndeployed(polsToUndep2);
278 List<ToscaPolicy> polsToDep2 = new LinkedList<>();
279 polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
280 polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
281 msg2.setPoliciesToBeDeployed(polsToDep2);
283 assertTrue(data.reconfigure(msg2));
285 List<ToscaConceptIdentifier> dataPols2 = data.getMessage().getPoliciesToBeDeployed().stream()
286 .map(ToscaPolicy::getIdentifier)
287 .collect(Collectors.toList());
288 assertThat(data.getMessage().getPoliciesToBeUndeployed())
289 .doesNotContainAnyElementsOf(dataPols2);
291 // some items only in undeploy
292 pols = policies.stream().map(ToscaPolicy::getIdentifier)
293 .collect(Collectors.toList());
294 msg2.setPoliciesToBeUndeployed(pols);
295 data.getMessage().setPoliciesToBeUndeployed(new LinkedList<>());
296 assertTrue(data.reconfigure(msg2));
297 assertThat(data.getMessage().getPoliciesToBeUndeployed()).containsAll(msg2.getPoliciesToBeUndeployed());
301 public void testIsSameContent() {
302 data = new UpdateReq(reqParams, MY_REQ_NAME, update);
303 data.setNotifier(notifier);
305 PdpUpdate msg2 = new PdpUpdate(update);
306 msg2.setName("world");
307 update.setPoliciesToBeDeployed(null);
308 List<ToscaPolicy> polsToDep2 = new LinkedList<>();
309 polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
310 polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
311 msg2.setPoliciesToBeDeployed(polsToDep2);
312 assertThat(data.isSameContent(msg2)).isFalse();
314 // both policy lists null
315 msg2.setPoliciesToBeDeployed(null);
316 assertEquals(data.getMessage().getPoliciesToBeDeployed(), msg2.getPoliciesToBeDeployed());
320 public void testIsSameContent_BothGroupNamesNull() {
321 PdpUpdate msg2 = new PdpUpdate(update);
322 msg2.setPdpGroup(null);
323 update.setPdpGroup(null);
324 assertEquals(data.getMessage().getPdpGroup(), msg2.getPdpGroup());
328 public void testIsSameContent_BothSubGroupNamesNull() {
329 PdpUpdate msg2 = new PdpUpdate(update);
330 msg2.setPdpSubgroup(null);
331 update.setPdpSubgroup(null);
332 assertEquals(data.getMessage().getPdpSubgroup(), msg2.getPdpSubgroup());
336 public void testIsSameContent_DiffGroup() {
337 PdpUpdate msg2 = new PdpUpdate(update);
338 msg2.setPdpGroup(null);
339 assertFalse(data.isSameContent(msg2));
341 msg2.setPdpGroup(DIFFERENT);
342 assertFalse(data.isSameContent(msg2));
344 update.setPdpGroup(null);
345 assertFalse(data.isSameContent(msg2));
349 public void testIsSameContent_DiffSubGroup() {
350 PdpUpdate msg2 = new PdpUpdate(update);
351 msg2.setPdpSubgroup(null);
352 assertFalse(data.isSameContent(msg2));
354 msg2.setPdpSubgroup(DIFFERENT);
355 assertFalse(data.isSameContent(msg2));
357 update.setPdpSubgroup(null);
358 assertFalse(data.isSameContent(msg2));
362 public void testIsSameContent_DiffPolicies() {
363 PdpUpdate msg2 = new PdpUpdate(update);
365 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
366 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
367 msg2.setPoliciesToBeDeployed(policies);
369 assertFalse(data.isSameContent(msg2));
373 public void testIsSameContent_DiffPolicies_NotNull_Null() {
374 PdpUpdate msg2 = new PdpUpdate(update);
375 msg2.setPoliciesToBeDeployed(null);
377 assertFalse(data.isSameContent(msg2));
381 public void testIsSameContent_DiffPolicies_Null_NotNull() {
382 final PdpUpdate msg2 = new PdpUpdate(update);
384 update.setPoliciesToBeDeployed(null);
385 List<ToscaPolicy> polsToDep2 = new LinkedList<>();
386 polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
387 polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
388 msg2.setPoliciesToBeDeployed(polsToDep2);
390 assertThat(data.isSameContent(msg2)).isFalse();
393 @SuppressWarnings("unchecked")
394 private void verifyResponse() {
395 verify(notifier).processResponse(any(), any(), any(Set.class), any(Set.class));
398 @SuppressWarnings("unchecked")
399 private void verifyNoResponse() {
400 verify(notifier, never()).processResponse(any(), any(), any(Set.class), any(Set.class));
404 * Makes an update message.
406 * @return a new update message
408 private PdpUpdate makeUpdate() {
409 PdpUpdate upd = new PdpUpdate();
411 upd.setDescription("update-description");
412 upd.setName(MY_NAME);
413 upd.setPdpGroup(MY_GROUP);
414 upd.setPdpSubgroup(MY_SUBGROUP);
416 ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
417 ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
419 upd.setPoliciesToBeDeployed(Arrays.asList(policy1, policy2));
420 upd.setPoliciesToBeUndeployed(Collections.emptyList());