2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019, 2021 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.io.FileOutputStream;
29 import java.nio.charset.StandardCharsets;
30 import java.security.SecureRandom;
31 import java.util.Properties;
32 import java.util.concurrent.atomic.AtomicBoolean;
33 import java.util.function.Function;
34 import javax.net.ssl.SSLContext;
35 import javax.ws.rs.client.Client;
36 import javax.ws.rs.client.ClientBuilder;
37 import javax.ws.rs.client.Invocation;
38 import javax.ws.rs.client.WebTarget;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41 import org.glassfish.jersey.client.ClientProperties;
42 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
48 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
49 import org.onap.policy.common.gson.GsonMessageBodyHandler;
50 import org.onap.policy.common.utils.network.NetworkUtil;
51 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
52 import org.onap.policy.common.utils.services.Registry;
53 import org.onap.policy.pap.main.PapConstants;
54 import org.onap.policy.pap.main.PolicyPapException;
55 import org.onap.policy.pap.main.parameters.CommonTestData;
56 import org.onap.policy.pap.main.startstop.Main;
57 import org.onap.policy.pap.main.startstop.PapActivator;
58 import org.powermock.reflect.Whitebox;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
63 * Class to perform unit test of {@link PapRestControllerV1}.
65 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
67 public class CommonPapRestServer {
69 protected static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json";
71 private static final Logger LOGGER = LoggerFactory.getLogger(CommonPapRestServer.class);
73 public static final String NOT_ALIVE = "not alive";
74 public static final String ALIVE = "alive";
75 public static final String SELF = NetworkUtil.getHostname();
76 public static final String NAME = "Policy PAP";
77 public static final String ENDPOINT_PREFIX = "policy/pap/v1/";
79 private static SelfSignedKeyStore keystore;
80 private static int port;
81 protected static String httpsPrefix;
83 private static Main main;
85 private boolean activatorWasAlive;
88 * Allocates a port for the server, writes a config file, and then starts Main.
90 * @throws Exception if an error occurs
93 public static void setUpBeforeClass() throws Exception {
94 setUpBeforeClass(true);
98 * Allocates a port for the server, writes a config file, and then starts Main, if
101 * @param shouldStart {@code true} if Main should be started, {@code false} otherwise
102 * @throws Exception if an error occurs
104 public static void setUpBeforeClass(boolean shouldStart) throws Exception {
105 keystore = new SelfSignedKeyStore();
106 port = NetworkUtil.allocPort();
108 httpsPrefix = "https://localhost:" + port + "/";
112 HttpServletServerFactoryInstance.getServerFactory().destroy();
113 TopicEndpointManager.getManager().shutdown();
115 CommonTestData.newDb();
126 public static void teardownAfterClass() {
130 } catch (PolicyPapException exp) {
131 LOGGER.error("cannot stop main", exp);
138 * @throws Exception if an error occurs
141 public void setUp() throws Exception {
142 // restart, if not currently running
147 activatorWasAlive = Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive();
151 * Restores the activator's "alive" state.
154 public void tearDown() {
155 markActivator(activatorWasAlive);
159 * Verifies that an endpoint appears within the swagger response.
161 * @param endpoint the endpoint of interest
162 * @throws Exception if an error occurs
164 protected void testSwagger(final String endpoint) throws Exception {
165 final Invocation.Builder invocationBuilder = sendFqeRequest(httpsPrefix + "swagger.yaml", true);
166 final String resp = invocationBuilder.get(String.class);
168 assertTrue(resp.contains(ENDPOINT_PREFIX + endpoint + ":"));
172 * Makes a parameter configuration file.
174 * @throws Exception if an error occurs
176 private static void makeConfigFile() throws Exception {
177 String json = new CommonTestData().getPapParameterGroupAsString(port);
179 File file = new File(CONFIG_FILE);
182 try (FileOutputStream output = new FileOutputStream(file)) {
183 output.write(json.getBytes(StandardCharsets.UTF_8));
190 * @throws Exception if an error occurs
192 protected static void startMain() throws Exception {
193 Registry.newRegistry();
195 // make sure port is available
196 if (NetworkUtil.isTcpPortOpen("localhost", port, 1, 1L)) {
197 throw new IllegalStateException("port " + port + " is still in use");
200 final Properties systemProps = System.getProperties();
201 systemProps.put("javax.net.ssl.keyStore", keystore.getKeystoreName());
202 systemProps.put("javax.net.ssl.keyStorePassword", SelfSignedKeyStore.KEYSTORE_PASSWORD);
203 System.setProperties(systemProps);
205 final String[] papConfigParameters = { "-c", CONFIG_FILE };
207 main = new Main(papConfigParameters);
209 if (!NetworkUtil.isTcpPortOpen("localhost", port, 6, 10000L)) {
210 throw new IllegalStateException("server is not listening on port " + port);
217 * @throws PolicyPapException if an error occurs
219 private static void stopMain() throws PolicyPapException {
229 * Mark the activator as dead, but leave its REST server running.
231 protected void markActivatorDead() {
232 markActivator(false);
235 private void markActivator(boolean wasAlive) {
236 Object manager = Whitebox.getInternalState(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class),
238 AtomicBoolean running = Whitebox.getInternalState(manager, "running");
239 running.set(wasAlive);
243 * Verifies that unauthorized requests fail.
245 * @param endpoint the target end point
246 * @param sender function that sends the requests to the target
247 * @throws Exception if an error occurs
249 protected void checkUnauthRequest(final String endpoint, Function<Invocation.Builder, Response> sender)
251 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(),
252 sender.apply(sendNoAuthRequest(endpoint)).getStatus());
256 * Sends a request to an endpoint.
258 * @param endpoint the target endpoint
259 * @return a request builder
260 * @throws Exception if an error occurs
262 protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
263 return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true);
267 * Sends a request to an endpoint, without any authorization header.
269 * @param endpoint the target endpoint
270 * @return a request builder
271 * @throws Exception if an error occurs
273 protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
274 return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false);
278 * Sends a request to a fully qualified endpoint.
280 * @param fullyQualifiedEndpoint the fully qualified target endpoint
281 * @param includeAuth if authorization header should be included
282 * @return a request builder
283 * @throws Exception if an error occurs
285 protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth)
287 final SSLContext sc = SSLContext.getInstance("TLSv1.2");
288 sc.init(null, NetworkUtil.getAlwaysTrustingManager(), new SecureRandom());
289 final ClientBuilder clientBuilder =
290 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
291 final Client client = clientBuilder.build();
293 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
294 client.register(GsonMessageBodyHandler.class);
297 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("policyadmin", "zb!XztG34");
298 client.register(feature);
301 final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
303 return webTarget.request(MediaType.APPLICATION_JSON);