8d29c8ef9fa0a0ff9b732df4c339a55899ccbc99
[policy/distribution.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.distribution.main.rest;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
27
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;
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;
41 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
42 import org.junit.Test;
43 import org.onap.policy.common.utils.network.NetworkUtil;
44 import org.onap.policy.distribution.main.PolicyDistributionException;
45 import org.onap.policy.distribution.main.startstop.Main;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * Class to perform unit test of HealthCheckMonitor.
51  *
52  * @author Libo Zhu (libo.zhu@intel.com)
53  */
54 public class TestHttpsStatisticDistributionRestServer {
55
56     private static final Logger LOGGER = LoggerFactory.getLogger(TestHttpsStatisticDistributionRestServer.class);
57     private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
58
59     @Test
60     public void testHttpsDistributionStatistic()
61             throws PolicyDistributionException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
62         try {
63             final Main main = startDistributionService();
64             final StatisticsReport report = performStatisticCheck();
65             validateReport(200, 0, 0, 0, 0, 0, 0, report);
66             stopDistributionService(main);
67         } catch (final Exception exp) {
68             LOGGER.error("testHttpsDistributionStatistic failed", exp);
69             fail("Test should not throw an exception");
70         }
71     }
72
73     private Main startDistributionService() {
74         final 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);
78
79         final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters_Https.json" };
80         return new Main(distributionConfigParameters);
81     }
82
83     private void stopDistributionService(final Main main) throws PolicyDistributionException {
84         main.shutdown();
85     }
86
87     private StatisticsReport performStatisticCheck() throws Exception {
88
89         final TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() {
90
91             @Override
92             public X509Certificate[] getAcceptedIssuers() {
93                 return new X509Certificate[0];
94             }
95
96             @Override
97             public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {
98             }
99
100             @Override
101             public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {
102             }
103         } };
104
105         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
106         sc.init(null, noopTrustManager, new SecureRandom());
107         final ClientBuilder clientBuilder =
108                 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
109         final Client client = clientBuilder.build();
110         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
111         client.register(feature);
112
113         final WebTarget webTarget = client.target("https://localhost:6969/statistics");
114
115         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
116
117         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
118             throw new IllegalStateException("cannot connect to port 6969");
119         }
120         return invocationBuilder.get(StatisticsReport.class);
121     }
122
123     private void validateReport(final int code, final int total, final int successCount, final int failureCount,
124             final int download, final int downloadSuccess, final int downloadFailure, final StatisticsReport report) {
125         assertEquals(code, report.getCode());
126         assertEquals(total, report.getTotalDistributionCount());
127         assertEquals(successCount, report.getDistributionSuccessCount());
128         assertEquals(failureCount, report.getDistributionFailureCount());
129         assertEquals(download, report.getTotalDownloadCount());
130         assertEquals(downloadSuccess, report.getDownloadSuccessCount());
131         assertEquals(downloadFailure, report.getDownloadFailureCount());
132     }
133 }