-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rest Client Reactive: SmallRye Stork integration
- Loading branch information
1 parent
e5b4d79
commit 9a09762
Showing
28 changed files
with
865 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...tive/deployment/src/test/java/io/quarkus/rest/client/reactive/stork/StorkDevModeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package io.quarkus.rest.client.reactive.stork; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; | ||
import static io.restassured.RestAssured.when; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
import java.io.File; | ||
import java.net.URI; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
|
||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import com.github.tomakehurst.wiremock.client.WireMock; | ||
|
||
import io.quarkus.test.QuarkusDevModeTest; | ||
|
||
public class StorkDevModeTest { | ||
|
||
public static final String WIREMOCK_RESPONSE = "response from the wiremock server"; | ||
public static final String HELLO_WORLD = "Hello, World!"; | ||
|
||
private static WireMockServer wireMockServer; | ||
|
||
@BeforeAll | ||
public static void setUp() { | ||
wireMockServer = new WireMockServer(options().port(8766)); | ||
wireMockServer.stubFor(WireMock.get("/hello") | ||
.willReturn(aResponse().withFixedDelay(1000) | ||
.withBody(WIREMOCK_RESPONSE).withStatus(200))); | ||
wireMockServer.start(); | ||
} | ||
|
||
@AfterAll | ||
public static void shutDown() { | ||
wireMockServer.stop(); | ||
} | ||
|
||
@RegisterExtension | ||
static QuarkusDevModeTest TEST = new QuarkusDevModeTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(PassThroughResource.class, HelloResource.class, HelloClient.class) | ||
.addAsResource( | ||
new File("src/test/resources/stork-dev-application.properties"), | ||
"application.properties")); | ||
|
||
@Test | ||
void shouldModifyStorkSettings() { | ||
// @formatter:off | ||
when() | ||
.get("/helper") | ||
.then() | ||
.statusCode(200) | ||
.body(equalTo(HELLO_WORLD)); | ||
// @formatter:on | ||
|
||
TEST.modifyResourceFile("application.properties", | ||
v -> v.replaceAll("stork.hello-service.service-discovery.1=.*", | ||
"stork.hello-service.service-discovery.1=localhost:8766")); | ||
// @formatter:off | ||
when() | ||
.get("/helper") | ||
.then() | ||
.statusCode(200) | ||
.body(equalTo(WIREMOCK_RESPONSE)); | ||
// @formatter:on | ||
} | ||
|
||
@Path("/helper") | ||
public static class PassThroughResource { | ||
|
||
@RestClient | ||
HelloClient client; | ||
|
||
@GET | ||
public String invokeClient() { | ||
HelloClient client = RestClientBuilder.newBuilder() | ||
.baseUri(URI.create("stork://hello-service/hello")) | ||
.build(HelloClient.class); | ||
return client.hello(); | ||
} | ||
|
||
@Path("/cdi") | ||
@GET | ||
public String invokeCdiClient() { | ||
return client.hello(); | ||
} | ||
} | ||
|
||
@Path("/") | ||
@RegisterRestClient(configKey = "hello2") | ||
public interface HelloClient { | ||
@GET | ||
String hello(); | ||
} | ||
|
||
@Path("/hello") | ||
public static class HelloResource { | ||
@GET | ||
public String hello() { | ||
return HELLO_WORLD; | ||
} | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
.../deployment/src/test/java/io/quarkus/rest/client/reactive/stork/StorkIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.quarkus.rest.client.reactive.stork; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.net.URI; | ||
|
||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.Timeout; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.rest.client.reactive.HelloClient2; | ||
import io.quarkus.rest.client.reactive.HelloResource; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class StorkIntegrationTest { | ||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(HelloClient2.class, HelloResource.class)) | ||
.withConfigurationResource("stork-application.properties"); | ||
|
||
@RestClient | ||
HelloClient2 client; | ||
|
||
@Test | ||
void shouldDetermineUrlViaStork() { | ||
String greeting = RestClientBuilder.newBuilder().baseUri(URI.create("stork://hello-service/hello")) | ||
.build(HelloClient2.class) | ||
.echo("black and white bird"); | ||
assertThat(greeting).isEqualTo("hello, black and white bird"); | ||
} | ||
|
||
@Test | ||
void shouldDetermineUrlViaStorkCDI() { | ||
String greeting = client.echo("big bird"); | ||
assertThat(greeting).isEqualTo("hello, big bird"); | ||
} | ||
|
||
@Test | ||
@Timeout(20) | ||
void shouldFailOnUnknownService() { | ||
HelloClient2 client2 = RestClientBuilder.newBuilder() | ||
.baseUri(URI.create("stork://nonexistent-service")) | ||
.build(HelloClient2.class); | ||
assertThatThrownBy(() -> client2.echo("foo")).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
@Timeout(20) | ||
void shouldFailForServiceWithoutEndpoints() { | ||
HelloClient2 client2 = RestClientBuilder.newBuilder() | ||
.baseUri(URI.create("stork://service-without-endpoints")) | ||
.build(HelloClient2.class); | ||
assertThatThrownBy(() -> client2.echo("foo")).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...rc/test/java/io/quarkus/rest/client/reactive/stork/StorkResponseTimeLoadBalancerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.quarkus.rest.client.reactive.stork; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import com.github.tomakehurst.wiremock.client.WireMock; | ||
|
||
import io.quarkus.rest.client.reactive.HelloClient2; | ||
import io.quarkus.rest.client.reactive.HelloResource; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class StorkResponseTimeLoadBalancerTest { | ||
|
||
private static final String SLOW_RESPONSE = "hello, I'm a slow server"; | ||
private static WireMockServer server; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(HelloClient2.class, HelloResource.class)) | ||
.withConfigurationResource("stork-stat-lb.properties"); | ||
|
||
@BeforeAll | ||
public static void setUp() { | ||
server = new WireMockServer(options().port(8766)); | ||
server.stubFor(WireMock.post("/hello/") | ||
.willReturn(aResponse().withFixedDelay(1000) | ||
.withBody(SLOW_RESPONSE).withStatus(200))); | ||
server.start(); | ||
} | ||
|
||
@AfterAll | ||
public static void shutDown() { | ||
server.shutdown(); | ||
} | ||
|
||
@RestClient | ||
HelloClient2 client; | ||
|
||
@Test | ||
void shouldUseFasterService() { | ||
Set<String> responses = new HashSet<>(); | ||
responses.add(client.echo("Bob")); | ||
responses.add(client.echo("Bob")); | ||
|
||
assertThat(responses).contains("hello, Bob", SLOW_RESPONSE); | ||
|
||
// after hitting the slow endpoint, we should only use the fast one: | ||
assertThat(client.echo("Alice")).isEqualTo("hello, Alice"); | ||
assertThat(client.echo("Alice")).isEqualTo("hello, Alice"); | ||
assertThat(client.echo("Alice")).isEqualTo("hello, Alice"); | ||
} | ||
|
||
} |
Oops, something went wrong.