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.Test;
45 import org.onap.policy.common.endpoints.report.HealthCheckReport;
46 import org.onap.policy.common.utils.network.NetworkUtil;
47 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
48 import org.onap.policy.pdpx.main.parameters.CommonTestData;
49 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
50 import org.onap.policy.pdpx.main.rest.model.StatisticsReport;
51 import org.onap.policy.pdpx.main.startstop.Main;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * Class to perform unit test of {@link XacmlPdpRestServer}.
59 public class TestXacmlPdpRestServer {
61 private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpRestServer.class);
62 private static final String NOT_ALIVE = "not alive";
63 private static final String ALIVE = "alive";
64 private static final String SELF = "self";
65 private static final String NAME = "Policy Xacml PDP";
66 private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
67 private static final String STATISTICS_ENDPOINT = "statistics";
68 private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
70 private XacmlPdpRestServer restServer;
73 * Method for cleanup after each test.
76 public void teardown() {
78 if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) {
80 stopXacmlPdpService(main);
83 if (restServer != null) {
87 } catch (IOException | PolicyXacmlPdpException e) {
88 LOGGER.error("teardown failed", e);
89 } catch (InterruptedException ie) {
91 LOGGER.error("teardown failed", ie);
96 public void testHealthCheckSuccess() throws IOException, InterruptedException {
97 main = startXacmlPdpService(true);
98 final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT);
99 final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
100 validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
104 public void testHealthCheckFailure() throws InterruptedException, IOException {
105 final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
106 restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
107 restServer = new XacmlPdpRestServer(restServerParams);
109 final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT);
110 final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
111 validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report);
112 assertTrue(restServer.isAlive());
113 assertTrue(restServer.toString().startsWith("XacmlPdpRestServer [servers="));
117 public void testHttpsHealthCheckSuccess() throws Exception {
118 main = startXacmlPdpService(false);
119 final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT);
120 final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
121 validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
125 public void testStatistics_200() throws IOException, InterruptedException {
126 main = startXacmlPdpService(true);
127 Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
128 StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
129 validateStatisticsReport(report, 0, 200);
130 updateXacmlPdpStatistics();
131 invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
132 report = invocationBuilder.get(StatisticsReport.class);
133 validateStatisticsReport(report, 1, 200);
134 XacmlPdpStatisticsManager.resetAllStatistics();
138 public void testStatistics_500() throws IOException, InterruptedException {
139 final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
140 restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
141 restServer = new XacmlPdpRestServer(restServerParams);
143 final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
144 final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
145 validateStatisticsReport(report, 0, 500);
146 XacmlPdpStatisticsManager.resetAllStatistics();
150 public void testHttpsStatistic() throws Exception {
151 main = startXacmlPdpService(false);
152 final Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT);
153 final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
154 validateStatisticsReport(report, 0, 200);
158 public void testStatisticsConstructorIsPrivate() {
160 final Constructor<XacmlPdpStatisticsManager> constructor =
161 XacmlPdpStatisticsManager.class.getDeclaredConstructor();
162 assertTrue(Modifier.isPrivate(constructor.getModifiers()));
163 constructor.setAccessible(true);
164 constructor.newInstance();
165 fail("Expected an InstantiationException to be thrown");
166 } catch (final Exception exp) {
167 assertTrue(exp.getCause().toString().contains("Instantiation of the class is not allowed"));
171 private Main startXacmlPdpService(final boolean http) {
172 final String[] xacmlPdpConfigParameters = new String[2];
174 xacmlPdpConfigParameters[0] = "-c";
175 xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters.json";
177 final Properties systemProps = System.getProperties();
178 systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
179 systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
180 System.setProperties(systemProps);
181 xacmlPdpConfigParameters[0] = "-c";
182 xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters_Https.json";
184 return new Main(xacmlPdpConfigParameters);
187 private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
191 private Invocation.Builder sendHttpRequest(final String endpoint) throws IOException, InterruptedException {
192 final ClientConfig clientConfig = new ClientConfig();
194 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
195 clientConfig.register(feature);
197 final Client client = ClientBuilder.newClient(clientConfig);
198 final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/" + endpoint);
200 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
202 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
203 throw new IllegalStateException("cannot connect to port 6969");
205 return invocationBuilder;
208 private Invocation.Builder sendHttpsRequest(final String endpoint) throws Exception {
210 final TrustManager[] noopTrustManager = new TrustManager[] {new X509TrustManager() {
213 public X509Certificate[] getAcceptedIssuers() {
214 return new X509Certificate[0];
218 public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
221 public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
224 final SSLContext sc = SSLContext.getInstance("TLSv1.2");
225 sc.init(null, noopTrustManager, new SecureRandom());
226 final ClientBuilder clientBuilder =
227 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
228 final Client client = clientBuilder.build();
229 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
230 client.register(feature);
232 final WebTarget webTarget = client.target("https://localhost:6969/policy/pdpx/v1/" + endpoint);
234 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
236 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
237 throw new IllegalStateException("cannot connect to port 6969");
239 return invocationBuilder;
242 private void updateXacmlPdpStatistics() {
243 XacmlPdpStatisticsManager.updateTotalPoliciesCount();
244 XacmlPdpStatisticsManager.updatePermitDecisionsCount();
245 XacmlPdpStatisticsManager.updateDenyDecisionsCount();
246 XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
247 XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
250 private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) {
251 assertEquals(code, report.getCode());
252 assertEquals(count, report.getTotalPoliciesCount());
253 assertEquals(count, report.getPermitDecisionsCount());
254 assertEquals(count, report.getDenyDecisionsCount());
255 assertEquals(count, report.getIndeterminantDecisionsCount());
256 assertEquals(count, report.getNotApplicableDecisionsCount());
259 private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
260 final String message, final HealthCheckReport report) {
261 assertEquals(name, report.getName());
262 assertEquals(url, report.getUrl());
263 assertEquals(healthy, report.isHealthy());
264 assertEquals(code, report.getCode());
265 assertEquals(message, report.getMessage());