add rest serve and distribution framework
[multicloud/framework.git] / artifactbroker / main / src / test / java / org / onap / policy / distribution / main / rest / TestHttpsStatisticDistributionRestServer.backup
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.main.rest;
22
23 import static org.junit.Assert.assertEquals;
24
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;
30
31 import javax.net.ssl.SSLContext;
32 import javax.net.ssl.TrustManager;
33 import javax.net.ssl.X509TrustManager;
34
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;
40
41 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
42 import org.junit.Test;
43 import org.onap.policy.common.logging.flexlogger.FlexLogger;
44 import org.onap.policy.common.logging.flexlogger.Logger;
45 import org.onap.policy.distribution.main.PolicyDistributionException;
46 import org.onap.policy.distribution.main.startstop.Main;
47
48 /**
49  * Class to perform unit test of HealthCheckMonitor.
50  *
51  * @author Libo Zhu (libo.zhu@intel.com)
52  */
53 public class TestHttpsStatisticDistributionRestServer {
54
55     private static final Logger LOGGER = FlexLogger.getLogger(TestHttpsStatisticDistributionRestServer.class);
56     private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
57
58     @Test
59     public void testDistributionStatistic()
60         throws PolicyDistributionException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
61         final String reportString = "StatisticsReport [code=200, totalDistributionCount=0, distributionSuccessCount=0, "
62             + "distributionFailureCount=0, totalDownloadCount=0, " + "downloadSuccessCount=0, downloadFailureCount=0]";
63         final Main main = startDistributionService();
64         final StatisticsReport report = performStatisticCheck();
65         validateReport(200, 0, 0, 0, 0, 0, 0, reportString, report);
66         stopDistributionService(main);
67     }
68
69     private Main startDistributionService() {
70         Properties systemProps = System.getProperties();
71         systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
72         systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
73         System.setProperties(systemProps);
74
75         final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters_Https.json" };
76         return new Main(distributionConfigParameters);
77     }
78
79     private void stopDistributionService(final Main main) throws PolicyDistributionException {
80         main.shutdown();
81     }
82
83     private StatisticsReport performStatisticCheck()
84         throws InterruptedException, KeyManagementException, NoSuchAlgorithmException {
85         StatisticsReport response = null;
86
87         TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() {
88
89             @Override
90             public X509Certificate[] getAcceptedIssuers() {
91                 return new X509Certificate[0];
92             }
93
94             @Override
95             public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
96             }
97
98             @Override
99             public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
100             }
101         } };
102
103         SSLContext sc = SSLContext.getInstance("TLSv1.2");
104         sc.init(null, noopTrustManager, new SecureRandom());
105         final ClientBuilder clientBuilder = ClientBuilder.newBuilder().sslContext(sc)
106             .hostnameVerifier((host, session) -> true);
107         final Client client = clientBuilder.build();
108         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
109         client.register(feature);
110
111         final WebTarget webTarget = client.target("https://localhost:6969/statistics");
112
113         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
114         while (response == null) {
115             try {
116                 response = invocationBuilder.get(StatisticsReport.class);
117             } catch (final Exception exp) {
118                 LOGGER.error("the server is not started yet. We will retry again", exp);
119             }
120         }
121         return response;
122     }
123
124     private void validateReport(final int code, final int total, final int successCount, final int failureCount,
125         final int download, final int downloadSuccess, final int downloadFailure, final String reportString,
126         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());
134         assertEquals(reportString, report.toString());
135     }
136 }