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