Add and modify jUnits for code coverage (continue admin)
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / admin / RESTfulPAPEngineTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.admin;
24
25 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
26 import static org.junit.Assert.assertNull;
27
28 import com.att.research.xacml.api.pap.PAPException;
29 import com.att.research.xacml.api.pap.PDPPolicy;
30 import com.att.research.xacml.util.XACMLProperties;
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.net.HttpURLConnection;
35 import java.net.URLConnection;
36 import javax.servlet.http.HttpServletResponse;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.mockito.Mockito;
41 import org.onap.policy.rest.adapter.PolicyRestAdapter;
42 import org.onap.policy.xacml.api.pap.OnapPDP;
43 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
44
45 public class RESTfulPAPEngineTest {
46
47     private RESTfulPAPEngine engine = null;
48     private String name = "testName";
49     private String id = "testID";
50     private String description = "testDescription";
51     private String policyType = "testType";
52     private String policyContent = "testContent";
53     private int jmxport = 0;
54     OnapPDPGroup group = Mockito.mock(OnapPDPGroup.class);
55     OnapPDPGroup newGroup = Mockito.mock(OnapPDPGroup.class);
56     OnapPDP pdp = Mockito.mock(OnapPDP.class);
57     InputStream policy;
58
59     /**
60      * BeforeClass does some simple code coverage and sets up the
61      * XACML properties.
62      */
63     @BeforeClass
64     public static void setUpBeforeClass() {
65         //
66         // Test constructor with bad URL
67         //
68         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
69             new RESTfulPAPEngine(null));
70
71         XACMLProperties.reloadProperties();
72     }
73
74     @AfterClass
75     public static void tearDownAfterClass() {
76         XACMLProperties.reloadProperties();
77     }
78
79     private void setupConnection(int responseCode, String location) throws Exception {
80         // Mock connection
81         HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
82
83         Mockito.when(connection.getResponseCode()).thenReturn(responseCode);
84         Mockito.when(connection.getHeaderField("Location")).thenReturn(location);
85
86         InputStream mockInputStream = Mockito.mock(InputStream.class);
87         Mockito.when(connection.getInputStream()).thenReturn(mockInputStream);
88
89         // Set the system property temporarily
90         String systemKey = "xacml.properties";
91         final String oldProperty = System.getProperty(systemKey);
92         System.setProperty(systemKey, "src/test/resources/xacml.admin.properties");
93
94         // Test constructor
95         String urlName = "localhost:1234";
96         engine = new RESTfulPAPEngine(urlName) {
97             @Override
98             protected URLConnection makeConnection(String fullUrl) throws IOException {
99                 return connection;
100             }
101         };
102
103         // Initialize policy
104         policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8"));
105
106         // Restore the original system property
107         if (oldProperty != null) {
108             System.setProperty(systemKey, oldProperty);
109         } else {
110             System.clearProperty(systemKey);
111         }
112     }
113
114     @Test
115     public void testAllTheExceptions() throws Exception {
116         setupConnection(HttpServletResponse.SC_NO_CONTENT, "localhost:5678");
117
118         engine.setDefaultGroup(group);
119         assertNull(engine.getDefaultGroup());
120         engine.newGroup(name, description);
121         engine.removeGroup(group, newGroup);
122         assertNull(engine.getPDPGroup(pdp));
123         assertNull(engine.getPDPGroup(id));
124         assertNull(engine.getPDP(id));
125         assertNull(engine.getStatus(pdp));
126
127         assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
128             engine.getOnapPDPGroups()
129         );
130
131         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
132             engine.updateGroup(group)
133         );
134
135         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
136             engine.updateGroup(group, "testUserName")
137         );
138
139         assertNull(engine.getGroup(name));
140         engine.movePDP(pdp, newGroup);
141
142         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
143             engine.newPDP(id, newGroup, name, description, jmxport)
144         );
145
146         assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
147             engine.updatePDP(pdp)
148         );
149
150         assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
151             engine.removePDP(pdp)
152         );
153
154         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
155             engine.validatePolicyRequest(new PolicyRestAdapter(), policyType)
156         );
157
158         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
159             engine.publishPolicy(id, name, false, policy, newGroup)
160         );
161
162         engine.copyFile(id, newGroup, policy);
163         PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class);
164         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
165             engine.copyPolicy(pdpPolicy, newGroup)
166         );
167
168         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
169             engine.removePolicy(null, group)
170         );
171
172         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
173             engine.copyPolicy(null, null)
174         );
175
176         //
177         // Change the mockito to take a different path
178         //
179         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
180             setupConnection(HttpServletResponse.SC_FOUND, "localhost:5678")
181         );
182
183         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
184             setupConnection(200, "localhost:5678")
185         );
186
187         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
188             setupConnection(350, "localhost:5678")
189         );
190
191         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
192             setupConnection(500, "localhost:5678")
193         );
194
195         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
196             setupConnection(350, null)
197         );
198     }
199
200 }