Rename username for PAP operations.
[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.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;
61
62 /**
63  * Class to perform unit test of {@link PapRestControllerV1}.
64  *
65  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
66  */
67 public class CommonPapRestServer {
68
69     protected static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json";
70
71     private static final Logger LOGGER = LoggerFactory.getLogger(CommonPapRestServer.class);
72
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/";
78
79     private static SelfSignedKeyStore keystore;
80     private static int port;
81     protected static String httpsPrefix;
82
83     private static Main main;
84
85     private boolean activatorWasAlive;
86
87     /**
88      * Allocates a port for the server, writes a config file, and then starts Main.
89      *
90      * @throws Exception if an error occurs
91      */
92     @BeforeClass
93     public static void setUpBeforeClass() throws Exception {
94         setUpBeforeClass(true);
95     }
96
97     /**
98      * Allocates a port for the server, writes a config file, and then starts Main, if
99      * specified.
100      *
101      * @param shouldStart {@code true} if Main should be started, {@code false} otherwise
102      * @throws Exception if an error occurs
103      */
104     public static void setUpBeforeClass(boolean shouldStart) throws Exception {
105         keystore = new SelfSignedKeyStore();
106         port = NetworkUtil.allocPort();
107
108         httpsPrefix = "https://localhost:" + port + "/";
109
110         makeConfigFile();
111
112         HttpServletServerFactoryInstance.getServerFactory().destroy();
113         TopicEndpointManager.getManager().shutdown();
114
115         CommonTestData.newDb();
116
117         if (shouldStart) {
118             startMain();
119         }
120     }
121
122     /**
123      * Stops Main.
124      */
125     @AfterClass
126     public static void teardownAfterClass() {
127         try {
128             stopMain();
129
130         } catch (PolicyPapException exp) {
131             LOGGER.error("cannot stop main", exp);
132         }
133     }
134
135     /**
136      * Set up.
137      *
138      * @throws Exception if an error occurs
139      */
140     @Before
141     public void setUp() throws Exception {
142         // restart, if not currently running
143         if (main == null) {
144             startMain();
145         }
146
147         activatorWasAlive = Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive();
148     }
149
150     /**
151      * Restores the activator's "alive" state.
152      */
153     @After
154     public void tearDown() {
155         markActivator(activatorWasAlive);
156     }
157
158     /**
159      * Verifies that an endpoint appears within the swagger response.
160      *
161      * @param endpoint the endpoint of interest
162      * @throws Exception if an error occurs
163      */
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);
167
168         assertTrue(resp.contains(ENDPOINT_PREFIX + endpoint + ":"));
169     }
170
171     /**
172      * Makes a parameter configuration file.
173      *
174      * @throws Exception if an error occurs
175      */
176     private static void makeConfigFile() throws Exception {
177         String json = new CommonTestData().getPapParameterGroupAsString(port);
178
179         File file = new File(CONFIG_FILE);
180         file.deleteOnExit();
181
182         try (FileOutputStream output = new FileOutputStream(file)) {
183             output.write(json.getBytes(StandardCharsets.UTF_8));
184         }
185     }
186
187     /**
188      * Starts the "Main".
189      *
190      * @throws Exception if an error occurs
191      */
192     protected static void startMain() throws Exception {
193         Registry.newRegistry();
194
195         // make sure port is available
196         if (NetworkUtil.isTcpPortOpen("localhost", port, 1, 1L)) {
197             throw new IllegalStateException("port " + port + " is still in use");
198         }
199
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);
204
205         final String[] papConfigParameters = { "-c", CONFIG_FILE };
206
207         main = new Main(papConfigParameters);
208
209         if (!NetworkUtil.isTcpPortOpen("localhost", port, 6, 10000L)) {
210             throw new IllegalStateException("server is not listening on port " + port);
211         }
212     }
213
214     /**
215      * Stops the "Main".
216      *
217      * @throws PolicyPapException if an error occurs
218      */
219     private static void stopMain() throws PolicyPapException {
220         if (main != null) {
221             Main main2 = main;
222             main = null;
223
224             main2.shutdown();
225         }
226     }
227
228     /**
229      * Mark the activator as dead, but leave its REST server running.
230      */
231     protected void markActivatorDead() {
232         markActivator(false);
233     }
234
235     private void markActivator(boolean wasAlive) {
236         Object manager = Whitebox.getInternalState(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class),
237                         "serviceManager");
238         AtomicBoolean running = Whitebox.getInternalState(manager, "running");
239         running.set(wasAlive);
240     }
241
242     /**
243      * Verifies that unauthorized requests fail.
244      *
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
248      */
249     protected void checkUnauthRequest(final String endpoint, Function<Invocation.Builder, Response> sender)
250                     throws Exception {
251         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(),
252                         sender.apply(sendNoAuthRequest(endpoint)).getStatus());
253     }
254
255     /**
256      * Sends a request to an endpoint.
257      *
258      * @param endpoint the target endpoint
259      * @return a request builder
260      * @throws Exception if an error occurs
261      */
262     protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
263         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true);
264     }
265
266     /**
267      * Sends a request to an endpoint, without any authorization header.
268      *
269      * @param endpoint the target endpoint
270      * @return a request builder
271      * @throws Exception if an error occurs
272      */
273     protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
274         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false);
275     }
276
277     /**
278      * Sends a request to a fully qualified endpoint.
279      *
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
284      */
285     protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth)
286                     throws Exception {
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();
292
293         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
294         client.register(GsonMessageBodyHandler.class);
295
296         if (includeAuth) {
297             final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("policyadmin", "zb!XztG34");
298             client.register(feature);
299         }
300
301         final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
302
303         return webTarget.request(MediaType.APPLICATION_JSON);
304     }
305 }