2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2018 Intel. All rights reserved.
 
   4  *  Copyright (C) 2019 Nordix Foundation.
 
   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.distribution.main.rest;
 
  24 import static org.junit.Assert.assertEquals;
 
  25 import static org.junit.Assert.fail;
 
  27 import java.security.KeyManagementException;
 
  28 import java.security.NoSuchAlgorithmException;
 
  29 import java.security.SecureRandom;
 
  30 import java.security.cert.X509Certificate;
 
  31 import java.util.Properties;
 
  33 import javax.net.ssl.SSLContext;
 
  34 import javax.net.ssl.TrustManager;
 
  35 import javax.net.ssl.X509TrustManager;
 
  36 import javax.ws.rs.client.Client;
 
  37 import javax.ws.rs.client.ClientBuilder;
 
  38 import javax.ws.rs.client.Invocation;
 
  39 import javax.ws.rs.client.WebTarget;
 
  40 import javax.ws.rs.core.MediaType;
 
  42 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
 
  43 import org.junit.Test;
 
  44 import org.onap.policy.common.utils.network.NetworkUtil;
 
  45 import org.onap.policy.distribution.main.PolicyDistributionException;
 
  46 import org.onap.policy.distribution.main.startstop.Main;
 
  47 import org.slf4j.Logger;
 
  48 import org.slf4j.LoggerFactory;
 
  51  * Class to perform unit test of HealthCheckMonitor.
 
  53  * @author Libo Zhu (libo.zhu@intel.com)
 
  55 public class TestHttpsStatisticDistributionRestServer {
 
  57     private static final Logger LOGGER = LoggerFactory.getLogger(TestHttpsStatisticDistributionRestServer.class);
 
  58     private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
 
  61     public void testHttpsDistributionStatistic()
 
  62             throws PolicyDistributionException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
 
  64             final Main main = startDistributionService();
 
  65             final StatisticsReport report = performStatisticCheck();
 
  66             validateReport(200, 0, 0, 0, 0, 0, 0, report);
 
  67             stopDistributionService(main);
 
  68         } catch (final Exception exp) {
 
  69             LOGGER.error("testHttpsDistributionStatistic failed", exp);
 
  70             fail("Test should not throw an exception");
 
  74     private Main startDistributionService() {
 
  75         final Properties systemProps = System.getProperties();
 
  76         systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
 
  77         systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
 
  78         System.setProperties(systemProps);
 
  80         final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters_Https.json" };
 
  81         return new Main(distributionConfigParameters);
 
  84     private void stopDistributionService(final Main main) throws PolicyDistributionException {
 
  88     private StatisticsReport performStatisticCheck() throws Exception {
 
  90         final TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() {
 
  93             public X509Certificate[] getAcceptedIssuers() {
 
  94                 return new X509Certificate[0];
 
  98             public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
 
 101             public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
 
 104         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
 
 105         sc.init(null, noopTrustManager, new SecureRandom());
 
 106         final ClientBuilder clientBuilder =
 
 107                 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
 
 108         final Client client = clientBuilder.build();
 
 109         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
 
 110         client.register(feature);
 
 112         final WebTarget webTarget = client.target("https://localhost:6969/statistics");
 
 114         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
 
 116         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
 
 117             throw new IllegalStateException("cannot connect to port 6969");
 
 119         return invocationBuilder.get(StatisticsReport.class);
 
 122     private void validateReport(final int code, final int total, final int successCount, final int failureCount,
 
 123             final int download, final int downloadSuccess, final int downloadFailure, final StatisticsReport report) {
 
 124         assertEquals(code, report.getCode());
 
 125         assertEquals(total, report.getTotalDistributionCount());
 
 126         assertEquals(successCount, report.getDistributionSuccessCount());
 
 127         assertEquals(failureCount, report.getDistributionFailureCount());
 
 128         assertEquals(download, report.getTotalDownloadCount());
 
 129         assertEquals(downloadSuccess, report.getDownloadSuccessCount());
 
 130         assertEquals(downloadFailure, report.getDownloadFailureCount());