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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.api.main.rest;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
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;
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;
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;
58 * Class to perform unit test of {@link ApiRestServer}.
60 * @author Chenfei Gao (cgao@research.att.com)
62 public class TestApiRestServer {
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";
73 private ApiRestServer restServer;
76 * Method for cleanup after each test.
79 public void teardown() {
81 if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) {
84 } else if (restServer != null) {
88 } catch (InterruptedException | IOException | PolicyApiException exp) {
89 LOGGER.error("teardown failed", exp);
94 public void testHealthCheckSuccess() {
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");
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);
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");
125 public void testHttpsHealthCheckSuccess() {
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");
138 public void testApiStatistics_200() {
140 main = startApiService(true);
141 Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
142 StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
143 validateStatisticsReport(report, 200);
144 updateApiStatistics();
145 invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
146 report = invocationBuilder.get(StatisticsReport.class);
147 validateStatisticsReport(report, 200);
148 ApiStatisticsManager.resetAllStatistics();
149 } catch (final Exception exp) {
150 LOGGER.error("testApiStatistics_200 failed", exp);
151 fail("Test should not throw an exception");
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);
162 final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
163 final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
164 validateStatisticsReport(report, 500);
165 ApiStatisticsManager.resetAllStatistics();
166 } catch (final Exception exp) {
167 LOGGER.error("testApiStatistics_500 failed", exp);
168 fail("Test should not throw an exception");
173 public void testHttpsApiStatistics() {
175 main = startApiService(false);
176 final Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT);
177 final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
178 validateStatisticsReport(report, 200);
179 } catch (final Exception exp) {
180 LOGGER.error("testHttpsApiStatistics failed", exp);
181 fail("Test should not throw an exception");
186 public void testApiStatisticsConstructorIsPrivate() {
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"));
197 private Main startApiService(final boolean http) {
198 final String[] apiConfigParameters = new String[2];
200 apiConfigParameters[0] = "-c";
201 apiConfigParameters[1] = "parameters/ApiConfigParameters.json";
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";
210 return new Main(apiConfigParameters);
213 private void stopApiService(final Main main) throws PolicyApiException {
217 private Invocation.Builder sendHttpRequest(final String endpoint) throws Exception {
218 final ClientConfig clientConfig = new ClientConfig();
220 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
221 clientConfig.register(feature);
223 final Client client = ClientBuilder.newClient(clientConfig);
224 final WebTarget webTarget = client.target("http://localhost:6969/policy/api/v1/" + endpoint);
226 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
228 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
229 throw new IllegalStateException("cannot connect to port 6969");
231 return invocationBuilder;
234 private Invocation.Builder sendHttpsRequest(final String endpoint) throws Exception {
236 final TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() {
239 public X509Certificate[] getAcceptedIssuers() {
240 return new X509Certificate[0];
244 public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
247 public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
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,
254 final Client client = clientBuilder.build();
255 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
256 client.register(feature);
258 final WebTarget webTarget = client.target("https://localhost:6969/policy/api/v1/" + endpoint);
260 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
262 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
263 throw new IllegalStateException("cannot connect to port 6969");
265 return invocationBuilder;
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();
286 private void validateStatisticsReport(final StatisticsReport report, final int code) {
287 assertEquals(code, report.getCode());
290 private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
291 final String message, final HealthCheckReport report) {
292 assertEquals(name, report.getName());
293 assertEquals(url, report.getUrl());
294 assertEquals(healthy, report.isHealthy());
295 assertEquals(code, report.getCode());
296 assertEquals(message, report.getMessage());