add rest serve and distribution framework
[multicloud/framework.git] / artifactbroker / main / src / test / java / org / onap / policy / distribution / main / rest / TestHttpsDistributionRestServer.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.endpoints.report.HealthCheckReport;
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.startstop.Main;
48
49 /**
50  * Class to perform unit test of HealthCheckMonitor.
51  *
52  * @author Libo Zhu (libo.zhu@intel.com)
53  */
54 public class TestHttpsDistributionRestServer {
55
56     private static final Logger LOGGER = FlexLogger.getLogger(TestDistributionRestServer.class);
57     private static final String ALIVE = "alive";
58     private static final String SELF = "self";
59     private static final String NAME = "Policy SSD";
60     private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
61
62     @Test
63     public void testHealthCheckSuccess()
64         throws PolicyDistributionException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
65         final String reportString = "Report [name=Policy SSD, url=self, healthy=true, code=200, message=alive]";
66         final Main main = startDistributionService();
67         final HealthCheckReport report = performHealthCheck();
68         validateReport(NAME, SELF, true, 200, ALIVE, reportString, report);
69         stopDistributionService(main);
70     }
71
72     private Main startDistributionService() {
73         Properties systemProps = System.getProperties();
74         systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
75         systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
76         System.setProperties(systemProps);
77
78         final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters_Https.json" };
79         return new Main(distributionConfigParameters);
80     }
81
82     private void stopDistributionService(final Main main) throws PolicyDistributionException {
83         main.shutdown();
84     }
85
86     private HealthCheckReport performHealthCheck()
87         throws InterruptedException, KeyManagementException, NoSuchAlgorithmException {
88         HealthCheckReport response = null;
89
90         TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() {
91
92             @Override
93             public X509Certificate[] getAcceptedIssuers() {
94                 return new X509Certificate[0];
95             }
96
97             @Override
98             public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
99             }
100
101             @Override
102             public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
103             }
104         } };
105
106         SSLContext sc = SSLContext.getInstance("TLSv1.2");
107         sc.init(null, noopTrustManager, new SecureRandom());
108         final ClientBuilder clientBuilder = ClientBuilder.newBuilder().sslContext(sc)
109             .hostnameVerifier((host, session) -> true);
110         final Client client = clientBuilder.build();
111         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
112         client.register(feature);
113
114         final WebTarget webTarget = client.target("https://localhost:6969/healthcheck");
115
116         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
117         while (response == null) {
118             try {
119                 response = invocationBuilder.get(HealthCheckReport.class);
120             } catch (final Exception exp) {
121                 LOGGER.error("the server is not started yet. We will retry again", exp);
122             }
123         }
124         return response;
125     }
126
127     private void validateReport(final String name, final String url, final boolean healthy, final int code,
128         final String message, final String reportString, final HealthCheckReport report) {
129         assertEquals(name, report.getName());
130         assertEquals(url, report.getUrl());
131         assertEquals(healthy, report.isHealthy());
132         assertEquals(code, report.getCode());
133         assertEquals(message, report.getMessage());
134         assertEquals(reportString, report.toString());
135     }
136 }