53e78e52f820251b746844012dfc0f10e8262aa2
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / msgdata / UpdateReqTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.pap.main.comm.msgdata;
23
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;
34
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;
40 import java.util.Set;
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;
53
54 @RunWith(MockitoJUnitRunner.class)
55 public class UpdateReqTest extends CommonRequestBase {
56
57     private UpdateReq data;
58     private PdpUpdate update;
59     private PdpStatus response;
60
61     /**
62      * Sets up.
63      *
64      * @throws Exception if an error occurs
65      */
66     @Override
67     @Before
68     public void setUp() throws Exception {
69         super.setUp();
70
71         response = new PdpStatus();
72
73         update = makeUpdate();
74
75         response.setName(MY_NAME);
76         response.setPdpGroup(update.getPdpGroup());
77         response.setPdpSubgroup(update.getPdpSubgroup());
78         response.setPolicies(
79                         update.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
80                                 .collect(Collectors.toList()));
81
82         data = new UpdateReq(reqParams, MY_REQ_NAME, update);
83         data.setNotifier(notifier);
84     }
85
86     @Test
87     public void testGetMessage() {
88         assertEquals(MY_REQ_NAME, data.getName());
89         assertSame(update, data.getMessage());
90     }
91
92     @Test
93     public void testCheckResponse() {
94         assertNull(data.checkResponse(response));
95         assertTrue(data.getUndeployPolicies().isEmpty());
96         verifyResponse();
97
98         // both policy lists null
99         update.setPoliciesToBeDeployed(null);
100         response.setPolicies(null);
101         assertNull(data.checkResponse(response));
102         assertTrue(data.getUndeployPolicies().isEmpty());
103     }
104
105     @Test
106     public void testCheckResponse_NullName() {
107         response.setName(null);
108
109         assertEquals("null PDP name", data.checkResponse(response));
110         assertTrue(data.getUndeployPolicies().isEmpty());
111         verifyNoResponse();
112     }
113
114     @Test
115     public void testCheckResponse_NullMsgName() {
116         update.setName(null);
117
118         assertEquals(null, data.checkResponse(response));
119         assertTrue(data.getUndeployPolicies().isEmpty());
120         verifyResponse();
121     }
122
123     @Test
124     public void testUpdateReqCheckResponse_MismatchedGroup() {
125         response.setPdpGroup(DIFFERENT);
126
127         assertEquals("group does not match", data.checkResponse(response));
128         assertTrue(data.getUndeployPolicies().isEmpty());
129         verifyNoResponse();
130     }
131
132     @Test
133     public void testUpdateReqCheckResponse_MismatchedSubGroup() {
134         response.setPdpSubgroup(DIFFERENT);
135
136         assertEquals("subgroup does not match", data.checkResponse(response));
137         assertTrue(data.getUndeployPolicies().isEmpty());
138         verifyNoResponse();
139     }
140
141     @Test
142     public void testCheckResponse_NullSubGroup() {
143         update.setPdpSubgroup(null);
144         response.setPdpSubgroup(null);
145
146         // different policy list - should have no impact
147         response.setPolicies(Collections.emptyList());
148
149         assertEquals(null, data.checkResponse(response));
150         assertTrue(data.getUndeployPolicies().isEmpty());
151         verifyNoResponse();
152     }
153
154     @Test
155     public void testUpdateReqCheckResponse_MismatchedPolicies() {
156         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
157         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
158
159         response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
160
161         assertEquals("policies do not match", data.checkResponse(response));
162         verifyResponse();
163
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());
167     }
168
169     @Test
170     public void testUpdateReqCheckResponse_MismatchedPolicies_Null_NotNull() {
171         update.setPoliciesToBeDeployed(null);
172
173         assertEquals(null, data.checkResponse(response));
174         assertTrue(data.getUndeployPolicies().isEmpty());
175         verifyResponse();
176     }
177
178     @Test
179     public void testUpdateReqCheckResponse_MismatchedPolicies_NotNull_Null() {
180         response.setPolicies(null);
181
182         assertEquals("policies do not match", data.checkResponse(response));
183         verifyResponse();
184
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());
189     }
190
191     @Test
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());
196
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());
201
202         // different content - should succeed and install NEW message
203         msg2.setPdpGroup(DIFFERENT);
204         assertTrue(data.reconfigure(msg2));
205         assertSame(msg2, data.getMessage());
206
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();
213
214         // both lists in the current msg (i.e., msg3) are null - should not throw an exception
215         assertThatCode(() -> data.reconfigure(update)).doesNotThrowAnyException();
216     }
217
218     @Test
219     public void testReconfigureIsFullSameAsDeployList() {
220         PdpUpdate msg2 = new PdpUpdate(update);
221         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
222
223         msg2.setPoliciesToBeDeployed(policies);
224         assertTrue(data.reconfigure(msg2));
225         assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
226     }
227
228     @Test
229     public void testListsNewVsResult() {
230         PdpUpdate msg2 = new PdpUpdate(update);
231         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
232
233         // some items in both deploy and newMessage.deploy
234         msg2.setPoliciesToBeDeployed(policies);
235         policies.remove(0);
236         data.getMessage().setPoliciesToBeDeployed(policies);
237         assertTrue(data.reconfigure(msg2));
238         assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
239
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);
245
246         policies.clear();
247         policies = new ArrayList<>(update.getPoliciesToBeDeployed());
248         List<ToscaConceptIdentifier> polsToUndep = policies.parallelStream()
249                 .map(ToscaPolicy::getIdentifier)
250                 .collect(Collectors.toList());
251         msg2.setPoliciesToBeUndeployed(polsToUndep);
252
253         assertTrue(data.reconfigure(msg2));
254
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());
261
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());
270
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);
277
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);
282
283         assertTrue(data.reconfigure(msg2));
284
285         List<ToscaConceptIdentifier> dataPols2 = data.getMessage().getPoliciesToBeDeployed().stream()
286                 .map(ToscaPolicy::getIdentifier)
287                 .collect(Collectors.toList());
288         assertThat(data.getMessage().getPoliciesToBeUndeployed())
289                 .doesNotContainAnyElementsOf(dataPols2);
290
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());
298     }
299
300     @Test
301     public void testIsSameContent() {
302         data = new UpdateReq(reqParams, MY_REQ_NAME, update);
303         data.setNotifier(notifier);
304
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();
313
314         // both policy lists null
315         msg2.setPoliciesToBeDeployed(null);
316         assertEquals(data.getMessage().getPoliciesToBeDeployed(), msg2.getPoliciesToBeDeployed());
317     }
318
319     @Test
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());
325     }
326
327     @Test
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());
333     }
334
335     @Test
336     public void testIsSameContent_DiffGroup() {
337         PdpUpdate msg2 = new PdpUpdate(update);
338         msg2.setPdpGroup(null);
339         assertFalse(data.isSameContent(msg2));
340
341         msg2.setPdpGroup(DIFFERENT);
342         assertFalse(data.isSameContent(msg2));
343
344         update.setPdpGroup(null);
345         assertFalse(data.isSameContent(msg2));
346     }
347
348     @Test
349     public void testIsSameContent_DiffSubGroup() {
350         PdpUpdate msg2 = new PdpUpdate(update);
351         msg2.setPdpSubgroup(null);
352         assertFalse(data.isSameContent(msg2));
353
354         msg2.setPdpSubgroup(DIFFERENT);
355         assertFalse(data.isSameContent(msg2));
356
357         update.setPdpSubgroup(null);
358         assertFalse(data.isSameContent(msg2));
359     }
360
361     @Test
362     public void testIsSameContent_DiffPolicies() {
363         PdpUpdate msg2 = new PdpUpdate(update);
364
365         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
366         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
367         msg2.setPoliciesToBeDeployed(policies);
368
369         assertFalse(data.isSameContent(msg2));
370     }
371
372     @Test
373     public void testIsSameContent_DiffPolicies_NotNull_Null() {
374         PdpUpdate msg2 = new PdpUpdate(update);
375         msg2.setPoliciesToBeDeployed(null);
376
377         assertFalse(data.isSameContent(msg2));
378     }
379
380     @Test
381     public void testIsSameContent_DiffPolicies_Null_NotNull() {
382         final PdpUpdate msg2 = new PdpUpdate(update);
383
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);
389
390         assertThat(data.isSameContent(msg2)).isFalse();
391     }
392
393     @SuppressWarnings("unchecked")
394     private void verifyResponse() {
395         verify(notifier).processResponse(any(), any(), any(Set.class), any(Set.class));
396     }
397
398     @SuppressWarnings("unchecked")
399     private void verifyNoResponse() {
400         verify(notifier, never()).processResponse(any(), any(), any(Set.class), any(Set.class));
401     }
402
403     /**
404      * Makes an update message.
405      *
406      * @return a new update message
407      */
408     private PdpUpdate makeUpdate() {
409         PdpUpdate upd = new PdpUpdate();
410
411         upd.setDescription("update-description");
412         upd.setName(MY_NAME);
413         upd.setPdpGroup(MY_GROUP);
414         upd.setPdpSubgroup(MY_SUBGROUP);
415
416         ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
417         ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
418
419         upd.setPoliciesToBeDeployed(Arrays.asList(policy1, policy2));
420         upd.setPoliciesToBeUndeployed(Collections.emptyList());
421
422         return upd;
423     }
424 }