236cc16f9d7131fc95259a61d2d3c5b58fff699e
[dcaegen2/services/sdk.git] /
1 /*
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
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  * ============LICENSE_END=====================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.standardization.moher.adapters.springwebflux;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import java.util.concurrent.atomic.AtomicReference;
26
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29
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;
37
38 import reactor.core.publisher.Mono;
39
40 class HealthControllerIT {
41
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())
47             .create();
48
49     @Test
50     void readinessProbeShouldReturnOkWhenHealthy() {
51         final Health expectedHealth = Health.createHealthy("Ready to go");
52         currentHealth.set(expectedHealth);
53
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);
58                 });
59     }
60
61     @Test
62     void readinessProbeShouldReturnUnavailableWhenNotHealthy() {
63         final Health expectedHealth = Health.createUnhealthy("Waiting for CBS update");
64         currentHealth.set(expectedHealth);
65
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);
70                 });
71     }
72
73     @Test
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);
78                 });
79     }
80
81 }