Java 17 / Spring 6 / Spring Boot 3 Upgrade
[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.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertSame;
30 import static org.junit.jupiter.api.Assertions.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.jupiter.api.BeforeEach;
44 import org.junit.jupiter.api.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;
51
52 class UpdateReqTest extends CommonRequestBase {
53
54     private UpdateReq data;
55     private PdpUpdate update;
56     private PdpStatus response;
57
58     /**
59      * Sets up.
60      *
61      * @throws Exception if an error occurs
62      */
63     @Override
64     @BeforeEach
65     public void setUp() throws Exception {
66         super.setUp();
67
68         response = new PdpStatus();
69
70         update = makeUpdate();
71
72         response.setName(MY_NAME);
73         response.setPdpGroup(update.getPdpGroup());
74         response.setPdpSubgroup(update.getPdpSubgroup());
75         response.setPolicies(
76             update.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
77                 .collect(Collectors.toList()));
78
79         data = new UpdateReq(reqParams, MY_REQ_NAME, update);
80         data.setNotifier(notifier);
81     }
82
83     @Test
84     void testGetMessage() {
85         assertEquals(MY_REQ_NAME, data.getName());
86         assertSame(update, data.getMessage());
87     }
88
89     @Test
90     void testCheckResponse() {
91         assertNull(data.checkResponse(response));
92         assertTrue(data.getUndeployPolicies().isEmpty());
93         verifyResponse();
94
95         // both policy lists null
96         update.setPoliciesToBeDeployed(null);
97         response.setPolicies(null);
98         assertNull(data.checkResponse(response));
99         assertTrue(data.getUndeployPolicies().isEmpty());
100     }
101
102     @Test
103     void testCheckResponse_NullName() {
104         response.setName(null);
105
106         assertEquals("null PDP name", data.checkResponse(response));
107         assertTrue(data.getUndeployPolicies().isEmpty());
108         verifyNoResponse();
109     }
110
111     @Test
112     void testCheckResponse_NullMsgName() {
113         update.setName(null);
114
115         assertNull(data.checkResponse(response));
116         assertTrue(data.getUndeployPolicies().isEmpty());
117         verifyResponse();
118     }
119
120     @Test
121     void testUpdateReqCheckResponse_MismatchedGroup() {
122         response.setPdpGroup(DIFFERENT);
123
124         assertEquals("group does not match", data.checkResponse(response));
125         assertTrue(data.getUndeployPolicies().isEmpty());
126         verifyNoResponse();
127     }
128
129     @Test
130     void testUpdateReqCheckResponse_MismatchedSubGroup() {
131         response.setPdpSubgroup(DIFFERENT);
132
133         assertEquals("subgroup does not match", data.checkResponse(response));
134         assertTrue(data.getUndeployPolicies().isEmpty());
135         verifyNoResponse();
136     }
137
138     @Test
139     void testCheckResponse_NullSubGroup() {
140         update.setPdpSubgroup(null);
141         response.setPdpSubgroup(null);
142
143         // different policy list - should have no impact
144         response.setPolicies(Collections.emptyList());
145
146         assertNull(data.checkResponse(response));
147         assertTrue(data.getUndeployPolicies().isEmpty());
148         verifyNoResponse();
149     }
150
151     @Test
152     void testUpdateReqCheckResponse_MismatchedPolicies() {
153         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
154         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
155
156         response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
157
158         assertEquals("policies do not match", data.checkResponse(response));
159         verifyResponse();
160
161         // the first policy from the original update is all that should be undeployed
162         assertEquals(Collections.singleton(update.getPoliciesToBeDeployed().get(0).getIdentifier()).toString(),
163             data.getUndeployPolicies().toString());
164     }
165
166     @Test
167     void testUpdateReqCheckResponse_MismatchedPolicies_Null_NotNull() {
168         update.setPoliciesToBeDeployed(null);
169
170         assertNull(data.checkResponse(response));
171         assertTrue(data.getUndeployPolicies().isEmpty());
172         verifyResponse();
173     }
174
175     @Test
176     void testUpdateReqCheckResponse_MismatchedPolicies_NotNull_Null() {
177         response.setPolicies(null);
178
179         assertEquals("policies do not match", data.checkResponse(response));
180         verifyResponse();
181
182         // all policies in the update should be undeployed
183         assertEquals(update.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
184             .toList().toString(), new TreeSet<>(data.getUndeployPolicies()).toString());
185     }
186
187     @Test
188     void testReconfigure() {
189         // different message type should fail and leave message unchanged
190         assertFalse(data.reconfigure(new PdpStateChange()));
191         assertSame(update, data.getMessage());
192
193         // same content - should succeed, but leave message unchanged
194         PdpUpdate msg2 = new PdpUpdate(update);
195         assertTrue(data.reconfigure(msg2));
196         assertSame(update, data.getMessage());
197
198         // different content - should succeed and install NEW message
199         msg2.setPdpGroup(DIFFERENT);
200         assertTrue(data.reconfigure(msg2));
201         assertSame(msg2, data.getMessage());
202
203         // both lists in the update are null - should not throw an exception
204         PdpUpdate msg3 = new PdpUpdate(update);
205         msg3.setPdpGroup(DIFFERENT);
206         msg3.setPoliciesToBeDeployed(null);
207         msg3.setPoliciesToBeUndeployed(null);
208         assertThatCode(() -> data.reconfigure(msg3)).doesNotThrowAnyException();
209
210         // both lists in the current msg (i.e., msg3) are null - should not throw an exception
211         assertThatCode(() -> data.reconfigure(update)).doesNotThrowAnyException();
212     }
213
214     @Test
215     void testReconfigureIsFullSameAsDeployList() {
216         PdpUpdate msg2 = new PdpUpdate(update);
217         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
218
219         msg2.setPoliciesToBeDeployed(policies);
220         assertTrue(data.reconfigure(msg2));
221         assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
222     }
223
224     @Test
225     void testListsNewVsResult() {
226         PdpUpdate msg2 = new PdpUpdate(update);
227         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
228
229         // some items in both deploy and newMessage.deploy
230         msg2.setPoliciesToBeDeployed(policies);
231         policies.remove(0);
232         data.getMessage().setPoliciesToBeDeployed(policies);
233         assertTrue(data.reconfigure(msg2));
234         assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
235
236         // some items in both deploy and newMessage.undeploy
237         policies = new ArrayList<>();
238         policies.add(makePolicy("policy-z-1", "1.0.0"));
239         policies.add(makePolicy("policy-y-1", "1.0.0"));
240         data.getMessage().setPoliciesToBeDeployed(policies);
241
242         policies.clear();
243         policies = new ArrayList<>(update.getPoliciesToBeDeployed());
244         List<ToscaConceptIdentifier> polsToUndep = policies.parallelStream()
245             .map(ToscaPolicy::getIdentifier)
246             .collect(Collectors.toList());
247         msg2.setPoliciesToBeUndeployed(polsToUndep);
248
249         assertTrue(data.reconfigure(msg2));
250
251         // some items only in deploy
252         policies = new ArrayList<>(update.getPoliciesToBeDeployed());
253         msg2.setPoliciesToBeDeployed(policies);
254         data.getMessage().setPoliciesToBeDeployed(new LinkedList<>());
255         assertTrue(data.reconfigure(msg2));
256         assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
257
258         // some items in both undeploy and newMessage.undeploy
259         List<ToscaConceptIdentifier> pols = policies.stream().map(ToscaPolicy::getIdentifier)
260             .collect(Collectors.toList());
261         msg2.setPoliciesToBeUndeployed(pols);
262         pols.add(makePolicy("policy-zz-1", "1.1.0").getIdentifier());
263         data.getMessage().setPoliciesToBeUndeployed(pols);
264         assertTrue(data.reconfigure(msg2));
265         assertThat(data.getMessage().getPoliciesToBeUndeployed()).containsAll(msg2.getPoliciesToBeUndeployed());
266
267         // some items in both undeploy and newMessage.deploy
268         policies = new ArrayList<>(update.getPoliciesToBeDeployed());
269         List<ToscaConceptIdentifier> polsToUndep2 = policies.parallelStream()
270             .map(ToscaPolicy::getIdentifier)
271             .collect(Collectors.toList());
272         data.getMessage().setPoliciesToBeUndeployed(polsToUndep2);
273
274         List<ToscaPolicy> polsToDep2 = new LinkedList<>();
275         polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
276         polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
277         msg2.setPoliciesToBeDeployed(polsToDep2);
278
279         assertTrue(data.reconfigure(msg2));
280
281         List<ToscaConceptIdentifier> dataPols2 = data.getMessage().getPoliciesToBeDeployed().stream()
282             .map(ToscaPolicy::getIdentifier)
283             .collect(Collectors.toList());
284         assertThat(data.getMessage().getPoliciesToBeUndeployed())
285             .doesNotContainAnyElementsOf(dataPols2);
286
287         // some items only in undeploy
288         pols = policies.stream().map(ToscaPolicy::getIdentifier)
289             .collect(Collectors.toList());
290         msg2.setPoliciesToBeUndeployed(pols);
291         data.getMessage().setPoliciesToBeUndeployed(new LinkedList<>());
292         assertTrue(data.reconfigure(msg2));
293         assertThat(data.getMessage().getPoliciesToBeUndeployed()).containsAll(msg2.getPoliciesToBeUndeployed());
294     }
295
296     @Test
297     void testIsSameContent() {
298         data = new UpdateReq(reqParams, MY_REQ_NAME, update);
299         data.setNotifier(notifier);
300
301         PdpUpdate msg2 = new PdpUpdate(update);
302         msg2.setName("world");
303         update.setPoliciesToBeDeployed(null);
304         List<ToscaPolicy> polsToDep2 = new LinkedList<>();
305         polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
306         polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
307         msg2.setPoliciesToBeDeployed(polsToDep2);
308         assertThat(data.isSameContent(msg2)).isFalse();
309
310         // both policy lists null
311         msg2.setPoliciesToBeDeployed(null);
312         assertEquals(data.getMessage().getPoliciesToBeDeployed(), msg2.getPoliciesToBeDeployed());
313     }
314
315     @Test
316     void testIsSameContent_BothGroupNamesNull() {
317         PdpUpdate msg2 = new PdpUpdate(update);
318         msg2.setPdpGroup(null);
319         update.setPdpGroup(null);
320         assertEquals(data.getMessage().getPdpGroup(), msg2.getPdpGroup());
321     }
322
323     @Test
324     void testIsSameContent_BothSubGroupNamesNull() {
325         PdpUpdate msg2 = new PdpUpdate(update);
326         msg2.setPdpSubgroup(null);
327         update.setPdpSubgroup(null);
328         assertEquals(data.getMessage().getPdpSubgroup(), msg2.getPdpSubgroup());
329     }
330
331     @Test
332     void testIsSameContent_DiffGroup() {
333         PdpUpdate msg2 = new PdpUpdate(update);
334         msg2.setPdpGroup(null);
335         assertFalse(data.isSameContent(msg2));
336
337         msg2.setPdpGroup(DIFFERENT);
338         assertFalse(data.isSameContent(msg2));
339
340         update.setPdpGroup(null);
341         assertFalse(data.isSameContent(msg2));
342     }
343
344     @Test
345     void testIsSameContent_DiffSubGroup() {
346         PdpUpdate msg2 = new PdpUpdate(update);
347         msg2.setPdpSubgroup(null);
348         assertFalse(data.isSameContent(msg2));
349
350         msg2.setPdpSubgroup(DIFFERENT);
351         assertFalse(data.isSameContent(msg2));
352
353         update.setPdpSubgroup(null);
354         assertFalse(data.isSameContent(msg2));
355     }
356
357     @Test
358     void testIsSameContent_DiffPolicies() {
359         PdpUpdate msg2 = new PdpUpdate(update);
360
361         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
362         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
363         msg2.setPoliciesToBeDeployed(policies);
364
365         assertFalse(data.isSameContent(msg2));
366     }
367
368     @Test
369     void testIsSameContent_DiffPolicies_NotNull_Null() {
370         PdpUpdate msg2 = new PdpUpdate(update);
371         msg2.setPoliciesToBeDeployed(null);
372
373         assertFalse(data.isSameContent(msg2));
374     }
375
376     @Test
377     void testIsSameContent_DiffPolicies_Null_NotNull() {
378         final PdpUpdate msg2 = new PdpUpdate(update);
379
380         update.setPoliciesToBeDeployed(null);
381         List<ToscaPolicy> polsToDep2 = new LinkedList<>();
382         polsToDep2.add(makePolicy("policy-m-1", "1.0.0"));
383         polsToDep2.add(makePolicy("policy-n-1", "1.0.0"));
384         msg2.setPoliciesToBeDeployed(polsToDep2);
385
386         assertThat(data.isSameContent(msg2)).isFalse();
387     }
388
389     @SuppressWarnings("unchecked")
390     private void verifyResponse() {
391         verify(notifier).processResponse(any(), any(), any(Set.class), any(Set.class));
392     }
393
394     @SuppressWarnings("unchecked")
395     private void verifyNoResponse() {
396         verify(notifier, never()).processResponse(any(), any(), any(Set.class), any(Set.class));
397     }
398
399     /**
400      * Makes an update message.
401      *
402      * @return a new update message
403      */
404     private PdpUpdate makeUpdate() {
405         PdpUpdate upd = new PdpUpdate();
406
407         upd.setDescription("update-description");
408         upd.setName(MY_NAME);
409         upd.setPdpGroup(MY_GROUP);
410         upd.setPdpSubgroup(MY_SUBGROUP);
411
412         ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
413         ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
414
415         upd.setPoliciesToBeDeployed(Arrays.asList(policy1, policy2));
416         upd.setPoliciesToBeUndeployed(Collections.emptyList());
417
418         return upd;
419     }
420 }