Started with test decision JSON objects.
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / TestXacmlPdpRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.rest;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.Modifier;
30 import java.security.SecureRandom;
31 import java.security.cert.X509Certificate;
32 import java.util.Properties;
33 import javax.net.ssl.SSLContext;
34 import javax.net.ssl.TrustManager;
35 import javax.net.ssl.X509TrustManager;
36 import javax.ws.rs.client.Client;
37 import javax.ws.rs.client.ClientBuilder;
38 import javax.ws.rs.client.Invocation;
39 import javax.ws.rs.client.WebTarget;
40 import javax.ws.rs.core.MediaType;
41 import org.glassfish.jersey.client.ClientConfig;
42 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
43 import org.junit.After;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.onap.policy.common.endpoints.report.HealthCheckReport;
47 import org.onap.policy.common.utils.network.NetworkUtil;
48 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
49 import org.onap.policy.pdpx.main.parameters.CommonTestData;
50 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
51 import org.onap.policy.pdpx.main.rest.model.StatisticsReport;
52 import org.onap.policy.pdpx.main.startstop.Main;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 /**
57  * Class to perform unit test of {@link XacmlPdpRestServer}.
58  *
59  */
60 public class TestXacmlPdpRestServer {
61
62     private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpRestServer.class);
63     private static final String NOT_ALIVE = "not alive";
64     private static final String ALIVE = "alive";
65     private static final String SELF = "self";
66     private static final String NAME = "Policy Xacml PDP";
67     private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
68     private static final String STATISTICS_ENDPOINT = "statistics";
69     private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
70     private Main main;
71     private XacmlPdpRestServer restServer;
72
73     /**
74      * setup.
75      */
76     @BeforeClass
77     public static void setUp() {
78         System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
79         System.setProperty("org.eclipse.jetty.LEVEL", "OFF");
80
81     }
82
83     /**
84      * Method for cleanup after each test.
85      */
86     @After
87     public void teardown() {
88         try {
89             if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) {
90                 if (main != null) {
91                     stopXacmlPdpService(main);
92                 }
93
94                 if (restServer != null) {
95                     restServer.stop();
96                 }
97             }
98         } catch (IOException | PolicyXacmlPdpException e) {
99             LOGGER.error("teardown failed", e);
100         } catch (InterruptedException ie) {
101             Thread.interrupted();
102             LOGGER.error("teardown failed", ie);
103         }
104     }
105
106     @Test
107     public void testHealthCheckSuccess() throws IOException, InterruptedException {
108         main = startXacmlPdpService(true);
109         final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT);
110         final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
111         validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
112     }
113
114     @Test
115     public void testHealthCheckFailure() throws InterruptedException, IOException {
116         final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
117         restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
118         restServer = new XacmlPdpRestServer(restServerParams);
119         restServer.start();
120         final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT);
121         final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
122         validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report);
123         assertTrue(restServer.isAlive());
124         assertTrue(restServer.toString().startsWith("XacmlPdpRestServer [servers="));
125     }
126
127     @Test
128     public void testHttpsHealthCheckSuccess() throws Exception {
129         main = startXacmlPdpService(false);
130         final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT);
131         final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
132         validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
133     }
134
135     @Test
136     public void testStatistics_200() throws IOException, InterruptedException {
137         main = startXacmlPdpService(true);
138         Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
139         StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
140         validateStatisticsReport(report, 0, 200);
141         updateXacmlPdpStatistics();
142         invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
143         report = invocationBuilder.get(StatisticsReport.class);
144         validateStatisticsReport(report, 1, 200);
145         XacmlPdpStatisticsManager.resetAllStatistics();
146     }
147
148     @Test
149     public void testStatistics_500() throws IOException, InterruptedException {
150         final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
151         restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
152         restServer = new XacmlPdpRestServer(restServerParams);
153         restServer.start();
154         final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
155         final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
156         validateStatisticsReport(report, 0, 500);
157         XacmlPdpStatisticsManager.resetAllStatistics();
158     }
159
160     @Test
161     public void testHttpsStatistic() throws Exception {
162         main = startXacmlPdpService(false);
163         final Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT);
164         final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
165         validateStatisticsReport(report, 0, 200);
166     }
167
168     @Test
169     public void testStatisticsConstructorIsPrivate() {
170         try {
171             final Constructor<XacmlPdpStatisticsManager> constructor =
172                     XacmlPdpStatisticsManager.class.getDeclaredConstructor();
173             assertTrue(Modifier.isPrivate(constructor.getModifiers()));
174             constructor.setAccessible(true);
175             constructor.newInstance();
176             fail("Expected an InstantiationException to be thrown");
177         } catch (final Exception exp) {
178             assertTrue(exp.getCause().toString().contains("Instantiation of the class is not allowed"));
179         }
180     }
181
182     private Main startXacmlPdpService(final boolean http) {
183         final String[] xacmlPdpConfigParameters = new String[2];
184         if (http) {
185             xacmlPdpConfigParameters[0] = "-c";
186             xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters.json";
187         } else {
188             final Properties systemProps = System.getProperties();
189             systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
190             systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
191             System.setProperties(systemProps);
192             xacmlPdpConfigParameters[0] = "-c";
193             xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters_Https.json";
194         }
195         return new Main(xacmlPdpConfigParameters);
196     }
197
198     private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
199         main.shutdown();
200     }
201
202     private Invocation.Builder sendHttpRequest(final String endpoint) throws IOException, InterruptedException {
203         final ClientConfig clientConfig = new ClientConfig();
204
205         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
206         clientConfig.register(feature);
207
208         final Client client = ClientBuilder.newClient(clientConfig);
209         final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/" + endpoint);
210
211         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
212
213         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
214             throw new IllegalStateException("cannot connect to port 6969");
215         }
216         return invocationBuilder;
217     }
218
219     private Invocation.Builder sendHttpsRequest(final String endpoint) throws Exception {
220
221         final TrustManager[] noopTrustManager = new TrustManager[] {new X509TrustManager() {
222
223             @Override
224             public X509Certificate[] getAcceptedIssuers() {
225                 return new X509Certificate[0];
226             }
227
228             @Override
229             public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
230
231             @Override
232             public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
233         } };
234
235         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
236         sc.init(null, noopTrustManager, new SecureRandom());
237         final ClientBuilder clientBuilder =
238                 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
239         final Client client = clientBuilder.build();
240         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
241         client.register(feature);
242
243         final WebTarget webTarget = client.target("https://localhost:6969/policy/pdpx/v1/" + endpoint);
244
245         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
246
247         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
248             throw new IllegalStateException("cannot connect to port 6969");
249         }
250         return invocationBuilder;
251     }
252
253     private void updateXacmlPdpStatistics() {
254         XacmlPdpStatisticsManager.updateTotalPoliciesCount();
255         XacmlPdpStatisticsManager.updatePermitDecisionsCount();
256         XacmlPdpStatisticsManager.updateDenyDecisionsCount();
257         XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
258         XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
259     }
260
261     private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) {
262         assertEquals(code, report.getCode());
263         assertEquals(count, report.getTotalPoliciesCount());
264         assertEquals(count, report.getPermitDecisionsCount());
265         assertEquals(count, report.getDenyDecisionsCount());
266         assertEquals(count, report.getIndeterminantDecisionsCount());
267         assertEquals(count, report.getNotApplicableDecisionsCount());
268     }
269
270     private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
271             final String message, final HealthCheckReport report) {
272         assertEquals(name, report.getName());
273         assertEquals(url, report.getUrl());
274         assertEquals(healthy, report.isHealthy());
275         assertEquals(code, report.getCode());
276         assertEquals(message, report.getMessage());
277     }
278 }