-
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.
Add @ClientFormParam to Reactive REST Client
- Loading branch information
Showing
11 changed files
with
630 additions
and
70 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
292 changes: 234 additions & 58 deletions
292
.../main/java/io/quarkus/rest/client/reactive/deployment/MicroProfileRestClientEnricher.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
153 changes: 153 additions & 0 deletions
153
...ent/src/test/java/io/quarkus/rest/client/reactive/form/ClientFormParamFromMethodTest.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,153 @@ | ||
package io.quarkus.rest.client.reactive.form; | ||
|
||
import io.quarkus.rest.client.reactive.ClientFormParam; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.ws.rs.*; | ||
import jakarta.ws.rs.core.MediaType; | ||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ClientFormParamFromMethodTest { | ||
|
||
@TestHTTPResource | ||
URI baseUri; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar.addClasses(Client.class, SubClient.class, Resource.class, ComputedParam.class)); | ||
|
||
@Test | ||
void shouldUseValuesOnlyFromClass() { | ||
Client client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(Client.class); | ||
|
||
assertThat(client.setFromClass()).isEqualTo("1/"); | ||
} | ||
|
||
@Test | ||
void shouldUseValuesFromClassAndMethod() { | ||
Client client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(Client.class); | ||
|
||
assertThat(client.setFromMethodAndClass()).isEqualTo("1/2"); | ||
} | ||
|
||
@Test | ||
void shouldUseValuesFromMethodWithParam() { | ||
Client client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(Client.class); | ||
|
||
assertThat(client.setFromMethodWithParam()).isEqualTo("-11/-2"); | ||
} | ||
|
||
@Test | ||
void shouldUseValuesFromFormParam() { | ||
Client client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(Client.class); | ||
|
||
assertThat(client.setFromFormParam("111")).isEqualTo("111/2"); | ||
} | ||
|
||
@Test | ||
void shouldUseValuesFromFormParams() { | ||
Client client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(Client.class); | ||
|
||
assertThat(client.setFromFormParams("111", "222")).isEqualTo("111/222"); | ||
} | ||
|
||
@Test | ||
void shouldUseValuesFromSubclientAnnotations() { | ||
Client client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(Client.class); | ||
|
||
assertThat(client.sub().sub("22")).isEqualTo("11/22"); | ||
} | ||
|
||
@Path("/") | ||
@ApplicationScoped | ||
public static class Resource { | ||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public String returnFormParamValues(@FormParam("first") List<String> first, | ||
@FormParam("second") List<String> second) { | ||
return String.join(",", first) + "/" + String.join(",", second); | ||
} | ||
} | ||
|
||
@ClientFormParam(name = "first", value = "{first}") | ||
public interface Client { | ||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
String setFromClass(); | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@ClientFormParam(name = "second", value = "{second}") | ||
String setFromMethodAndClass(); | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@ClientFormParam(name = "second", value = "{second}") | ||
String setFromFormParam(@FormParam("first") String first); | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@ClientFormParam(name = "second", value = "{second}") | ||
String setFromFormParams(@FormParam("first") String first, @FormParam("second") String second); | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@ClientFormParam(name = "first", value = "{io.quarkus.rest.client.reactive.form.ComputedParam.withParam}") | ||
@ClientFormParam(name = "second", value = "{withParam}") | ||
String setFromMethodWithParam(); | ||
|
||
@Path("") | ||
SubClient sub(); | ||
|
||
default String first() { | ||
return "1"; | ||
} | ||
|
||
default String second() { | ||
return "2"; | ||
} | ||
|
||
default String withParam(String name) { | ||
if ("first".equals(name)) { | ||
return "-1"; | ||
} else if ("second".equals(name)) { | ||
return "-2"; | ||
} | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
@ClientFormParam(name = "first", value = "{first}") | ||
public interface SubClient { | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
String sub(@FormParam("second") String second); | ||
|
||
default String first() { | ||
return "11"; | ||
} | ||
} | ||
|
||
} |
103 changes: 103 additions & 0 deletions
103
...t/src/test/java/io/quarkus/rest/client/reactive/form/ClientFormParamFromPropertyTest.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,103 @@ | ||
package io.quarkus.rest.client.reactive.form; | ||
|
||
import io.quarkus.rest.client.reactive.ClientFormParam; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.ws.rs.*; | ||
import jakarta.ws.rs.core.MediaType; | ||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import java.net.URI; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class ClientFormParamFromPropertyTest { | ||
private static final String FORM_VALUE = "foo"; | ||
|
||
@TestHTTPResource | ||
URI baseUri; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar.addClasses(Client.class, Resource.class) | ||
.addAsResource( | ||
new StringAsset("my.property-value=" + FORM_VALUE), | ||
"application.properties")); | ||
|
||
@Test | ||
void shouldSetFromProperties() { | ||
Client client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(Client.class); | ||
|
||
assertThat(client.getWithParam()).isEqualTo(FORM_VALUE); | ||
} | ||
|
||
@Test | ||
void shouldFailOnMissingRequiredProperty() { | ||
Client client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(Client.class); | ||
|
||
assertThatThrownBy(client::missingRequiredProperty) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
void shouldSucceedOnMissingNonRequiredProperty() { | ||
Client client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(Client.class); | ||
|
||
assertThat(client.missingNonRequiredProperty()).isEqualTo(FORM_VALUE); | ||
} | ||
|
||
@Test | ||
void shouldSucceedOnMissingNonRequiredPropertyAndUseOverriddenValue() { | ||
Client client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(Client.class); | ||
|
||
assertThat(client.missingNonRequiredPropertyAndOverriddenValue()).isEqualTo("other"); | ||
} | ||
|
||
@Path("/") | ||
@ApplicationScoped | ||
public static class Resource { | ||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public String returnFormParamValue(@FormParam("my-param") String param) { | ||
return param; | ||
} | ||
} | ||
|
||
@ClientFormParam(name = "my-param", value = "${my.property-value}") | ||
public interface Client { | ||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
String getWithParam(); | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@ClientFormParam(name = "some-other-param", value = "${non-existent-property}") | ||
String missingRequiredProperty(); | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@ClientFormParam(name = "some-other-param", value = "${non-existent-property}", required = false) | ||
String missingNonRequiredProperty(); | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@ClientFormParam(name = "some-other-param", value = "${non-existent-property}", required = false) | ||
@ClientFormParam(name = "my-param", value = "other") | ||
String missingNonRequiredPropertyAndOverriddenValue(); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/form/ComputedParam.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,13 @@ | ||
package io.quarkus.rest.client.reactive.form; | ||
|
||
public class ComputedParam { | ||
|
||
public static String withParam(String name) { | ||
if ("first".equals(name)) { | ||
return "-11"; | ||
} else if ("second".equals(name)) { | ||
return "-22"; | ||
} | ||
throw new IllegalArgumentException(); | ||
} | ||
} |
Oops, something went wrong.