359f9cb8a0728a6b810409dac92907c3979babea
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestHealthCheckRestControllerV1.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
5  *  Modifications Copyright (C) 2022 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.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import static org.mockito.Mockito.when;
28
29 import javax.ws.rs.client.Invocation;
30 import org.junit.Test;
31 import org.onap.policy.common.endpoints.report.HealthCheckReport;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.springframework.boot.test.mock.mockito.MockBean;
34
35 /**
36  * Class to perform unit test of {@link PapRestServer}.
37  *
38  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
39  */
40 public class TestHealthCheckRestControllerV1 extends CommonPapRestServer {
41
42     private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
43
44     @MockBean
45     private PolicyStatusProvider policyStatusProvider;
46
47     @Test
48     public void testSwagger() throws Exception {
49         super.testSwagger(HEALTHCHECK_ENDPOINT);
50     }
51
52     @Test
53     public void testHealthCheckSuccess() throws Exception {
54         final Invocation.Builder invocationBuilder = sendRequest(HEALTHCHECK_ENDPOINT);
55         final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
56         validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
57
58         // verify it fails when no authorization info is included
59         checkUnauthRequest(HEALTHCHECK_ENDPOINT, req -> req.get());
60     }
61
62     @Test
63     public void testHealthCheckActivatorFailure() throws Exception {
64
65         markActivatorDead();
66
67         final Invocation.Builder invocationBuilder = sendRequest(HEALTHCHECK_ENDPOINT);
68         var response = invocationBuilder.get();
69         var report = response.readEntity(HealthCheckReport.class);
70         assertThat(response.getStatus()).isEqualTo(503);
71         validateHealthCheckReport(NAME, SELF, false, 503, NOT_ALIVE, report);
72     }
73
74     @Test
75     public void testHealthCheckDbConnectionFailure() throws Exception {
76         when(policyStatusProvider.getPolicyStatus()).thenThrow(PfModelRuntimeException.class);
77         final Invocation.Builder invocationBuilder = sendRequest(HEALTHCHECK_ENDPOINT);
78         var response = invocationBuilder.get();
79         var report = response.readEntity(HealthCheckReport.class);
80         assertThat(response.getStatus()).isEqualTo(503);
81         validateHealthCheckReport(NAME, SELF, false, 503, NOT_ALIVE, report);
82     }
83
84     private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
85             final String message, final HealthCheckReport report) {
86         assertEquals(name, report.getName());
87         assertEquals(url, report.getUrl());
88         assertEquals(healthy, report.isHealthy());
89         assertEquals(code, report.getCode());
90         assertEquals(message, report.getMessage());
91     }
92 }