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