9520afb687d9a11c3550598270d7593ccbd1cd5c
[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-2021 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.io.IOException;
30 import java.security.SecureRandom;
31 import java.util.Properties;
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.TrustManager;
34 import javax.ws.rs.client.Client;
35 import javax.ws.rs.client.ClientBuilder;
36 import javax.ws.rs.client.Invocation;
37 import javax.ws.rs.client.WebTarget;
38 import javax.ws.rs.core.MediaType;
39 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
40 import org.junit.Test;
41 import org.onap.policy.common.utils.network.NetworkUtil;
42 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
43 import org.onap.policy.distribution.main.PolicyDistributionException;
44 import org.onap.policy.distribution.main.parameters.CommonTestData;
45 import org.onap.policy.distribution.main.startstop.Main;
46
47 /**
48  * Class to perform unit test of HealthCheckMonitor.
49  *
50  * @author Libo Zhu (libo.zhu@intel.com)
51  */
52 public class TestHttpsStatisticDistributionRestServer {
53
54     private int port;
55
56     @Test
57     public void testHttpsDistributionStatistic() {
58         assertThatCode(() -> {
59             final Main main = startDistributionService();
60             final StatisticsReport report = performStatisticCheck();
61             validateReport(200, 0, 0, 0, 0, 0, 0, report);
62             stopDistributionService(main);
63         }).doesNotThrowAnyException();
64     }
65
66     private Main startDistributionService() throws IOException, InterruptedException {
67         final Properties systemProps = System.getProperties();
68         systemProps.put("javax.net.ssl.keyStore", new SelfSignedKeyStore().getKeystoreName());
69         systemProps.put("javax.net.ssl.keyStorePassword", SelfSignedKeyStore.KEYSTORE_PASSWORD);
70         System.setProperties(systemProps);
71
72         port = CommonTestData.makeConfigFile("parameters/DistributionConfigParameters_Https.json");
73         final String[] distributionConfigParameters = { "-c", CommonTestData.CONFIG_FILE };
74         return new Main(distributionConfigParameters);
75     }
76
77     private void stopDistributionService(final Main main) throws PolicyDistributionException {
78         main.shutdown();
79     }
80
81     private StatisticsReport performStatisticCheck() throws Exception {
82         final TrustManager[] noopTrustManager = NetworkUtil.getAlwaysTrustingManager();
83
84         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
85         sc.init(null, noopTrustManager, new SecureRandom());
86         final ClientBuilder clientBuilder =
87                 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
88         final Client client = clientBuilder.build();
89         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
90         client.register(feature);
91
92         final WebTarget webTarget = client.target("https://localhost:" + port + "/statistics");
93
94         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
95
96         CommonTestData.awaitServer(port);
97         return invocationBuilder.get(StatisticsReport.class);
98     }
99
100     private void validateReport(final int code, final int total, final int successCount, final int failureCount,
101             final int download, final int downloadSuccess, final int downloadFailure, final StatisticsReport report) {
102         assertEquals(code, report.getCode());
103         assertEquals(total, report.getTotalDistributionCount());
104         assertEquals(successCount, report.getDistributionSuccessCount());
105         assertEquals(failureCount, report.getDistributionFailureCount());
106         assertEquals(download, report.getTotalDownloadCount());
107         assertEquals(downloadSuccess, report.getDownloadSuccessCount());
108         assertEquals(downloadFailure, report.getDownloadFailureCount());
109     }
110 }