2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.rest;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
28 import java.security.SecureRandom;
30 import java.util.Properties;
31 import java.util.function.Function;
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 javax.ws.rs.core.Response;
39 import org.glassfish.jersey.client.ClientProperties;
40 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
41 import org.junit.After;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.onap.policy.common.gson.GsonMessageBodyHandler;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.StandardCoder;
48 import org.onap.policy.common.utils.network.NetworkUtil;
49 import org.onap.policy.common.utils.services.Registry;
50 import org.onap.policy.pap.main.PapConstants;
51 import org.onap.policy.pap.main.PolicyPapException;
52 import org.onap.policy.pap.main.parameters.CommonTestData;
53 import org.onap.policy.pap.main.startstop.Main;
54 import org.onap.policy.pap.main.startstop.PapActivator;
55 import org.powermock.reflect.Whitebox;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * Class to perform unit test of {@link PapRestServer}.
62 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
64 public class CommonPapRestServer {
66 private static final Logger LOGGER = LoggerFactory.getLogger(CommonPapRestServer.class);
68 private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
70 private static Coder coder = new StandardCoder();
72 public static final String NOT_ALIVE = "not alive";
73 public static final String ALIVE = "alive";
74 public static final String SELF = "self";
75 public static final String NAME = "Policy PAP";
76 public static final String ENDPOINT_PREFIX = "policy/pap/v1/";
78 private static int port;
79 protected static String httpsPrefix;
81 private static Main main;
83 private boolean activatorWasAlive;
86 * Allocates a port for the server, writes a config file, and then starts Main.
88 * @throws Exception if an error occurs
91 public static void setUpBeforeClass() throws Exception {
92 port = NetworkUtil.allocPort();
94 httpsPrefix = "https://localhost:" + port + "/";
105 public static void teardownAfterClass() {
109 } catch (PolicyPapException exp) {
110 LOGGER.error("cannot stop main", exp);
117 * @throws Exception if an error occurs
120 public void setUp() throws Exception {
121 // restart, if not currently running
126 activatorWasAlive = Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive();
130 * Restores the activator's "alive" state.
133 public void tearDown() {
134 markActivator(activatorWasAlive);
138 * Verifies that an endpoint appears within the swagger response.
140 * @param endpoint the endpoint of interest
141 * @throws Exception if an error occurs
143 protected void testSwagger(final String endpoint) throws Exception {
144 final Invocation.Builder invocationBuilder = sendFqeRequest(httpsPrefix + "swagger.yaml", true);
145 final String resp = invocationBuilder.get(String.class);
147 assertTrue(resp.contains(ENDPOINT_PREFIX + endpoint + ":"));
151 * Makes a parameter configuration file.
153 * @throws Exception if an error occurs
155 private static void makeConfigFile() throws Exception {
156 Map<String, Object> config = new CommonTestData().getPapParameterGroupMap("PapGroup");
158 @SuppressWarnings("unchecked")
159 Map<String, Object> restParams = (Map<String, Object>) config.get("restServerParameters");
160 restParams.put("port", port);
162 File file = new File("src/test/resources/parameters/TestConfigParams.json");
165 coder.encode(file, config);
171 * @throws Exception if an error occurs
173 private static void startMain() throws Exception {
174 Registry.newRegistry();
176 // make sure port is available
177 if (NetworkUtil.isTcpPortOpen("localhost", port, 1, 1L)) {
178 throw new IllegalStateException("port " + port + " is still in use");
181 final Properties systemProps = System.getProperties();
182 systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
183 systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
184 System.setProperties(systemProps);
187 final String[] papConfigParameters = {
188 "-c", "src/test/resources/parameters/TestConfigParams.json",
189 "-p", "src/test/resources/parameters/topic.properties"
193 main = new Main(papConfigParameters);
195 if (!NetworkUtil.isTcpPortOpen("localhost", port, 6, 10000L)) {
196 throw new IllegalStateException("server is not listening on port " + port);
203 * @throws Exception if an error occurs
205 private static void stopMain() throws PolicyPapException {
215 * Mark the activator as dead, but leave its REST server running.
217 protected void markActivatorDead() {
218 markActivator(false);
221 private void markActivator(boolean wasAlive) {
222 Object manager = Whitebox.getInternalState(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class),
224 Whitebox.setInternalState(manager, "running", wasAlive);
228 * Verifies that unauthorized requests fail.
230 * @param endpoint the target end point
231 * @param sender function that sends the requests to the target
232 * @throws Exception if an error occurs
234 protected void checkUnauthRequest(final String endpoint, Function<Invocation.Builder, Response> sender)
236 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(),
237 sender.apply(sendNoAuthRequest(endpoint)).getStatus());
241 * Sends a request to an endpoint.
243 * @param endpoint the target endpoint
244 * @return a request builder
245 * @throws Exception if an error occurs
247 protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
248 return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true);
252 * Sends a request to an endpoint, without any authorization header.
254 * @param endpoint the target endpoint
255 * @return a request builder
256 * @throws Exception if an error occurs
258 protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
259 return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false);
263 * Sends a request to a fully qualified endpoint.
265 * @param fullyQualifiedEndpoint the fully qualified target endpoint
266 * @param includeAuth if authorization header should be included
267 * @return a request builder
268 * @throws Exception if an error occurs
270 protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth)
272 final SSLContext sc = SSLContext.getInstance("TLSv1.2");
273 sc.init(null, NetworkUtil.getAlwaysTrustingManager(), new SecureRandom());
274 final ClientBuilder clientBuilder =
275 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
276 final Client client = clientBuilder.build();
278 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
279 client.register(GsonMessageBodyHandler.class);
282 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
283 client.register(feature);
286 final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
288 return webTarget.request(MediaType.APPLICATION_JSON);