65d901d4113ccef4a10b574c52201cba94cd2ea8
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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.apex.services.onappf.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.apex.services.onappf.ApexStarterActivator;
46 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
47 import org.onap.policy.apex.services.onappf.ApexStarterMain;
48 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
49 import org.onap.policy.apex.services.onappf.parameters.CommonTestData;
50 import org.onap.policy.common.gson.GsonMessageBodyHandler;
51 import org.onap.policy.common.utils.coder.Coder;
52 import org.onap.policy.common.utils.coder.StandardCoder;
53 import org.onap.policy.common.utils.network.NetworkUtil;
54 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
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 Coder coder = new StandardCoder();
70
71     public static final String NOT_ALIVE = "not alive";
72     public static final String ALIVE = "alive";
73     public static final String SELF = "self";
74     public static final String ENDPOINT_PREFIX = "policy/apex-pdp/v1/";
75
76     private static int port;
77     protected static String httpsPrefix;
78
79     private static ApexStarterMain main;
80
81     private boolean activatorWasAlive;
82
83     /**
84      * Allocates a port for the server, writes a config file, and then starts Main.
85      *
86      * @throws Exception if an error occurs
87      */
88     @BeforeClass
89     public static void setUpBeforeClass() throws Exception {
90         port = NetworkUtil.allocPort();
91
92         httpsPrefix = "https://localhost:" + port + "/";
93
94         makeConfigFile();
95
96         startMain();
97     }
98
99     /**
100      * Stops Main.
101      */
102     @AfterClass
103     public static void teardownAfterClass() {
104         try {
105             stopMain();
106
107         } catch (final ApexStarterException exp) {
108             LOGGER.error("cannot stop main", exp);
109         }
110     }
111
112     /**
113      * Set up.
114      *
115      * @throws Exception if an error occurs
116      */
117     @Before
118     public void setUp() throws Exception {
119         // restart, if not currently running
120         if (main == null) {
121             startMain();
122         }
123
124         activatorWasAlive =
125                 Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class).isAlive();
126     }
127
128     /**
129      * Restores the activator's "alive" state.
130      */
131     @After
132     public void tearDown() {
133         markActivator(activatorWasAlive);
134     }
135
136     /**
137      * Verifies that an endpoint appears within the swagger response.
138      *
139      * @param endpoint the endpoint of interest
140      * @throws Exception if an error occurs
141      */
142     protected void testSwagger(final String endpoint) throws Exception {
143         final Invocation.Builder invocationBuilder = sendFqeRequest(httpsPrefix + "swagger.yaml", true);
144         final String resp = invocationBuilder.get(String.class);
145
146         assertTrue(resp.contains(ENDPOINT_PREFIX + endpoint + ":"));
147     }
148
149     /**
150      * Makes a parameter configuration file.
151      *
152      * @throws Exception if an error occurs
153      */
154     private static void makeConfigFile() throws Exception {
155         final Map<String, Object> config =
156                 new CommonTestData().getApexStarterParameterGroupMap("ApexStarterParameterGroup");
157
158         @SuppressWarnings("unchecked")
159         final Map<String, Object> restParams = (Map<String, Object>) config.get("restServerParameters");
160         restParams.put("port", port);
161
162         final File file = new File("src/test/resources/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", new SelfSignedKeyStore().getKeystoreName());
183         systemProps.put("javax.net.ssl.keyStorePassword", SelfSignedKeyStore.KEYSTORE_PASSWORD);
184         System.setProperties(systemProps);
185
186         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/TestConfigParams.json" };
187
188         main = new ApexStarterMain(apexStarterConfigParameters);
189
190         if (!NetworkUtil.isTcpPortOpen("localhost", port, 6, 10000L)) {
191             throw new IllegalStateException("server is not listening on port " + port);
192         }
193     }
194
195     /**
196      * Stops the "Main".
197      *
198      * @throws Exception if an error occurs
199      */
200     private static void stopMain() throws ApexStarterException {
201         if (main != null) {
202             final ApexStarterMain main2 = main;
203             main = null;
204
205             main2.shutdown();
206         }
207     }
208
209     private void markActivator(final boolean wasAlive) {
210         final Object manager = Whitebox.getInternalState(
211                 Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class), "manager");
212         Whitebox.setInternalState(manager, "running", wasAlive);
213     }
214
215     /**
216      * Verifies that unauthorized requests fail.
217      *
218      * @param endpoint the target end point
219      * @param sender function that sends the requests to the target
220      * @throws Exception if an error occurs
221      */
222     protected void checkUnauthRequest(final String endpoint, final Function<Invocation.Builder, Response> sender)
223             throws Exception {
224         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(),
225                 sender.apply(sendNoAuthRequest(endpoint)).getStatus());
226     }
227
228     /**
229      * Sends a request to an endpoint.
230      *
231      * @param endpoint the target endpoint
232      * @return a request builder
233      * @throws Exception if an error occurs
234      */
235     protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
236         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true);
237     }
238
239     /**
240      * Sends a request to an endpoint, without any authorization header.
241      *
242      * @param endpoint the target endpoint
243      * @return a request builder
244      * @throws Exception if an error occurs
245      */
246     protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
247         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false);
248     }
249
250     /**
251      * Sends a request to a fully qualified endpoint.
252      *
253      * @param fullyQualifiedEndpoint the fully qualified target endpoint
254      * @param includeAuth if authorization header should be included
255      * @return a request builder
256      * @throws Exception if an error occurs
257      */
258     protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, final boolean includeAuth)
259             throws Exception {
260         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
261         sc.init(null, NetworkUtil.getAlwaysTrustingManager(), new SecureRandom());
262         final ClientBuilder clientBuilder =
263                 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
264         final Client client = clientBuilder.build();
265
266         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
267         client.register(GsonMessageBodyHandler.class);
268
269         if (includeAuth) {
270             final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
271             client.register(feature);
272         }
273
274         final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
275
276         return webTarget.request(MediaType.APPLICATION_JSON);
277     }
278 }