2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2022 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 javax.ws.rs.client.SyncInvoker;
31 import org.junit.Test;
32 import org.onap.policy.common.endpoints.report.HealthCheckReport;
33 import org.onap.policy.models.base.PfModelRuntimeException;
34 import org.springframework.boot.test.mock.mockito.MockBean;
35 import org.springframework.test.context.ActiveProfiles;
38 * Class to perform unit test of {@link HealthCheckRestControllerV1}.
40 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
42 @ActiveProfiles({ "test", "default" })
43 public class TestHealthCheckRestControllerV1 extends CommonPapRestServer {
45 private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
48 private PolicyStatusProvider policyStatusProvider;
51 public void testSwagger() throws Exception {
52 super.testSwagger(HEALTHCHECK_ENDPOINT);
56 public void testHealthCheckSuccess() throws Exception {
57 final Invocation.Builder invocationBuilder = sendRequest(HEALTHCHECK_ENDPOINT);
58 final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
59 validateHealthCheckReport(true, 200, ALIVE, report);
61 // verify it fails when no authorization info is included
62 checkUnauthRequest(HEALTHCHECK_ENDPOINT, SyncInvoker::get);
66 public void testHealthCheckActivatorFailure() throws Exception {
70 final Invocation.Builder invocationBuilder = sendRequest(HEALTHCHECK_ENDPOINT);
71 var response = invocationBuilder.get();
72 var report = response.readEntity(HealthCheckReport.class);
73 assertThat(response.getStatus()).isEqualTo(503);
74 validateHealthCheckReport(false, 503, NOT_ALIVE, report);
78 public void testHealthCheckDbConnectionFailure() throws Exception {
79 when(policyStatusProvider.getPolicyStatus()).thenThrow(PfModelRuntimeException.class);
80 final Invocation.Builder invocationBuilder = sendRequest(HEALTHCHECK_ENDPOINT);
81 var response = invocationBuilder.get();
82 var report = response.readEntity(HealthCheckReport.class);
83 assertThat(response.getStatus()).isEqualTo(503);
84 validateHealthCheckReport(false, 503, NOT_ALIVE, report);
87 private void validateHealthCheckReport(final boolean healthy, final int code,
88 final String message, final HealthCheckReport report) {
89 assertEquals(CommonPapRestServer.NAME, report.getName());
90 assertEquals(CommonPapRestServer.SELF, report.getUrl());
91 assertEquals(healthy, report.isHealthy());
92 assertEquals(code, report.getCode());
93 assertEquals(message, report.getMessage());