Skip to content

Commit

Permalink
Update MessagingHealthTest to use Nima WebClient instead of Reactive …
Browse files Browse the repository at this point in the history
…WebClient (#6814)

* Update MessagingHealthTest to use Nima WebClient instead of Reactive WebClient

Signed-off-by: aserkes <andrii.serkes@oracle.com>

* Update pom.xml

* Update pom.xml

* replace Nima WebClient by jakarta.ws.rs.client.Client

Signed-off-by: aserkes <andrii.serkes@oracle.com>

* add WebTarget

Signed-off-by: aserkes <andrii.serkes@oracle.com>

---------

Signed-off-by: aserkes <andrii.serkes@oracle.com>
  • Loading branch information
aserkes authored May 17, 2023
1 parent f5ae01c commit 0fd0d0a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 48 deletions.
15 changes: 0 additions & 15 deletions microprofile/messaging/health/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.reactive.webclient</groupId>
<artifactId>helidon-reactive-webclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.reactive.media</groupId>
<artifactId>helidon-reactive-media-jsonp</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.reactive.media</groupId>
<artifactId>helidon-reactive-media-jsonb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-junit5</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,14 +30,15 @@
import io.helidon.microprofile.tests.junit5.AddExtensions;
import io.helidon.microprofile.tests.junit5.DisableDiscovery;
import io.helidon.microprofile.tests.junit5.HelidonTest;
import io.helidon.reactive.media.jsonp.JsonpSupport;
import io.helidon.reactive.webclient.WebClient;

import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Response;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;
Expand Down Expand Up @@ -78,41 +79,36 @@
@AddExtension(JaxRsCdiExtension.class),
@AddExtension(HealthCdiExtension.class),
@AddExtension(MessagingCdiExtension.class),
@AddExtension(CdiComponentProvider.class)
})
public class MessagingHealthTest {

private static final String ERROR_MESSAGE = "BOOM!";

private WebClient client;

@BeforeEach
void setUp() {
ServerCdiExtension server = CDI.current().select(ServerCdiExtension.class).get();
client = WebClient.builder()
.baseUri("http://localhost:" + server.port())
.addReader(JsonpSupport.reader())
.build();
}

@Test
void alivenessWithErrorSignal(SeContainer container) {
void alivenessWithErrorSignal(WebTarget webTarget, SeContainer container) {
TestMessagingBean bean = container.select(TestMessagingBean.class).get();

assertMessagingHealth(UP, Map.of(
assertMessagingHealth(webTarget, UP, Map.of(
CHANNEL_1, UP,
CHANNEL_2, UP
));

bean.getEmitter1().fail(new RuntimeException(ERROR_MESSAGE));
assertMessagingHealth(DOWN, Map.of(
assertMessagingHealth(webTarget, DOWN, Map.of(
CHANNEL_1, DOWN,
CHANNEL_2, UP
));
assertThat(bean.getSubscriber1().error().await(200, TimeUnit.MILLISECONDS).getMessage(),
equalTo(ERROR_MESSAGE));

bean.getEmitter2().fail(new RuntimeException(ERROR_MESSAGE));
assertMessagingHealth(DOWN, Map.of(
assertMessagingHealth(webTarget, DOWN, Map.of(
CHANNEL_1, DOWN,
CHANNEL_2, DOWN
));
Expand All @@ -121,51 +117,46 @@ void alivenessWithErrorSignal(SeContainer container) {
}

@Test
void alivenessWithCancelSignal(SeContainer container) {
void alivenessWithCancelSignal(WebTarget webTarget, SeContainer container) {
TestMessagingBean bean = container.select(TestMessagingBean.class).get();

assertMessagingHealth(UP, Map.of(
assertMessagingHealth(webTarget, UP, Map.of(
CHANNEL_1, UP,
CHANNEL_2, UP
));
assertThat(bean.getEmitter1().isCancelled(), equalTo(Boolean.FALSE));
assertThat(bean.getEmitter2().isCancelled(), equalTo(Boolean.FALSE));

bean.getSubscriber1().cancel();
assertMessagingHealth(DOWN, Map.of(
assertMessagingHealth(webTarget, DOWN, Map.of(
CHANNEL_1, DOWN,
CHANNEL_2, UP
));
assertThat(bean.getEmitter1().isCancelled(), equalTo(Boolean.TRUE));

bean.getSubscriber2().cancel();
assertMessagingHealth(DOWN, Map.of(
assertMessagingHealth(webTarget, DOWN, Map.of(
CHANNEL_1, DOWN,
CHANNEL_2, DOWN
));
assertThat(bean.getEmitter2().isCancelled(), equalTo(Boolean.TRUE));
}

private void assertMessagingHealth(HealthCheckResponse.Status rootState, Map<String, HealthCheckResponse.Status> channels) {
JsonObject messaging = getHealthCheck("messaging");
private void assertMessagingHealth(WebTarget webTarget, HealthCheckResponse.Status rootState, Map<String, HealthCheckResponse.Status> channels) {
JsonObject messaging = getHealthCheck(webTarget, "messaging");
assertThat(messaging.getString("status"), equalTo(rootState.name()));
JsonObject data = messaging.getJsonObject("data");
channels.forEach((name, state) -> assertThat(data.getString(name), equalTo(state.name())));
}

private JsonObject getHealthCheck(String checkName) {
return client.get()
.path("/health")
.submit()
.await(5, TimeUnit.SECONDS)
.content()
.as(JsonObject.class)
.await(500, TimeUnit.MILLISECONDS)
.getValue("/checks")
.asJsonArray().stream()
.map(JsonValue::asJsonObject)
.filter(check -> check.getString("name").equals(checkName))
.findFirst()
.orElseThrow(() -> new AssertionFailedError("Health check 'messaging' is missing!"));
private JsonObject getHealthCheck(WebTarget webTarget, String checkName) {
Response response = webTarget.path("/health").request().get();
JsonObject jsonObject = response.readEntity(JsonObject.class);
return jsonObject.getValue("/checks")
.asJsonArray().stream()
.map(JsonValue::asJsonObject)
.filter(check -> check.getString("name").equals(checkName))
.findFirst()
.orElseThrow(() -> new AssertionFailedError("Health check 'messaging' is missing!"));
}
}

0 comments on commit 0fd0d0a

Please sign in to comment.