Fix sonars in policy-pap
[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  * ================================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.main.comm.msgdata;
22
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;
33
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;
39 import java.util.Set;
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;
50
51 public class UpdateReqTest extends CommonRequestBase {
52
53     private UpdateReq data;
54     private PdpUpdate update;
55     private PdpStatus response;
56
57     /**
58      * Sets up.
59      *
60      * @throws Exception if an error occurs
61      */
62     @Before
63     public void setUp() throws Exception {
64         super.setUp();
65
66         response = new PdpStatus();
67
68         update = makeUpdate();
69
70         response.setName(MY_NAME);
71         response.setPdpGroup(update.getPdpGroup());
72         response.setPdpSubgroup(update.getPdpSubgroup());
73         response.setPolicies(
74                         update.getPolicies().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
75
76         data = new UpdateReq(reqParams, MY_REQ_NAME, update);
77         data.setNotifier(notifier);
78     }
79
80     @Test
81     public void testGetMessage() {
82         assertEquals(MY_REQ_NAME, data.getName());
83         assertSame(update, data.getMessage());
84     }
85
86     @Test
87     public void testCheckResponse() {
88         assertNull(data.checkResponse(response));
89         assertTrue(data.getUndeployPolicies().isEmpty());
90         verifyResponse();
91
92         // both policy lists null
93         update.setPolicies(null);
94         response.setPolicies(null);
95         assertNull(data.checkResponse(response));
96         assertTrue(data.getUndeployPolicies().isEmpty());
97     }
98
99     @Test
100     public void testCheckResponse_NullName() {
101         response.setName(null);
102
103         assertEquals("null PDP name", data.checkResponse(response));
104         assertTrue(data.getUndeployPolicies().isEmpty());
105         verifyNoResponse();
106     }
107
108     @Test
109     public void testCheckResponse_NullMsgName() {
110         update.setName(null);
111
112         assertEquals(null, data.checkResponse(response));
113         assertTrue(data.getUndeployPolicies().isEmpty());
114         verifyResponse();
115     }
116
117     @Test
118     public void testUpdateReqCheckResponse_MismatchedGroup() {
119         response.setPdpGroup(DIFFERENT);
120
121         assertEquals("group does not match", data.checkResponse(response));
122         assertTrue(data.getUndeployPolicies().isEmpty());
123         verifyNoResponse();
124     }
125
126     @Test
127     public void testUpdateReqCheckResponse_MismatchedSubGroup() {
128         response.setPdpSubgroup(DIFFERENT);
129
130         assertEquals("subgroup does not match", data.checkResponse(response));
131         assertTrue(data.getUndeployPolicies().isEmpty());
132         verifyNoResponse();
133     }
134
135     @Test
136     public void testCheckResponse_NullSubGroup() {
137         update.setPdpSubgroup(null);
138         response.setPdpSubgroup(null);
139
140         // different policy list - should have no impact
141         response.setPolicies(Collections.emptyList());
142
143         assertEquals(null, data.checkResponse(response));
144         assertTrue(data.getUndeployPolicies().isEmpty());
145         verifyNoResponse();
146     }
147
148     @Test
149     public void testUpdateReqCheckResponse_MismatchedPolicies() {
150         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
151         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
152
153         response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
154
155         assertEquals("policies do not match", data.checkResponse(response));
156         verifyResponse();
157
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());
161     }
162
163     @Test
164     public void testUpdateReqCheckResponse_MismatchedPolicies_Null_NotNull() {
165         update.setPolicies(null);
166
167         assertEquals("policies do not match", data.checkResponse(response));
168         assertTrue(data.getUndeployPolicies().isEmpty());
169         verifyResponse();
170     }
171
172     @Test
173     public void testUpdateReqCheckResponse_MismatchedPolicies_NotNull_Null() {
174         response.setPolicies(null);
175
176         assertEquals("policies do not match", data.checkResponse(response));
177         verifyResponse();
178
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());
182     }
183
184     @Test
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());
189
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());
194
195         // different content - should succeed and install NEW message
196         msg2.setPdpGroup(DIFFERENT);
197         assertTrue(data.reconfigure(msg2));
198         assertSame(msg2, data.getMessage());
199     }
200
201     @Test
202     public void testReconfigureIsFullSameAsDeployList() {
203         PdpUpdate msg2 = new PdpUpdate(update);
204         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
205
206         msg2.setPolicies(policies);
207         msg2.setPoliciesToBeDeployed(policies);
208         assertTrue(data.reconfigure(msg2));
209         assertThat(data.getMessage().getPolicies()).containsAll(msg2.getPolicies());
210     }
211
212     @Test
213     public void testListsNewVsResult() {
214         PdpUpdate msg2 = new PdpUpdate(update);
215         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
216
217         // some items in both deploy and newMessage.deploy
218         msg2.setPoliciesToBeDeployed(policies);
219         policies.remove(0);
220         data.getMessage().setPolicies(policies);
221         assertTrue(data.reconfigure(msg2));
222         assertThat(data.getMessage().getPoliciesToBeDeployed()).containsAll(msg2.getPoliciesToBeDeployed());
223
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);
229
230         policies.clear();
231         policies = new ArrayList<>(update.getPolicies());
232         List<ToscaConceptIdentifier> polsToUndep = policies.parallelStream()
233                 .map(ToscaPolicy::getIdentifier)
234                 .collect(Collectors.toList());
235         msg2.setPoliciesToBeUndeployed(polsToUndep);
236
237         assertTrue(data.reconfigure(msg2));
238
239         assertThat(data.getMessage().getPoliciesToBeDeployed()).isEmpty();
240
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());
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.remove(0);
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.getPolicies());
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         assertFalse(data.isSameContent(msg2));
294
295         // both policy lists null
296         update.setPolicies(null);
297         msg2.setPolicies(null);
298         assertEquals(data.getMessage().getPolicies(), msg2.getPolicies());
299     }
300
301     @Test
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());
307     }
308
309     @Test
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());
315     }
316
317     @Test
318     public void testIsSameContent_DiffGroup() {
319         PdpUpdate msg2 = new PdpUpdate(update);
320         msg2.setPdpGroup(null);
321         assertFalse(data.isSameContent(msg2));
322
323         msg2.setPdpGroup(DIFFERENT);
324         assertFalse(data.isSameContent(msg2));
325
326         update.setPdpGroup(null);
327         assertFalse(data.isSameContent(msg2));
328     }
329
330     @Test
331     public void testIsSameContent_DiffSubGroup() {
332         PdpUpdate msg2 = new PdpUpdate(update);
333         msg2.setPdpSubgroup(null);
334         assertFalse(data.isSameContent(msg2));
335
336         msg2.setPdpSubgroup(DIFFERENT);
337         assertFalse(data.isSameContent(msg2));
338
339         update.setPdpSubgroup(null);
340         assertFalse(data.isSameContent(msg2));
341     }
342
343     @Test
344     public void testIsSameContent_DiffPolicies() {
345         PdpUpdate msg2 = new PdpUpdate(update);
346
347         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
348         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
349         msg2.setPolicies(policies);
350
351         assertFalse(data.isSameContent(msg2));
352     }
353
354     @Test
355     public void testIsSameContent_DiffPolicies_NotNull_Null() {
356         PdpUpdate msg2 = new PdpUpdate(update);
357         msg2.setPolicies(null);
358
359         assertFalse(data.isSameContent(msg2));
360     }
361
362     @Test
363     public void testIsSameContent_DiffPolicies_Null_NotNull() {
364         PdpUpdate msg2 = new PdpUpdate(update);
365
366         update.setPolicies(null);
367
368         assertFalse(data.isSameContent(msg2));
369     }
370
371     @SuppressWarnings("unchecked")
372     private void verifyResponse() {
373         verify(notifier).processResponse(any(), any(), any(Set.class), any(Set.class));
374     }
375
376     @SuppressWarnings("unchecked")
377     private void verifyNoResponse() {
378         verify(notifier, never()).processResponse(any(), any(), any(Set.class), any(Set.class));
379     }
380
381     /**
382      * Makes an update message.
383      *
384      * @return a new update message
385      */
386     private PdpUpdate makeUpdate() {
387         PdpUpdate upd = new PdpUpdate();
388
389         upd.setDescription("update-description");
390         upd.setName(MY_NAME);
391         upd.setPdpGroup(MY_GROUP);
392         upd.setPdpSubgroup(MY_SUBGROUP);
393
394         ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
395         ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
396
397         upd.setPolicies(Arrays.asList(policy1, policy2));
398
399         return upd;
400     }
401 }