add rest serve and distribution framework
[multicloud/framework.git] / artifactbroker / main / src / test / java / org / onap / policy / distribution / main / rest / TestDistributionRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. 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 import static org.junit.Assert.assertTrue;
25
26 import javax.ws.rs.client.Client;
27 import javax.ws.rs.client.ClientBuilder;
28 import javax.ws.rs.client.Invocation;
29 import javax.ws.rs.client.WebTarget;
30 import javax.ws.rs.core.MediaType;
31
32 import org.glassfish.jersey.client.ClientConfig;
33 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
34 import org.junit.Test;
35 import org.onap.policy.common.endpoints.report.HealthCheckReport;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.distribution.main.PolicyDistributionException;
39 import org.onap.policy.distribution.main.parameters.RestServerParameters;
40 import org.onap.policy.distribution.main.startstop.Main;
41
42 /**
43  * Class to perform unit test of HealthCheckMonitor.
44  *
45  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
46  */
47 public class TestDistributionRestServer {
48
49     private static final Logger LOGGER = FlexLogger.getLogger(TestDistributionRestServer.class);
50     private static final String NOT_ALIVE = "not alive";
51     private static final String ALIVE = "alive";
52     private static final String SELF = "self";
53     private static final String NAME = "Policy SSD";
54
55     @Test
56     public void testHealthCheckSuccess() throws PolicyDistributionException, InterruptedException {
57         final String reportString = "Report [name=Policy SSD, url=self, healthy=true, code=200, message=alive]";
58         final Main main = startDistributionService();
59         final HealthCheckReport report = performHealthCheck();
60         validateReport(NAME, SELF, true, 200, ALIVE, reportString, report);
61         stopDistributionService(main);
62     }
63
64     private Main startDistributionService() {
65         final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters.json" };
66         return new Main(distributionConfigParameters);
67     }
68
69     private void stopDistributionService(final Main main) throws PolicyDistributionException {
70         main.shutdown();
71     }
72
73     private HealthCheckReport performHealthCheck() throws InterruptedException {
74         HealthCheckReport response = null;
75         final ClientConfig clientConfig = new ClientConfig();
76
77         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
78         clientConfig.register(feature);
79
80         final Client client = ClientBuilder.newClient(clientConfig);
81         final WebTarget webTarget = client.target("http://localhost:6969/healthcheck");
82
83         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
84         while (response == null) {
85             try {
86                 response = invocationBuilder.get(HealthCheckReport.class);
87             } catch (final Exception exp) {
88                 LOGGER.info("the server is not started yet. We will retry again");
89             }
90         }
91         return response;
92     }
93
94     private void validateReport(final String name, final String url, final boolean healthy, final int code,
95             final String message, final String reportString, final HealthCheckReport report) {
96         assertEquals(name, report.getName());
97         assertEquals(url, report.getUrl());
98         assertEquals(healthy, report.isHealthy());
99         assertEquals(code, report.getCode());
100         assertEquals(message, report.getMessage());
101         assertEquals(reportString, report.toString());
102     }
103 }