Add more endpoints and swagger annotations
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / rest / TestApiRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4  *  Copyright (C) 2019 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.api.main.rest;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.IOException;
29 import java.lang.reflect.Constructor;
30 import java.lang.reflect.Modifier;
31 import java.security.SecureRandom;
32 import java.security.cert.X509Certificate;
33 import java.util.Properties;
34
35 import javax.net.ssl.SSLContext;
36 import javax.net.ssl.TrustManager;
37 import javax.net.ssl.X509TrustManager;
38 import javax.ws.rs.client.Client;
39 import javax.ws.rs.client.ClientBuilder;
40 import javax.ws.rs.client.Invocation;
41 import javax.ws.rs.client.WebTarget;
42 import javax.ws.rs.core.MediaType;
43
44 import org.glassfish.jersey.client.ClientConfig;
45 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
46 import org.junit.After;
47 import org.junit.Test;
48 import org.onap.policy.api.main.exception.PolicyApiException;
49 import org.onap.policy.api.main.parameters.CommonTestData;
50 import org.onap.policy.api.main.parameters.RestServerParameters;
51 import org.onap.policy.api.main.startstop.Main;
52 import org.onap.policy.common.endpoints.report.HealthCheckReport;
53 import org.onap.policy.common.utils.network.NetworkUtil;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  * Class to perform unit test of {@link ApiRestServer}.
59  *
60  * @author Chenfei Gao (cgao@research.att.com)
61  */
62 public class TestApiRestServer {
63
64     private static final Logger LOGGER = LoggerFactory.getLogger(TestApiRestServer.class);
65     private static final String NOT_ALIVE = "not alive";
66     private static final String ALIVE = "alive";
67     private static final String SELF = "self";
68     private static final String NAME = "Policy API";
69     private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
70     private static final String STATISTICS_ENDPOINT = "statistics";
71     private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
72     private Main main;
73     private ApiRestServer restServer;
74
75     /**
76      * Method for cleanup after each test.
77      */
78     @After
79     public void teardown() {
80         try {
81             if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) {
82                 if (main != null) {
83                     stopApiService(main);
84                 } else if (restServer != null) {
85                     restServer.stop();
86                 }
87             }
88         } catch (InterruptedException | IOException | PolicyApiException exp) {
89             LOGGER.error("teardown failed", exp);
90         }
91     }
92
93     @Test
94     public void testHealthCheckSuccess() {
95         try {
96             main = startApiService(true);
97             final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT);
98             final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
99             validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
100         } catch (final Exception exp) {
101             LOGGER.error("testHealthCheckSuccess failed", exp);
102             fail("Test should not throw an exception");
103         }
104     }
105
106     @Test
107     public void testHealthCheckFailure() throws InterruptedException, IOException {
108         final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
109         restServerParams.setName(CommonTestData.API_GROUP_NAME);
110         restServer = new ApiRestServer(restServerParams);
111         try {
112             restServer.start();
113             final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT);
114             final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
115             validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report);
116             assertTrue(restServer.isAlive());
117             assertTrue(restServer.toString().startsWith("ApiRestServer [servers="));
118         } catch (final Exception exp) {
119             LOGGER.error("testHealthCheckFailure failed", exp);
120             fail("Test should not throw an exception");
121         }
122     }
123
124     @Test
125     public void testHttpsHealthCheckSuccess() {
126         try {
127             main = startApiService(false);
128             final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT);
129             final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
130             validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
131         } catch (final Exception exp) {
132             LOGGER.error("testHttpsHealthCheckSuccess failed", exp);
133             fail("Test should not throw an exception");
134         }
135     }
136
137     @Test
138     public void testApiStatistics_200() {
139         try {
140             main = startApiService(true);
141             Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
142             StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
143             validateStatisticsReport(report, 0, 200);
144             updateApiStatistics();
145             invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
146             report = invocationBuilder.get(StatisticsReport.class);
147             validateStatisticsReport(report, 1, 200);
148             ApiStatisticsManager.resetAllStatistics();
149         } catch (final Exception exp) {
150             LOGGER.error("testApiStatistics_200 failed", exp);
151             fail("Test should not throw an exception");
152         }
153     }
154
155     @Test
156     public void testApiStatistics_500() {
157         final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
158         restServerParams.setName(CommonTestData.API_GROUP_NAME);
159         restServer = new ApiRestServer(restServerParams);
160         try {
161             restServer.start();
162             final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
163             final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
164             validateStatisticsReport(report, 0, 500);
165             ApiStatisticsManager.resetAllStatistics();
166         } catch (final Exception exp) {
167             LOGGER.error("testApiStatistics_500 failed", exp);
168             fail("Test should not throw an exception");
169         }
170     }
171
172     @Test
173     public void testHttpsApiStatistics() {
174         try {
175             main = startApiService(false);
176             final Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT);
177             final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
178             validateStatisticsReport(report, 0, 200);
179         } catch (final Exception exp) {
180             LOGGER.error("testHttpsApiStatistics failed", exp);
181             fail("Test should not throw an exception");
182         }
183     }
184
185     @Test
186     public void testApiStatisticsConstructorIsPrivate() {
187         try {
188             final Constructor<ApiStatisticsManager> constructor = ApiStatisticsManager.class.getDeclaredConstructor();
189             assertTrue(Modifier.isPrivate(constructor.getModifiers()));
190             constructor.setAccessible(true);
191             constructor.newInstance();
192         } catch (final Exception exp) {
193             assertTrue(exp.getCause().toString().contains("Instantiation of the class is not allowed"));
194         }
195     }
196
197     private Main startApiService(final boolean http) {
198         final String[] apiConfigParameters = new String[2];
199         if (http) {
200             apiConfigParameters[0] = "-c";
201             apiConfigParameters[1] = "parameters/ApiConfigParameters.json";
202         } else {
203             final Properties systemProps = System.getProperties();
204             systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
205             systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
206             System.setProperties(systemProps);
207             apiConfigParameters[0] = "-c";
208             apiConfigParameters[1] = "parameters/ApiConfigParameters_Https.json";
209         }
210         return new Main(apiConfigParameters);
211     }
212
213     private void stopApiService(final Main main) throws PolicyApiException {
214         main.shutdown();
215     }
216
217     private Invocation.Builder sendHttpRequest(final String endpoint) throws Exception {
218         final ClientConfig clientConfig = new ClientConfig();
219
220         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
221         clientConfig.register(feature);
222
223         final Client client = ClientBuilder.newClient(clientConfig);
224         final WebTarget webTarget = client.target("http://localhost:6969/policy/api/v1/" + endpoint);
225
226         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
227
228         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
229             throw new IllegalStateException("cannot connect to port 6969");
230         }
231         return invocationBuilder;
232     }
233
234     private Invocation.Builder sendHttpsRequest(final String endpoint) throws Exception {
235
236         final TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() {
237
238             @Override
239             public X509Certificate[] getAcceptedIssuers() {
240                 return new X509Certificate[0];
241             }
242
243             @Override
244             public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
245
246             @Override
247             public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
248         } };
249
250         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
251         sc.init(null, noopTrustManager, new SecureRandom());
252         final ClientBuilder clientBuilder = ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host,
253                 session) -> true);
254         final Client client = clientBuilder.build();
255         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
256         client.register(feature);
257
258         final WebTarget webTarget = client.target("https://localhost:6969/policy/api/v1/" + endpoint);
259
260         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
261
262         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
263             throw new IllegalStateException("cannot connect to port 6969");
264         }
265         return invocationBuilder;
266     }
267
268     private void updateApiStatistics() {
269         ApiStatisticsManager.updateTotalApiCallCount();
270         ApiStatisticsManager.updateApiCallSuccessCount();
271         ApiStatisticsManager.updateApiCallFailureCount();
272         ApiStatisticsManager.updateTotalPolicyGetCount();
273         ApiStatisticsManager.updateTotalPolicyPostCount();
274         ApiStatisticsManager.updateTotalPolicyTypeGetCount();
275         ApiStatisticsManager.updateTotalPolicyTypePostCount();
276         ApiStatisticsManager.updatePolicyGetSuccessCount();
277         ApiStatisticsManager.updatePolicyGetFailureCount();
278         ApiStatisticsManager.updatePolicyPostSuccessCount();
279         ApiStatisticsManager.updatePolicyPostFailureCount();
280         ApiStatisticsManager.updatePolicyTypeGetSuccessCount();
281         ApiStatisticsManager.updatePolicyTypeGetFailureCount();
282         ApiStatisticsManager.updatePolicyTypePostSuccessCount();
283         ApiStatisticsManager.updatePolicyTypePostFailureCount();
284     }
285
286     private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) {
287         assertEquals(code, report.getCode());
288         assertEquals(count, report.getTotalApiCallCount());
289         assertEquals(count, report.getApiCallSuccessCount());
290         assertEquals(count, report.getApiCallFailureCount());
291         assertEquals(count, report.getTotalPolicyGetCount());
292         assertEquals(count, report.getTotalPolicyPostCount());
293         assertEquals(count, report.getTotalPolicyTypeGetCount());
294         assertEquals(count, report.getTotalPolicyTypePostCount());
295         assertEquals(count, report.getPolicyGetSuccessCount());
296         assertEquals(count, report.getPolicyGetFailureCount());
297         assertEquals(count, report.getPolicyPostSuccessCount());
298         assertEquals(count, report.getPolicyPostFailureCount());
299         assertEquals(count, report.getPolicyTypeGetSuccessCount());
300         assertEquals(count, report.getPolicyTypeGetFailureCount());
301         assertEquals(count, report.getPolicyTypePostSuccessCount());
302         assertEquals(count, report.getPolicyTypePostFailureCount());
303     }
304
305     private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
306             final String message, final HealthCheckReport report) {
307         assertEquals(name, report.getName());
308         assertEquals(url, report.getUrl());
309         assertEquals(healthy, report.isHealthy());
310         assertEquals(code, report.getCode());
311         assertEquals(message, report.getMessage());
312     }
313 }