306418c44526f9a7033d3d1e7793429066495660
[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 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.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;
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.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 public 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     @Before
64     public void setUp() throws Exception {
65         super.setUp();
66
67         response = new PdpStatus();
68
69         update = makeUpdate();
70
71         response.setName(MY_NAME);
72         response.setPdpGroup(update.getPdpGroup());
73         response.setPdpSubgroup(update.getPdpSubgroup());
74         response.setPolicies(
75                         update.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
76                                 .collect(Collectors.toList()));
77
78         data = new UpdateReq(reqParams, MY_REQ_NAME, update);
79         data.setNotifier(notifier);
80     }
81
82     @Test
83     public void testGetMessage() {
84         assertEquals(MY_REQ_NAME, data.getName());
85         assertSame(update, data.getMessage());
86     }
87
88     @Test
89     public void testCheckResponse() {
90         assertNull(data.checkResponse(response));
91         assertTrue(data.getUndeployPolicies().isEmpty());
92         verifyResponse();
93
94         // both policy lists null
95         update.setPoliciesToBeDeployed(null);
96         response.setPolicies(null);
97         assertNull(data.checkResponse(response));
98         assertTrue(data.getUndeployPolicies().isEmpty());
99     }
100
101     @Test
102     public void testCheckResponse_NullName() {
103         response.setName(null);
104
105         assertEquals("null PDP name", data.checkResponse(response));
106         assertTrue(data.getUndeployPolicies().isEmpty());
107         verifyNoResponse();
108     }
109
110     @Test
111     public void testCheckResponse_NullMsgName() {
112         update.setName(null);
113
114         assertEquals(null, data.checkResponse(response));
115         assertTrue(data.getUndeployPolicies().isEmpty());
116         verifyResponse();
117     }
118
119     @Test
120     public void testUpdateReqCheckResponse_MismatchedGroup() {
121         response.setPdpGroup(DIFFERENT);
122
123         assertEquals("group does not match", data.checkResponse(response));
124         assertTrue(data.getUndeployPolicies().isEmpty());
125         verifyNoResponse();
126     }
127
128     @Test
129     public void testUpdateReqCheckResponse_MismatchedSubGroup() {
130         response.setPdpSubgroup(DIFFERENT);
131
132         assertEquals("subgroup does not match", data.checkResponse(response));
133         assertTrue(data.getUndeployPolicies().isEmpty());
134         verifyNoResponse();
135     }
136
137     @Test
138     public void testCheckResponse_NullSubGroup() {
139         update.setPdpSubgroup(null);
140         response.setPdpSubgroup(null);
141
142         // different policy list - should have no impact
143         response.setPolicies(Collections.emptyList());
144
145         assertEquals(null, data.checkResponse(response));
146         assertTrue(data.getUndeployPolicies().isEmpty());
147         verifyNoResponse();
148     }
149
150     @Test
151     public void testUpdateReqCheckResponse_MismatchedPolicies() {
152         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
153         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
154
155         response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
156
157         assertEquals("policies do not match", data.checkResponse(response));
158         verifyResponse();
159
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());
163     }
164
165     @Test
166     public void testUpdateReqCheckResponse_MismatchedPolicies_Null_NotNull() {
167         update.setPoliciesToBeDeployed(null);
168
169         assertEquals(null, data.checkResponse(response));
170         assertTrue(data.getUndeployPolicies().isEmpty());
171         verifyResponse();
172     }
173
174     @Test
175     public void testUpdateReqCheckResponse_MismatchedPolicies_NotNull_Null() {
176         response.setPolicies(null);
177
178         assertEquals("policies do not match", data.checkResponse(response));
179         verifyResponse();
180
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());
185     }
186
187     @Test
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());
192
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());
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
204     @Test
205     public void testReconfigureIsFullSameAsDeployList() {
206         PdpUpdate msg2 = new PdpUpdate(update);
207         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
208
209         msg2.setPoliciesToBeDeployed(policies);
210         assertTrue(data.reconfigure(msg2));
211         assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
212     }
213
214     @Test
215     public void testListsNewVsResult() {
216         PdpUpdate msg2 = new PdpUpdate(update);
217         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
218
219         // some items in both deploy and newMessage.deploy
220         msg2.setPoliciesToBeDeployed(policies);
221         policies.remove(0);
222         data.getMessage().setPoliciesToBeDeployed(policies);
223         assertTrue(data.reconfigure(msg2));
224         assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
225
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);
231
232         policies.clear();
233         policies = new ArrayList<>(update.getPoliciesToBeDeployed());
234         List<ToscaConceptIdentifier> polsToUndep = policies.parallelStream()
235                 .map(ToscaPolicy::getIdentifier)
236                 .collect(Collectors.toList());
237         msg2.setPoliciesToBeUndeployed(polsToUndep);
238
239         assertTrue(data.reconfigure(msg2));
240
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());
247
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());
256
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);
263
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);
268
269         assertTrue(data.reconfigure(msg2));
270
271         List<ToscaConceptIdentifier> dataPols2 = data.getMessage().getPoliciesToBeDeployed().stream()
272                 .map(ToscaPolicy::getIdentifier)
273                 .collect(Collectors.toList());
274         assertThat(data.getMessage().getPoliciesToBeUndeployed())
275                 .doesNotContainAnyElementsOf(dataPols2);
276
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());
284     }
285
286     @Test
287     public void testIsSameContent() {
288         data = new UpdateReq(reqParams, MY_REQ_NAME, update);
289         data.setNotifier(notifier);
290
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();
299
300         // both policy lists null
301         msg2.setPoliciesToBeDeployed(null);
302         assertEquals(data.getMessage().getPoliciesToBeDeployed(), msg2.getPoliciesToBeDeployed());
303     }
304
305     @Test
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());
311     }
312
313     @Test
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());
319     }
320
321     @Test
322     public void testIsSameContent_DiffGroup() {
323         PdpUpdate msg2 = new PdpUpdate(update);
324         msg2.setPdpGroup(null);
325         assertFalse(data.isSameContent(msg2));
326
327         msg2.setPdpGroup(DIFFERENT);
328         assertFalse(data.isSameContent(msg2));
329
330         update.setPdpGroup(null);
331         assertFalse(data.isSameContent(msg2));
332     }
333
334     @Test
335     public void testIsSameContent_DiffSubGroup() {
336         PdpUpdate msg2 = new PdpUpdate(update);
337         msg2.setPdpSubgroup(null);
338         assertFalse(data.isSameContent(msg2));
339
340         msg2.setPdpSubgroup(DIFFERENT);
341         assertFalse(data.isSameContent(msg2));
342
343         update.setPdpSubgroup(null);
344         assertFalse(data.isSameContent(msg2));
345     }
346
347     @Test
348     public void testIsSameContent_DiffPolicies() {
349         PdpUpdate msg2 = new PdpUpdate(update);
350
351         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPoliciesToBeDeployed());
352         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
353         msg2.setPoliciesToBeDeployed(policies);
354
355         assertFalse(data.isSameContent(msg2));
356     }
357
358     @Test
359     public void testIsSameContent_DiffPolicies_NotNull_Null() {
360         PdpUpdate msg2 = new PdpUpdate(update);
361         msg2.setPoliciesToBeDeployed(null);
362
363         assertFalse(data.isSameContent(msg2));
364     }
365
366     @Test
367     public void testIsSameContent_DiffPolicies_Null_NotNull() {
368         final PdpUpdate msg2 = new PdpUpdate(update);
369
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);
375
376         assertThat(data.isSameContent(msg2)).isFalse();
377     }
378
379     @SuppressWarnings("unchecked")
380     private void verifyResponse() {
381         verify(notifier).processResponse(any(), any(), any(Set.class), any(Set.class));
382     }
383
384     @SuppressWarnings("unchecked")
385     private void verifyNoResponse() {
386         verify(notifier, never()).processResponse(any(), any(), any(Set.class), any(Set.class));
387     }
388
389     /**
390      * Makes an update message.
391      *
392      * @return a new update message
393      */
394     private PdpUpdate makeUpdate() {
395         PdpUpdate upd = new PdpUpdate();
396
397         upd.setDescription("update-description");
398         upd.setName(MY_NAME);
399         upd.setPdpGroup(MY_GROUP);
400         upd.setPdpSubgroup(MY_SUBGROUP);
401
402         ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
403         ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
404
405         upd.setPoliciesToBeDeployed(Arrays.asList(policy1, policy2));
406         upd.setPoliciesToBeUndeployed(Collections.emptyList());
407
408         return upd;
409     }
410 }