2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.models.pdp.concepts;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertTrue;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.Iterator;
34 import java.util.List;
36 import org.junit.Test;
37 import org.onap.policy.common.parameters.ValidationResult;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 public class PdpGroupsTest {
43 public void testValidatePapRest_toMapList() {
44 PdpGroup group1 = new PdpGroup();
45 group1.setName("group-1");
47 PdpSubGroup subgrp = new PdpSubGroup();
48 subgrp.setDesiredInstanceCount(1);
49 subgrp.setPdpType("pdp-type");
50 subgrp.setSupportedPolicyTypes(Arrays.asList(new ToscaConceptIdentifier("policy-type", "9.8.7")));
51 subgrp.setPolicies(Collections.emptyList());
53 group1.setPdpSubgroups(Arrays.asList(subgrp));
55 PdpGroup group2 = new PdpGroup();
56 group2.setName("group-2");
57 group2.setPdpSubgroups(Arrays.asList(subgrp));
59 PdpGroups groups = new PdpGroups();
60 groups.setGroups(Arrays.asList(group1, group2));
63 ValidationResult result = groups.validatePapRest();
64 assertNotNull(result);
65 assertTrue(result.isValid());
66 assertNull(result.getResult());
69 List<Map<String, PdpGroup>> lst = groups.toMapList();
70 assertEquals(1, lst.size());
72 Map<String, PdpGroup> map = lst.get(0);
73 assertEquals(2, map.size());
75 Iterator<PdpGroup> iter = map.values().iterator();
76 assertSame(group1, iter.next());
77 assertSame(group2, iter.next());
80 groups = new PdpGroups();
81 groups.setGroups(null);
82 assertInvalid(groups);
85 groups = new PdpGroups();
86 groups.setGroups(Arrays.asList(group1, null));
87 assertInvalid(groups);
90 PdpGroup groupX = new PdpGroup(group1);
92 groups.setGroups(Arrays.asList(group1, groupX));
93 assertInvalid(groups);
96 groups = new PdpGroups();
97 groups.setGroups(Arrays.asList(group1, group2, group1));
98 assertInvalid(groups);
101 private void assertInvalid(PdpGroups groups) {
102 ValidationResult result = groups.validatePapRest();
103 assertNotNull(result);
104 assertFalse(result.isValid());
105 assertNotNull(result.getResult());