2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.pdpx.main;
27 import java.io.IOException;
28 import java.nio.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.security.SecureRandom;
32 import javax.net.ssl.SSLContext;
33 import javax.ws.rs.client.Client;
34 import javax.ws.rs.client.ClientBuilder;
35 import javax.ws.rs.client.Invocation;
36 import javax.ws.rs.client.WebTarget;
37 import javax.ws.rs.core.MediaType;
38 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.onap.policy.common.utils.network.NetworkUtil;
44 import org.onap.policy.common.utils.resources.ResourceUtils;
45 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
46 import org.onap.policy.pdpx.main.startstop.Main;
47 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
48 import org.powermock.reflect.Whitebox;
51 * Common base class for REST service tests.
53 public class CommonRest {
54 private static final String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
57 * Full path to the config file.
59 public static final String CONFIG_FILE;
62 * Path corresponding to {@link #CONFIG_FILE}.
64 private static final Path CONFIG_PATH;
67 * Contents read from the "standard" config file, which still contains ${xxx}
70 private static final String STD_CONFIG;
73 * Port that was last allocated for the server.
75 protected static int port;
78 * "Main" that was last started.
80 private static Main main;
83 * Records the "alive" state of the activator while it's temporarily updated by
84 * various junit tests. The {@link #tearDown()} method restores the "alive" state back
87 private boolean activatorWasAlive;
91 File file = new File(ResourceUtils.getFilePath4Resource("parameters/XacmlPdpConfigParameters_Std.json"));
92 STD_CONFIG = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
94 file = new File(file.getParentFile(), "Test_XacmlPdpConfigParameters.json");
97 CONFIG_FILE = file.getAbsolutePath();
98 CONFIG_PATH = new File(CONFIG_FILE).toPath();
100 } catch (IOException e) {
101 throw new ExceptionInInitializerError(e);
106 * Configures system properties and creates a JSON config file.
108 * @throws Exception if an error occurs
111 public static void setUpBeforeClass() throws Exception {
112 System.setProperty("javax.net.ssl.keyStore", KEYSTORE);
113 System.setProperty("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
115 System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
116 System.setProperty("org.eclipse.jetty.LEVEL", "OFF");
120 final String[] xacmlPdpConfigParameters = {"-c", CommonRest.CONFIG_FILE};
121 main = new Main(xacmlPdpConfigParameters);
123 // start xacml rest controller
124 XacmlPdpActivator.getCurrent().startXacmlRestController();
126 if (!NetworkUtil.isTcpPortOpen("localhost", port, 20, 1000L)) {
127 throw new IllegalStateException("server is not listening on port " + port);
135 public static void tearDownAfterClass() {
140 * Resets the statistics.
143 public void setUp() {
144 activatorWasAlive = XacmlPdpActivator.getCurrent().isAlive();
145 XacmlPdpStatisticsManager.getCurrent().resetAllStatistics();
149 * Restores the "alive" status of the activator.
152 public void tearDown() {
153 markActivator(activatorWasAlive);
159 protected static void stopMain() {
164 * Writes a JSON config file, substituting an allocated port number for occurrences of
167 * @return the allocated server port
168 * @throws IOException if the config file cannot be created
170 public static int writeJsonConfig() throws IOException {
171 port = NetworkUtil.allocPort();
173 String config = STD_CONFIG.replace("${port}", String.valueOf(port));
174 Files.write(CONFIG_PATH, config.getBytes(StandardCharsets.UTF_8));
180 * Sends an HTTPS request to an endpoint of the PDP's REST API.
182 * @param endpoint target endpoint
183 * @return a request builder
184 * @throws Exception if an error occurs
186 protected Invocation.Builder sendHttpsRequest(final String endpoint) throws Exception {
187 // always trust the certificate
188 final SSLContext sc = SSLContext.getInstance("TLSv1.2");
189 sc.init(null, NetworkUtil.getAlwaysTrustingManager(), new SecureRandom());
191 // always trust the host name
192 final ClientBuilder clientBuilder =
193 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
195 final Client client = clientBuilder.build();
196 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
197 client.register(feature);
199 final WebTarget webTarget = client.target("https://localhost:" + port + "/policy/pdpx/v1/" + endpoint);
201 return webTarget.request(MediaType.APPLICATION_JSON);
205 * Mark the activator as dead, but leave its REST server running.
207 protected void markActivatorDead() {
208 markActivator(false);
212 * Changes the internal "alive" status of the activator to a new value.
214 * @param newAlive the new "alive" status
216 private void markActivator(boolean newAlive) {
217 Object manager = Whitebox.getInternalState(XacmlPdpActivator.getCurrent(), "serviceManager");
218 Whitebox.setInternalState(manager, "running", newAlive);