2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 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.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotSame;
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.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 public class UpdateReqTest extends CommonRequestBase {
54 private UpdateReq data;
55 private PdpUpdate update;
56 private PdpStatus response;
61 * @throws Exception if an error occurs
64 public void setUp() throws Exception {
67 response = new PdpStatus();
69 update = makeUpdate();
71 response.setName(MY_NAME);
72 response.setPdpGroup(update.getPdpGroup());
73 response.setPdpSubgroup(update.getPdpSubgroup());
75 update.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
76 .collect(Collectors.toList()));
78 data = new UpdateReq(reqParams, MY_REQ_NAME, update);
79 data.setNotifier(notifier);
83 public void testGetMessage() {
84 assertEquals(MY_REQ_NAME, data.getName());
85 assertSame(update, data.getMessage());
89 public void testCheckResponse() {
90 assertNull(data.checkResponse(response));
91 assertTrue(data.getUndeployPolicies().isEmpty());
94 // both policy lists null
95 update.setPoliciesToBeDeployed(null);
96 response.setPolicies(null);
97 assertNull(data.checkResponse(response));
98 assertTrue(data.getUndeployPolicies().isEmpty());
102 public void testCheckResponse_NullName() {
103 response.setName(null);
105 assertEquals("null PDP name", data.checkResponse(response));
106 assertTrue(data.getUndeployPolicies().isEmpty());
111 public void testCheckResponse_NullMsgName() {
112 update.setName(null);
114 assertEquals(null, data.checkResponse(response));
115 assertTrue(data.getUndeployPolicies().isEmpty());
120 public void testUpdateReqCheckResponse_MismatchedGroup() {
121 response.setPdpGroup(DIFFERENT);
123 assertEquals("group does not match", data.checkResponse(response));
124 assertTrue(data.getUndeployPolicies().isEmpty());
129 public void testUpdateReqCheckResponse_MismatchedSubGroup() {
130 response.setPdpSubgroup(DIFFERENT);
132 assertEquals("subgroup does not match", data.checkResponse(response));
133 assertTrue(data.getUndeployPolicies().isEmpty());
138 public void testCheckResponse_NullSubGroup() {
139 update.setPdpSubgroup(null);
140 response.setPdpSubgroup(null);
142 // different policy list - should have no impact
143 response.setPolicies(Collections.emptyList());
145 assertEquals(null, data.checkResponse(response));
146 assertTrue(data.getUndeployPolicies().isEmpty());
151 public void testUpdateReqCheckResponse_MismatchedPolicies() {
152 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
153 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
155 response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
157 assertEquals("policies do not match", data.checkResponse(response));
160 // the first policy from the original update is all that should be undeployed
161 assertEquals(Collections.singleton(update.getPoliciesToBeDeployed().get(0).getIdentifier()).toString(),
162 data.getUndeployPolicies().toString());
166 public void testUpdateReqCheckResponse_MismatchedPolicies_Null_NotNull() {
167 update.setPoliciesToBeDeployed(null);
169 assertEquals(null, data.checkResponse(response));
170 assertTrue(data.getUndeployPolicies().isEmpty());
175 public void testUpdateReqCheckResponse_MismatchedPolicies_NotNull_Null() {
176 response.setPolicies(null);
178 assertEquals("policies do not match", data.checkResponse(response));
181 // all policies in the update should be undeployed
182 assertEquals(update.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
183 .collect(Collectors.toList())
184 .toString(), new TreeSet<>(data.getUndeployPolicies()).toString());
188 public 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 assertNotSame(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());
205 public void testReconfigureIsFullSameAsDeployList() {
206 PdpUpdate msg2 = new PdpUpdate(update);
207 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
209 msg2.setPoliciesToBeDeployed(policies);
210 assertTrue(data.reconfigure(msg2));
211 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
215 public void testListsNewVsResult() {
216 PdpUpdate msg2 = new PdpUpdate(update);
217 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
219 // some items in both deploy and newMessage.deploy
220 msg2.setPoliciesToBeDeployed(policies);
222 data.getMessage().setPoliciesToBeDeployed(policies);
223 assertTrue(data.reconfigure(msg2));
224 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
226 // some items in both deploy and newMessage.undeploy
227 policies = new ArrayList<>();
228 policies.add(makePolicy("policy-z-1", "1.0.0"));
229 policies.add(makePolicy("policy-y-1", "1.0.0"));
230 data.getMessage().setPoliciesToBeDeployed(policies);
233 policies = new ArrayList<>(update.getPoliciesToBeDeployed());
234 List<ToscaConceptIdentifier> polsToUndep = policies.parallelStream()
235 .map(ToscaPolicy::getIdentifier)
236 .collect(Collectors.toList());
237 msg2.setPoliciesToBeUndeployed(polsToUndep);
239 assertTrue(data.reconfigure(msg2));
241 // some items only in deploy
242 policies = new ArrayList<>(update.getPoliciesToBeDeployed());
243 msg2.setPoliciesToBeDeployed(policies);
244 data.getMessage().setPoliciesToBeDeployed(new LinkedList<>());
245 assertTrue(data.reconfigure(msg2));
246 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
248 // some items in both undeploy and newMessage.undeploy
249 List<ToscaConceptIdentifier> pols = policies.stream().map(ToscaPolicy::getIdentifier)
250 .collect(Collectors.toList());
251 msg2.setPoliciesToBeUndeployed(pols);
252 pols.add(makePolicy("policy-zz-1", "1.1.0").getIdentifier());
253 data.getMessage().setPoliciesToBeUndeployed(pols);
254 assertTrue(data.reconfigure(msg2));
255 assertThat(data.getMessage().getPoliciesToBeUndeployed()).containsAll(msg2.getPoliciesToBeUndeployed());
257 // some items in both undeploy and newMessage.deploy
258 policies = new ArrayList<>(update.getPoliciesToBeDeployed());
259 List<ToscaConceptIdentifier> polsToUndep2 = policies.parallelStream()
260 .map(ToscaPolicy::getIdentifier)
261 .collect(Collectors.toList());
262 data.getMessage().setPoliciesToBeUndeployed(polsToUndep2);
264 List<ToscaPolicy> polsToDep2 = new LinkedList<>();
265 polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
266 polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
267 msg2.setPoliciesToBeDeployed(polsToDep2);
269 assertTrue(data.reconfigure(msg2));
271 List<ToscaConceptIdentifier> dataPols2 = data.getMessage().getPoliciesToBeDeployed().stream()
272 .map(ToscaPolicy::getIdentifier)
273 .collect(Collectors.toList());
274 assertThat(data.getMessage().getPoliciesToBeUndeployed())
275 .doesNotContainAnyElementsOf(dataPols2);
277 // some items only in undeploy
278 pols = policies.stream().map(ToscaPolicy::getIdentifier)
279 .collect(Collectors.toList());
280 msg2.setPoliciesToBeUndeployed(pols);
281 data.getMessage().setPoliciesToBeUndeployed(new LinkedList<>());
282 assertTrue(data.reconfigure(msg2));
283 assertThat(data.getMessage().getPoliciesToBeUndeployed()).containsAll(msg2.getPoliciesToBeUndeployed());
287 public void testIsSameContent() {
288 data = new UpdateReq(reqParams, MY_REQ_NAME, update);
289 data.setNotifier(notifier);
291 PdpUpdate msg2 = new PdpUpdate(update);
292 msg2.setName("world");
293 update.setPoliciesToBeDeployed(null);
294 List<ToscaPolicy> polsToDep2 = new LinkedList<>();
295 polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
296 polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
297 msg2.setPoliciesToBeDeployed(polsToDep2);
298 assertThat(data.isSameContent(msg2)).isFalse();
300 // both policy lists null
301 msg2.setPoliciesToBeDeployed(null);
302 assertEquals(data.getMessage().getPoliciesToBeDeployed(), msg2.getPoliciesToBeDeployed());
306 public void testIsSameContent_BothGroupNamesNull() {
307 PdpUpdate msg2 = new PdpUpdate(update);
308 msg2.setPdpGroup(null);
309 update.setPdpGroup(null);
310 assertEquals(data.getMessage().getPdpGroup(), msg2.getPdpGroup());
314 public void testIsSameContent_BothSubGroupNamesNull() {
315 PdpUpdate msg2 = new PdpUpdate(update);
316 msg2.setPdpSubgroup(null);
317 update.setPdpSubgroup(null);
318 assertEquals(data.getMessage().getPdpSubgroup(), msg2.getPdpSubgroup());
322 public void testIsSameContent_DiffGroup() {
323 PdpUpdate msg2 = new PdpUpdate(update);
324 msg2.setPdpGroup(null);
325 assertFalse(data.isSameContent(msg2));
327 msg2.setPdpGroup(DIFFERENT);
328 assertFalse(data.isSameContent(msg2));
330 update.setPdpGroup(null);
331 assertFalse(data.isSameContent(msg2));
335 public void testIsSameContent_DiffSubGroup() {
336 PdpUpdate msg2 = new PdpUpdate(update);
337 msg2.setPdpSubgroup(null);
338 assertFalse(data.isSameContent(msg2));
340 msg2.setPdpSubgroup(DIFFERENT);
341 assertFalse(data.isSameContent(msg2));
343 update.setPdpSubgroup(null);
344 assertFalse(data.isSameContent(msg2));
348 public void testIsSameContent_DiffPolicies() {
349 PdpUpdate msg2 = new PdpUpdate(update);
351 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
352 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
353 msg2.setPoliciesToBeDeployed(policies);
355 assertFalse(data.isSameContent(msg2));
359 public void testIsSameContent_DiffPolicies_NotNull_Null() {
360 PdpUpdate msg2 = new PdpUpdate(update);
361 msg2.setPoliciesToBeDeployed(null);
363 assertFalse(data.isSameContent(msg2));
367 public void testIsSameContent_DiffPolicies_Null_NotNull() {
368 final PdpUpdate msg2 = new PdpUpdate(update);
370 update.setPoliciesToBeDeployed(null);
371 List<ToscaPolicy> polsToDep2 = new LinkedList<>();
372 polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
373 polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
374 msg2.setPoliciesToBeDeployed(polsToDep2);
376 assertThat(data.isSameContent(msg2)).isFalse();
379 @SuppressWarnings("unchecked")
380 private void verifyResponse() {
381 verify(notifier).processResponse(any(), any(), any(Set.class), any(Set.class));
384 @SuppressWarnings("unchecked")
385 private void verifyNoResponse() {
386 verify(notifier, never()).processResponse(any(), any(), any(Set.class), any(Set.class));
390 * Makes an update message.
392 * @return a new update message
394 private PdpUpdate makeUpdate() {
395 PdpUpdate upd = new PdpUpdate();
397 upd.setDescription("update-description");
398 upd.setName(MY_NAME);
399 upd.setPdpGroup(MY_GROUP);
400 upd.setPdpSubgroup(MY_SUBGROUP);
402 ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
403 ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
405 upd.setPoliciesToBeDeployed(Arrays.asList(policy1, policy2));
406 upd.setPoliciesToBeUndeployed(Collections.emptyList());