Migrate pap startup & controllers to spring boot
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPdpGroupCreateOrUpdateProvider.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 Nordix Foundation.
6  * Modifications Copyright (C) 2021 AT&T Intellectual Property.
7  * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertSame;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.never;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.TreeMap;
39 import javax.ws.rs.core.Response.Status;
40 import org.assertj.core.api.Assertions;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.onap.policy.common.utils.services.Registry;
45 import org.onap.policy.models.base.PfModelException;
46 import org.onap.policy.models.pdp.concepts.PdpGroup;
47 import org.onap.policy.models.pdp.concepts.PdpGroups;
48 import org.onap.policy.models.pdp.concepts.PdpStateChange;
49 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
50 import org.onap.policy.models.pdp.concepts.PdpUpdate;
51 import org.onap.policy.models.pdp.enums.PdpState;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
53 import org.onap.policy.pap.main.PapConstants;
54
55 public class TestPdpGroupCreateOrUpdateProvider extends ProviderSuper {
56     private static final String EXPECTED_EXCEPTION = "expected exception";
57
58     private static final String PDP2 = "pdpB";
59     private static final String PDP4 = "pdpD";
60
61     private PdpGroupCreateOrUpdateProvider prov;
62
63     @AfterClass
64     public static void tearDownAfterClass() {
65         Registry.newRegistry();
66     }
67
68     /**
69      * Configures mocks and objects.
70      *
71      * @throws Exception if an error occurs
72      */
73     @Before
74     @Override
75     public void setUp() throws Exception {
76
77         super.setUp();
78
79         when(dao.getPolicyTypeList("typeA", "100.2.3")).thenReturn(Arrays.asList(loadPolicyType("daoPolicyType.json")));
80
81         prov = new PdpGroupCreateOrUpdateProvider();
82         prov.initialize();
83     }
84
85     @Test
86     public void testCreateOrUpdateGroups() throws Exception {
87         prov.createOrUpdateGroups(loadPdpGroups("emptyGroups.json"));
88
89         // no groups, so no action should have been taken
90         assertNoGroupAction();
91     }
92
93     @Test
94     public void testCreateOrUpdateGroups_InvalidRequest() throws Exception {
95         assertThatThrownBy(() -> prov.createOrUpdateGroups(new PdpGroups())).isInstanceOf(PfModelException.class)
96             .hasMessageContaining("is null");
97
98         assertNoGroupAction();
99     }
100
101     @Test
102     public void testCreateOrUpdate_Invalid() throws Exception {
103         PdpGroups groups = loadPdpGroups("createGroups.json");
104         groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED);
105
106         assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
107             .hasMessageContaining("pdpGroupState");
108
109         assertNoGroupAction();
110     }
111
112     @Test
113     public void testAddGroup() throws Exception {
114         PdpGroups groups = loadPdpGroups("createGroups.json");
115         PdpGroup group = groups.getGroups().get(0);
116         group.setPdpGroupState(PdpState.PASSIVE);
117
118         prov.createOrUpdateGroups(groups);
119
120         // should not have updated the state
121         assertEquals(PdpState.PASSIVE, group.getPdpGroupState());
122
123         assertSame(group, getGroupCreates().get(0));
124     }
125
126     @Test
127     public void testAddGroup_Invalid() throws Exception {
128         PdpGroups groups = loadPdpGroups("createGroups.json");
129         groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED);
130
131         assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
132             .hasMessageContaining("pdpGroupState");
133
134         assertNoGroupAction();
135     }
136
137     @Test
138     public void testAddGroup_InvalidSubGroup() throws Exception {
139         PdpGroups groups = loadPdpGroups("createGroups.json");
140
141         groups.getGroups().get(0).getPdpSubgroups().get(0).getSupportedPolicyTypes().get(0).setVersion("99.99.99");
142
143         assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
144             .hasMessageContaining("unknown policy type");
145
146         assertNoGroupAction();
147     }
148
149     @Test
150     public void testValidateGroupOnly_NullState() {
151         PdpGroups groups = loadPdpGroups("createGroups.json");
152         groups.getGroups().get(0).setPdpGroupState(null);
153         Assertions.assertThatCode(() -> prov.createOrUpdateGroups(groups)).doesNotThrowAnyException();
154     }
155
156     @Test
157     public void testValidateGroupOnly_Active() {
158         PdpGroups groups = loadPdpGroups("createGroups.json");
159         groups.getGroups().get(0).setPdpGroupState(PdpState.ACTIVE);
160         Assertions.assertThatCode(() -> prov.createOrUpdateGroups(groups)).doesNotThrowAnyException();
161     }
162
163     @Test
164     public void testValidateGroupOnly_Passive() {
165         PdpGroups groups = loadPdpGroups("createGroups.json");
166         groups.getGroups().get(0).setPdpGroupState(PdpState.PASSIVE);
167         Assertions.assertThatCode(() -> prov.createOrUpdateGroups(groups)).doesNotThrowAnyException();
168     }
169
170     @Test
171     public void testValidateGroupOnly_Invalid() {
172         PdpGroups groups = loadPdpGroups("createGroups.json");
173         groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED);
174
175         assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
176             .hasMessageContaining("pdpGroupState");
177     }
178
179     @Test
180     public void testUpdateGroup() throws Exception {
181         PdpGroups groups = loadPdpGroups("createGroups.json");
182
183         // DB group = new group
184         PdpGroup group = new PdpGroup(groups.getGroups().get(0));
185         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
186
187         prov.createOrUpdateGroups(groups);
188
189         assertNoGroupAction();
190     }
191
192     @Test
193     public void testUpdateGroup_PropertiesChanged() throws Exception {
194         PdpGroups groups = loadPdpGroups("createGroups.json");
195
196         PdpGroup group = new PdpGroup(groups.getGroups().get(0));
197         group.setProperties(new TreeMap<>());
198
199         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
200
201         assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
202             .hasMessageContaining("properties");
203
204         assertNoGroupAction();
205     }
206
207     @Test
208     public void testUpdateGroup_NewDescription() throws Exception {
209         PdpGroups groups = loadPdpGroups("createGroups.json");
210         PdpGroup newgrp = groups.getGroups().get(0);
211         PdpGroup group = new PdpGroup(newgrp);
212         group.setDescription("old description");
213         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
214
215         prov.createOrUpdateGroups(groups);
216
217         assertGroupUpdateOnly(group);
218
219         assertEquals("my description", group.getDescription());
220         assertEquals(newgrp.toString(), group.toString());
221     }
222
223     @Test
224     public void testUpdateGroup_NewState() throws Exception {
225         PdpGroups groups = loadPdpGroups("createGroups.json");
226         PdpGroup newgrp = groups.getGroups().get(0);
227         PdpGroup group = new PdpGroup(newgrp);
228         group.setPdpGroupState(PdpState.TEST);
229         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
230
231         prov.createOrUpdateGroups(groups);
232
233         assertGroupUpdateOnly(group);
234
235         assertEquals(PdpState.ACTIVE, group.getPdpGroupState());
236         assertEquals(newgrp.toString(), group.toString());
237     }
238
239     @Test
240     public void testUpdateGroup_NewSubGroup() throws Exception {
241         PdpGroups groups = loadPdpGroups("createGroupsNewSub.json");
242         PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0);
243         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
244
245         prov.createOrUpdateGroups(groups);
246
247         PdpGroup newgrp = groups.getGroups().get(0);
248         assertEquals(newgrp.toString(), group.toString());
249         assertGroupUpdateOnly(group);
250     }
251
252     @Test
253     public void testUpdateGroup_UpdatedSubGroup() throws Exception {
254         PdpGroups groups = loadPdpGroups("createGroups.json");
255         PdpGroup newgrp = groups.getGroups().get(0);
256         PdpGroup group = new PdpGroup(newgrp);
257         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
258
259         // something different in this subgroup
260         group.getPdpSubgroups().get(0).setDesiredInstanceCount(10);
261
262         prov.createOrUpdateGroups(groups);
263
264         assertEquals(newgrp.toString(), group.toString());
265         assertGroupUpdateOnly(group);
266     }
267
268     @Test
269     public void testUpdateGroup_notifyPdpsDelSubGroups() throws Exception {
270         PdpGroup dbgroup = new PdpGroup(loadPdpGroups("createGroupsDelSub.json").getGroups().get(0));
271         when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
272
273         PdpGroups groups = loadPdpGroups("createGroups.json");
274
275         prov.createOrUpdateGroups(groups);
276
277         // verify that DB group was updated
278         List<PdpGroup> updates = getGroupUpdates();
279         assertEquals(1, updates.size());
280         dbgroup = updates.get(0);
281
282         PdpGroup newgrp = groups.getGroups().get(0);
283
284         Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
285         Collections.sort(dbgroup.getPdpSubgroups().get(0).getPolicies());
286
287         assertEquals(newgrp.toString(), dbgroup.toString());
288
289         // no deployment notifications
290         checkEmptyNotification();
291
292         // this requires a PDP UPDATE message
293         List<PdpUpdate> pdpUpdates = getUpdateRequests(2);
294         assertEquals(2, pdpUpdates.size());
295
296         PdpUpdate pdpUpdate = pdpUpdates.get(0);
297         assertEquals(PapConstants.PAP_NAME, pdpUpdate.getSource());
298         assertEquals(PDP2, pdpUpdate.getName());
299         assertNull(pdpUpdate.getPdpGroup());
300
301         pdpUpdate = pdpUpdates.get(1);
302         assertEquals(PapConstants.PAP_NAME, pdpUpdate.getSource());
303         assertEquals(PDP4, pdpUpdate.getName());
304         assertNull(pdpUpdate.getPdpGroup());
305
306         // it also requires a PDP STATE-CHANGE message
307         List<PdpStateChange> changes = getStateChangeRequests(2);
308         assertEquals(2, changes.size());
309
310         PdpStateChange change = changes.get(0);
311         assertEquals(PapConstants.PAP_NAME, change.getSource());
312         assertEquals(PDP2, change.getName());
313         assertEquals(PdpState.PASSIVE, change.getState());
314
315         change = changes.get(1);
316         assertEquals(PapConstants.PAP_NAME, change.getSource());
317         assertEquals(PDP4, change.getName());
318         assertEquals(PdpState.PASSIVE, change.getState());
319     }
320
321     @Test
322     public void testUpdateField_Unchanged() throws Exception {
323         PdpGroups groups = loadPdpGroups("createGroups.json");
324         PdpGroup newgrp = groups.getGroups().get(0);
325         PdpGroup group = new PdpGroup(newgrp);
326         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
327
328         prov.createOrUpdateGroups(groups);
329
330         assertNoGroupAction();
331     }
332
333     @Test
334     public void testUpdateField_WasNull() throws Exception {
335         PdpGroups groups = loadPdpGroups("createGroups.json");
336         PdpGroup newgrp = groups.getGroups().get(0);
337         PdpGroup group = new PdpGroup(newgrp);
338         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
339
340         group.setDescription(null);
341
342         prov.createOrUpdateGroups(groups);
343
344         assertEquals(newgrp.toString(), group.toString());
345         assertGroupUpdateOnly(group);
346     }
347
348     @Test
349     public void testUpdateField_NowNull() throws Exception {
350         PdpGroups groups = loadPdpGroups("createGroups.json");
351         PdpGroup newgrp = groups.getGroups().get(0);
352         PdpGroup group = new PdpGroup(newgrp);
353         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
354
355         newgrp.setDescription(null);
356
357         prov.createOrUpdateGroups(groups);
358
359         assertEquals(newgrp.toString(), group.toString());
360         assertGroupUpdateOnly(group);
361     }
362
363     @Test
364     public void testUpdateField_Changed() throws Exception {
365         PdpGroups groups = loadPdpGroups("createGroups.json");
366         PdpGroup newgrp = groups.getGroups().get(0);
367         PdpGroup group = new PdpGroup(newgrp);
368         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
369
370         newgrp.setDescription(group.getDescription() + "-changed");
371
372         prov.createOrUpdateGroups(groups);
373
374         assertEquals(newgrp.toString(), group.toString());
375         assertGroupUpdateOnly(group);
376     }
377
378     @Test
379     public void testAddSubGroup() throws Exception {
380         PdpGroups groups = loadPdpGroups("createGroupsNewSub.json");
381         PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0);
382         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
383
384         prov.createOrUpdateGroups(groups);
385
386         PdpGroup newgrp = groups.getGroups().get(0);
387
388         PdpSubGroup newsub = newgrp.getPdpSubgroups().get(1);
389         newsub.setCurrentInstanceCount(0);
390         newsub.setPdpInstances(new ArrayList<>(0));
391
392         assertEquals(newgrp.toString(), group.toString());
393         assertGroupUpdateOnly(group);
394     }
395
396     /**
397      * Tests addSubgroup() when the new subgroup has a wild-card policy type.
398      *
399      * @throws Exception if an error occurs
400      */
401     @Test
402     public void testAddSubGroupWildCardPolicyType() throws Exception {
403         when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCard.json"));
404         when(dao.getPolicyTypeList("some.*", "2.3.4")).thenReturn(Collections.emptyList());
405
406         PdpGroups groups = loadPdpGroups("createGroupsWildCard.json");
407         PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0);
408         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
409
410         prov.createOrUpdateGroups(groups);
411
412         PdpGroup newgrp = groups.getGroups().get(0);
413
414         PdpSubGroup newsub = newgrp.getPdpSubgroups().get(1);
415         newsub.setCurrentInstanceCount(0);
416         newsub.setPdpInstances(new ArrayList<>(0));
417
418         assertEquals(newgrp.toString(), group.toString());
419     }
420
421     @Test
422     public void testAddSubGroup_ValidationPolicyTypeNotFound() throws Exception {
423         PdpGroups groups = loadPdpGroups("createGroupsNewSub.json");
424         PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0);
425         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
426
427         when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList());
428
429         assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).hasMessageContaining("unknown policy type");
430     }
431
432     @Test
433     public void testAddSubGroup_ValidationPolicyTypeDaoEx() throws Exception {
434         PdpGroups groups = loadPdpGroups("createGroupsNewSub.json");
435         PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0);
436         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
437
438         PfModelException exc = new PfModelException(Status.CONFLICT, EXPECTED_EXCEPTION);
439         when(dao.getPolicyTypeList(any(), any())).thenThrow(exc);
440
441         assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isSameAs(exc);
442     }
443
444     @Test
445     public void testAddSubGroup_ValidateVersionPrefixMatch() throws Exception {
446         PdpGroups groups = loadPdpGroups("createGroups.json");
447         PdpGroup newgrp = groups.getGroups().get(0);
448         PdpGroup dbgroup = new PdpGroup(newgrp);
449         when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
450
451         when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
452             .thenReturn(loadPolicies("daoPolicyList.json")).thenReturn(loadPolicies("createGroupNewPolicy.json"));
453
454         PdpGroups reqgroups = loadPdpGroups("createGroupsVersPrefix.json");
455
456         prov.createOrUpdateGroups(reqgroups);
457
458         Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
459         Collections.sort(dbgroup.getPdpSubgroups().get(0).getPolicies());
460
461         assertEquals(newgrp.toString(), dbgroup.toString());
462     }
463
464     @Test
465     public void testUpdateSubGroup_Invalid() throws Exception {
466         PdpGroups groups = loadPdpGroups("createGroups.json");
467         PdpGroup newgrp = groups.getGroups().get(0);
468         PdpGroup group = new PdpGroup(newgrp);
469         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
470
471         // change properties
472         newgrp.getPdpSubgroups().get(0).setProperties(new TreeMap<>());
473
474         assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
475             .hasMessageContaining("properties");
476
477         assertNoGroupAction();
478     }
479
480     @Test
481     public void testUpdateSubGroup_SupportedPolicies() throws Exception {
482         PdpGroups groups = loadPdpGroups("createGroups.json");
483         PdpGroup newgrp = groups.getGroups().get(0);
484         PdpGroup group = new PdpGroup(newgrp);
485         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
486
487         newgrp.getPdpSubgroups().get(0).getSupportedPolicyTypes()
488             .add(new ToscaConceptIdentifier("typeX.*", "9.8.7"));
489
490         // the group is updated with a new supported policy type in subgroup
491         assertEquals(2, newgrp.getPdpSubgroups().get(0).getSupportedPolicyTypes().size());
492         prov.createOrUpdateGroups(groups);
493         // PdpGroup update doesn't allow supported policy type modifications
494         // during pdp group update, the ones in db is maintained
495         assertEquals(1, newgrp.getPdpSubgroups().get(0).getSupportedPolicyTypes().size());
496         assertEquals(newgrp.toString(), group.toString());
497     }
498
499     @Test
500     public void testUpdateSubGroup_DesiredCount() throws Exception {
501         PdpGroups groups = loadPdpGroups("createGroups.json");
502         PdpGroup newgrp = groups.getGroups().get(0);
503         PdpGroup group = new PdpGroup(newgrp);
504         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
505
506         newgrp.getPdpSubgroups().get(0).setDesiredInstanceCount(20);
507
508         prov.createOrUpdateGroups(groups);
509
510         assertEquals(newgrp.toString(), group.toString());
511         assertGroupUpdateOnly(group);
512     }
513
514     @Test
515     public void testUpdateSubGroup_Unchanged() throws Exception {
516         PdpGroups groups = loadPdpGroups("createGroups.json");
517         PdpGroup newgrp = groups.getGroups().get(0);
518         PdpGroup group = new PdpGroup(newgrp);
519         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
520
521         prov.createOrUpdateGroups(groups);
522
523         Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
524         Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
525
526         assertEquals(newgrp.toString(), group.toString());
527
528         // no notifications
529         checkEmptyNotification();
530
531         // no group updates
532         assertNoGroupAction();
533     }
534
535     @Test
536     public void testValidateSubGroup_PropertiesMismatch() throws Exception {
537         PdpGroups groups = loadPdpGroups("createGroups.json");
538         PdpGroup newgrp = groups.getGroups().get(0);
539         PdpGroup group = new PdpGroup(newgrp);
540         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
541
542         newgrp.setProperties(new TreeMap<>());
543
544         assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
545             .hasMessageContaining("properties");
546
547         assertNoGroupAction();
548     }
549
550     private void assertNoGroupAction() throws Exception {
551         verify(dao, never()).createPdpGroups(any());
552         verify(dao, never()).updatePdpGroups(any());
553         verify(reqmap, never()).addRequest(any(), any());
554     }
555
556     private void assertGroupUpdateOnly(PdpGroup group) throws Exception {
557         verify(dao, never()).createPdpGroups(any());
558         verify(reqmap, never()).addRequest(any(), any());
559
560         List<PdpGroup> updates = getGroupUpdates();
561         assertEquals(Arrays.asList(group), updates);
562     }
563 }