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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.rest;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import static org.mockito.Mockito.when;
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;
36 * Class to perform unit test of {@link PapRestServer}.
38 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
40 public class TestHealthCheckRestControllerV1 extends CommonPapRestServer {
42 private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
45 private PolicyStatusProvider policyStatusProvider;
48 public void testSwagger() throws Exception {
49 super.testSwagger(HEALTHCHECK_ENDPOINT);
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);
58 // verify it fails when no authorization info is included
59 checkUnauthRequest(HEALTHCHECK_ENDPOINT, req -> req.get());
63 public void testHealthCheckActivatorFailure() throws Exception {
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);
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);
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());