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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.rest;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
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;
57 * Class to perform unit test of {@link XacmlPdpRestServer}.
60 public class TestXacmlPdpRestServer {
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";
71 private XacmlPdpRestServer restServer;
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");
84 * Method for cleanup after each test.
87 public void teardown() {
89 if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) {
91 stopXacmlPdpService(main);
94 if (restServer != null) {
98 } catch (IOException | PolicyXacmlPdpException e) {
99 LOGGER.error("teardown failed", e);
100 } catch (InterruptedException ie) {
101 Thread.interrupted();
102 LOGGER.error("teardown failed", ie);
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);
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);
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="));
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);
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();
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);
154 final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
155 final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
156 validateStatisticsReport(report, 0, 500);
157 XacmlPdpStatisticsManager.resetAllStatistics();
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);
169 public void testStatisticsConstructorIsPrivate() {
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"));
182 private Main startXacmlPdpService(final boolean http) {
183 final String[] xacmlPdpConfigParameters = new String[2];
185 xacmlPdpConfigParameters[0] = "-c";
186 xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters.json";
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";
195 return new Main(xacmlPdpConfigParameters);
198 private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
202 private Invocation.Builder sendHttpRequest(final String endpoint) throws IOException, InterruptedException {
203 final ClientConfig clientConfig = new ClientConfig();
205 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
206 clientConfig.register(feature);
208 final Client client = ClientBuilder.newClient(clientConfig);
209 final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/" + endpoint);
211 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
213 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
214 throw new IllegalStateException("cannot connect to port 6969");
216 return invocationBuilder;
219 private Invocation.Builder sendHttpsRequest(final String endpoint) throws Exception {
221 final TrustManager[] noopTrustManager = new TrustManager[] {new X509TrustManager() {
224 public X509Certificate[] getAcceptedIssuers() {
225 return new X509Certificate[0];
229 public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
232 public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
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);
243 final WebTarget webTarget = client.target("https://localhost:6969/policy/pdpx/v1/" + endpoint);
245 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
247 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
248 throw new IllegalStateException("cannot connect to port 6969");
250 return invocationBuilder;
253 private void updateXacmlPdpStatistics() {
254 XacmlPdpStatisticsManager.updateTotalPoliciesCount();
255 XacmlPdpStatisticsManager.updatePermitDecisionsCount();
256 XacmlPdpStatisticsManager.updateDenyDecisionsCount();
257 XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
258 XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
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());
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());