Add test cases for consolidated policy health check
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPolicyComponentsHealthCheckControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
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.pap.main.rest;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import javax.ws.rs.client.Invocation;
31 import javax.ws.rs.core.GenericType;
32 import javax.ws.rs.core.Response;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
37 import org.onap.policy.common.parameters.ParameterService;
38 import org.onap.policy.pap.main.parameters.PapParameterGroup;
39 import org.powermock.reflect.Whitebox;
40
41 /**
42  * Class to perform unit test of {@link PolicyComponentsHealthCheckControllerV1}.
43  *
44  * @author Yehui Wang (yehui.wang@est.tech)
45  */
46 public class TestPolicyComponentsHealthCheckControllerV1 extends CommonPapRestServer {
47
48     private static final String ENDPOINT = "components/healthcheck";
49     private static List<BusTopicParams> savedBusTopicParams;
50
51     /**
52      * Set up for the test class.
53      */
54     @BeforeClass
55     public static void setUpClass() {
56         // To skip calling to the remote components
57         PapParameterGroup papParameterGroup = ParameterService.get("PapGroup");
58         List<BusTopicParams> lo = Whitebox.getInternalState(papParameterGroup,
59             "healthCheckRestClientParameters");
60         savedBusTopicParams = new ArrayList<>(lo);
61         lo.clear();
62     }
63
64     /**
65      * Tear down for the test class.
66      */
67     @AfterClass
68     public static void tearDownClass() {
69         PapParameterGroup papParameterGroup = ParameterService.get("PapGroup");
70         List<BusTopicParams> lo = Whitebox.getInternalState(papParameterGroup,
71             "healthCheckRestClientParameters");
72         lo.addAll(savedBusTopicParams);
73     }
74
75     @Test
76     public void testSwagger() throws Exception {
77         super.testSwagger(ENDPOINT);
78     }
79
80     @Test
81     public void testPolicyComponentsHealthCheck() throws Exception {
82         Invocation.Builder invocationBuilder = sendRequest(ENDPOINT);
83         Response response = invocationBuilder.get();
84         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
85         Map<String, Object> result = new HashMap<>();
86         result = (Map<String, Object>) response.readEntity(GenericType.forInstance(result));
87         // No PDP configured, healthy is false
88         assertFalse((Boolean) result.get("healthy"));
89     }
90 }