24835d70cde6727dc2850373fb7949dbb523f1a2
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019,2023 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 jakarta.ws.rs.client.Client;
28 import jakarta.ws.rs.client.ClientBuilder;
29 import jakarta.ws.rs.client.Invocation;
30 import jakarta.ws.rs.client.WebTarget;
31 import jakarta.ws.rs.core.MediaType;
32 import jakarta.ws.rs.core.Response;
33 import java.io.File;
34 import java.security.SecureRandom;
35 import java.util.Map;
36 import java.util.Properties;
37 import java.util.concurrent.atomic.AtomicBoolean;
38 import java.util.function.Function;
39 import javax.net.ssl.SSLContext;
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.security.SelfSignedKeyStore;
56 import org.onap.policy.common.utils.services.Registry;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.springframework.test.util.ReflectionTestUtils;
60
61 /**
62  * Class to perform unit test of {@link ApexStarterRestServer}.
63  *
64  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
65  */
66 public class CommonApexStarterRestServer {
67
68     private static final Logger LOGGER = LoggerFactory.getLogger(CommonApexStarterRestServer.class);
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 ENDPOINT_PREFIX = "policy/apex-pdp/v1/";
76
77     private static int port;
78     protected static String httpsPrefix;
79
80     private static ApexStarterMain main;
81
82     private boolean activatorWasAlive;
83
84     /**
85      * Allocates a port for the server, writes a config file, and then starts Main.
86      *
87      * @throws Exception if an error occurs
88      */
89     @BeforeClass
90     public static void setUpBeforeClass() throws Exception {
91         port = NetworkUtil.allocPort();
92
93         httpsPrefix = "https://localhost:" + port + "/";
94
95         makeConfigFile();
96
97         startMain();
98     }
99
100     /**
101      * Stops Main.
102      */
103     @AfterClass
104     public static void teardownAfterClass() {
105         try {
106             stopMain();
107
108         } catch (final ApexStarterException exp) {
109             LOGGER.error("cannot stop main", exp);
110         }
111     }
112
113     /**
114      * Set up.
115      *
116      * @throws Exception if an error occurs
117      */
118     @Before
119     public void setUp() throws Exception {
120         // restart, if not currently running
121         if (main == null) {
122             startMain();
123         }
124
125         activatorWasAlive =
126                 Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.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", 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         final Map<String, Object> config =
157                 new CommonTestData().getApexStarterParameterGroupMap("ApexStarterParameterGroup");
158
159         @SuppressWarnings("unchecked")
160         final Map<String, Object> restParams = (Map<String, Object>) config.get("restServerParameters");
161         restParams.put("port", port);
162
163         final File file = new File("src/test/resources/TestConfigParams.json");
164         file.deleteOnExit();
165
166         coder.encode(file, config);
167     }
168
169     /**
170      * Starts the "Main".
171      *
172      * @throws Exception if an error occurs
173      */
174     private static void startMain() throws Exception {
175         Registry.newRegistry();
176
177         // make sure port is available
178         if (NetworkUtil.isTcpPortOpen("localhost", port, 1, 1L)) {
179             throw new IllegalStateException("port " + port + " is still in use");
180         }
181
182         final Properties systemProps = System.getProperties();
183         systemProps.put("javax.net.ssl.keyStore", new SelfSignedKeyStore().getKeystoreName());
184         systemProps.put("javax.net.ssl.keyStorePassword", SelfSignedKeyStore.KEYSTORE_PASSWORD);
185         System.setProperties(systemProps);
186
187         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/TestConfigParams.json" };
188
189         main = new ApexStarterMain(apexStarterConfigParameters);
190
191         if (!NetworkUtil.isTcpPortOpen("localhost", port, 6, 10000L)) {
192             throw new IllegalStateException("server is not listening on port " + port);
193         }
194     }
195
196     /**
197      * Stops the "Main".
198      *
199      * @throws Exception if an error occurs
200      */
201     private static void stopMain() throws ApexStarterException {
202         if (main != null) {
203             final ApexStarterMain main2 = main;
204             main = null;
205
206             main2.shutdown();
207         }
208     }
209
210     private void markActivator(final boolean wasAlive) {
211         final Object manager = ReflectionTestUtils.getField(
212                 Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class), "manager");
213         AtomicBoolean running = (AtomicBoolean) ReflectionTestUtils.getField(manager, "running");
214         running.set(wasAlive);
215     }
216
217     /**
218      * Verifies that unauthorized requests fail.
219      *
220      * @param endpoint the target end point
221      * @param sender function that sends the requests to the target
222      * @throws Exception if an error occurs
223      */
224     protected void checkUnauthRequest(final String endpoint, final Function<Invocation.Builder, Response> sender)
225             throws Exception {
226         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(),
227                 sender.apply(sendNoAuthRequest(endpoint)).getStatus());
228     }
229
230     /**
231      * Sends a request to an endpoint.
232      *
233      * @param endpoint the target endpoint
234      * @return a request builder
235      * @throws Exception if an error occurs
236      */
237     protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
238         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true);
239     }
240
241     /**
242      * Sends a request to an endpoint, without any authorization header.
243      *
244      * @param endpoint the target endpoint
245      * @return a request builder
246      * @throws Exception if an error occurs
247      */
248     protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
249         return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false);
250     }
251
252     /**
253      * Sends a request to a fully qualified endpoint.
254      *
255      * @param fullyQualifiedEndpoint the fully qualified target endpoint
256      * @param includeAuth if authorization header should be included
257      * @return a request builder
258      * @throws Exception if an error occurs
259      */
260     protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, final boolean includeAuth)
261             throws Exception {
262         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
263         sc.init(null, NetworkUtil.getAlwaysTrustingManager(), new SecureRandom());
264         final ClientBuilder clientBuilder =
265                 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
266         final Client client = clientBuilder.build();
267
268         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
269         client.register(GsonMessageBodyHandler.class);
270
271         if (includeAuth) {
272             final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
273             client.register(feature);
274         }
275
276         final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
277
278         return webTarget.request(MediaType.APPLICATION_JSON);
279     }
280 }