Changed identifiers to concept identifiers
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / concepts / PdpGroupsTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.models.pdp.concepts;
23
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;
30
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36 import org.junit.Test;
37 import org.onap.policy.common.parameters.ValidationResult;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39
40 public class PdpGroupsTest {
41
42     @Test
43     public void testValidatePapRest_toMapList() {
44         PdpGroup group1 = new PdpGroup();
45         group1.setName("group-1");
46
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());
52
53         group1.setPdpSubgroups(Arrays.asList(subgrp));
54
55         PdpGroup group2 = new PdpGroup();
56         group2.setName("group-2");
57         group2.setPdpSubgroups(Arrays.asList(subgrp));
58
59         PdpGroups groups = new PdpGroups();
60         groups.setGroups(Arrays.asList(group1, group2));
61
62         // valid
63         ValidationResult result = groups.validatePapRest();
64         assertNotNull(result);
65         assertTrue(result.isValid());
66         assertNull(result.getResult());
67
68         // check toMapList()
69         List<Map<String, PdpGroup>> lst = groups.toMapList();
70         assertEquals(1, lst.size());
71
72         Map<String, PdpGroup> map = lst.get(0);
73         assertEquals(2, map.size());
74
75         Iterator<PdpGroup> iter = map.values().iterator();
76         assertSame(group1, iter.next());
77         assertSame(group2, iter.next());
78
79         // null group list
80         groups = new PdpGroups();
81         groups.setGroups(null);
82         assertInvalid(groups);
83
84         // null group
85         groups = new PdpGroups();
86         groups.setGroups(Arrays.asList(group1, null));
87         assertInvalid(groups);
88
89         // invalid group
90         PdpGroup groupX = new PdpGroup(group1);
91         groupX.setName(null);
92         groups.setGroups(Arrays.asList(group1, groupX));
93         assertInvalid(groups);
94
95         // duplicate groups
96         groups = new PdpGroups();
97         groups.setGroups(Arrays.asList(group1, group2, group1));
98         assertInvalid(groups);
99     }
100
101     private void assertInvalid(PdpGroups groups) {
102         ValidationResult result = groups.validatePapRest();
103         assertNotNull(result);
104         assertFalse(result.isValid());
105         assertNotNull(result.getResult());
106     }
107 }