Use Java Bean for connection details and add more tests (#1317)

* Use Java Bean for connection details and add more tests

Signed-off-by: Artur Ciocanu <ciocanu@adobe.com>

* Simplify mock setup

Signed-off-by: Artur Ciocanu <ciocanu@adobe.com>

* Adding even more tests for test coverage

Signed-off-by: Artur Ciocanu <ciocanu@adobe.com>

---------

Signed-off-by: Artur Ciocanu <ciocanu@adobe.com>
Co-authored-by: Artur Ciocanu <ciocanu@adobe.com>
Co-authored-by: Cassie Coyle <cassie@diagrid.io>
This commit is contained in:
artur-ciocanu 2025-04-23 00:00:42 +03:00 committed by GitHub
parent 67c5991697
commit de2dc63389
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 252 additions and 77 deletions

View File

@ -43,19 +43,31 @@ public class DaprClientAutoConfiguration {
@Bean
@ConditionalOnMissingBean
DaprClientBuilder daprClientBuilder(DaprConnectionDetails daprConnectionDetails) {
DaprClientBuilder builder = new DaprClientBuilder();
if (daprConnectionDetails.httpEndpoint() != null) {
builder.withPropertyOverride(Properties.HTTP_ENDPOINT, daprConnectionDetails.httpEndpoint());
DaprClientBuilder builder = createDaprClientBuilder();
String httpEndpoint = daprConnectionDetails.getHttpEndpoint();
if (httpEndpoint != null) {
builder.withPropertyOverride(Properties.HTTP_ENDPOINT, httpEndpoint);
}
if (daprConnectionDetails.grpcEndpoint() != null) {
builder.withPropertyOverride(Properties.GRPC_ENDPOINT, daprConnectionDetails.grpcEndpoint());
String grpcEndpoint = daprConnectionDetails.getGrpcEndpoint();
if (grpcEndpoint != null) {
builder.withPropertyOverride(Properties.GRPC_ENDPOINT, grpcEndpoint);
}
if (daprConnectionDetails.httpPort() != null) {
builder.withPropertyOverride(Properties.HTTP_PORT, String.valueOf(daprConnectionDetails.httpPort()));
Integer httpPort = daprConnectionDetails.getHttpPort();
if (httpPort != null) {
builder.withPropertyOverride(Properties.HTTP_PORT, String.valueOf(httpPort));
}
if (daprConnectionDetails.grpcPort() != null) {
builder.withPropertyOverride(Properties.GRPC_PORT, String.valueOf(daprConnectionDetails.grpcPort()));
Integer grpcPort = daprConnectionDetails.getGrpcPort();
if (grpcPort != null) {
builder.withPropertyOverride(Properties.GRPC_PORT, String.valueOf(grpcPort));
}
return builder;
}
@ -90,22 +102,50 @@ public class DaprClientAutoConfiguration {
@ConditionalOnMissingBean
WorkflowRuntimeBuilder daprWorkflowRuntimeBuilder(DaprConnectionDetails daprConnectionDetails) {
Properties properties = createPropertiesFromConnectionDetails(daprConnectionDetails);
return new WorkflowRuntimeBuilder(properties);
}
private Properties createPropertiesFromConnectionDetails(DaprConnectionDetails daprConnectionDetails) {
final Map<String, String> propertyOverrides = new HashMap<>();
propertyOverrides.put(Properties.HTTP_ENDPOINT.getName(), daprConnectionDetails.httpEndpoint());
if (daprConnectionDetails.httpPort() != null) {
propertyOverrides.put(Properties.HTTP_PORT.getName(), String.valueOf(daprConnectionDetails.httpPort()));
/**
* We use this method in tests to override the default DaprClientBuilder.
*/
protected DaprClientBuilder createDaprClientBuilder() {
return new DaprClientBuilder();
}
/**
* Creates a Properties object from the DaprConnectionDetails.
*
* @param daprConnectionDetails the DaprConnectionDetails
* @return the Properties object
*/
protected Properties createPropertiesFromConnectionDetails(DaprConnectionDetails daprConnectionDetails) {
Map<String, String> propertyOverrides = new HashMap<>();
String httpEndpoint = daprConnectionDetails.getHttpEndpoint();
if (httpEndpoint != null) {
propertyOverrides.put(Properties.HTTP_ENDPOINT.getName(), httpEndpoint);
}
propertyOverrides.put(Properties.GRPC_ENDPOINT.getName(), daprConnectionDetails.grpcEndpoint());
if (daprConnectionDetails.grpcPort() != null) {
propertyOverrides.put(Properties.GRPC_PORT.getName(), String.valueOf(daprConnectionDetails.grpcPort()));
Integer httpPort = daprConnectionDetails.getHttpPort();
if (httpPort != null) {
propertyOverrides.put(Properties.HTTP_PORT.getName(), String.valueOf(httpPort));
}
String grpcEndpoint = daprConnectionDetails.getGrpcEndpoint();
if (grpcEndpoint != null) {
propertyOverrides.put(Properties.GRPC_ENDPOINT.getName(), grpcEndpoint);
}
Integer grpcPort = daprConnectionDetails.getGrpcPort();
if (grpcPort != null) {
propertyOverrides.put(Properties.GRPC_PORT.getName(), String.valueOf(grpcPort));
}
return new Properties(propertyOverrides);
}
}

View File

@ -16,11 +16,11 @@ package io.dapr.spring.boot.autoconfigure.client;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
public interface DaprConnectionDetails extends ConnectionDetails {
String httpEndpoint();
String getHttpEndpoint();
String grpcEndpoint();
String getGrpcEndpoint();
Integer httpPort();
Integer getHttpPort();
Integer grpcPort();
Integer getGrpcPort();
}

View File

@ -22,22 +22,22 @@ class PropertiesDaprConnectionDetails implements DaprConnectionDetails {
}
@Override
public String httpEndpoint() {
public String getHttpEndpoint() {
return this.daprClientProperties.getHttpEndpoint();
}
@Override
public String grpcEndpoint() {
public String getGrpcEndpoint() {
return this.daprClientProperties.getGrpcEndpoint();
}
@Override
public Integer httpPort() {
public Integer getHttpPort() {
return this.daprClientProperties.getHttpPort();
}
@Override
public Integer grpcPort() {
public Integer getGrpcPort() {
return this.daprClientProperties.getGrpcPort();
}
}

View File

@ -0,0 +1,176 @@
/*
* Copyright 2024 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
limitations under the License.
*/
package io.dapr.spring.boot.autoconfigure.client;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.config.Properties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Unit tests for {@link DaprClientAutoConfiguration}.
*/
@ExtendWith(MockitoExtension.class)
class DaprClientAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(DaprClientAutoConfiguration.class));
@Mock
private DaprConnectionDetails connectionDetails;
@Mock
private DaprClientBuilder builder;
private DaprClientAutoConfiguration configuration;
@Test
void daprClientBuilder() {
contextRunner.run(context -> assertThat(context).hasSingleBean(DaprClientBuilder.class));
}
@Test
void daprClient() {
contextRunner.run(context -> assertThat(context).hasSingleBean(DaprClient.class));
}
@BeforeEach
void setUp() {
configuration = new TestDaprClientAutoConfiguration(builder);
}
@Test
@DisplayName("Should override HTTP endpoint if it exists")
void shouldOverrideHttpEndpointIfExists() {
String httpEndpoint = "http://localhost:3500";
when(connectionDetails.getHttpEndpoint()).thenReturn(httpEndpoint);
configuration.daprClientBuilder(connectionDetails);
verify(builder).withPropertyOverride(Properties.HTTP_ENDPOINT, httpEndpoint);
}
@Test
@DisplayName("Should override GRPC endpoint if it exists")
void shouldOverrideGrpcEndpointIfExists() {
String grpcEndpoint = "grpc://localhost:5001";
when(connectionDetails.getGrpcEndpoint()).thenReturn(grpcEndpoint);
configuration.daprClientBuilder(connectionDetails);
verify(builder).withPropertyOverride(Properties.GRPC_ENDPOINT, grpcEndpoint);
}
@Test
@DisplayName("Should override HTTP port if it exists")
void shouldOverrideHttpPortIfExists() {
Integer httpPort = 3600;
when(connectionDetails.getHttpPort()).thenReturn(httpPort);
configuration.daprClientBuilder(connectionDetails);
verify(builder).withPropertyOverride(Properties.HTTP_PORT, String.valueOf(httpPort));
}
@Test
@DisplayName("Should override GRPC port if it exists")
void shouldOverrideGrpcPortIfExists() {
Integer grpcPort = 6001;
when(connectionDetails.getGrpcPort()).thenReturn(grpcPort);
configuration.daprClientBuilder(connectionDetails);
verify(builder).withPropertyOverride(Properties.GRPC_PORT, String.valueOf(grpcPort));
}
@Test
@DisplayName("Should override HTTP endpoint in properties if it exists")
void shouldOverrideHttpEndpointInPropertiesIfExists() {
String httpEndpoint = "http://localhost:3500";
when(connectionDetails.getHttpEndpoint()).thenReturn(httpEndpoint);
Properties reuslt = configuration.createPropertiesFromConnectionDetails(connectionDetails);
assertThat(reuslt.getValue(Properties.HTTP_ENDPOINT)).isEqualTo(httpEndpoint);
}
@Test
@DisplayName("Should override GRPC endpoint in properties if it exists")
void shouldOverrideGrpcEndpointPropertiesIfExists() {
String grpcEndpoint = "grpc://localhost:3500";
when(connectionDetails.getGrpcEndpoint()).thenReturn(grpcEndpoint);
Properties result = configuration.createPropertiesFromConnectionDetails(connectionDetails);
assertThat(result.getValue(Properties.GRPC_ENDPOINT)).isEqualTo(grpcEndpoint);
}
@Test
@DisplayName("Should override HTTP port in properties if it exists")
void shouldOverrideHttpPortPropertiesIfExists() {
Integer httpPort = 3600;
when(connectionDetails.getHttpPort()).thenReturn(httpPort);
Properties result = configuration.createPropertiesFromConnectionDetails(connectionDetails);
assertThat(result.getValue(Properties.HTTP_PORT)).isEqualTo(httpPort);
}
@Test
@DisplayName("Should override GRPC port in properties if it exists")
void shouldOverrideGrpcPortPropertiesIfExists() {
Integer grpcPort = 6001;
when(connectionDetails.getGrpcPort()).thenReturn(grpcPort);
Properties result = configuration.createPropertiesFromConnectionDetails(connectionDetails);
assertThat(result.getValue(Properties.GRPC_PORT)).isEqualTo(grpcPort);
}
private static class TestDaprClientAutoConfiguration extends DaprClientAutoConfiguration {
private final DaprClientBuilder daprClientBuilder;
public TestDaprClientAutoConfiguration(DaprClientBuilder daprClientBuilder) {
this.daprClientBuilder = daprClientBuilder;
}
@Override
protected DaprClientBuilder createDaprClientBuilder() {
return daprClientBuilder;
}
}
}

View File

@ -1,42 +0,0 @@
/*
* Copyright 2024 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
limitations under the License.
*/
package io.dapr.spring.boot.autoconfigure.client;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link DaprClientAutoConfiguration}.
*/
class DaprClientAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(DaprClientAutoConfiguration.class));
@Test
void daprClientBuilder() {
contextRunner.run(context -> assertThat(context).hasSingleBean(DaprClientBuilder.class));
}
@Test
void daprClient() {
contextRunner.run(context -> assertThat(context).hasSingleBean(DaprClient.class));
}
}

View File

@ -23,22 +23,22 @@ public class DaprContainerConnectionDetailsFactory
}
@Override
public String httpEndpoint() {
public String getHttpEndpoint() {
return getContainer().getHttpEndpoint();
}
@Override
public String grpcEndpoint() {
public String getGrpcEndpoint() {
return getContainer().getGrpcEndpoint();
}
@Override
public Integer httpPort() {
public Integer getHttpPort() {
return getContainer().getHttpPort();
}
@Override
public Integer grpcPort() {
public Integer getGrpcPort() {
return getContainer().getGrpcPort();
}
}

View File

@ -162,11 +162,12 @@ public class DaprClientBuilder {
* @throws java.lang.IllegalStateException if either host is missing or if port is missing or a negative number.
*/
private DaprClientImpl buildDaprClient() {
final Properties properties = new Properties(this.propertyOverrides);
final ManagedChannel channel = NetworkUtils.buildGrpcManagedChannel(properties);
final DaprHttp daprHttp = this.daprHttpBuilder.build(properties);
final GrpcChannelFacade channelFacade = new GrpcChannelFacade(channel);
Properties properties = new Properties(this.propertyOverrides);
ManagedChannel channel = NetworkUtils.buildGrpcManagedChannel(properties);
DaprHttp daprHttp = this.daprHttpBuilder.build(properties);
GrpcChannelFacade channelFacade = new GrpcChannelFacade(channel);
DaprGrpc.DaprStub asyncStub = DaprGrpc.newStub(channel);
return new DaprClientImpl(
channelFacade,
asyncStub,