ca81ab7d856f60c079450c33a3c73c9f05427d43
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / CommonPapRestServer.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019,2023 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.File;
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.springframework.boot.test.context.SpringBootTest;
59 import org.springframework.boot.test.web.server.LocalServerPort;
60 import org.springframework.test.annotation.DirtiesContext;
61 import org.springframework.test.annotation.DirtiesContext.ClassMode;
62 import org.springframework.test.context.DynamicPropertyRegistry;
63 import org.springframework.test.context.DynamicPropertySource;
64 import org.springframework.test.context.junit4.SpringRunner;
65 import org.springframework.test.util.ReflectionTestUtils;
66
67 /**
68  * Class to perform unit test of {@link PapRestControllerV1}.
69  *
70  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
71  */
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 {
77
78     protected static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json";
79
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/";
85
86     private static SelfSignedKeyStore keystore;
87
88     private boolean activatorWasAlive;
89     protected String httpsPrefix;
90
91     @LocalServerPort
92     private int port;
93
94     private PapActivator papActivator;
95
96     /**
97      * Allocates a new db url, writes a config file.
98      *
99      * @throws Exception if an error occurs
100      */
101     @BeforeClass
102     public static void setUpBeforeClass() throws Exception {
103         keystore = new SelfSignedKeyStore();
104         CommonTestData.newDb();
105         makeConfigFile();
106
107         HttpServletServerFactoryInstance.getServerFactory().destroy();
108         TopicEndpointManager.getManager().shutdown();
109         Registry.newRegistry();
110     }
111
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);
118     }
119
120     /**
121      * Set up.
122      *
123      * @throws Exception if an error occurs
124      */
125     @Before
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();
130     }
131
132     /**
133      * Restores the activator's "alive" state.
134      */
135     @After
136     public void tearDown() {
137         markActivator(activatorWasAlive);
138     }
139
140     /**
141      * Verifies that an endpoint appears within the swagger response.
142      *
143      * @param endpoint the endpoint of interest
144      * @throws Exception if an error occurs
145      */
146     protected void testSwagger(final String endpoint) throws Exception {
147         final Invocation.Builder invocationBuilder = sendFqeRequest(httpsPrefix
148                 + ENDPOINT_PREFIX + "v3/api-docs", true, MediaType.APPLICATION_JSON);
149         final String resp = invocationBuilder.get(String.class);
150         assertTrue(resp.contains(endpoint));
151     }
152
153     /**
154      * Makes a parameter configuration file.
155      *
156      * @throws Exception if an error occurs
157      */
158     private static void makeConfigFile() throws Exception {
159         String json = new CommonTestData().getPapParameterGroupAsString(6969);
160
161         File file = new File(CONFIG_FILE);
162         file.deleteOnExit();
163
164         try (FileOutputStream output = new FileOutputStream(file)) {
165             output.write(json.getBytes(StandardCharsets.UTF_8));
166         }
167     }
168
169     /**
170      * Mark the activator as dead, but leave its REST server running.
171      */
172     protected void markActivatorDead() {
173         markActivator(false);
174     }
175
176     private void markActivator(boolean wasAlive) {
177         Object manager = ReflectionTestUtils.getField(papActivator, "serviceManager");
178         AtomicBoolean running = (AtomicBoolean) ReflectionTestUtils.getField(manager, "running");
179         running.set(wasAlive);
180     }
181
182     /**
183      * Verifies that unauthorized requests fail.
184      *
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
188      */
189     protected void checkUnauthRequest(final String endpoint, Function<Invocation.Builder, Response> sender)
190                     throws Exception {
191         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(),
192                         sender.apply(sendNoAuthRequest(endpoint)).getStatus());
193     }
194
195     /**
196      * Sends a request to an endpoint.
197      *
198      * @param endpoint the target endpoint
199      * @return a request builder
200      * @throws Exception if an error occurs
201      */
202     protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
203         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true, MediaType.APPLICATION_JSON);
204     }
205
206     /**
207      * Sends a request to an endpoint.
208      *
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
213      */
214     protected Invocation.Builder sendRequest(final String endpoint, String mediaType) throws Exception {
215         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true, mediaType);
216     }
217
218     /**
219      * Sends a request to an endpoint, without any authorization header.
220      *
221      * @param endpoint the target endpoint
222      * @return a request builder
223      * @throws Exception if an error occurs
224      */
225     protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
226         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false, MediaType.APPLICATION_JSON);
227     }
228
229     /**
230      * Sends a request to a fully qualified endpoint.
231      *
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
236      */
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();
244
245         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
246         client.register((mediaType.equalsIgnoreCase(MediaType.APPLICATION_JSON) ? GsonMessageBodyHandler.class
247                         : YamlMessageBodyHandler.class));
248
249         if (includeAuth) {
250             final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("policyadmin", "zb!XztG34");
251             client.register(feature);
252         }
253
254         final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
255
256         return webTarget.request(mediaType);
257     }
258 }