b49d77042e1197f6485149950f85642cf17f6123
[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  *  Modifications Copyright (C) 2020-2021 AT&T Inc.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import javax.ws.rs.client.Invocation;
32 import javax.ws.rs.core.GenericType;
33 import javax.ws.rs.core.Response;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.common.endpoints.parameters.RestClientParameters;
38 import org.onap.policy.common.parameters.ParameterService;
39 import org.onap.policy.pap.main.parameters.PapParameterGroup;
40 import org.powermock.reflect.Whitebox;
41
42 /**
43  * Class to perform unit test of {@link PolicyComponentsHealthCheckControllerV1}.
44  *
45  * @author Yehui Wang (yehui.wang@est.tech)
46  */
47 public class TestPolicyComponentsHealthCheckControllerV1 extends CommonPapRestServer {
48
49     private static final String ENDPOINT = "components/healthcheck";
50     private static List<RestClientParameters> savedRestClientParameters;
51
52     /**
53      * Set up for the test class.
54      */
55     @BeforeClass
56     public static void setUpClass() {
57         // To skip calling to the remote components
58         PapParameterGroup papParameterGroup = ParameterService.get("PapGroup");
59         List<RestClientParameters> lo = Whitebox.getInternalState(papParameterGroup, "healthCheckRestClientParameters");
60         savedRestClientParameters = 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<RestClientParameters> lo = Whitebox.getInternalState(papParameterGroup, "healthCheckRestClientParameters");
71         lo.addAll(savedRestClientParameters);
72     }
73
74     @Test
75     public void testSwagger() throws Exception {
76         super.testSwagger(ENDPOINT);
77     }
78
79     @Test
80     @SuppressWarnings("unchecked")
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 }