2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.comm.msgdata;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotSame;
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.getPolicies().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
76 data = new UpdateReq(reqParams, MY_REQ_NAME, update);
77 data.setNotifier(notifier);
81 public void testGetMessage() {
82 assertEquals(MY_REQ_NAME, data.getName());
83 assertSame(update, data.getMessage());
87 public void testCheckResponse() {
88 assertNull(data.checkResponse(response));
89 assertTrue(data.getUndeployPolicies().isEmpty());
92 // both policy lists null
93 update.setPolicies(null);
94 response.setPolicies(null);
95 assertNull(data.checkResponse(response));
96 assertTrue(data.getUndeployPolicies().isEmpty());
100 public void testCheckResponse_NullName() {
101 response.setName(null);
103 assertEquals("null PDP name", data.checkResponse(response));
104 assertTrue(data.getUndeployPolicies().isEmpty());
109 public void testCheckResponse_NullMsgName() {
110 update.setName(null);
112 assertEquals(null, data.checkResponse(response));
113 assertTrue(data.getUndeployPolicies().isEmpty());
118 public void testUpdateReqCheckResponse_MismatchedGroup() {
119 response.setPdpGroup(DIFFERENT);
121 assertEquals("group does not match", data.checkResponse(response));
122 assertTrue(data.getUndeployPolicies().isEmpty());
127 public void testUpdateReqCheckResponse_MismatchedSubGroup() {
128 response.setPdpSubgroup(DIFFERENT);
130 assertEquals("subgroup does not match", data.checkResponse(response));
131 assertTrue(data.getUndeployPolicies().isEmpty());
136 public void testCheckResponse_NullSubGroup() {
137 update.setPdpSubgroup(null);
138 response.setPdpSubgroup(null);
140 // different policy list - should have no impact
141 response.setPolicies(Collections.emptyList());
143 assertEquals(null, data.checkResponse(response));
144 assertTrue(data.getUndeployPolicies().isEmpty());
149 public void testUpdateReqCheckResponse_MismatchedPolicies() {
150 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
151 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
153 response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
155 assertEquals("policies do not match", data.checkResponse(response));
158 // the first policy from the original update is all that should be undeployed
159 assertEquals(Collections.singleton(update.getPolicies().get(0).getIdentifier()).toString(),
160 data.getUndeployPolicies().toString());
164 public void testUpdateReqCheckResponse_MismatchedPolicies_Null_NotNull() {
165 update.setPolicies(null);
167 assertEquals("policies do not match", data.checkResponse(response));
168 assertTrue(data.getUndeployPolicies().isEmpty());
173 public void testUpdateReqCheckResponse_MismatchedPolicies_NotNull_Null() {
174 response.setPolicies(null);
176 assertEquals("policies do not match", data.checkResponse(response));
179 // all policies in the update should be undeployed
180 assertEquals(update.getPolicies().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList())
181 .toString(), new TreeSet<>(data.getUndeployPolicies()).toString());
185 public void testReconfigure() {
186 // different message type should fail and leave message unchanged
187 assertFalse(data.reconfigure(new PdpStateChange()));
188 assertSame(update, data.getMessage());
190 // same content - should succeed, but leave message unchanged
191 PdpUpdate msg2 = new PdpUpdate(update);
192 assertTrue(data.reconfigure(msg2));
193 assertNotSame(update, data.getMessage());
195 // different content - should succeed and install NEW message
196 msg2.setPdpGroup(DIFFERENT);
197 assertTrue(data.reconfigure(msg2));
198 assertSame(msg2, data.getMessage());
202 public void testReconfigureIsFullSameAsDeployList() {
203 PdpUpdate msg2 = new PdpUpdate(update);
204 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
206 msg2.setPolicies(policies);
207 msg2.setPoliciesToBeDeployed(policies);
208 assertTrue(data.reconfigure(msg2));
209 assertThat(data.getMessage().getPolicies()).containsAll(msg2.getPolicies());
213 public void testListsNewVsResult() {
214 PdpUpdate msg2 = new PdpUpdate(update);
215 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
217 // some items in both deploy and newMessage.deploy
218 msg2.setPoliciesToBeDeployed(policies);
220 data.getMessage().setPolicies(policies);
221 assertTrue(data.reconfigure(msg2));
222 assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
224 // some items in both deploy and newMessage.undeploy
225 policies = new ArrayList<>();
226 policies.add(makePolicy("policy-z-1", "1.0.0"));
227 policies.add(makePolicy("policy-y-1", "1.0.0"));
228 data.getMessage().setPoliciesToBeDeployed(policies);
231 policies = new ArrayList<>(update.getPolicies());
232 List<ToscaConceptIdentifier> polsToUndep = policies.parallelStream()
233 .map(ToscaPolicy::getIdentifier)
234 .collect(Collectors.toList());
235 msg2.setPoliciesToBeUndeployed(polsToUndep);
237 assertTrue(data.reconfigure(msg2));
239 assertThat(data.getMessage().getPoliciesToBeDeployed()).isEmpty();
241 // some items only in deploy
242 policies = new ArrayList<>(update.getPolicies());
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);
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.getPolicies());
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 assertFalse(data.isSameContent(msg2));
295 // both policy lists null
296 update.setPolicies(null);
297 msg2.setPolicies(null);
298 assertEquals(data.getMessage().getPolicies(), msg2.getPolicies());
302 public void testIsSameContent_BothGroupNamesNull() {
303 PdpUpdate msg2 = new PdpUpdate(update);
304 msg2.setPdpGroup(null);
305 update.setPdpGroup(null);
306 assertEquals(data.getMessage().getPdpGroup(), msg2.getPdpGroup());
310 public void testIsSameContent_BothSubGroupNamesNull() {
311 PdpUpdate msg2 = new PdpUpdate(update);
312 msg2.setPdpSubgroup(null);
313 update.setPdpSubgroup(null);
314 assertEquals(data.getMessage().getPdpSubgroup(), msg2.getPdpSubgroup());
318 public void testIsSameContent_DiffGroup() {
319 PdpUpdate msg2 = new PdpUpdate(update);
320 msg2.setPdpGroup(null);
321 assertFalse(data.isSameContent(msg2));
323 msg2.setPdpGroup(DIFFERENT);
324 assertFalse(data.isSameContent(msg2));
326 update.setPdpGroup(null);
327 assertFalse(data.isSameContent(msg2));
331 public void testIsSameContent_DiffSubGroup() {
332 PdpUpdate msg2 = new PdpUpdate(update);
333 msg2.setPdpSubgroup(null);
334 assertFalse(data.isSameContent(msg2));
336 msg2.setPdpSubgroup(DIFFERENT);
337 assertFalse(data.isSameContent(msg2));
339 update.setPdpSubgroup(null);
340 assertFalse(data.isSameContent(msg2));
344 public void testIsSameContent_DiffPolicies() {
345 PdpUpdate msg2 = new PdpUpdate(update);
347 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
348 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
349 msg2.setPolicies(policies);
351 assertFalse(data.isSameContent(msg2));
355 public void testIsSameContent_DiffPolicies_NotNull_Null() {
356 PdpUpdate msg2 = new PdpUpdate(update);
357 msg2.setPolicies(null);
359 assertFalse(data.isSameContent(msg2));
363 public void testIsSameContent_DiffPolicies_Null_NotNull() {
364 PdpUpdate msg2 = new PdpUpdate(update);
366 update.setPolicies(null);
368 assertFalse(data.isSameContent(msg2));
371 @SuppressWarnings("unchecked")
372 private void verifyResponse() {
373 verify(notifier).processResponse(any(), any(), any(Set.class), any(Set.class));
376 @SuppressWarnings("unchecked")
377 private void verifyNoResponse() {
378 verify(notifier, never()).processResponse(any(), any(), any(Set.class), any(Set.class));
382 * Makes an update message.
384 * @return a new update message
386 private PdpUpdate makeUpdate() {
387 PdpUpdate upd = new PdpUpdate();
389 upd.setDescription("update-description");
390 upd.setName(MY_NAME);
391 upd.setPdpGroup(MY_GROUP);
392 upd.setPdpSubgroup(MY_SUBGROUP);
394 ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
395 ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
397 upd.setPolicies(Arrays.asList(policy1, policy2));