Add and Modify JUnits for Code Coverage
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / model / PDPGroupContainerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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.model;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.mockito.Mockito.doThrow;
26
27 import com.att.research.xacml.api.pap.PAPException;
28 import java.awt.Checkbox;
29 import java.util.Set;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.onap.policy.rest.util.PolicyContainer.ItemSetChangeListener;
33 import org.onap.policy.xacml.api.pap.OnapPDP;
34 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
35 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
36
37 public class PDPGroupContainerTest {
38     private OnapPDPGroup group = Mockito.mock(OnapPDPGroup.class);
39     private OnapPDPGroup newGroup = Mockito.mock(OnapPDPGroup.class);
40     private OnapPDP pdp = Mockito.mock(OnapPDP.class);
41     private PAPPolicyEngine engine = Mockito.mock(PAPPolicyEngine.class);
42     private PDPGroupContainer container = new PDPGroupContainer(engine);
43
44     @Test
45     public void testContainer() throws PAPException {
46         Object itemId = new Object();
47         assertEquals(container.isSupported(itemId), false);
48
49         container.refreshGroups();
50         assertEquals(container.getGroups().size(), 0);
51
52         container.makeDefault(group);
53         container.removeGroup(group, newGroup);
54         container.updatePDP(pdp);
55         container.updateGroup(group);
56         container.updateGroup(group, "testUserName");
57         assertNull(container.getContainerPropertyIds());
58         assertEquals(container.getItemIds().size(), 0);
59         assertEquals(container.getType(itemId), null);
60         assertEquals(container.size(), 0);
61         assertEquals(container.containsId(itemId), false);
62         String name = "testName";
63         String description = "testDescription";
64         container.addNewGroup(name, description);
65         String id = "testID";
66         int jmxport = 0;
67         container.addNewPDP(id, newGroup, name, description, jmxport);
68         container.movePDP(pdp, newGroup);
69         ItemSetChangeListener listener = null;
70         container.addItemSetChangeListener(listener);
71         container.nextItemId(itemId);
72         container.prevItemId(itemId);
73         container.firstItemId();
74         container.lastItemId();
75         container.isFirstId(itemId);
76         container.isLastId(itemId);
77         container.indexOfId(itemId);
78         assertEquals(container.getItemIds().size(), 0);
79         container.removeItem(itemId);
80         container.removePDP(pdp, newGroup);
81     }
82
83     @Test(expected = UnsupportedOperationException.class)
84     public void testAddItem() {
85         container.addItem();
86     }
87
88     @Test(expected = UnsupportedOperationException.class)
89     public void testAddContainerProperty() {
90         container.addContainerProperty(null, null, null);
91     }
92
93     @Test(expected = UnsupportedOperationException.class)
94     public void testRemoveContainerProperty() {
95         container.removeContainerProperty(null);
96     }
97
98     @Test(expected = UnsupportedOperationException.class)
99     public void testRemoveAllItems() {
100         container.removeAllItems();
101     }
102
103     @Test(expected = UnsupportedOperationException.class)
104     public void testAddItemAfter() {
105         container.addItemAfter(null);
106     }
107
108     @Test(expected = IndexOutOfBoundsException.class)
109     public void testGetIdByIndex() {
110         container.getIdByIndex(0);
111     }
112
113     @Test(expected = UnsupportedOperationException.class)
114     public void testAddItemAt() {
115         container.addItemAt(0);
116     }
117
118     @Test(expected = IllegalArgumentException.class)
119     public void testGetItemIds() {
120         container.getItemIds(0, 1);
121     }
122
123     @Test
124     public void testGetType() {
125         assertEquals(Boolean.class, container.getType("Default"));
126         assertEquals(Checkbox.class, container.getType("Selected"));
127         assertEquals(Set.class, container.getType("PDPs"));
128         assertEquals(Set.class, container.getType("Policies"));
129         assertEquals(Set.class, container.getType("PIP Configurations"));
130         assertEquals(String.class, container.getType("Id"));
131         assertEquals(String.class, container.getType("Name"));
132         assertEquals(String.class, container.getType("Description"));
133         assertEquals(String.class, container.getType("Status"));
134     }
135
136     @Test
137     public void testContainerPAPExceptions() throws PAPException {
138         doThrow(PAPException.class).when(engine).getOnapPDPGroups();
139         container.refreshGroups();
140
141         doThrow(PAPException.class).when(engine).setDefaultGroup(group);
142         container.makeDefault(group);
143
144         doThrow(PAPException.class).when(engine).updatePDP(pdp);
145         container.updatePDP(pdp);
146
147         doThrow(PAPException.class).when(engine).updateGroup(group);
148         container.updateGroup(group);
149
150         doThrow(PAPException.class).when(engine).updateGroup(group, "testUserName");
151         container.updateGroup(group, "testUserName");
152
153         doThrow(PAPException.class).when(engine).movePDP(pdp, group);
154         container.movePDP(pdp, group);
155     }
156
157     @Test(expected = PAPException.class)
158     public void testContainerRemoveGroup() throws PAPException {
159         doThrow(PAPException.class).when(engine).removeGroup(group, newGroup);
160         container.removeGroup(group, newGroup);
161     }
162
163     @Test(expected = PAPException.class)
164     public void testContainerRemovePDP() throws PAPException {
165         doThrow(PAPException.class).when(engine).removePDP(pdp);
166         container.removePDP(pdp, group);
167     }
168 }