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