ff91ebe98562a5984625fb0dff224188d2bb3973
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPolicyComponentsHealthCheckControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020, 2022 Nordix Foundation.
4  *  Modifications Copyright (C) 2020-2021 AT&T Inc.
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27
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.Test;
35 import org.onap.policy.common.endpoints.parameters.RestClientParameters;
36 import org.onap.policy.pap.main.parameters.PapParameterGroup;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.test.context.ActiveProfiles;
39
40 /**
41  * Class to perform unit test of {@link PolicyComponentsHealthCheckControllerV1}.
42  *
43  * @author Yehui Wang (yehui.wang@est.tech)
44  */
45 @ActiveProfiles({ "test", "default" })
46 public class TestPolicyComponentsHealthCheckControllerV1 extends CommonPapRestServer {
47
48     private static final String ENDPOINT = "components/healthcheck";
49     private List<RestClientParameters> savedRestClientParameters;
50
51     @Autowired
52     private PapParameterGroup papParameterGroup;
53
54     @Test
55     public void testSwagger() throws Exception {
56         super.testSwagger(ENDPOINT);
57     }
58
59     @Test
60     @SuppressWarnings("unchecked")
61     public void testPolicyComponentsHealthCheck() throws Exception {
62         // take out the other components for healthcheck
63         savedRestClientParameters = papParameterGroup.getHealthCheckRestClientParameters();
64         papParameterGroup.setHealthCheckRestClientParameters(null);
65
66         Invocation.Builder invocationBuilder = sendRequest(ENDPOINT);
67         Response response = invocationBuilder.get();
68         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
69         Map<String, Object> result = new HashMap<>();
70         result = (Map<String, Object>) response.readEntity(GenericType.forInstance(result));
71         // No PDP configured, healthy is false
72         assertFalse((Boolean) result.get("healthy"));
73
74         // put back the other components
75         papParameterGroup.setHealthCheckRestClientParameters(savedRestClientParameters);
76     }
77 }