diff --git a/dapr-spring/dapr-spring-boot-properties/src/main/java/io/dapr/spring/boot/properties/client/DaprClientProperties.java b/dapr-spring/dapr-spring-boot-properties/src/main/java/io/dapr/spring/boot/properties/client/DaprClientProperties.java index e8e18a6de..d887f9b4a 100644 --- a/dapr-spring/dapr-spring-boot-properties/src/main/java/io/dapr/spring/boot/properties/client/DaprClientProperties.java +++ b/dapr-spring/dapr-spring-boot-properties/src/main/java/io/dapr/spring/boot/properties/client/DaprClientProperties.java @@ -17,6 +17,12 @@ @ConfigurationProperties(prefix = "dapr.client") public class DaprClientProperties { + + public static final String DEFAULT_HTTP_ENDPOINT = "http://localhost"; + public static final String DEFAULT_GRPC_ENDPOINT = "localhost"; + public static final int DEFAULT_HTTP_PORT = 3500; + public static final int DEFAULT_GRPC_PORT = 50001; + private String httpEndpoint; private String grpcEndpoint; private Integer httpPort; @@ -47,19 +53,19 @@ public DaprClientProperties(String httpEndpoint, String grpcEndpoint, Integer ht } public String getHttpEndpoint() { - return httpEndpoint; + return (httpEndpoint != null) ? httpEndpoint : DEFAULT_HTTP_ENDPOINT; } public String getGrpcEndpoint() { - return grpcEndpoint; + return (grpcEndpoint != null) ? grpcEndpoint : DEFAULT_GRPC_ENDPOINT; } public Integer getHttpPort() { - return httpPort; + return (httpPort != null) ? httpPort : DEFAULT_HTTP_PORT; } public Integer getGrpcPort() { - return grpcPort; + return (grpcPort != null) ? grpcPort : DEFAULT_GRPC_PORT; } public void setHttpEndpoint(String httpEndpoint) { diff --git a/dapr-spring/dapr-spring-boot-properties/src/test/java/io/dapr/spring/boot/properties/client/DaprClientPropertiesTest.java b/dapr-spring/dapr-spring-boot-properties/src/test/java/io/dapr/spring/boot/properties/client/DaprClientPropertiesTest.java index aeb6f4518..097b232f6 100644 --- a/dapr-spring/dapr-spring-boot-properties/src/test/java/io/dapr/spring/boot/properties/client/DaprClientPropertiesTest.java +++ b/dapr-spring/dapr-spring-boot-properties/src/test/java/io/dapr/spring/boot/properties/client/DaprClientPropertiesTest.java @@ -26,6 +26,21 @@ public class DaprClientPropertiesTest { private final ApplicationContextRunner runner = new ApplicationContextRunner() .withUserConfiguration(EnableDaprClientProperties.class); + @Test + @DisplayName("Should have correct default values when using no-arg constructor") + public void shouldHaveCorrectDefaults() { + + DaprClientProperties properties = new DaprClientProperties(); + + SoftAssertions.assertSoftly(softly -> { + softly.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost"); + softly.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost"); + softly.assertThat(properties.getHttpPort()).isEqualTo(3500); + softly.assertThat(properties.getGrpcPort()).isEqualTo(50001); + softly.assertThat(properties.getApiToken()).isNull(); + }); + } + @Test @DisplayName("Should create DaprClientProperties correctly through constructor") public void shouldCreateDaprClientPropertiesCorrectly() { @@ -49,21 +64,36 @@ public void shouldSetDaprClientPropertiesCorrectly() { DaprClientProperties properties = new DaprClientProperties(); - properties.setGrpcEndpoint("localhost"); - properties.setGrpcPort(50001); - properties.setHttpEndpoint("http://localhost"); - properties.setHttpPort(3500); + properties.setGrpcEndpoint("custom-host"); + properties.setGrpcPort(60001); + properties.setHttpEndpoint("http://custom-host"); + properties.setHttpPort(4500); properties.setApiToken("ABC"); SoftAssertions.assertSoftly(softAssertions -> { - softAssertions.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost"); - softAssertions.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost"); - softAssertions.assertThat(properties.getHttpPort()).isEqualTo(3500); - softAssertions.assertThat(properties.getGrpcPort()).isEqualTo(50001); + softAssertions.assertThat(properties.getGrpcEndpoint()).isEqualTo("custom-host"); + softAssertions.assertThat(properties.getHttpEndpoint()).isEqualTo("http://custom-host"); + softAssertions.assertThat(properties.getHttpPort()).isEqualTo(4500); + softAssertions.assertThat(properties.getGrpcPort()).isEqualTo(60001); softAssertions.assertThat(properties.getApiToken()).isEqualTo("ABC"); }); } + @Test + @DisplayName("Should have correct defaults when no properties are configured") + public void shouldHaveDefaultsWhenNoPropertiesConfigured() { + runner.run(context -> { + DaprClientProperties properties = context.getBean(DaprClientProperties.class); + SoftAssertions.assertSoftly(softly -> { + softly.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost"); + softly.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost"); + softly.assertThat(properties.getHttpPort()).isEqualTo(3500); + softly.assertThat(properties.getGrpcPort()).isEqualTo(50001); + softly.assertThat(properties.getApiToken()).isNull(); + }); + }); + } + @Test @DisplayName("Should map DaprClient properties correctly") public void shouldMapDaprClientProperties() {