2 * ============LICENSE_START=======================================================
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
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.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;
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;
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;
64 public static void tearDownAfterClass() {
65 Registry.newRegistry();
69 * Configures mocks and objects.
71 * @throws Exception if an error occurs
75 public void setUp() throws Exception {
79 when(dao.getPolicyTypeList("typeA", "100.2.3")).thenReturn(Arrays.asList(loadPolicyType("daoPolicyType.json")));
81 prov = new PdpGroupCreateOrUpdateProvider();
86 public void testCreateOrUpdateGroups() throws Exception {
87 prov.createOrUpdateGroups(loadPdpGroups("emptyGroups.json"));
89 // no groups, so no action should have been taken
90 assertNoGroupAction();
94 public void testCreateOrUpdateGroups_InvalidRequest() throws Exception {
95 assertThatThrownBy(() -> prov.createOrUpdateGroups(new PdpGroups())).isInstanceOf(PfModelException.class)
96 .hasMessageContaining("is null");
98 assertNoGroupAction();
102 public void testCreateOrUpdate_Invalid() throws Exception {
103 PdpGroups groups = loadPdpGroups("createGroups.json");
104 groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED);
106 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
107 .hasMessageContaining("pdpGroupState");
109 assertNoGroupAction();
113 public void testAddGroup() throws Exception {
114 PdpGroups groups = loadPdpGroups("createGroups.json");
115 PdpGroup group = groups.getGroups().get(0);
116 group.setPdpGroupState(PdpState.PASSIVE);
118 prov.createOrUpdateGroups(groups);
120 // should not have updated the state
121 assertEquals(PdpState.PASSIVE, group.getPdpGroupState());
123 assertSame(group, getGroupCreates().get(0));
127 public void testAddGroup_Invalid() throws Exception {
128 PdpGroups groups = loadPdpGroups("createGroups.json");
129 groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED);
131 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
132 .hasMessageContaining("pdpGroupState");
134 assertNoGroupAction();
138 public void testAddGroup_InvalidSubGroup() throws Exception {
139 PdpGroups groups = loadPdpGroups("createGroups.json");
141 groups.getGroups().get(0).getPdpSubgroups().get(0).getSupportedPolicyTypes().get(0).setVersion("99.99.99");
143 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
144 .hasMessageContaining("unknown policy type");
146 assertNoGroupAction();
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();
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();
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();
171 public void testValidateGroupOnly_Invalid() {
172 PdpGroups groups = loadPdpGroups("createGroups.json");
173 groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED);
175 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
176 .hasMessageContaining("pdpGroupState");
180 public void testUpdateGroup() throws Exception {
181 PdpGroups groups = loadPdpGroups("createGroups.json");
183 // DB group = new group
184 PdpGroup group = new PdpGroup(groups.getGroups().get(0));
185 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
187 prov.createOrUpdateGroups(groups);
189 assertNoGroupAction();
193 public void testUpdateGroup_PropertiesChanged() throws Exception {
194 PdpGroups groups = loadPdpGroups("createGroups.json");
196 PdpGroup group = new PdpGroup(groups.getGroups().get(0));
197 group.setProperties(new TreeMap<>());
199 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
201 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
202 .hasMessageContaining("properties");
204 assertNoGroupAction();
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));
215 prov.createOrUpdateGroups(groups);
217 assertGroupUpdateOnly(group);
219 assertEquals("my description", group.getDescription());
220 assertEquals(newgrp.toString(), group.toString());
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));
231 prov.createOrUpdateGroups(groups);
233 assertGroupUpdateOnly(group);
235 assertEquals(PdpState.ACTIVE, group.getPdpGroupState());
236 assertEquals(newgrp.toString(), group.toString());
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));
245 prov.createOrUpdateGroups(groups);
247 PdpGroup newgrp = groups.getGroups().get(0);
248 assertEquals(newgrp.toString(), group.toString());
249 assertGroupUpdateOnly(group);
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));
259 // something different in this subgroup
260 group.getPdpSubgroups().get(0).setDesiredInstanceCount(10);
262 prov.createOrUpdateGroups(groups);
264 assertEquals(newgrp.toString(), group.toString());
265 assertGroupUpdateOnly(group);
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));
273 PdpGroups groups = loadPdpGroups("createGroups.json");
275 prov.createOrUpdateGroups(groups);
277 // verify that DB group was updated
278 List<PdpGroup> updates = getGroupUpdates();
279 assertEquals(1, updates.size());
280 dbgroup = updates.get(0);
282 PdpGroup newgrp = groups.getGroups().get(0);
284 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
285 Collections.sort(dbgroup.getPdpSubgroups().get(0).getPolicies());
287 assertEquals(newgrp.toString(), dbgroup.toString());
289 // no deployment notifications
290 checkEmptyNotification();
292 // this requires a PDP UPDATE message
293 List<PdpUpdate> pdpUpdates = getUpdateRequests(2);
294 assertEquals(2, pdpUpdates.size());
296 PdpUpdate pdpUpdate = pdpUpdates.get(0);
297 assertEquals(PapConstants.PAP_NAME, pdpUpdate.getSource());
298 assertEquals(PDP2, pdpUpdate.getName());
299 assertNull(pdpUpdate.getPdpGroup());
301 pdpUpdate = pdpUpdates.get(1);
302 assertEquals(PapConstants.PAP_NAME, pdpUpdate.getSource());
303 assertEquals(PDP4, pdpUpdate.getName());
304 assertNull(pdpUpdate.getPdpGroup());
306 // it also requires a PDP STATE-CHANGE message
307 List<PdpStateChange> changes = getStateChangeRequests(2);
308 assertEquals(2, changes.size());
310 PdpStateChange change = changes.get(0);
311 assertEquals(PapConstants.PAP_NAME, change.getSource());
312 assertEquals(PDP2, change.getName());
313 assertEquals(PdpState.PASSIVE, change.getState());
315 change = changes.get(1);
316 assertEquals(PapConstants.PAP_NAME, change.getSource());
317 assertEquals(PDP4, change.getName());
318 assertEquals(PdpState.PASSIVE, change.getState());
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));
328 prov.createOrUpdateGroups(groups);
330 assertNoGroupAction();
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));
340 group.setDescription(null);
342 prov.createOrUpdateGroups(groups);
344 assertEquals(newgrp.toString(), group.toString());
345 assertGroupUpdateOnly(group);
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));
355 newgrp.setDescription(null);
357 prov.createOrUpdateGroups(groups);
359 assertEquals(newgrp.toString(), group.toString());
360 assertGroupUpdateOnly(group);
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));
370 newgrp.setDescription(group.getDescription() + "-changed");
372 prov.createOrUpdateGroups(groups);
374 assertEquals(newgrp.toString(), group.toString());
375 assertGroupUpdateOnly(group);
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));
384 prov.createOrUpdateGroups(groups);
386 PdpGroup newgrp = groups.getGroups().get(0);
388 PdpSubGroup newsub = newgrp.getPdpSubgroups().get(1);
389 newsub.setCurrentInstanceCount(0);
390 newsub.setPdpInstances(new ArrayList<>(0));
392 assertEquals(newgrp.toString(), group.toString());
393 assertGroupUpdateOnly(group);
397 * Tests addSubgroup() when the new subgroup has a wild-card policy type.
399 * @throws Exception if an error occurs
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());
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));
410 prov.createOrUpdateGroups(groups);
412 PdpGroup newgrp = groups.getGroups().get(0);
414 PdpSubGroup newsub = newgrp.getPdpSubgroups().get(1);
415 newsub.setCurrentInstanceCount(0);
416 newsub.setPdpInstances(new ArrayList<>(0));
418 assertEquals(newgrp.toString(), group.toString());
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));
427 when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList());
429 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).hasMessageContaining("unknown policy type");
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));
438 PfModelException exc = new PfModelException(Status.CONFLICT, EXPECTED_EXCEPTION);
439 when(dao.getPolicyTypeList(any(), any())).thenThrow(exc);
441 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isSameAs(exc);
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));
451 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
452 .thenReturn(loadPolicies("daoPolicyList.json")).thenReturn(loadPolicies("createGroupNewPolicy.json"));
454 PdpGroups reqgroups = loadPdpGroups("createGroupsVersPrefix.json");
456 prov.createOrUpdateGroups(reqgroups);
458 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
459 Collections.sort(dbgroup.getPdpSubgroups().get(0).getPolicies());
461 assertEquals(newgrp.toString(), dbgroup.toString());
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));
472 newgrp.getPdpSubgroups().get(0).setProperties(new TreeMap<>());
474 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
475 .hasMessageContaining("properties");
477 assertNoGroupAction();
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));
487 newgrp.getPdpSubgroups().get(0).getSupportedPolicyTypes()
488 .add(new ToscaConceptIdentifier("typeX.*", "9.8.7"));
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());
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));
506 newgrp.getPdpSubgroups().get(0).setDesiredInstanceCount(20);
508 prov.createOrUpdateGroups(groups);
510 assertEquals(newgrp.toString(), group.toString());
511 assertGroupUpdateOnly(group);
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));
521 prov.createOrUpdateGroups(groups);
523 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
524 Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
526 assertEquals(newgrp.toString(), group.toString());
529 checkEmptyNotification();
532 assertNoGroupAction();
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));
542 newgrp.setProperties(new TreeMap<>());
544 assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class)
545 .hasMessageContaining("properties");
547 assertNoGroupAction();
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());
556 private void assertGroupUpdateOnly(PdpGroup group) throws Exception {
557 verify(dao, never()).createPdpGroups(any());
558 verify(reqmap, never()).addRequest(any(), any());
560 List<PdpGroup> updates = getGroupUpdates();
561 assertEquals(Arrays.asList(group), updates);