764625657b16ec50726ab35731375774376348e4
[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  *  Modifications Copyright (C) 2020 Nordix Foundation
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.distribution.main.rest;
25
26 import static org.assertj.core.api.Assertions.assertThatCode;
27 import static org.junit.Assert.assertEquals;
28
29 import java.security.SecureRandom;
30 import java.security.cert.X509Certificate;
31 import java.util.Properties;
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.TrustManager;
34 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;
40 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
41 import org.junit.Test;
42 import org.onap.policy.common.utils.network.NetworkUtil;
43 import org.onap.policy.distribution.main.PolicyDistributionException;
44 import org.onap.policy.distribution.main.startstop.Main;
45
46 /**
47  * Class to perform unit test of HealthCheckMonitor.
48  *
49  * @author Libo Zhu (libo.zhu@intel.com)
50  */
51 public class TestHttpsStatisticDistributionRestServer {
52
53     private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
54
55     @Test
56     public void testHttpsDistributionStatistic() {
57         assertThatCode(() -> {
58             final Main main = startDistributionService();
59             final StatisticsReport report = performStatisticCheck();
60             validateReport(200, 0, 0, 0, 0, 0, 0, report);
61             stopDistributionService(main);
62         }).doesNotThrowAnyException();
63     }
64
65     private Main startDistributionService() {
66         final Properties systemProps = System.getProperties();
67         systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
68         systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
69         System.setProperties(systemProps);
70
71         final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters_Https.json" };
72         return new Main(distributionConfigParameters);
73     }
74
75     private void stopDistributionService(final Main main) throws PolicyDistributionException {
76         main.shutdown();
77     }
78
79     private StatisticsReport performStatisticCheck() throws Exception {
80
81         final TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() {
82
83             @Override
84             public X509Certificate[] getAcceptedIssuers() {
85                 return new X509Certificate[0];
86             }
87
88             @Override
89             public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {
90             }
91
92             @Override
93             public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {
94             }
95         } };
96
97         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
98         sc.init(null, noopTrustManager, new SecureRandom());
99         final ClientBuilder clientBuilder =
100                 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
101         final Client client = clientBuilder.build();
102         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
103         client.register(feature);
104
105         final WebTarget webTarget = client.target("https://localhost:6969/statistics");
106
107         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
108
109         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
110             throw new IllegalStateException("cannot connect to port 6969");
111         }
112         return invocationBuilder.get(StatisticsReport.class);
113     }
114
115     private void validateReport(final int code, final int total, final int successCount, final int failureCount,
116             final int download, final int downloadSuccess, final int downloadFailure, final StatisticsReport report) {
117         assertEquals(code, report.getCode());
118         assertEquals(total, report.getTotalDistributionCount());
119         assertEquals(successCount, report.getDistributionSuccessCount());
120         assertEquals(failureCount, report.getDistributionFailureCount());
121         assertEquals(download, report.getTotalDownloadCount());
122         assertEquals(downloadSuccess, report.getDownloadSuccessCount());
123         assertEquals(downloadFailure, report.getDownloadFailureCount());
124     }
125 }