2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Intel. All rights reserved.
4 * Copyright (C) 2019 Nordix Foundation.
5 * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.distribution.main.rest;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
28 import java.security.KeyManagementException;
29 import java.security.NoSuchAlgorithmException;
30 import java.security.SecureRandom;
31 import java.security.cert.X509Certificate;
32 import java.util.Properties;
34 import javax.net.ssl.SSLContext;
35 import javax.net.ssl.TrustManager;
36 import javax.net.ssl.X509TrustManager;
37 import javax.ws.rs.client.Client;
38 import javax.ws.rs.client.ClientBuilder;
39 import javax.ws.rs.client.Invocation;
40 import javax.ws.rs.client.WebTarget;
41 import javax.ws.rs.core.MediaType;
43 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
44 import org.junit.Test;
45 import org.onap.policy.common.utils.network.NetworkUtil;
46 import org.onap.policy.distribution.main.PolicyDistributionException;
47 import org.onap.policy.distribution.main.startstop.Main;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * Class to perform unit test of HealthCheckMonitor.
54 * @author Libo Zhu (libo.zhu@intel.com)
56 public class TestHttpsStatisticDistributionRestServer {
58 private static final Logger LOGGER = LoggerFactory.getLogger(TestHttpsStatisticDistributionRestServer.class);
59 private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
62 public void testHttpsDistributionStatistic()
63 throws PolicyDistributionException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
65 final Main main = startDistributionService();
66 final StatisticsReport report = performStatisticCheck();
67 validateReport(200, 0, 0, 0, 0, 0, 0, report);
68 stopDistributionService(main);
69 } catch (final Exception exp) {
70 LOGGER.error("testHttpsDistributionStatistic failed", exp);
71 fail("Test should not throw an exception");
75 private Main startDistributionService() {
76 final Properties systemProps = System.getProperties();
77 systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
78 systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
79 System.setProperties(systemProps);
81 final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters_Https.json" };
82 return new Main(distributionConfigParameters);
85 private void stopDistributionService(final Main main) throws PolicyDistributionException {
89 private StatisticsReport performStatisticCheck() throws Exception {
91 final TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() {
94 public X509Certificate[] getAcceptedIssuers() {
95 return new X509Certificate[0];
99 public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {
103 public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {
107 final SSLContext sc = SSLContext.getInstance("TLSv1.2");
108 sc.init(null, noopTrustManager, new SecureRandom());
109 final ClientBuilder clientBuilder =
110 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
111 final Client client = clientBuilder.build();
112 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
113 client.register(feature);
115 final WebTarget webTarget = client.target("https://localhost:6969/statistics");
117 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
119 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
120 throw new IllegalStateException("cannot connect to port 6969");
122 return invocationBuilder.get(StatisticsReport.class);
125 private void validateReport(final int code, final int total, final int successCount, final int failureCount,
126 final int download, final int downloadSuccess, final int downloadFailure, final StatisticsReport report) {
127 assertEquals(code, report.getCode());
128 assertEquals(total, report.getTotalDistributionCount());
129 assertEquals(successCount, report.getDistributionSuccessCount());
130 assertEquals(failureCount, report.getDistributionFailureCount());
131 assertEquals(download, report.getTotalDownloadCount());
132 assertEquals(downloadSuccess, report.getDownloadSuccessCount());
133 assertEquals(downloadFailure, report.getDownloadFailureCount());