From e171e2cd8a5ba301ba82a82803d8ee1b3ddb692c Mon Sep 17 00:00:00 2001 From: Jose Carvajal Date: Tue, 20 Jul 2021 16:54:37 +0200 Subject: [PATCH] Add coverage to inject maps using the @ConfigProperty annotation (#241) This use case is now possible thanks to https://github.com/quarkusio/quarkus/issues/17269. --- 022-quarkus-properties-config-all/README.md | 2 ++ .../BindMapsUsingConfigValueResource.java | 31 ++++++++++++++++ .../src/main/resources/application.properties | 9 ++++- .../qe/bulk/BindMapsUsingConfigValueTest.java | 36 +++++++++++++++++++ .../NativeBindMapsUsingConfigValueIT.java | 7 ++++ 5 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 022-quarkus-properties-config-all/src/main/java/io/quarkus/qe/bulk/BindMapsUsingConfigValueResource.java create mode 100644 022-quarkus-properties-config-all/src/test/java/io/quarkus/qe/bulk/BindMapsUsingConfigValueTest.java create mode 100644 022-quarkus-properties-config-all/src/test/java/io/quarkus/qe/bulk/NativeBindMapsUsingConfigValueIT.java diff --git a/022-quarkus-properties-config-all/README.md b/022-quarkus-properties-config-all/README.md index 55ffb2f4..54276a5d 100644 --- a/022-quarkus-properties-config-all/README.md +++ b/022-quarkus-properties-config-all/README.md @@ -26,6 +26,8 @@ supported classes (e.x. String, Boolean, Double, etc.) 7. Injecting properties using the `@ConfigMapping` annotation. More info in: https://smallrye.io/docs/smallrye-config/mapping/mapping.html +8. Injecting a map using the `@ConfigProperty` annotation. More info in: https://github.com/quarkusio/quarkus/issues/17269 + ___ #### Wiki: **Protagonist**: the leading character in the story, whose purpose is to move story to its final diff --git a/022-quarkus-properties-config-all/src/main/java/io/quarkus/qe/bulk/BindMapsUsingConfigValueResource.java b/022-quarkus-properties-config-all/src/main/java/io/quarkus/qe/bulk/BindMapsUsingConfigValueResource.java new file mode 100644 index 00000000..b61bb03a --- /dev/null +++ b/022-quarkus-properties-config-all/src/main/java/io/quarkus/qe/bulk/BindMapsUsingConfigValueResource.java @@ -0,0 +1,31 @@ +package io.quarkus.qe.bulk; + +import java.util.Map; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +@Path("/bind-maps-using-config-value") +public class BindMapsUsingConfigValueResource { + + @ConfigProperty(name = "maps.labels") + Map labels; + + @ConfigProperty(name = "maps.numbers") + Map numbers; + + @GET + @Path("/labels/{label}") + public String getLabel(@PathParam("label") String label) { + return labels.get(label); + } + + @GET + @Path("/numbers/{number}") + public Integer getNumber(@PathParam("number") Integer number) { + return numbers.get(number); + } +} diff --git a/022-quarkus-properties-config-all/src/main/resources/application.properties b/022-quarkus-properties-config-all/src/main/resources/application.properties index 65e1e62c..ccc7111a 100644 --- a/022-quarkus-properties-config-all/src/main/resources/application.properties +++ b/022-quarkus-properties-config-all/src/main/resources/application.properties @@ -29,4 +29,11 @@ person.labels.B=Label 2 # Overridden Person overrides.person.name=Karen -overrides.person.age=23 \ No newline at end of file +overrides.person.age=23 + +# Maps +maps.labels.A=X +maps.labels.B=Y + +maps.numbers.1=1 +maps.numbers.2=2 \ No newline at end of file diff --git a/022-quarkus-properties-config-all/src/test/java/io/quarkus/qe/bulk/BindMapsUsingConfigValueTest.java b/022-quarkus-properties-config-all/src/test/java/io/quarkus/qe/bulk/BindMapsUsingConfigValueTest.java new file mode 100644 index 00000000..24cef1c0 --- /dev/null +++ b/022-quarkus-properties-config-all/src/test/java/io/quarkus/qe/bulk/BindMapsUsingConfigValueTest.java @@ -0,0 +1,36 @@ +package io.quarkus.qe.bulk; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.is; + +import org.apache.http.HttpStatus; +import org.hamcrest.Matcher; +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +public class BindMapsUsingConfigValueTest { + + @Test + public void shouldInjectMapsWithStringAsKeyIntoConfigValue() { + assertResponseIs("/labels/A", "X"); + assertResponseIs("/labels/B", "Y"); + } + + @Test + public void shouldInjectMapsWithIntegerAsKeyIntoConfigValue() { + assertResponseIs("/numbers/1", "1"); + assertResponseIs("/numbers/2", "2"); + } + + private void assertResponseIs(String path, T expected) { + assertResponse(path, is(expected.toString())); + } + + private void assertResponse(String path, Matcher matcher) { + given().when().get("/bind-maps-using-config-value" + path) + .then().statusCode(HttpStatus.SC_OK) + .body(matcher); + } +} diff --git a/022-quarkus-properties-config-all/src/test/java/io/quarkus/qe/bulk/NativeBindMapsUsingConfigValueIT.java b/022-quarkus-properties-config-all/src/test/java/io/quarkus/qe/bulk/NativeBindMapsUsingConfigValueIT.java new file mode 100644 index 00000000..a7e4dfb5 --- /dev/null +++ b/022-quarkus-properties-config-all/src/test/java/io/quarkus/qe/bulk/NativeBindMapsUsingConfigValueIT.java @@ -0,0 +1,7 @@ +package io.quarkus.qe.bulk; + +import io.quarkus.test.junit.NativeImageTest; + +@NativeImageTest +public class NativeBindMapsUsingConfigValueIT extends BindMapsUsingConfigValueTest { +} \ No newline at end of file