bd98f18c35a4cde47a4adbf39b634de99a785492
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.pdp.concepts;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
35 import org.junit.Test;
36 import org.onap.policy.common.parameters.ValidationResult;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
38
39 public class PdpGroupsTest {
40
41     @Test
42     public void testValidatePapRest_toMapList() {
43         PdpGroup group1 = new PdpGroup();
44         group1.setName("group-1");
45
46         PdpSubGroup subgrp = new PdpSubGroup();
47         subgrp.setDesiredInstanceCount(1);
48         subgrp.setPdpType("pdp-type");
49         subgrp.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("policy-type", "9.8.7")));
50         subgrp.setPolicies(Collections.emptyList());
51
52         group1.setPdpSubgroups(Arrays.asList(subgrp));
53
54         PdpGroup group2 = new PdpGroup();
55         group2.setName("group-2");
56         group2.setPdpSubgroups(Arrays.asList(subgrp));
57
58         PdpGroups groups = new PdpGroups();
59         groups.setGroups(Arrays.asList(group1, group2));
60
61         // valid
62         ValidationResult result = groups.validatePapRest();
63         assertNotNull(result);
64         assertTrue(result.isValid());
65         assertNull(result.getResult());
66
67         // check toMapList()
68         List<Map<String, PdpGroup>> lst = groups.toMapList();
69         assertEquals(1, lst.size());
70
71         Map<String, PdpGroup> map = lst.get(0);
72         assertEquals(2, map.size());
73
74         Iterator<PdpGroup> iter = map.values().iterator();
75         assertSame(group1, iter.next());
76         assertSame(group2, iter.next());
77
78         // null group list
79         groups = new PdpGroups();
80         groups.setGroups(null);
81         assertInvalid(groups);
82
83         // null group
84         groups = new PdpGroups();
85         groups.setGroups(Arrays.asList(group1, null));
86         assertInvalid(groups);
87
88         // invalid group
89         PdpGroup groupX = new PdpGroup(group1);
90         groupX.setName(null);
91         groups.setGroups(Arrays.asList(group1, groupX));
92         assertInvalid(groups);
93
94         // duplicate groups
95         groups = new PdpGroups();
96         groups.setGroups(Arrays.asList(group1, group2, group1));
97         assertInvalid(groups);
98     }
99
100     private void assertInvalid(PdpGroups groups) {
101         ValidationResult result = groups.validatePapRest();
102         assertNotNull(result);
103         assertFalse(result.isValid());
104         assertNotNull(result.getResult());
105     }
106 }