Cleanup POLICY-SDK-APP CheckPDP
[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-2019 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
32 import java.io.ByteArrayInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.net.HttpURLConnection;
36 import java.net.URLConnection;
37
38 import javax.servlet.http.HttpServletResponse;
39
40 import org.junit.AfterClass;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.mockito.Mockito;
44 import org.onap.policy.rest.adapter.PolicyRestAdapter;
45 import org.onap.policy.xacml.api.pap.OnapPDP;
46 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
47
48 public class RESTfulPAPEngineTest {
49
50     private RESTfulPAPEngine engine = null;
51     private String name = "testName";
52     private String id = "testID";
53     private String description = "testDescription";
54     private String policyType = "testType";
55     private String policyContent = "testContent";
56     private int jmxport = 0;
57     OnapPDPGroup group = Mockito.mock(OnapPDPGroup.class);
58     OnapPDPGroup newGroup = Mockito.mock(OnapPDPGroup.class);
59     OnapPDP pdp = Mockito.mock(OnapPDP.class);
60     InputStream policy;
61
62     /**
63      * BeforeClass does some simple code coverage and sets up the
64      * XACML properties.
65      */
66     @BeforeClass
67     public static void setUpBeforeClass() {
68         //
69         // Test constructor with bad URL
70         //
71         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
72             new RESTfulPAPEngine(null));
73
74         XACMLProperties.reloadProperties();
75     }
76
77     @AfterClass
78     public static void tearDownAfterClass() {
79         XACMLProperties.reloadProperties();
80     }
81
82     private void setupConnection(int responseCode) throws Exception {
83         // Mock connection
84         HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
85         Mockito.when(connection.getResponseCode()).thenReturn(responseCode);
86         Mockito.when(connection.getHeaderField("Location")).thenReturn("localhost:5678");
87
88         // Set the system property temporarily
89         String systemKey = "xacml.properties";
90         final String oldProperty = System.getProperty(systemKey);
91         System.setProperty(systemKey, "src/test/resources/xacml.admin.properties");
92
93         // Test constructor
94         String urlName = "localhost:1234";
95         engine = new RESTfulPAPEngine(urlName) {
96             @Override
97             protected URLConnection makeConnection(String fullUrl) throws IOException {
98                 return connection;
99             }
100         };
101
102         // Initialize policy
103         policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8"));
104
105         // Restore the original system property
106         if (oldProperty != null) {
107             System.setProperty(systemKey, oldProperty);
108         } else {
109             System.clearProperty(systemKey);
110         }
111     }
112
113     @Test
114     public void testAllTheExceptions() throws Exception {
115         setupConnection(HttpServletResponse.SC_NO_CONTENT);
116
117         engine.setDefaultGroup(group);
118         assertNull(engine.getDefaultGroup());
119         engine.newGroup(name, description);
120         engine.removeGroup(group, newGroup);
121         assertNull(engine.getPDPGroup(pdp));
122         assertNull(engine.getPDPGroup(id));
123         assertNull(engine.getPDP(id));
124         assertNull(engine.getStatus(pdp));
125
126         assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
127             engine.getOnapPDPGroups()
128         );
129
130         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
131             engine.updateGroup(group)
132         );
133
134         assertNull(engine.getGroup(name));
135         engine.movePDP(pdp, newGroup);
136
137         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
138             engine.newPDP(id, newGroup, name, description, jmxport)
139         );
140
141         assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
142             engine.updatePDP(pdp)
143         );
144
145         assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
146             engine.removePDP(pdp)
147         );
148
149         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
150             engine.validatePolicyRequest(new PolicyRestAdapter(), policyType)
151         );
152
153         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
154             engine.publishPolicy(id, name, false, policy, newGroup)
155         );
156
157         engine.copyFile(id, newGroup, policy);
158         PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class);
159         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
160             engine.copyPolicy(pdpPolicy, newGroup)
161         );
162
163         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
164             engine.removePolicy(null, group)
165         );
166
167         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
168             engine.copyPolicy(null, null)
169         );
170
171         //
172         // Change the mockito to take a different path
173         //
174         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
175             setupConnection(HttpServletResponse.SC_FOUND)
176         );
177     }
178 }