Update css file name in conf.py
[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     private String userId = "testId";
55     OnapPDPGroup group = Mockito.mock(OnapPDPGroup.class);
56     OnapPDPGroup newGroup = Mockito.mock(OnapPDPGroup.class);
57     OnapPDP pdp = Mockito.mock(OnapPDP.class);
58     InputStream policy;
59
60     /**
61      * BeforeClass does some simple code coverage and sets up the
62      * XACML properties.
63      */
64     @BeforeClass
65     public static void setUpBeforeClass() {
66         //
67         // Test constructor with bad URL
68         //
69         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
70             new RESTfulPAPEngine(null));
71
72         XACMLProperties.reloadProperties();
73     }
74
75     @AfterClass
76     public static void tearDownAfterClass() {
77         XACMLProperties.reloadProperties();
78     }
79
80     private void setupConnection(int responseCode, String location) throws Exception {
81         // Mock connection
82         HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
83
84         Mockito.when(connection.getResponseCode()).thenReturn(responseCode);
85         Mockito.when(connection.getHeaderField("Location")).thenReturn(location);
86
87         InputStream mockInputStream = Mockito.mock(InputStream.class);
88         Mockito.when(connection.getInputStream()).thenReturn(mockInputStream);
89
90         // Set the system property temporarily
91         String systemKey = "xacml.properties";
92         final String oldProperty = System.getProperty(systemKey);
93         System.setProperty(systemKey, "src/test/resources/xacml.admin.properties");
94
95         // Test constructor
96         String urlName = "localhost:1234";
97         engine = new RESTfulPAPEngine(urlName) {
98             @Override
99             protected URLConnection makeConnection(String fullUrl) throws IOException {
100                 return connection;
101             }
102         };
103
104         // Initialize policy
105         policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8"));
106
107         // Restore the original system property
108         if (oldProperty != null) {
109             System.setProperty(systemKey, oldProperty);
110         } else {
111             System.clearProperty(systemKey);
112         }
113     }
114
115     @Test
116     public void testAllTheExceptions() throws Exception {
117         setupConnection(HttpServletResponse.SC_NO_CONTENT, "localhost:5678");
118
119         engine.setDefaultGroup(group);
120         assertNull(engine.getDefaultGroup());
121         engine.newGroup(name, description);
122         engine.removeGroup(group, newGroup);
123         assertNull(engine.getPDPGroup(pdp));
124         assertNull(engine.getPDPGroup(id));
125         assertNull(engine.getPDP(id));
126         assertNull(engine.getStatus(pdp));
127
128         assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
129             engine.getOnapPDPGroups()
130         );
131
132         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
133             engine.updateGroup(group)
134         );
135
136         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
137             engine.updateGroup(group, "testUserName")
138         );
139
140         assertNull(engine.getGroup(name));
141         engine.movePDP(pdp, newGroup);
142
143         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
144             engine.newPDP(id, newGroup, name, description, jmxport)
145         );
146
147         assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
148             engine.updatePDP(pdp)
149         );
150
151         assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
152             engine.removePDP(pdp)
153         );
154
155         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
156             engine.validatePolicyRequest(new PolicyRestAdapter(), policyType)
157         );
158
159         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
160             engine.publishPolicy(id, name, false, policy, newGroup)
161         );
162
163         engine.copyFile(id, newGroup, policy, userId);
164         PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class);
165         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
166             engine.copyPolicy(pdpPolicy, newGroup, userId)
167         );
168
169         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
170             engine.removePolicy(null, group)
171         );
172
173         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
174             engine.copyPolicy(null, null, null)
175         );
176
177         //
178         // Change the mockito to take a different path
179         //
180         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
181             setupConnection(HttpServletResponse.SC_FOUND, "localhost:5678")
182         );
183
184         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
185             setupConnection(200, "localhost:5678")
186         );
187
188         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
189             setupConnection(350, "localhost:5678")
190         );
191
192         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
193             setupConnection(500, "localhost:5678")
194         );
195
196         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
197             setupConnection(350, null)
198         );
199     }
200
201 }