2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019 Nokia. 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.
18 * ============LICENSE_END=====================================
21 package org.onap.dcaegen2.services.sdk.standardization.moher.adapters.springwebflux;
23 import static org.assertj.core.api.Assertions.assertThat;
25 import java.util.concurrent.atomic.AtomicReference;
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
30 import org.junit.jupiter.api.Test;
31 import org.onap.dcaegen2.services.sdk.standardization.moher.health.api.AliveMessage;
32 import org.onap.dcaegen2.services.sdk.standardization.moher.health.api.GsonAdaptersHealth;
33 import org.onap.dcaegen2.services.sdk.standardization.moher.health.api.Health;
34 import org.onap.dcaegen2.services.sdk.standardization.moher.health.api.HealthProvider;
35 import org.springframework.http.MediaType;
36 import org.springframework.test.web.reactive.server.WebTestClient;
38 import reactor.core.publisher.Mono;
40 class HealthControllerIT {
42 private final AtomicReference<Health> currentHealth = new AtomicReference<>();
43 private final HealthProvider healthProvider = () -> Mono.fromCallable(currentHealth::get);
44 private final HealthController sut = HealthController.create(healthProvider);
45 private final WebTestClient client = WebTestClient.bindToController(sut).build();
46 private final Gson gsonForDeserialization = new GsonBuilder().registerTypeAdapterFactory(new GsonAdaptersHealth())
50 void readinessProbeShouldReturnOkWhenHealthy() {
51 final Health expectedHealth = Health.createHealthy("Ready to go");
52 currentHealth.set(expectedHealth);
54 client.get().uri("/health/ready").accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk()
55 .expectBody(String.class).value(body -> {
56 final Health actualHealth = gsonForDeserialization.fromJson(body, Health.class);
57 assertThat(actualHealth).isEqualTo(expectedHealth);
62 void readinessProbeShouldReturnUnavailableWhenNotHealthy() {
63 final Health expectedHealth = Health.createUnhealthy("Waiting for CBS update");
64 currentHealth.set(expectedHealth);
66 client.get().uri("/health/ready").accept(MediaType.APPLICATION_JSON).exchange().expectStatus()
67 .is5xxServerError().expectBody(String.class).value(body -> {
68 final Health actualHealth = gsonForDeserialization.fromJson(body, Health.class);
69 assertThat(actualHealth).isEqualTo(expectedHealth);
74 void livenessProbeShouldAlwaysReturnOk() {
75 client.get().uri("/health/alive").accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk()
76 .expectBody(String.class).value(body -> {
77 assertThat(body).isEqualTo(AliveMessage.ALIVE_MESSAGE_JSON);