006cad0b9ac77e1f878a30d97a8e455db04396bf
[policy/apex-pdp.git] / services / services-onappf / src / test / java / org / onap / policy / apex / services / onappf / rest / CommonApexStarterRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.services.onappf.rest;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.File;
27 import java.security.SecureRandom;
28 import java.util.Map;
29 import java.util.Properties;
30 import java.util.function.Function;
31
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
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.apex.services.onappf.ApexStarterActivator;
47 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
48 import org.onap.policy.apex.services.onappf.ApexStarterMain;
49 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
50 import org.onap.policy.apex.services.onappf.parameters.CommonTestData;
51 import org.onap.policy.common.gson.GsonMessageBodyHandler;
52 import org.onap.policy.common.utils.coder.Coder;
53 import org.onap.policy.common.utils.coder.StandardCoder;
54 import org.onap.policy.common.utils.network.NetworkUtil;
55 import org.onap.policy.common.utils.services.Registry;
56 import org.powermock.reflect.Whitebox;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 /**
61  * Class to perform unit test of {@link ApexStarterRestServer}.
62  *
63  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
64  */
65 public class CommonApexStarterRestServer {
66
67     private static final Logger LOGGER = LoggerFactory.getLogger(CommonApexStarterRestServer.class);
68
69     private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
70
71     private static Coder coder = new StandardCoder();
72
73     public static final String NOT_ALIVE = "not alive";
74     public static final String ALIVE = "alive";
75     public static final String SELF = "self";
76     public static final String NAME = "Policy PDP-A";
77     public static final String ENDPOINT_PREFIX = "policy/apex-pdp/v1/";
78
79     private static int port;
80     protected static String httpsPrefix;
81
82     private static ApexStarterMain 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         port = NetworkUtil.allocPort();
94
95         httpsPrefix = "https://localhost:" + port + "/";
96
97         makeConfigFile();
98
99         startMain();
100     }
101
102     /**
103      * Stops Main.
104      */
105     @AfterClass
106     public static void teardownAfterClass() {
107         try {
108             stopMain();
109
110         } catch (final ApexStarterException exp) {
111             LOGGER.error("cannot stop main", exp);
112         }
113     }
114
115     /**
116      * Set up.
117      *
118      * @throws Exception if an error occurs
119      */
120     @Before
121     public void setUp() throws Exception {
122         // restart, if not currently running
123         if (main == null) {
124             startMain();
125         }
126
127         activatorWasAlive =
128                 Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class).isAlive();
129     }
130
131     /**
132      * Restores the activator's "alive" state.
133      */
134     @After
135     public void tearDown() {
136         markActivator(activatorWasAlive);
137     }
138
139     /**
140      * Verifies that an endpoint appears within the swagger response.
141      *
142      * @param endpoint the endpoint of interest
143      * @throws Exception if an error occurs
144      */
145     protected void testSwagger(final String endpoint) throws Exception {
146         final Invocation.Builder invocationBuilder = sendFqeRequest(httpsPrefix + "swagger.yaml", true);
147         final String resp = invocationBuilder.get(String.class);
148
149         assertTrue(resp.contains(ENDPOINT_PREFIX + endpoint + ":"));
150     }
151
152     /**
153      * Makes a parameter configuration file.
154      *
155      * @throws Exception if an error occurs
156      */
157     private static void makeConfigFile() throws Exception {
158         final Map<String, Object> config =
159                 new CommonTestData().getApexStarterParameterGroupMap("ApexStarterParameterGroup");
160
161         @SuppressWarnings("unchecked")
162         final Map<String, Object> restParams = (Map<String, Object>) config.get("restServerParameters");
163         restParams.put("port", port);
164
165         final File file = new File("src/test/resources/TestConfigParams.json");
166         file.deleteOnExit();
167
168         coder.encode(file, config);
169     }
170
171     /**
172      * Starts the "Main".
173      *
174      * @throws Exception if an error occurs
175      */
176     private static void startMain() throws Exception {
177         Registry.newRegistry();
178
179         // make sure port is available
180         if (NetworkUtil.isTcpPortOpen("localhost", port, 1, 1L)) {
181             throw new IllegalStateException("port " + port + " is still in use");
182         }
183
184         final Properties systemProps = System.getProperties();
185         systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
186         systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
187         System.setProperties(systemProps);
188
189         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/TestConfigParams.json" };
190
191         main = new ApexStarterMain(apexStarterConfigParameters);
192
193         if (!NetworkUtil.isTcpPortOpen("localhost", port, 6, 10000L)) {
194             throw new IllegalStateException("server is not listening on port " + port);
195         }
196     }
197
198     /**
199      * Stops the "Main".
200      *
201      * @throws Exception if an error occurs
202      */
203     private static void stopMain() throws ApexStarterException {
204         if (main != null) {
205             final ApexStarterMain main2 = main;
206             main = null;
207
208             main2.shutdown();
209         }
210     }
211
212     private void markActivator(final boolean wasAlive) {
213         final Object manager = Whitebox.getInternalState(
214                 Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class), "manager");
215         Whitebox.setInternalState(manager, "running", wasAlive);
216     }
217
218     /**
219      * Verifies that unauthorized requests fail.
220      *
221      * @param endpoint the target end point
222      * @param sender function that sends the requests to the target
223      * @throws Exception if an error occurs
224      */
225     protected void checkUnauthRequest(final String endpoint, final Function<Invocation.Builder, Response> sender)
226             throws Exception {
227         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(),
228                 sender.apply(sendNoAuthRequest(endpoint)).getStatus());
229     }
230
231     /**
232      * Sends a request to an endpoint.
233      *
234      * @param endpoint the target endpoint
235      * @return a request builder
236      * @throws Exception if an error occurs
237      */
238     protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
239         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true);
240     }
241
242     /**
243      * Sends a request to an endpoint, without any authorization header.
244      *
245      * @param endpoint the target endpoint
246      * @return a request builder
247      * @throws Exception if an error occurs
248      */
249     protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
250         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false);
251     }
252
253     /**
254      * Sends a request to a fully qualified endpoint.
255      *
256      * @param fullyQualifiedEndpoint the fully qualified target endpoint
257      * @param includeAuth if authorization header should be included
258      * @return a request builder
259      * @throws Exception if an error occurs
260      */
261     protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, final boolean includeAuth)
262             throws Exception {
263         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
264         sc.init(null, NetworkUtil.getAlwaysTrustingManager(), new SecureRandom());
265         final ClientBuilder clientBuilder =
266                 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
267         final Client client = clientBuilder.build();
268
269         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
270         client.register(GsonMessageBodyHandler.class);
271
272         if (includeAuth) {
273             final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
274             client.register(feature);
275         }
276
277         final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
278
279         return webTarget.request(MediaType.APPLICATION_JSON);
280     }
281 }