2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5 * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.rest;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
29 import java.io.FileOutputStream;
30 import java.nio.charset.StandardCharsets;
31 import java.security.SecureRandom;
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.Before;
45 import org.junit.BeforeClass;
46 import org.junit.runner.RunWith;
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.endpoints.http.server.YamlMessageBodyHandler;
50 import org.onap.policy.common.gson.GsonMessageBodyHandler;
51 import org.onap.policy.common.utils.network.NetworkUtil;
52 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
53 import org.onap.policy.common.utils.services.Registry;
54 import org.onap.policy.pap.main.PapConstants;
55 import org.onap.policy.pap.main.PolicyPapApplication;
56 import org.onap.policy.pap.main.parameters.CommonTestData;
57 import org.onap.policy.pap.main.startstop.PapActivator;
58 import org.powermock.reflect.Whitebox;
59 import org.springframework.boot.test.context.SpringBootTest;
60 import org.springframework.boot.web.server.LocalServerPort;
61 import org.springframework.test.annotation.DirtiesContext;
62 import org.springframework.test.annotation.DirtiesContext.ClassMode;
63 import org.springframework.test.context.DynamicPropertyRegistry;
64 import org.springframework.test.context.DynamicPropertySource;
65 import org.springframework.test.context.junit4.SpringRunner;
68 * Class to perform unit test of {@link PapRestControllerV1}.
70 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
72 @RunWith(SpringRunner.class)
73 @SpringBootTest(classes = PolicyPapApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
74 properties = {"db.initialize=false"})
75 @DirtiesContext(classMode = ClassMode.AFTER_CLASS)
76 public abstract class CommonPapRestServer {
78 protected static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json";
80 public static final String NOT_ALIVE = "not alive";
81 public static final String ALIVE = "alive";
82 public static final String SELF = NetworkUtil.getHostname();
83 public static final String NAME = "Policy PAP";
84 public static final String ENDPOINT_PREFIX = "policy/pap/v1/";
86 private static SelfSignedKeyStore keystore;
88 private boolean activatorWasAlive;
89 protected String httpsPrefix;
94 private PapActivator papActivator;
97 * Allocates a new db url, writes a config file.
99 * @throws Exception if an error occurs
102 public static void setUpBeforeClass() throws Exception {
103 keystore = new SelfSignedKeyStore();
104 CommonTestData.newDb();
107 HttpServletServerFactoryInstance.getServerFactory().destroy();
108 TopicEndpointManager.getManager().shutdown();
109 Registry.newRegistry();
112 @DynamicPropertySource
113 static void registerPgProperties(DynamicPropertyRegistry registry) {
114 registry.add("spring.datasource.url", () -> "jdbc:h2:mem:testdb" + CommonTestData.dbNum);
115 registry.add("server.ssl.enabled", () -> "true");
116 registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
117 registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
123 * @throws Exception if an error occurs
126 public void setUp() throws Exception {
127 httpsPrefix = "https://localhost:" + port + "/";
128 papActivator = Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class);
129 activatorWasAlive = papActivator.isAlive();
133 * Restores the activator's "alive" state.
136 public void tearDown() {
137 markActivator(activatorWasAlive);
141 * Verifies that an endpoint appears within the swagger response.
143 * @param endpoint the endpoint of interest
144 * @throws Exception if an error occurs
146 protected void testSwagger(final String endpoint) throws Exception {
147 final Invocation.Builder invocationBuilder =
148 sendFqeRequest(httpsPrefix + "v3/api-docs", true, MediaType.APPLICATION_JSON);
149 final String resp = invocationBuilder.get(String.class);
150 assertTrue(resp.contains(ENDPOINT_PREFIX + endpoint));
154 * Makes a parameter configuration file.
156 * @throws Exception if an error occurs
158 private static void makeConfigFile() throws Exception {
159 String json = new CommonTestData().getPapParameterGroupAsString(6969);
161 File file = new File(CONFIG_FILE);
164 try (FileOutputStream output = new FileOutputStream(file)) {
165 output.write(json.getBytes(StandardCharsets.UTF_8));
170 * Mark the activator as dead, but leave its REST server running.
172 protected void markActivatorDead() {
173 markActivator(false);
176 private void markActivator(boolean wasAlive) {
177 Object manager = Whitebox.getInternalState(papActivator, "serviceManager");
178 AtomicBoolean running = Whitebox.getInternalState(manager, "running");
179 running.set(wasAlive);
183 * Verifies that unauthorized requests fail.
185 * @param endpoint the target end point
186 * @param sender function that sends the requests to the target
187 * @throws Exception if an error occurs
189 protected void checkUnauthRequest(final String endpoint, Function<Invocation.Builder, Response> sender)
191 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(),
192 sender.apply(sendNoAuthRequest(endpoint)).getStatus());
196 * Sends a request to an endpoint.
198 * @param endpoint the target endpoint
199 * @return a request builder
200 * @throws Exception if an error occurs
202 protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
203 return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true, MediaType.APPLICATION_JSON);
207 * Sends a request to an endpoint.
209 * @param endpoint the target endpoint
210 * @param mediaType the media type for the request
211 * @return a request builder
212 * @throws Exception if an error occurs
214 protected Invocation.Builder sendRequest(final String endpoint, String mediaType) throws Exception {
215 return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true, mediaType);
219 * Sends a request to an endpoint, without any authorization header.
221 * @param endpoint the target endpoint
222 * @return a request builder
223 * @throws Exception if an error occurs
225 protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
226 return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false, MediaType.APPLICATION_JSON);
230 * Sends a request to a fully qualified endpoint.
232 * @param fullyQualifiedEndpoint the fully qualified target endpoint
233 * @param includeAuth if authorization header should be included
234 * @return a request builder
235 * @throws Exception if an error occurs
237 protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth,
238 String mediaType) throws Exception {
239 final SSLContext sc = SSLContext.getInstance("TLSv1.2");
240 sc.init(null, NetworkUtil.getAlwaysTrustingManager(), new SecureRandom());
241 final ClientBuilder clientBuilder =
242 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
243 final Client client = clientBuilder.build();
245 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
246 client.register((mediaType.equalsIgnoreCase(MediaType.APPLICATION_JSON) ? GsonMessageBodyHandler.class
247 : YamlMessageBodyHandler.class));
250 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("policyadmin", "zb!XztG34");
251 client.register(feature);
254 final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
256 return webTarget.request(mediaType);