Add validation methods for PAP REST API
[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
38 public class PdpGroupsTest {
39
40     @Test
41     public void testValidatePapRest_toMapList() {
42         PdpGroup group1 = new PdpGroup();
43         group1.setName("group-1");
44         group1.setPdpSubgroups(Collections.emptyList());
45
46         PdpGroup group2 = new PdpGroup();
47         group2.setName("group-2");
48         group2.setPdpSubgroups(Collections.emptyList());
49
50         PdpGroups groups = new PdpGroups();
51         groups.setGroups(Arrays.asList(group1, group2));
52
53         // valid
54         ValidationResult result = groups.validatePapRest();
55         assertNotNull(result);
56         assertTrue(result.isValid());
57         assertNull(result.getResult());
58
59         // check toMapList()
60         List<Map<String, PdpGroup>> lst = groups.toMapList();
61         assertEquals(1, lst.size());
62
63         Map<String, PdpGroup> map = lst.get(0);
64         assertEquals(2, map.size());
65
66         Iterator<PdpGroup> iter = map.values().iterator();
67         assertSame(group1, iter.next());
68         assertSame(group2, iter.next());
69
70         // null group list
71         groups = new PdpGroups();
72         groups.setGroups(null);
73         assertInvalid(groups);
74
75         // null group
76         groups = new PdpGroups();
77         groups.setGroups(Arrays.asList(group1, null));
78         assertInvalid(groups);
79
80         // invalid group
81         PdpGroup groupX = new PdpGroup(group1);
82         groupX.setName(null);
83         groups.setGroups(Arrays.asList(group1, groupX));
84         assertInvalid(groups);
85     }
86
87     private void assertInvalid(PdpGroups groups) {
88         ValidationResult result = groups.validatePapRest();
89         assertNotNull(result);
90         assertFalse(result.isValid());
91         assertNotNull(result.getResult());
92     }
93 }