mirror of https://github.com/dapr/java-sdk.git
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:
parent
67c5991697
commit
de2dc63389
|
@ -43,19 +43,31 @@ public class DaprClientAutoConfiguration {
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
DaprClientBuilder daprClientBuilder(DaprConnectionDetails daprConnectionDetails) {
|
DaprClientBuilder daprClientBuilder(DaprConnectionDetails daprConnectionDetails) {
|
||||||
DaprClientBuilder builder = new DaprClientBuilder();
|
DaprClientBuilder builder = createDaprClientBuilder();
|
||||||
if (daprConnectionDetails.httpEndpoint() != null) {
|
String httpEndpoint = daprConnectionDetails.getHttpEndpoint();
|
||||||
builder.withPropertyOverride(Properties.HTTP_ENDPOINT, daprConnectionDetails.httpEndpoint());
|
|
||||||
|
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;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,22 +102,50 @@ public class DaprClientAutoConfiguration {
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
WorkflowRuntimeBuilder daprWorkflowRuntimeBuilder(DaprConnectionDetails daprConnectionDetails) {
|
WorkflowRuntimeBuilder daprWorkflowRuntimeBuilder(DaprConnectionDetails daprConnectionDetails) {
|
||||||
Properties properties = createPropertiesFromConnectionDetails(daprConnectionDetails);
|
Properties properties = createPropertiesFromConnectionDetails(daprConnectionDetails);
|
||||||
|
|
||||||
return new WorkflowRuntimeBuilder(properties);
|
return new WorkflowRuntimeBuilder(properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Properties createPropertiesFromConnectionDetails(DaprConnectionDetails daprConnectionDetails) {
|
/**
|
||||||
final Map<String, String> propertyOverrides = new HashMap<>();
|
* We use this method in tests to override the default DaprClientBuilder.
|
||||||
propertyOverrides.put(Properties.HTTP_ENDPOINT.getName(), daprConnectionDetails.httpEndpoint());
|
*/
|
||||||
if (daprConnectionDetails.httpPort() != null) {
|
protected DaprClientBuilder createDaprClientBuilder() {
|
||||||
propertyOverrides.put(Properties.HTTP_PORT.getName(), String.valueOf(daprConnectionDetails.httpPort()));
|
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) {
|
Integer httpPort = daprConnectionDetails.getHttpPort();
|
||||||
propertyOverrides.put(Properties.GRPC_PORT.getName(), String.valueOf(daprConnectionDetails.grpcPort()));
|
|
||||||
|
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);
|
return new Properties(propertyOverrides);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,11 +16,11 @@ package io.dapr.spring.boot.autoconfigure.client;
|
||||||
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
|
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
|
||||||
|
|
||||||
public interface DaprConnectionDetails extends ConnectionDetails {
|
public interface DaprConnectionDetails extends ConnectionDetails {
|
||||||
String httpEndpoint();
|
String getHttpEndpoint();
|
||||||
|
|
||||||
String grpcEndpoint();
|
String getGrpcEndpoint();
|
||||||
|
|
||||||
Integer httpPort();
|
Integer getHttpPort();
|
||||||
|
|
||||||
Integer grpcPort();
|
Integer getGrpcPort();
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,22 +22,22 @@ class PropertiesDaprConnectionDetails implements DaprConnectionDetails {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String httpEndpoint() {
|
public String getHttpEndpoint() {
|
||||||
return this.daprClientProperties.getHttpEndpoint();
|
return this.daprClientProperties.getHttpEndpoint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String grpcEndpoint() {
|
public String getGrpcEndpoint() {
|
||||||
return this.daprClientProperties.getGrpcEndpoint();
|
return this.daprClientProperties.getGrpcEndpoint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer httpPort() {
|
public Integer getHttpPort() {
|
||||||
return this.daprClientProperties.getHttpPort();
|
return this.daprClientProperties.getHttpPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer grpcPort() {
|
public Integer getGrpcPort() {
|
||||||
return this.daprClientProperties.getGrpcPort();
|
return this.daprClientProperties.getGrpcPort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -23,22 +23,22 @@ public class DaprContainerConnectionDetailsFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String httpEndpoint() {
|
public String getHttpEndpoint() {
|
||||||
return getContainer().getHttpEndpoint();
|
return getContainer().getHttpEndpoint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String grpcEndpoint() {
|
public String getGrpcEndpoint() {
|
||||||
return getContainer().getGrpcEndpoint();
|
return getContainer().getGrpcEndpoint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer httpPort() {
|
public Integer getHttpPort() {
|
||||||
return getContainer().getHttpPort();
|
return getContainer().getHttpPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer grpcPort() {
|
public Integer getGrpcPort() {
|
||||||
return getContainer().getGrpcPort();
|
return getContainer().getGrpcPort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
* @throws java.lang.IllegalStateException if either host is missing or if port is missing or a negative number.
|
||||||
*/
|
*/
|
||||||
private DaprClientImpl buildDaprClient() {
|
private DaprClientImpl buildDaprClient() {
|
||||||
final Properties properties = new Properties(this.propertyOverrides);
|
Properties properties = new Properties(this.propertyOverrides);
|
||||||
final ManagedChannel channel = NetworkUtils.buildGrpcManagedChannel(properties);
|
ManagedChannel channel = NetworkUtils.buildGrpcManagedChannel(properties);
|
||||||
final DaprHttp daprHttp = this.daprHttpBuilder.build(properties);
|
DaprHttp daprHttp = this.daprHttpBuilder.build(properties);
|
||||||
final GrpcChannelFacade channelFacade = new GrpcChannelFacade(channel);
|
GrpcChannelFacade channelFacade = new GrpcChannelFacade(channel);
|
||||||
DaprGrpc.DaprStub asyncStub = DaprGrpc.newStub(channel);
|
DaprGrpc.DaprStub asyncStub = DaprGrpc.newStub(channel);
|
||||||
|
|
||||||
return new DaprClientImpl(
|
return new DaprClientImpl(
|
||||||
channelFacade,
|
channelFacade,
|
||||||
asyncStub,
|
asyncStub,
|
||||||
|
|
Loading…
Reference in New Issue