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;
 
  24 import static org.junit.Assert.assertTrue;
 
  26 import java.security.KeyManagementException;
 
  27 import java.security.NoSuchAlgorithmException;
 
  28 import java.security.SecureRandom;
 
  29 import java.security.cert.X509Certificate;
 
  30 import java.util.Properties;
 
  32 import javax.net.ssl.SSLContext;
 
  33 import javax.net.ssl.TrustManager;
 
  34 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.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.parameters.CommonTestData;
 
  48 import org.onap.policy.distribution.main.parameters.RestServerParameters;
 
  49 import org.onap.policy.distribution.main.startstop.Main;
 
  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 = FlexLogger.getLogger(TestHttpsStatisticDistributionRestServer.class);
 
  59     private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
 
  62     public void testHealthCheckSuccess() throws PolicyDistributionException, InterruptedException,
 
  63         KeyManagementException, NoSuchAlgorithmException {
 
  64         final String reportString = "StatisticsReport [code=200, totalDistributionCount=0, distributionSuccessCount=0, "
 
  65                                     + "distributionFailureCount=0, totalDownloadCount=0, "
 
  66                                     + "downloadSuccessCount=0, downloadFailureCount=0]";
 
  67         final Main main = startDistributionService();
 
  68         final StatisticsReport report = performStatisticCheck();
 
  69         validateReport(200, 0, 0, 0, 0, 0, 0, reportString, report);
 
  70         stopDistributionService(main);
 
  73     private Main startDistributionService() {
 
  74         Properties systemProps = System.getProperties();
 
  75         systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
 
  76         systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
 
  77         System.setProperties(systemProps);
 
  79         final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters_Https.json" };
 
  80         return new Main(distributionConfigParameters);
 
  83     private void stopDistributionService(final Main main) throws PolicyDistributionException {
 
  87     private StatisticsReport performStatisticCheck() throws InterruptedException, KeyManagementException,
 
  88         NoSuchAlgorithmException {
 
  89         StatisticsReport response = null;
 
  91         TrustManager[] noopTrustManager = new TrustManager[]{
 
  92             new X509TrustManager() {
 
  95                 public X509Certificate[] getAcceptedIssuers() {
 
  96                     return new X509Certificate[0];
 
 100                 public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
 
 104                 public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
 
 109         SSLContext sc = SSLContext.getInstance("TLSv1.2");
 
 110         sc.init(null, noopTrustManager, new SecureRandom());
 
 111         final ClientBuilder clientBuilder =
 
 112             ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
 
 113         final Client client = clientBuilder.build();
 
 114         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
 
 115         client.register(feature);
 
 117         final WebTarget webTarget = client.target("https://localhost:6969/statistics");
 
 119         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
 
 120         while (response == null) {
 
 122                 response = invocationBuilder.get(StatisticsReport.class);
 
 123             } catch (final Exception exp) {
 
 124                 LOGGER.info("the server is not started yet. We will retry again");
 
 131     private void validateReport(final int code, final int total, final int successCount, final int failureCount,
 
 132             final int download, final int downloadSuccess, final int downloadFailure, final String reportString, 
 
 133             final StatisticsReport report) {
 
 134         assertEquals(code, report.getCode());
 
 135         assertEquals(total, report.getTotalDistributionCount());
 
 136         assertEquals(successCount, report.getDistributionSuccessCount());
 
 137         assertEquals(failureCount, report.getDistributionFailureCount());
 
 138         assertEquals(download, report.getTotalDownloadCount());
 
 139         assertEquals(downloadSuccess, report.getDownloadSuccessCount());
 
 140         assertEquals(downloadFailure, report.getDownloadFailureCount());
 
 141         assertEquals(reportString, report.toString());