2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.pap.main.rest;
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;
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;
55 public class TestPdpGroupCreateOrUpdateProvider extends ProviderSuper {
56 private static final String EXPECTED_EXCEPTION = "expected exception";
58 private static final String PDP2 = "pdpB";
59 private static final String PDP4 = "pdpD";
61 private PdpGroupCreateOrUpdateProvider prov;
65 public static void tearDownAfterClass() {
66 Registry.newRegistry();
70 * Configures mocks and objects.
72 * @throws Exception if an error occurs
76 public void setUp() throws Exception {
78 prov = new PdpGroupCreateOrUpdateProvider();
79 super.initialize(prov);
80 when(toscaService.getPolicyTypeList("typeA", "100.2.3"))
81 .thenReturn(Arrays.asList(loadPolicyType("daoPolicyType.json")));
85 void testCreateOrUpdateGroups() throws Exception {
86 prov.createOrUpdateGroups(loadPdpGroups("emptyGroups.json"));
88 // no groups, so no action should have been taken
89 assertNoGroupAction();
93 void testCreateOrUpdateGroups_InvalidRequest() throws Exception {
94 assertThatThrownBy(() -> prov.createOrUpdateGroups(new PdpGroups())).isInstanceOf(PfModelException.class)
95 .hasMessageContaining("is null");
97 assertNoGroupAction();
101 void testCreateOrUpdate_Invalid() throws Exception {
102 PdpGroups groups = loadPdpGroups("createGroups.json");
103 groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED);
105 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
106 .hasMessageContaining("pdpGroupState");
108 assertNoGroupAction();
112 void testAddGroup() throws Exception {
113 PdpGroups groups = loadPdpGroups("createGroups.json");
114 PdpGroup group = groups.getGroups().get(0);
115 group.setPdpGroupState(PdpState.PASSIVE);
117 prov.createOrUpdateGroups(groups);
119 // should not have updated the state
120 assertEquals(PdpState.PASSIVE, group.getPdpGroupState());
122 assertSame(group, getGroupCreates().get(0));
126 void testAddGroup_Invalid() throws Exception {
127 PdpGroups groups = loadPdpGroups("createGroups.json");
128 groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED);
130 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
131 .hasMessageContaining("pdpGroupState");
133 assertNoGroupAction();
137 void testAddGroup_InvalidSubGroup() throws Exception {
138 PdpGroups groups = loadPdpGroups("createGroups.json");
140 groups.getGroups().get(0).getPdpSubgroups().get(0).getSupportedPolicyTypes().get(0).setVersion("99.99.99");
142 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
143 .hasMessageContaining("unknown policy type");
145 assertNoGroupAction();
149 void testValidateGroupOnly_NullState() {
150 PdpGroups groups = loadPdpGroups("createGroups.json");
151 groups.getGroups().get(0).setPdpGroupState(null);
152 Assertions.assertThatCode(() -> prov.createOrUpdateGroups(groups)).doesNotThrowAnyException();
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();
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();
170 void testValidateGroupOnly_Invalid() {
171 PdpGroups groups = loadPdpGroups("createGroups.json");
172 groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED);
174 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
175 .hasMessageContaining("pdpGroupState");
179 void testUpdateGroup() throws Exception {
180 PdpGroups groups = loadPdpGroups("createGroups.json");
182 // DB group = new group
183 PdpGroup group = new PdpGroup(groups.getGroups().get(0));
184 when(pdpGroupService.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
186 prov.createOrUpdateGroups(groups);
188 assertNoGroupAction();
192 void testUpdateGroup_PropertiesChanged() throws Exception {
193 PdpGroups groups = loadPdpGroups("createGroups.json");
195 PdpGroup group = new PdpGroup(groups.getGroups().get(0));
196 group.setProperties(new TreeMap<>());
198 when(pdpGroupService.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
200 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
201 .hasMessageContaining("properties");
203 assertNoGroupAction();
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));
214 prov.createOrUpdateGroups(groups);
216 assertGroupUpdateOnly(group);
218 assertEquals("my description", group.getDescription());
219 assertEquals(newgrp.toString(), group.toString());
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));
230 prov.createOrUpdateGroups(groups);
232 assertGroupUpdateOnly(group);
234 assertEquals(PdpState.ACTIVE, group.getPdpGroupState());
235 assertEquals(newgrp.toString(), group.toString());
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));
244 prov.createOrUpdateGroups(groups);
246 PdpGroup newgrp = groups.getGroups().get(0);
247 assertEquals(newgrp.toString(), group.toString());
248 assertGroupUpdateOnly(group);
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));
258 // something different in this subgroup
259 group.getPdpSubgroups().get(0).setDesiredInstanceCount(10);
261 prov.createOrUpdateGroups(groups);
263 assertEquals(newgrp.toString(), group.toString());
264 assertGroupUpdateOnly(group);
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));
272 PdpGroups groups = loadPdpGroups("createGroups.json");
274 prov.createOrUpdateGroups(groups);
276 // verify that DB group was updated
277 List<PdpGroup> updates = getGroupUpdates();
278 assertEquals(1, updates.size());
279 dbgroup = updates.get(0);
281 PdpGroup newgrp = groups.getGroups().get(0);
283 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
284 Collections.sort(dbgroup.getPdpSubgroups().get(0).getPolicies());
286 assertEquals(newgrp.toString(), dbgroup.toString());
288 // no deployment notifications
289 checkEmptyNotification();
291 // this requires a PDP UPDATE message
292 List<PdpUpdate> pdpUpdates = getUpdateRequests(2);
293 assertEquals(2, pdpUpdates.size());
295 PdpUpdate pdpUpdate = pdpUpdates.get(0);
296 assertEquals(PapConstants.PAP_NAME, pdpUpdate.getSource());
297 assertEquals(PDP2, pdpUpdate.getName());
298 assertNull(pdpUpdate.getPdpGroup());
300 pdpUpdate = pdpUpdates.get(1);
301 assertEquals(PapConstants.PAP_NAME, pdpUpdate.getSource());
302 assertEquals(PDP4, pdpUpdate.getName());
303 assertNull(pdpUpdate.getPdpGroup());
305 // it also requires a PDP STATE-CHANGE message
306 List<PdpStateChange> changes = getStateChangeRequests(2);
307 assertEquals(2, changes.size());
309 PdpStateChange change = changes.get(0);
310 assertEquals(PapConstants.PAP_NAME, change.getSource());
311 assertEquals(PDP2, change.getName());
312 assertEquals(PdpState.PASSIVE, change.getState());
314 change = changes.get(1);
315 assertEquals(PapConstants.PAP_NAME, change.getSource());
316 assertEquals(PDP4, change.getName());
317 assertEquals(PdpState.PASSIVE, change.getState());
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));
327 prov.createOrUpdateGroups(groups);
329 assertNoGroupAction();
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));
339 group.setDescription(null);
341 prov.createOrUpdateGroups(groups);
343 assertEquals(newgrp.toString(), group.toString());
344 assertGroupUpdateOnly(group);
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));
354 newgrp.setDescription(null);
356 prov.createOrUpdateGroups(groups);
358 assertEquals(newgrp.toString(), group.toString());
359 assertGroupUpdateOnly(group);
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));
369 newgrp.setDescription(group.getDescription() + "-changed");
371 prov.createOrUpdateGroups(groups);
373 assertEquals(newgrp.toString(), group.toString());
374 assertGroupUpdateOnly(group);
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));
383 prov.createOrUpdateGroups(groups);
385 PdpGroup newgrp = groups.getGroups().get(0);
387 PdpSubGroup newsub = newgrp.getPdpSubgroups().get(1);
388 newsub.setCurrentInstanceCount(0);
389 newsub.setPdpInstances(new ArrayList<>(0));
391 assertEquals(newgrp.toString(), group.toString());
392 assertGroupUpdateOnly(group);
396 * Tests addSubgroup() when the new subgroup has a wild-card policy type.
398 * @throws Exception if an error occurs
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());
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));
409 prov.createOrUpdateGroups(groups);
411 PdpGroup newgrp = groups.getGroups().get(0);
413 PdpSubGroup newsub = newgrp.getPdpSubgroups().get(1);
414 newsub.setCurrentInstanceCount(0);
415 newsub.setPdpInstances(new ArrayList<>(0));
417 assertEquals(newgrp.toString(), group.toString());
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));
426 when(toscaService.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList());
428 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).hasMessageContaining("unknown policy type");
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));
437 PfModelException exc = new PfModelException(Status.CONFLICT, EXPECTED_EXCEPTION);
438 when(toscaService.getPolicyTypeList(any(), any())).thenThrow(exc);
440 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isSameAs(exc);
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));
450 when(toscaService.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
451 .thenReturn(loadPolicies("daoPolicyList.json")).thenReturn(loadPolicies("createGroupNewPolicy.json"));
453 PdpGroups reqgroups = loadPdpGroups("createGroupsVersPrefix.json");
455 prov.createOrUpdateGroups(reqgroups);
457 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
458 Collections.sort(dbgroup.getPdpSubgroups().get(0).getPolicies());
460 assertEquals(newgrp.toString(), dbgroup.toString());
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));
471 newgrp.getPdpSubgroups().get(0).setProperties(new TreeMap<>());
473 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
474 .hasMessageContaining("properties");
476 assertNoGroupAction();
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));
486 newgrp.getPdpSubgroups().get(0).getSupportedPolicyTypes()
487 .add(new ToscaConceptIdentifier("typeX.*", "9.8.7"));
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());
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));
505 newgrp.getPdpSubgroups().get(0).setDesiredInstanceCount(20);
507 prov.createOrUpdateGroups(groups);
509 assertEquals(newgrp.toString(), group.toString());
510 assertGroupUpdateOnly(group);
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));
520 prov.createOrUpdateGroups(groups);
522 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
523 Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
525 assertEquals(newgrp.toString(), group.toString());
528 checkEmptyNotification();
531 assertNoGroupAction();
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));
541 newgrp.setProperties(new TreeMap<>());
543 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
544 .hasMessageContaining("properties");
546 assertNoGroupAction();
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());
555 private void assertGroupUpdateOnly(PdpGroup group) throws Exception {
556 verify(pdpGroupService, never()).createPdpGroups(any());
557 verify(reqmap, never()).addRequest(any(), any());
559 List<PdpGroup> updates = getGroupUpdates();
560 assertEquals(Arrays.asList(group), updates);