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.assertNull;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.never;
32 import static org.mockito.Mockito.verify;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.LinkedList;
38 import java.util.List;
40 import java.util.TreeSet;
41 import java.util.stream.Collectors;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.onap.policy.models.pdp.concepts.PdpStateChange;
45 import org.onap.policy.models.pdp.concepts.PdpStatus;
46 import org.onap.policy.models.pdp.concepts.PdpUpdate;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
49 import org.onap.policy.pap.main.comm.CommonRequestBase;
51 public class UpdateReqTest extends CommonRequestBase {
53 private UpdateReq data;
54 private PdpUpdate update;
55 private PdpStatus response;
60 * @throws Exception if an error occurs
63 public void setUp() throws Exception {
66 response = new PdpStatus();
68 update = makeUpdate();
70 response.setName(MY_NAME);
71 response.setPdpGroup(update.getPdpGroup());
72 response.setPdpSubgroup(update.getPdpSubgroup());
74 update.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
75 .collect(Collectors.toList()));
77 data = new UpdateReq(reqParams, MY_REQ_NAME, update);
78 data.setNotifier(notifier);
82 public void testGetMessage() {
83 assertEquals(MY_REQ_NAME, data.getName());
84 assertSame(update, data.getMessage());
88 public void testCheckResponse() {
89 assertNull(data.checkResponse(response));
90 assertTrue(data.getUndeployPolicies().isEmpty());
93 // both policy lists null
94 update.setPoliciesToBeDeployed(null);
95 response.setPolicies(null);
96 assertNull(data.checkResponse(response));
97 assertTrue(data.getUndeployPolicies().isEmpty());
101 public void testCheckResponse_NullName() {
102 response.setName(null);
104 assertEquals("null PDP name", data.checkResponse(response));
105 assertTrue(data.getUndeployPolicies().isEmpty());
110 public void testCheckResponse_NullMsgName() {
111 update.setName(null);
113 assertEquals(null, data.checkResponse(response));
114 assertTrue(data.getUndeployPolicies().isEmpty());
119 public void testUpdateReqCheckResponse_MismatchedGroup() {
120 response.setPdpGroup(DIFFERENT);
122 assertEquals("group does not match", data.checkResponse(response));
123 assertTrue(data.getUndeployPolicies().isEmpty());
128 public void testUpdateReqCheckResponse_MismatchedSubGroup() {
129 response.setPdpSubgroup(DIFFERENT);
131 assertEquals("subgroup does not match", data.checkResponse(response));
132 assertTrue(data.getUndeployPolicies().isEmpty());
137 public void testCheckResponse_NullSubGroup() {
138 update.setPdpSubgroup(null);
139 response.setPdpSubgroup(null);
141 // different policy list - should have no impact
142 response.setPolicies(Collections.emptyList());
144 assertEquals(null, data.checkResponse(response));
145 assertTrue(data.getUndeployPolicies().isEmpty());
150 public void testUpdateReqCheckResponse_MismatchedPolicies() {
151 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
152 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
154 response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
156 assertEquals("policies do not match", data.checkResponse(response));
159 // the first policy from the original update is all that should be undeployed
160 assertEquals(Collections.singleton(update.getPoliciesToBeDeployed().get(0).getIdentifier()).toString(),
161 data.getUndeployPolicies().toString());
165 public void testUpdateReqCheckResponse_MismatchedPolicies_Null_NotNull() {
166 update.setPoliciesToBeDeployed(null);
168 assertEquals(null, data.checkResponse(response));
169 assertTrue(data.getUndeployPolicies().isEmpty());
174 public void testUpdateReqCheckResponse_MismatchedPolicies_NotNull_Null() {
175 response.setPolicies(null);
177 assertEquals("policies do not match", data.checkResponse(response));
180 // all policies in the update should be undeployed
181 assertEquals(update.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
182 .collect(Collectors.toList())
183 .toString(), new TreeSet<>(data.getUndeployPolicies()).toString());
187 public void testReconfigure() {
188 // different message type should fail and leave message unchanged
189 assertFalse(data.reconfigure(new PdpStateChange()));
190 assertSame(update, data.getMessage());
192 // same content - should succeed, but leave message unchanged
193 PdpUpdate msg2 = new PdpUpdate(update);
194 assertTrue(data.reconfigure(msg2));
195 assertSame(update, data.getMessage());
197 // different content - should succeed and install NEW message
198 msg2.setPdpGroup(DIFFERENT);
199 assertTrue(data.reconfigure(msg2));
200 assertSame(msg2, data.getMessage());
204 public void testReconfigureIsFullSameAsDeployList() {
205 PdpUpdate msg2 = new PdpUpdate(update);
206 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
208 msg2.setPoliciesToBeDeployed(policies);
209 assertTrue(data.reconfigure(msg2));
210 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
214 public void testListsNewVsResult() {
215 PdpUpdate msg2 = new PdpUpdate(update);
216 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
218 // some items in both deploy and newMessage.deploy
219 msg2.setPoliciesToBeDeployed(policies);
221 data.getMessage().setPoliciesToBeDeployed(policies);
222 assertTrue(data.reconfigure(msg2));
223 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
225 // some items in both deploy and newMessage.undeploy
226 policies = new ArrayList<>();
227 policies.add(makePolicy("policy-z-1", "1.0.0"));
228 policies.add(makePolicy("policy-y-1", "1.0.0"));
229 data.getMessage().setPoliciesToBeDeployed(policies);
232 policies = new ArrayList<>(update.getPoliciesToBeDeployed());
233 List<ToscaConceptIdentifier> polsToUndep = policies.parallelStream()
234 .map(ToscaPolicy::getIdentifier)
235 .collect(Collectors.toList());
236 msg2.setPoliciesToBeUndeployed(polsToUndep);
238 assertTrue(data.reconfigure(msg2));
240 // some items only in deploy
241 policies = new ArrayList<>(update.getPoliciesToBeDeployed());
242 msg2.setPoliciesToBeDeployed(policies);
243 data.getMessage().setPoliciesToBeDeployed(new LinkedList<>());
244 assertTrue(data.reconfigure(msg2));
245 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
247 // some items in both undeploy and newMessage.undeploy
248 List<ToscaConceptIdentifier> pols = policies.stream().map(ToscaPolicy::getIdentifier)
249 .collect(Collectors.toList());
250 msg2.setPoliciesToBeUndeployed(pols);
251 pols.add(makePolicy("policy-zz-1", "1.1.0").getIdentifier());
252 data.getMessage().setPoliciesToBeUndeployed(pols);
253 assertTrue(data.reconfigure(msg2));
254 assertThat(data.getMessage().getPoliciesToBeUndeployed()).containsAll(msg2.getPoliciesToBeUndeployed());
256 // some items in both undeploy and newMessage.deploy
257 policies = new ArrayList<>(update.getPoliciesToBeDeployed());
258 List<ToscaConceptIdentifier> polsToUndep2 = policies.parallelStream()
259 .map(ToscaPolicy::getIdentifier)
260 .collect(Collectors.toList());
261 data.getMessage().setPoliciesToBeUndeployed(polsToUndep2);
263 List<ToscaPolicy> polsToDep2 = new LinkedList<>();
264 polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
265 polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
266 msg2.setPoliciesToBeDeployed(polsToDep2);
268 assertTrue(data.reconfigure(msg2));
270 List<ToscaConceptIdentifier> dataPols2 = data.getMessage().getPoliciesToBeDeployed().stream()
271 .map(ToscaPolicy::getIdentifier)
272 .collect(Collectors.toList());
273 assertThat(data.getMessage().getPoliciesToBeUndeployed())
274 .doesNotContainAnyElementsOf(dataPols2);
276 // some items only in undeploy
277 pols = policies.stream().map(ToscaPolicy::getIdentifier)
278 .collect(Collectors.toList());
279 msg2.setPoliciesToBeUndeployed(pols);
280 data.getMessage().setPoliciesToBeUndeployed(new LinkedList<>());
281 assertTrue(data.reconfigure(msg2));
282 assertThat(data.getMessage().getPoliciesToBeUndeployed()).containsAll(msg2.getPoliciesToBeUndeployed());
286 public void testIsSameContent() {
287 data = new UpdateReq(reqParams, MY_REQ_NAME, update);
288 data.setNotifier(notifier);
290 PdpUpdate msg2 = new PdpUpdate(update);
291 msg2.setName("world");
292 update.setPoliciesToBeDeployed(null);
293 List<ToscaPolicy> polsToDep2 = new LinkedList<>();
294 polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
295 polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
296 msg2.setPoliciesToBeDeployed(polsToDep2);
297 assertThat(data.isSameContent(msg2)).isFalse();
299 // both policy lists null
300 msg2.setPoliciesToBeDeployed(null);
301 assertEquals(data.getMessage().getPoliciesToBeDeployed(), msg2.getPoliciesToBeDeployed());
305 public void testIsSameContent_BothGroupNamesNull() {
306 PdpUpdate msg2 = new PdpUpdate(update);
307 msg2.setPdpGroup(null);
308 update.setPdpGroup(null);
309 assertEquals(data.getMessage().getPdpGroup(), msg2.getPdpGroup());
313 public void testIsSameContent_BothSubGroupNamesNull() {
314 PdpUpdate msg2 = new PdpUpdate(update);
315 msg2.setPdpSubgroup(null);
316 update.setPdpSubgroup(null);
317 assertEquals(data.getMessage().getPdpSubgroup(), msg2.getPdpSubgroup());
321 public void testIsSameContent_DiffGroup() {
322 PdpUpdate msg2 = new PdpUpdate(update);
323 msg2.setPdpGroup(null);
324 assertFalse(data.isSameContent(msg2));
326 msg2.setPdpGroup(DIFFERENT);
327 assertFalse(data.isSameContent(msg2));
329 update.setPdpGroup(null);
330 assertFalse(data.isSameContent(msg2));
334 public void testIsSameContent_DiffSubGroup() {
335 PdpUpdate msg2 = new PdpUpdate(update);
336 msg2.setPdpSubgroup(null);
337 assertFalse(data.isSameContent(msg2));
339 msg2.setPdpSubgroup(DIFFERENT);
340 assertFalse(data.isSameContent(msg2));
342 update.setPdpSubgroup(null);
343 assertFalse(data.isSameContent(msg2));
347 public void testIsSameContent_DiffPolicies() {
348 PdpUpdate msg2 = new PdpUpdate(update);
350 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
351 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
352 msg2.setPoliciesToBeDeployed(policies);
354 assertFalse(data.isSameContent(msg2));
358 public void testIsSameContent_DiffPolicies_NotNull_Null() {
359 PdpUpdate msg2 = new PdpUpdate(update);
360 msg2.setPoliciesToBeDeployed(null);
362 assertFalse(data.isSameContent(msg2));
366 public void testIsSameContent_DiffPolicies_Null_NotNull() {
367 final PdpUpdate msg2 = new PdpUpdate(update);
369 update.setPoliciesToBeDeployed(null);
370 List<ToscaPolicy> polsToDep2 = new LinkedList<>();
371 polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
372 polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
373 msg2.setPoliciesToBeDeployed(polsToDep2);
375 assertThat(data.isSameContent(msg2)).isFalse();
378 @SuppressWarnings("unchecked")
379 private void verifyResponse() {
380 verify(notifier).processResponse(any(), any(), any(Set.class), any(Set.class));
383 @SuppressWarnings("unchecked")
384 private void verifyNoResponse() {
385 verify(notifier, never()).processResponse(any(), any(), any(Set.class), any(Set.class));
389 * Makes an update message.
391 * @return a new update message
393 private PdpUpdate makeUpdate() {
394 PdpUpdate upd = new PdpUpdate();
396 upd.setDescription("update-description");
397 upd.setName(MY_NAME);
398 upd.setPdpGroup(MY_GROUP);
399 upd.setPdpSubgroup(MY_SUBGROUP);
401 ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
402 ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
404 upd.setPoliciesToBeDeployed(Arrays.asList(policy1, policy2));
405 upd.setPoliciesToBeUndeployed(Collections.emptyList());