c0a695156e90163d0e5355d5b50892bf6c417e4f
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / CommonPapRestServer.java
1 /*
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.File;
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.function.Function;
33 import javax.net.ssl.SSLContext;
34 import javax.ws.rs.client.Client;
35 import javax.ws.rs.client.ClientBuilder;
36 import javax.ws.rs.client.Invocation;
37 import javax.ws.rs.client.WebTarget;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40 import org.glassfish.jersey.client.ClientProperties;
41 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
42 import org.junit.After;
43 import org.junit.AfterClass;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
47 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
48 import org.onap.policy.common.gson.GsonMessageBodyHandler;
49 import org.onap.policy.common.utils.network.NetworkUtil;
50 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
51 import org.onap.policy.common.utils.services.Registry;
52 import org.onap.policy.pap.main.PapConstants;
53 import org.onap.policy.pap.main.PolicyPapException;
54 import org.onap.policy.pap.main.parameters.CommonTestData;
55 import org.onap.policy.pap.main.startstop.Main;
56 import org.onap.policy.pap.main.startstop.PapActivator;
57 import org.powermock.reflect.Whitebox;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 /**
62  * Class to perform unit test of {@link PapRestServer}.
63  *
64  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
65  */
66 public class CommonPapRestServer {
67
68     protected static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json";
69
70     private static final Logger LOGGER = LoggerFactory.getLogger(CommonPapRestServer.class);
71
72     public static final String NOT_ALIVE = "not alive";
73     public static final String ALIVE = "alive";
74     public static final String SELF = NetworkUtil.getHostname();
75     public static final String NAME = "Policy PAP";
76     public static final String ENDPOINT_PREFIX = "policy/pap/v1/";
77
78     private static SelfSignedKeyStore keystore;
79     private static int port;
80     protected static String httpsPrefix;
81
82     private static Main main;
83
84     private boolean activatorWasAlive;
85
86     /**
87      * Allocates a port for the server, writes a config file, and then starts Main.
88      *
89      * @throws Exception if an error occurs
90      */
91     @BeforeClass
92     public static void setUpBeforeClass() throws Exception {
93         setUpBeforeClass(true);
94     }
95
96     /**
97      * Allocates a port for the server, writes a config file, and then starts Main, if
98      * specified.
99      *
100      * @param shouldStart {@code true} if Main should be started, {@code false} otherwise
101      * @throws Exception if an error occurs
102      */
103     public static void setUpBeforeClass(boolean shouldStart) throws Exception {
104         keystore = new SelfSignedKeyStore();
105         port = NetworkUtil.allocPort();
106
107         httpsPrefix = "https://localhost:" + port + "/";
108
109         makeConfigFile();
110
111         HttpServletServerFactoryInstance.getServerFactory().destroy();
112         TopicEndpointManager.getManager().shutdown();
113
114         CommonTestData.newDb();
115
116         if (shouldStart) {
117             startMain();
118         }
119     }
120
121     /**
122      * Stops Main.
123      */
124     @AfterClass
125     public static void teardownAfterClass() {
126         try {
127             stopMain();
128
129         } catch (PolicyPapException exp) {
130             LOGGER.error("cannot stop main", exp);
131         }
132     }
133
134     /**
135      * Set up.
136      *
137      * @throws Exception if an error occurs
138      */
139     @Before
140     public void setUp() throws Exception {
141         // restart, if not currently running
142         if (main == null) {
143             startMain();
144         }
145
146         activatorWasAlive = Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive();
147     }
148
149     /**
150      * Restores the activator's "alive" state.
151      */
152     @After
153     public void tearDown() {
154         markActivator(activatorWasAlive);
155     }
156
157     /**
158      * Verifies that an endpoint appears within the swagger response.
159      *
160      * @param endpoint the endpoint of interest
161      * @throws Exception if an error occurs
162      */
163     protected void testSwagger(final String endpoint) throws Exception {
164         final Invocation.Builder invocationBuilder = sendFqeRequest(httpsPrefix + "swagger.yaml", true);
165         final String resp = invocationBuilder.get(String.class);
166
167         assertTrue(resp.contains(ENDPOINT_PREFIX + endpoint + ":"));
168     }
169
170     /**
171      * Makes a parameter configuration file.
172      *
173      * @throws Exception if an error occurs
174      */
175     private static void makeConfigFile() throws Exception {
176         String json = new CommonTestData().getPapParameterGroupAsString(port);
177
178         File file = new File(CONFIG_FILE);
179         file.deleteOnExit();
180
181         try (FileOutputStream output = new FileOutputStream(file)) {
182             output.write(json.getBytes(StandardCharsets.UTF_8));
183         }
184     }
185
186     /**
187      * Starts the "Main".
188      *
189      * @throws Exception if an error occurs
190      */
191     protected static void startMain() throws Exception {
192         Registry.newRegistry();
193
194         // make sure port is available
195         if (NetworkUtil.isTcpPortOpen("localhost", port, 1, 1L)) {
196             throw new IllegalStateException("port " + port + " is still in use");
197         }
198
199         final Properties systemProps = System.getProperties();
200         systemProps.put("javax.net.ssl.keyStore", keystore.getKeystoreName());
201         systemProps.put("javax.net.ssl.keyStorePassword", SelfSignedKeyStore.KEYSTORE_PASSWORD);
202         System.setProperties(systemProps);
203
204         final String[] papConfigParameters = { "-c", CONFIG_FILE };
205
206         main = new Main(papConfigParameters);
207
208         if (!NetworkUtil.isTcpPortOpen("localhost", port, 6, 10000L)) {
209             throw new IllegalStateException("server is not listening on port " + port);
210         }
211     }
212
213     /**
214      * Stops the "Main".
215      *
216      * @throws Exception if an error occurs
217      */
218     private static void stopMain() throws PolicyPapException {
219         if (main != null) {
220             Main main2 = main;
221             main = null;
222
223             main2.shutdown();
224         }
225     }
226
227     /**
228      * Mark the activator as dead, but leave its REST server running.
229      */
230     protected void markActivatorDead() {
231         markActivator(false);
232     }
233
234     private void markActivator(boolean wasAlive) {
235         Object manager = Whitebox.getInternalState(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class),
236                         "serviceManager");
237         Whitebox.setInternalState(manager, "running", wasAlive);
238     }
239
240     /**
241      * Verifies that unauthorized requests fail.
242      *
243      * @param endpoint the target end point
244      * @param sender function that sends the requests to the target
245      * @throws Exception if an error occurs
246      */
247     protected void checkUnauthRequest(final String endpoint, Function<Invocation.Builder, Response> sender)
248                     throws Exception {
249         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(),
250                         sender.apply(sendNoAuthRequest(endpoint)).getStatus());
251     }
252
253     /**
254      * Sends a request to an endpoint.
255      *
256      * @param endpoint the target endpoint
257      * @return a request builder
258      * @throws Exception if an error occurs
259      */
260     protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
261         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true);
262     }
263
264     /**
265      * Sends a request to an endpoint, without any authorization header.
266      *
267      * @param endpoint the target endpoint
268      * @return a request builder
269      * @throws Exception if an error occurs
270      */
271     protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
272         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false);
273     }
274
275     /**
276      * Sends a request to a fully qualified endpoint.
277      *
278      * @param fullyQualifiedEndpoint the fully qualified target endpoint
279      * @param includeAuth if authorization header should be included
280      * @return a request builder
281      * @throws Exception if an error occurs
282      */
283     protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth)
284                     throws Exception {
285         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
286         sc.init(null, NetworkUtil.getAlwaysTrustingManager(), new SecureRandom());
287         final ClientBuilder clientBuilder =
288                         ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
289         final Client client = clientBuilder.build();
290
291         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
292         client.register(GsonMessageBodyHandler.class);
293
294         if (includeAuth) {
295             final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
296             client.register(feature);
297         }
298
299         final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
300
301         return webTarget.request(MediaType.APPLICATION_JSON);
302     }
303 }