2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Intel. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.main.rest;
23 import static org.junit.Assert.assertEquals;
25 import java.security.KeyManagementException;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.SecureRandom;
28 import java.security.cert.X509Certificate;
29 import java.util.Properties;
31 import javax.net.ssl.SSLContext;
32 import javax.net.ssl.TrustManager;
33 import javax.net.ssl.X509TrustManager;
35 import javax.ws.rs.client.Client;
36 import javax.ws.rs.client.ClientBuilder;
37 import javax.ws.rs.client.Invocation;
38 import javax.ws.rs.client.WebTarget;
39 import javax.ws.rs.core.MediaType;
41 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
42 import org.junit.Test;
43 import org.onap.policy.common.endpoints.report.HealthCheckReport;
44 import org.onap.policy.common.logging.flexlogger.FlexLogger;
45 import org.onap.policy.common.logging.flexlogger.Logger;
46 import org.onap.policy.distribution.main.PolicyDistributionException;
47 import org.onap.policy.distribution.main.startstop.Main;
50 * Class to perform unit test of HealthCheckMonitor.
52 * @author Libo Zhu (libo.zhu@intel.com)
54 public class TestHttpsDistributionRestServer {
56 private static final Logger LOGGER = FlexLogger.getLogger(TestDistributionRestServer.class);
57 private static final String ALIVE = "alive";
58 private static final String SELF = "self";
59 private static final String NAME = "Policy SSD";
60 private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
63 public void testHealthCheckSuccess()
64 throws PolicyDistributionException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
65 final String reportString = "Report [name=Policy SSD, url=self, healthy=true, code=200, message=alive]";
66 final Main main = startDistributionService();
67 final HealthCheckReport report = performHealthCheck();
68 validateReport(NAME, SELF, true, 200, ALIVE, reportString, report);
69 stopDistributionService(main);
72 private Main startDistributionService() {
73 Properties systemProps = System.getProperties();
74 systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
75 systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
76 System.setProperties(systemProps);
78 final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters_Https.json" };
79 return new Main(distributionConfigParameters);
82 private void stopDistributionService(final Main main) throws PolicyDistributionException {
86 private HealthCheckReport performHealthCheck()
87 throws InterruptedException, KeyManagementException, NoSuchAlgorithmException {
88 HealthCheckReport response = null;
90 TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() {
93 public X509Certificate[] getAcceptedIssuers() {
94 return new X509Certificate[0];
98 public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
102 public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
106 SSLContext sc = SSLContext.getInstance("TLSv1.2");
107 sc.init(null, noopTrustManager, new SecureRandom());
108 final ClientBuilder clientBuilder = ClientBuilder.newBuilder().sslContext(sc)
109 .hostnameVerifier((host, session) -> true);
110 final Client client = clientBuilder.build();
111 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
112 client.register(feature);
114 final WebTarget webTarget = client.target("https://localhost:6969/healthcheck");
116 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
117 while (response == null) {
119 response = invocationBuilder.get(HealthCheckReport.class);
120 } catch (final Exception exp) {
121 LOGGER.error("the server is not started yet. We will retry again", exp);
127 private void validateReport(final String name, final String url, final boolean healthy, final int code,
128 final String message, final String reportString, final HealthCheckReport report) {
129 assertEquals(name, report.getName());
130 assertEquals(url, report.getUrl());
131 assertEquals(healthy, report.isHealthy());
132 assertEquals(code, report.getCode());
133 assertEquals(message, report.getMessage());
134 assertEquals(reportString, report.toString());