diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 070f163dc..e4964523b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -38,9 +38,9 @@ jobs: GOARCH: amd64 GOPROXY: https://proxy.golang.org JDK_VER: ${{ matrix.java }} - DAPR_CLI_VER: 1.14.0-rc.6 - DAPR_RUNTIME_VER: 1.14.0-rc.6 - DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.14.0-rc.4/install/install.sh + DAPR_CLI_VER: 1.14.0 + DAPR_RUNTIME_VER: 1.14.4 + DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.14.0/install/install.sh DAPR_CLI_REF: DAPR_REF: TOXIPROXY_URL: https://github.com/Shopify/toxiproxy/releases/download/v2.5.0/toxiproxy-server-linux-amd64 diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index ef6bc85b0..58ea173f9 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -37,9 +37,9 @@ jobs: GOARCH: amd64 GOPROXY: https://proxy.golang.org JDK_VER: ${{ matrix.java }} - DAPR_CLI_VER: 1.14.0-rc.6 - DAPR_RUNTIME_VER: 1.14.0-rc.6 - DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.14.0-rc.3/install/install.sh + DAPR_CLI_VER: 1.14.0 + DAPR_RUNTIME_VER: 1.14.4 + DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.14.0/install/install.sh DAPR_CLI_REF: DAPR_REF: steps: diff --git a/dapr-spring/dapr-spring-boot-autoconfigure/src/main/java/io/dapr/spring/boot/autoconfigure/client/DaprClientAutoConfiguration.java b/dapr-spring/dapr-spring-boot-autoconfigure/src/main/java/io/dapr/spring/boot/autoconfigure/client/DaprClientAutoConfiguration.java index 3aa422cf1..f77683424 100644 --- a/dapr-spring/dapr-spring-boot-autoconfigure/src/main/java/io/dapr/spring/boot/autoconfigure/client/DaprClientAutoConfiguration.java +++ b/dapr-spring/dapr-spring-boot-autoconfigure/src/main/java/io/dapr/spring/boot/autoconfigure/client/DaprClientAutoConfiguration.java @@ -37,10 +37,18 @@ DaprConnectionDetails daprConnectionDetails(DaprClientProperties properties) { @ConditionalOnMissingBean DaprClientBuilder daprClientBuilder(DaprConnectionDetails daprConnectionDetails) { DaprClientBuilder builder = new DaprClientBuilder(); - builder.withPropertyOverride(Properties.HTTP_ENDPOINT, daprConnectionDetails.httpEndpoint()); - builder.withPropertyOverride(Properties.GRPC_ENDPOINT, daprConnectionDetails.grpcEndpoint()); - builder.withPropertyOverride(Properties.HTTP_PORT, String.valueOf(daprConnectionDetails.httpPort())); - builder.withPropertyOverride(Properties.GRPC_PORT, String.valueOf(daprConnectionDetails.grpcPort())); + if (daprConnectionDetails.httpEndpoint() != null) { + builder.withPropertyOverride(Properties.HTTP_ENDPOINT, daprConnectionDetails.httpEndpoint()); + } + if (daprConnectionDetails.grpcEndpoint() != null) { + builder.withPropertyOverride(Properties.GRPC_ENDPOINT, daprConnectionDetails.grpcEndpoint()); + } + if (daprConnectionDetails.httpPort() != null) { + builder.withPropertyOverride(Properties.HTTP_PORT, String.valueOf(daprConnectionDetails.httpPort())); + } + if (daprConnectionDetails.grpcPort() != null) { + builder.withPropertyOverride(Properties.GRPC_PORT, String.valueOf(daprConnectionDetails.grpcPort())); + } return builder; } diff --git a/dapr-spring/dapr-spring-data/src/main/java/io/dapr/spring/data/AbstractDaprKeyValueAdapter.java b/dapr-spring/dapr-spring-data/src/main/java/io/dapr/spring/data/AbstractDaprKeyValueAdapter.java index ecacda243..82b59dbd6 100644 --- a/dapr-spring/dapr-spring-data/src/main/java/io/dapr/spring/data/AbstractDaprKeyValueAdapter.java +++ b/dapr-spring/dapr-spring-data/src/main/java/io/dapr/spring/data/AbstractDaprKeyValueAdapter.java @@ -27,7 +27,7 @@ import java.util.Map; public abstract class AbstractDaprKeyValueAdapter implements KeyValueAdapter { - private static final Map CONTENT_TYPE_META = Collections.singletonMap( + private static final Map CONTENT_TYPE_META = Map.of( "contentType", "application/json"); private final DaprClient daprClient; diff --git a/dapr-spring/dapr-spring-data/src/main/java/io/dapr/spring/data/MySQLDaprKeyValueAdapter.java b/dapr-spring/dapr-spring-data/src/main/java/io/dapr/spring/data/MySQLDaprKeyValueAdapter.java index ad401817f..16701ba23 100644 --- a/dapr-spring/dapr-spring-data/src/main/java/io/dapr/spring/data/MySQLDaprKeyValueAdapter.java +++ b/dapr-spring/dapr-spring-data/src/main/java/io/dapr/spring/data/MySQLDaprKeyValueAdapter.java @@ -164,13 +164,13 @@ private String createSql(String sqlPattern, String keyspace, Object criteria) { } private void execUsingBinding(String sql) { - Map meta = Collections.singletonMap("sql", sql); + Map meta = Map.of("sql", sql); daprClient.invokeBinding(bindingName, "exec", null, meta).block(); } private T queryUsingBinding(String sql, TypeRef typeRef) { - Map meta = Collections.singletonMap("sql", sql); + Map meta = Map.of("sql", sql); return daprClient.invokeBinding(bindingName, "query", null, meta, typeRef).block(); } diff --git a/dapr-spring/dapr-spring-data/src/main/java/io/dapr/spring/data/PostgreSQLDaprKeyValueAdapter.java b/dapr-spring/dapr-spring-data/src/main/java/io/dapr/spring/data/PostgreSQLDaprKeyValueAdapter.java index 40c22db2f..e5cbf2b48 100644 --- a/dapr-spring/dapr-spring-data/src/main/java/io/dapr/spring/data/PostgreSQLDaprKeyValueAdapter.java +++ b/dapr-spring/dapr-spring-data/src/main/java/io/dapr/spring/data/PostgreSQLDaprKeyValueAdapter.java @@ -172,13 +172,13 @@ private String createSql(String sqlPattern, String keyspace, Object criteria) { } private void execUsingBinding(String sql) { - Map meta = Collections.singletonMap("sql", sql); + Map meta = Map.of("sql", sql); daprClient.invokeBinding(bindingName, "exec", null, meta).block(); } private T queryUsingBinding(String sql, TypeRef typeRef) { - Map meta = Collections.singletonMap("sql", sql); + Map meta = Map.of("sql", sql); return daprClient.invokeBinding(bindingName, "query", null, meta, typeRef).block(); } diff --git a/dapr-spring/dapr-spring-messaging/src/main/java/io/dapr/spring/messaging/DaprMessagingTemplate.java b/dapr-spring/dapr-spring-messaging/src/main/java/io/dapr/spring/messaging/DaprMessagingTemplate.java index 6d94fdeb4..03e4cb61b 100644 --- a/dapr-spring/dapr-spring-messaging/src/main/java/io/dapr/spring/messaging/DaprMessagingTemplate.java +++ b/dapr-spring/dapr-spring-messaging/src/main/java/io/dapr/spring/messaging/DaprMessagingTemplate.java @@ -31,9 +31,13 @@ import org.springframework.lang.Nullable; import reactor.core.publisher.Hooks; import reactor.core.publisher.Mono; +<<<<<<< HEAD import reactor.util.context.Context; import java.util.Collections; import java.util.HashMap; +======= + +>>>>>>> 7490434d (Remove all global state from setProperty (#1138)) import java.util.Map; /** diff --git a/examples/src/main/java/io/dapr/examples/pubsub/CloudEventPublisher.java b/examples/src/main/java/io/dapr/examples/pubsub/CloudEventPublisher.java index 62264e34f..fbe23a244 100644 --- a/examples/src/main/java/io/dapr/examples/pubsub/CloudEventPublisher.java +++ b/examples/src/main/java/io/dapr/examples/pubsub/CloudEventPublisher.java @@ -19,6 +19,7 @@ import io.dapr.client.domain.Metadata; import io.dapr.client.domain.PublishEventRequest; +import java.util.Map; import java.util.UUID; import static java.util.Collections.singletonMap; @@ -66,7 +67,7 @@ public static void main(String[] args) throws Exception { client.publishEvent( new PublishEventRequest(PUBSUB_NAME, TOPIC_NAME, cloudEvent) .setContentType(CloudEvent.CONTENT_TYPE) - .setMetadata(singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS))).block(); + .setMetadata(Map.of(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS))).block(); System.out.println("Published cloud event with message: " + cloudEvent.getData()); try { diff --git a/sdk-actors/src/main/java/io/dapr/actors/client/ActorClient.java b/sdk-actors/src/main/java/io/dapr/actors/client/ActorClient.java index 6befd413f..c2d7a4444 100644 --- a/sdk-actors/src/main/java/io/dapr/actors/client/ActorClient.java +++ b/sdk-actors/src/main/java/io/dapr/actors/client/ActorClient.java @@ -20,8 +20,6 @@ import io.grpc.Channel; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; /** @@ -29,8 +27,6 @@ */ public class ActorClient implements AutoCloseable { - private static final Logger LOGGER = LoggerFactory.getLogger(ActorClient.class); - /** * gRPC channel for communication with Dapr sidecar. */ @@ -45,7 +41,7 @@ public class ActorClient implements AutoCloseable { * Instantiates a new channel for Dapr sidecar communication. */ public ActorClient() { - this(null); + this(new Properties(), null); } /** @@ -53,8 +49,27 @@ public ActorClient() { * * @param resiliencyOptions Client resiliency options. */ - private ActorClient(ResiliencyOptions resiliencyOptions) { - this(buildManagedChannel(), resiliencyOptions); + public ActorClient(ResiliencyOptions resiliencyOptions) { + this(new Properties(), resiliencyOptions); + } + + /** + * Instantiates a new channel for Dapr sidecar communication. + * + * @param overrideProperties Override properties. + */ + public ActorClient(Properties overrideProperties) { + this(buildManagedChannel(overrideProperties), null); + } + + /** + * Instantiates a new channel for Dapr sidecar communication. + * + * @param overrideProperties Override properties. + * @param resiliencyOptions Client resiliency options. + */ + public ActorClient(Properties overrideProperties, ResiliencyOptions resiliencyOptions) { + this(buildManagedChannel(overrideProperties), resiliencyOptions); } /** @@ -96,15 +111,18 @@ public void close() { /** * Creates a GRPC managed channel (or null, if not applicable). * + * @param overrideProperties Overrides * @return GRPC managed channel or null. */ - private static ManagedChannel buildManagedChannel() { - int port = Properties.GRPC_PORT.get(); + private static ManagedChannel buildManagedChannel(Properties overrideProperties) { + int port = overrideProperties.getValue(Properties.GRPC_PORT); if (port <= 0) { throw new IllegalArgumentException("Invalid port."); } - return ManagedChannelBuilder.forAddress(Properties.SIDECAR_IP.get(), port) + var sidecarHost = overrideProperties.getValue(Properties.SIDECAR_IP); + + return ManagedChannelBuilder.forAddress(sidecarHost, port) .usePlaintext() .userAgent(Version.getSdkVersion()) .build(); diff --git a/sdk-tests/src/test/java/io/dapr/it/BaseIT.java b/sdk-tests/src/test/java/io/dapr/it/BaseIT.java index abef394b7..f50025e21 100644 --- a/sdk-tests/src/test/java/io/dapr/it/BaseIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/BaseIT.java @@ -15,6 +15,7 @@ import io.dapr.actors.client.ActorClient; import io.dapr.client.resiliency.ResiliencyOptions; +import io.dapr.config.Properties; import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.jupiter.api.AfterAll; @@ -46,7 +47,7 @@ protected static DaprRun startDaprApp( Class serviceClass, Boolean useAppPort, int maxWaitMilliseconds) throws Exception { - return startDaprApp(testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, GRPC); + return startDaprApp(testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, HTTP); } protected static DaprRun startDaprApp( @@ -55,7 +56,7 @@ protected static DaprRun startDaprApp( Class serviceClass, AppRun.AppProtocol appProtocol, int maxWaitMilliseconds) throws Exception { - return startDaprApp(testName, successMessage, serviceClass, true, maxWaitMilliseconds, GRPC, appProtocol); + return startDaprApp(testName, successMessage, serviceClass, true, maxWaitMilliseconds, appProtocol); } protected static DaprRun startDaprApp( @@ -64,25 +65,6 @@ protected static DaprRun startDaprApp( Class serviceClass, Boolean useAppPort, int maxWaitMilliseconds, - AppRun.AppProtocol protocol) throws Exception { - return startDaprApp( - testName, - successMessage, - serviceClass, - useAppPort, - true, - maxWaitMilliseconds, - protocol, - HTTP); - } - - protected static DaprRun startDaprApp( - String testName, - String successMessage, - Class serviceClass, - Boolean useAppPort, - int maxWaitMilliseconds, - AppRun.AppProtocol protocol, AppRun.AppProtocol appProtocol) throws Exception { return startDaprApp( testName, @@ -91,7 +73,6 @@ protected static DaprRun startDaprApp( useAppPort, true, maxWaitMilliseconds, - protocol, appProtocol); } @@ -105,7 +86,6 @@ protected static DaprRun startDaprApp( false, true, maxWaitMilliseconds, - GRPC, HTTP); } @@ -116,7 +96,6 @@ protected static DaprRun startDaprApp( Boolean useAppPort, Boolean useDaprPorts, int maxWaitMilliseconds, - AppRun.AppProtocol protocol, AppRun.AppProtocol appProtocol) throws Exception { DaprRun.Builder builder = new DaprRun.Builder( testName, @@ -128,7 +107,6 @@ protected static DaprRun startDaprApp( TO_BE_STOPPED.add(run); DAPR_RUN_BUILDERS.put(run.getAppName(), builder); run.start(); - run.use(); return run; } @@ -138,24 +116,12 @@ protected static ImmutablePair startSplitDaprAndApp( Class serviceClass, Boolean useAppPort, int maxWaitMilliseconds) throws Exception { - return startSplitDaprAndApp( - testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, AppRun.AppProtocol.GRPC); - } - - protected static ImmutablePair startSplitDaprAndApp( - String testName, - String successMessage, - Class serviceClass, - Boolean useAppPort, - int maxWaitMilliseconds, - AppRun.AppProtocol protocol) throws Exception { return startSplitDaprAndApp( testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, - protocol, HTTP); } @@ -165,7 +131,6 @@ protected static ImmutablePair startSplitDaprAndApp( Class serviceClass, Boolean useAppPort, int maxWaitMilliseconds, - AppRun.AppProtocol protocol, AppRun.AppProtocol appProtocol) throws Exception { DaprRun.Builder builder = new DaprRun.Builder( testName, @@ -179,10 +144,14 @@ protected static ImmutablePair startSplitDaprAndApp( DAPR_RUN_BUILDERS.put(runs.right.getAppName(), builder); runs.left.start(); runs.right.start(); - runs.right.use(); return runs; } + protected static T deferClose(T object) { + TO_BE_CLOSED.add(object); + return object; + } + @AfterAll public static void cleanUp() throws Exception { while (!TO_BE_CLOSED.isEmpty()) { @@ -194,8 +163,8 @@ public static void cleanUp() throws Exception { } } - protected static ActorClient newActorClient() { - return newActorClient(null); + protected static ActorClient newActorClient(Properties properties) { + return new ActorClient(properties, null); } protected static ActorClient newActorClient(ResiliencyOptions resiliencyOptions) throws RuntimeException { diff --git a/sdk-tests/src/test/java/io/dapr/it/DaprPorts.java b/sdk-tests/src/test/java/io/dapr/it/DaprPorts.java index aaf186e87..b71062f7c 100644 --- a/sdk-tests/src/test/java/io/dapr/it/DaprPorts.java +++ b/sdk-tests/src/test/java/io/dapr/it/DaprPorts.java @@ -14,12 +14,14 @@ package io.dapr.it; import io.dapr.config.Properties; +import io.dapr.config.Property; import java.io.IOException; import java.net.ServerSocket; import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; public class DaprPorts { @@ -30,10 +32,18 @@ public class DaprPorts { private final Integer appPort; + private final Map, String> overrides; + private DaprPorts(Integer appPort, Integer httpPort, Integer grpcPort) { this.grpcPort = grpcPort; this.httpPort = httpPort; this.appPort = appPort; + this.overrides = Map.of( + Properties.GRPC_PORT, grpcPort.toString(), + Properties.HTTP_PORT, httpPort.toString(), + Properties.HTTP_ENDPOINT, "http://127.0.0.1:" + httpPort, + Properties.GRPC_ENDPOINT, "127.0.0.1:" + grpcPort + ); } public static DaprPorts build(boolean appPort, boolean httpPort, boolean grpcPort) { @@ -48,20 +58,6 @@ public static DaprPorts build(boolean appPort, boolean httpPort, boolean grpcPor } } - public void use() { - if (this.httpPort != null) { - System.getProperties().setProperty(Properties.HTTP_PORT.getName(), String.valueOf(this.httpPort)); - System.getProperties().setProperty( - Properties.HTTP_ENDPOINT.getName(), "http://127.0.0.1:" + this.httpPort); - } - - if (this.grpcPort != null) { - System.getProperties().setProperty(Properties.GRPC_PORT.getName(), String.valueOf(this.grpcPort)); - System.getProperties().setProperty( - Properties.GRPC_ENDPOINT.getName(), "127.0.0.1:" + this.grpcPort); - } - } - public Integer getGrpcPort() { return grpcPort; } @@ -74,6 +70,10 @@ public Integer getAppPort() { return appPort; } + public Map, String> getPropertyOverrides() { + return this.overrides; + } + private static Set findFreePorts(int n) throws IOException { Set output = new HashSet<>(); for (int i = 0; i < n;) { diff --git a/sdk-tests/src/test/java/io/dapr/it/DaprRun.java b/sdk-tests/src/test/java/io/dapr/it/DaprRun.java index 9a7533a27..fce5ac976 100644 --- a/sdk-tests/src/test/java/io/dapr/it/DaprRun.java +++ b/sdk-tests/src/test/java/io/dapr/it/DaprRun.java @@ -14,7 +14,11 @@ package io.dapr.it; import com.google.protobuf.Empty; +import io.dapr.actors.client.ActorClient; +import io.dapr.client.DaprClientBuilder; +import io.dapr.client.resiliency.ResiliencyOptions; import io.dapr.config.Properties; +import io.dapr.config.Property; import io.dapr.v1.AppCallbackHealthCheckGrpc; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; @@ -24,6 +28,7 @@ import org.apache.commons.lang3.tuple.ImmutablePair; import java.io.IOException; +import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Supplier; @@ -141,8 +146,20 @@ public void stop() throws InterruptedException, IOException { } } - public void use() { - this.ports.use(); + public Map, String> getPropertyOverrides() { + return this.ports.getPropertyOverrides(); + } + + public DaprClientBuilder newDaprClientBuilder() { + return new DaprClientBuilder().withPropertyOverrides(this.getPropertyOverrides()); + } + + public ActorClient newActorClient() { + return this.newActorClient(null); + } + + public ActorClient newActorClient(ResiliencyOptions resiliencyOptions) { + return new ActorClient(new Properties(this.getPropertyOverrides()), resiliencyOptions); } public void waitForAppHealth(int maxWaitMilliseconds) throws InterruptedException { diff --git a/sdk-tests/src/test/java/io/dapr/it/ToxiProxyRun.java b/sdk-tests/src/test/java/io/dapr/it/ToxiProxyRun.java index 1f592fd61..ad248ec4b 100644 --- a/sdk-tests/src/test/java/io/dapr/it/ToxiProxyRun.java +++ b/sdk-tests/src/test/java/io/dapr/it/ToxiProxyRun.java @@ -16,10 +16,16 @@ import eu.rekawek.toxiproxy.Proxy; import eu.rekawek.toxiproxy.ToxiproxyClient; import eu.rekawek.toxiproxy.model.ToxicDirection; +import io.dapr.actors.client.ActorClient; +import io.dapr.client.DaprClientBuilder; +import io.dapr.client.resiliency.ResiliencyOptions; +import io.dapr.config.Properties; +import io.dapr.config.Property; import io.dapr.utils.NetworkUtils; import java.io.IOException; import java.time.Duration; +import java.util.Map; public class ToxiProxyRun implements Stoppable { @@ -30,7 +36,9 @@ public class ToxiProxyRun implements Stoppable { private final Duration jitter; - private Command toxiProxyServer; + private final Command toxiProxyServer; + + private final DaprPorts toxiProxyPorts; private ToxiproxyClient toxiproxyClient; @@ -38,8 +46,6 @@ public class ToxiProxyRun implements Stoppable { private Proxy httpProxy; - private DaprPorts toxiProxyPorts; - public ToxiProxyRun(DaprRun run, Duration latency, Duration jitter) { this.daprRun = run; this.latency = latency; @@ -78,8 +84,20 @@ public void start() throws IOException, InterruptedException { } } - public void use() { - this.toxiProxyPorts.use(); + public Map, String> getPropertyOverrides() { + return this.toxiProxyPorts.getPropertyOverrides(); + } + + public DaprClientBuilder newDaprClientBuilder() { + return this.daprRun.newDaprClientBuilder().withPropertyOverrides(this.getPropertyOverrides()); + } + + public ActorClient newActorClient() { + return this.newActorClient(null); + } + + public ActorClient newActorClient(ResiliencyOptions resiliencyOptions) { + return new ActorClient(new Properties(this.getPropertyOverrides()), resiliencyOptions); } @Override diff --git a/sdk-tests/src/test/java/io/dapr/it/actors/ActivationDeactivationIT.java b/sdk-tests/src/test/java/io/dapr/it/actors/ActivationDeactivationIT.java index f57ce0905..369d02945 100644 --- a/sdk-tests/src/test/java/io/dapr/it/actors/ActivationDeactivationIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/actors/ActivationDeactivationIT.java @@ -37,7 +37,7 @@ public class ActivationDeactivationIT extends BaseIT { @Test public void activateInvokeDeactivate() throws Exception { // The call below will fail if service cannot start successfully. - startDaprApp( + var run = startDaprApp( ActivationDeactivationIT.class.getSimpleName(), DemoActorService.SUCCESS_MESSAGE, DemoActorService.class, @@ -46,7 +46,8 @@ public void activateInvokeDeactivate() throws Exception { final AtomicInteger atomicInteger = new AtomicInteger(1); logger.debug("Creating proxy builder"); - ActorProxyBuilder proxyBuilder = new ActorProxyBuilder(DemoActor.class, newActorClient()); + ActorProxyBuilder proxyBuilder + = new ActorProxyBuilder(DemoActor.class, deferClose(run.newActorClient())); logger.debug("Creating actorId"); ActorId actorId1 = new ActorId(Integer.toString(atomicInteger.getAndIncrement())); logger.debug("Building proxy"); diff --git a/sdk-tests/src/test/java/io/dapr/it/actors/ActorExceptionIT.java b/sdk-tests/src/test/java/io/dapr/it/actors/ActorExceptionIT.java index e8e1cd0c3..3b97084cb 100644 --- a/sdk-tests/src/test/java/io/dapr/it/actors/ActorExceptionIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/actors/ActorExceptionIT.java @@ -33,7 +33,7 @@ public class ActorExceptionIT extends BaseIT { @Test public void exceptionTest() throws Exception { // The call below will fail if service cannot start successfully. - startDaprApp( + var run = startDaprApp( ActorExceptionIT.class.getSimpleName(), MyActorService.SUCCESS_MESSAGE, MyActorService.class, @@ -42,7 +42,7 @@ public void exceptionTest() throws Exception { logger.debug("Creating proxy builder"); ActorProxyBuilder proxyBuilder = - new ActorProxyBuilder("MyActorTest", MyActor.class, newActorClient()); + new ActorProxyBuilder("MyActorTest", MyActor.class, deferClose(run.newActorClient())); logger.debug("Creating actorId"); ActorId actorId1 = new ActorId("1"); logger.debug("Building proxy"); diff --git a/sdk-tests/src/test/java/io/dapr/it/actors/ActorMethodNameIT.java b/sdk-tests/src/test/java/io/dapr/it/actors/ActorMethodNameIT.java index 65c6232c8..bf9a2eb74 100644 --- a/sdk-tests/src/test/java/io/dapr/it/actors/ActorMethodNameIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/actors/ActorMethodNameIT.java @@ -33,7 +33,7 @@ public class ActorMethodNameIT extends BaseIT { @Test public void actorMethodNameChange() throws Exception { // The call below will fail if service cannot start successfully. - startDaprApp( + var run = startDaprApp( ActorMethodNameIT.class.getSimpleName(), MyActorService.SUCCESS_MESSAGE, MyActorService.class, @@ -42,7 +42,7 @@ public void actorMethodNameChange() throws Exception { logger.debug("Creating proxy builder"); ActorProxyBuilder proxyBuilder = - new ActorProxyBuilder("MyActorTest", MyActor.class, newActorClient()); + new ActorProxyBuilder("MyActorTest", MyActor.class, deferClose(run.newActorClient())); logger.debug("Creating actorId"); ActorId actorId1 = new ActorId("1"); logger.debug("Building proxy"); @@ -57,7 +57,7 @@ public void actorMethodNameChange() throws Exception { logger.debug("Creating proxy builder 2"); ActorProxyBuilder proxyBuilder2 = - new ActorProxyBuilder("MyActorTest", ActorProxy.class, newActorClient()); + new ActorProxyBuilder("MyActorTest", ActorProxy.class, deferClose(run.newActorClient())); logger.debug("Building proxy 2"); ActorProxy proxy2 = proxyBuilder2.build(actorId1); diff --git a/sdk-tests/src/test/java/io/dapr/it/actors/ActorReminderFailoverIT.java b/sdk-tests/src/test/java/io/dapr/it/actors/ActorReminderFailoverIT.java index efd9aebac..8e7b64c93 100644 --- a/sdk-tests/src/test/java/io/dapr/it/actors/ActorReminderFailoverIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/actors/ActorReminderFailoverIT.java @@ -14,8 +14,10 @@ package io.dapr.it.actors; import io.dapr.actors.ActorId; +import io.dapr.actors.client.ActorClient; import io.dapr.actors.client.ActorProxy; import io.dapr.actors.client.ActorProxyBuilder; +import io.dapr.config.Properties; import io.dapr.it.BaseIT; import io.dapr.it.DaprRun; import io.dapr.it.actors.app.MyActorService; @@ -72,7 +74,7 @@ public void init() throws Exception { logger.debug("Creating proxy builder"); ActorProxyBuilder proxyBuilder = - new ActorProxyBuilder(actorType, ActorProxy.class, newActorClient()); + new ActorProxyBuilder(actorType, ActorProxy.class, deferClose(clientAppRun.newActorClient())); logger.debug("Creating actorId"); logger.debug("Building proxy"); proxy = proxyBuilder.build(actorId); @@ -91,8 +93,6 @@ public void tearDown() { */ @Test public void reminderRecoveryTest() throws Exception { - clientAppRun.use(); - logger.debug("Invoking actor method 'startReminder' which will register a reminder"); proxy.invokeMethod("startReminder", "myReminder").block(); diff --git a/sdk-tests/src/test/java/io/dapr/it/actors/ActorReminderRecoveryIT.java b/sdk-tests/src/test/java/io/dapr/it/actors/ActorReminderRecoveryIT.java index 73f0eb382..c388a906a 100644 --- a/sdk-tests/src/test/java/io/dapr/it/actors/ActorReminderRecoveryIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/actors/ActorReminderRecoveryIT.java @@ -96,7 +96,6 @@ public void setup(String actorType) throws Exception { // Run that will stay up for integration tests. // appId must not contain the appId from the other run, otherwise ITs will not run properly. clientRun = startDaprApp("ActorReminderRecoveryTestClient", 5000); - clientRun.use(); Thread.sleep(3000); @@ -104,7 +103,7 @@ public void setup(String actorType) throws Exception { logger.debug("Creating proxy builder"); ActorProxyBuilder proxyBuilder = - new ActorProxyBuilder(actorType, ActorProxy.class, newActorClient()); + new ActorProxyBuilder(actorType, ActorProxy.class, deferClose(clientRun.newActorClient())); logger.debug("Creating actorId"); logger.debug("Building proxy"); proxy = proxyBuilder.build(actorId); diff --git a/sdk-tests/src/test/java/io/dapr/it/actors/ActorSdkResiliencyIT.java b/sdk-tests/src/test/java/io/dapr/it/actors/ActorSdkResiliencyIT.java index e8140d68c..c9b7f27b7 100644 --- a/sdk-tests/src/test/java/io/dapr/it/actors/ActorSdkResiliencyIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/actors/ActorSdkResiliencyIT.java @@ -14,9 +14,9 @@ package io.dapr.it.actors; import io.dapr.actors.ActorId; +import io.dapr.actors.client.ActorClient; import io.dapr.actors.client.ActorProxyBuilder; import io.dapr.client.DaprClient; -import io.dapr.client.DaprClientBuilder; import io.dapr.client.resiliency.ResiliencyOptions; import io.dapr.it.BaseIT; import io.dapr.it.DaprRun; @@ -75,22 +75,22 @@ public static void init() throws Exception { true, 60000); - demoActor = buildDemoActorProxy(null); - daprClient = new DaprClientBuilder().build(); + demoActor = buildDemoActorProxy(deferClose(daprRun.newActorClient())); + daprClient = daprRun.newDaprClientBuilder().build(); toxiProxyRun = new ToxiProxyRun(daprRun, LATENCY, JITTER); toxiProxyRun.start(); - toxiProxyRun.use(); - toxiDemoActor = buildDemoActorProxy(new ResiliencyOptions().setTimeout(TIMEOUT)); + + toxiDemoActor = buildDemoActorProxy( + toxiProxyRun.newActorClient(new ResiliencyOptions().setTimeout(TIMEOUT))); resilientDemoActor = buildDemoActorProxy( - new ResiliencyOptions().setTimeout(TIMEOUT).setMaxRetries(MAX_RETRIES)); + toxiProxyRun.newActorClient(new ResiliencyOptions().setTimeout(TIMEOUT).setMaxRetries(MAX_RETRIES))); oneRetryDemoActor = buildDemoActorProxy( - new ResiliencyOptions().setTimeout(TIMEOUT).setMaxRetries(1)); + toxiProxyRun.newActorClient(new ResiliencyOptions().setTimeout(TIMEOUT).setMaxRetries(1))); } - private static DemoActor buildDemoActorProxy(ResiliencyOptions resiliencyOptions) { - ActorProxyBuilder builder = - new ActorProxyBuilder(DemoActor.class, newActorClient(resiliencyOptions)); + private static DemoActor buildDemoActorProxy(ActorClient actorClient) { + ActorProxyBuilder builder = new ActorProxyBuilder(DemoActor.class, actorClient); return builder.build(ACTOR_ID); } diff --git a/sdk-tests/src/test/java/io/dapr/it/actors/ActorStateIT.java b/sdk-tests/src/test/java/io/dapr/it/actors/ActorStateIT.java index 534fd481d..67d6b9659 100644 --- a/sdk-tests/src/test/java/io/dapr/it/actors/ActorStateIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/actors/ActorStateIT.java @@ -16,19 +16,14 @@ import io.dapr.actors.ActorId; import io.dapr.actors.client.ActorProxy; import io.dapr.actors.client.ActorProxyBuilder; -import io.dapr.it.AppRun; import io.dapr.it.BaseIT; import io.dapr.it.DaprRun; import io.dapr.it.actors.services.springboot.StatefulActor; import io.dapr.it.actors.services.springboot.StatefulActorService; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.stream.Stream; - import static io.dapr.it.Retry.callWithRetry; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -38,51 +33,37 @@ public class ActorStateIT extends BaseIT { private static Logger logger = LoggerFactory.getLogger(ActorStateIT.class); - /** - * Parameters for this test. - * Param #1: useGrpc. - * @return Collection of parameter tuples. - */ - public static Stream data() { - return Stream.of( - Arguments.of(AppRun.AppProtocol.HTTP ), - Arguments.of(AppRun.AppProtocol.GRPC ) - ); - } - - @ParameterizedTest - @MethodSource("data") - public void writeReadState(AppRun.AppProtocol serviceAppProtocol) throws Exception { + @Test + public void writeReadState() throws Exception { logger.debug("Starting actor runtime ..."); // The call below will fail if service cannot start successfully. - DaprRun runtime = startDaprApp( + DaprRun run = startDaprApp( this.getClass().getSimpleName(), StatefulActorService.SUCCESS_MESSAGE, StatefulActorService.class, true, - 60000, - serviceAppProtocol); + 60000); String message = "This is a message to be saved and retrieved."; String name = "Jon Doe"; byte[] bytes = new byte[] { 0x1 }; ActorId actorId = new ActorId( - String.format("%d-%b", System.currentTimeMillis(), serviceAppProtocol)); + String.format("%d", System.currentTimeMillis())); String actorType = "StatefulActorTest"; logger.debug("Building proxy ..."); ActorProxyBuilder proxyBuilder = - new ActorProxyBuilder(actorType, ActorProxy.class, newActorClient()); + new ActorProxyBuilder(actorType, ActorProxy.class, deferClose(run.newActorClient())); ActorProxy proxy = proxyBuilder.build(actorId); // wating for actor to be activated - Thread.sleep(2000); + Thread.sleep(5000); // Validate conditional read works. callWithRetry(() -> { logger.debug("Invoking readMessage where data is not present yet ... "); String result = proxy.invokeMethod("readMessage", String.class).block(); assertNull(result); - }, 5000); + }, 10000); callWithRetry(() -> { logger.debug("Invoking writeMessage ... "); @@ -146,7 +127,7 @@ public void writeReadState(AppRun.AppProtocol serviceAppProtocol) throws Excepti Thread.sleep(10000); logger.debug("Stopping service ..."); - runtime.stop(); + run.stop(); logger.debug("Starting service ..."); DaprRun run2 = startDaprApp( @@ -154,11 +135,10 @@ public void writeReadState(AppRun.AppProtocol serviceAppProtocol) throws Excepti StatefulActorService.SUCCESS_MESSAGE, StatefulActorService.class, true, - 60000, - serviceAppProtocol); + 60000); // Need new proxy builder because the proxy builder holds the channel. - proxyBuilder = new ActorProxyBuilder(actorType, ActorProxy.class, newActorClient()); + proxyBuilder = new ActorProxyBuilder(actorType, ActorProxy.class, deferClose(run2.newActorClient())); ActorProxy newProxy = proxyBuilder.build(actorId); // wating for actor to be activated diff --git a/sdk-tests/src/test/java/io/dapr/it/actors/ActorTimerRecoveryIT.java b/sdk-tests/src/test/java/io/dapr/it/actors/ActorTimerRecoveryIT.java index 796aa4e66..809cd21a9 100644 --- a/sdk-tests/src/test/java/io/dapr/it/actors/ActorTimerRecoveryIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/actors/ActorTimerRecoveryIT.java @@ -59,7 +59,7 @@ public void timerRecoveryTest() throws Exception { logger.debug("Creating proxy builder"); ActorProxyBuilder proxyBuilder = - new ActorProxyBuilder(actorType, ActorProxy.class, newActorClient()); + new ActorProxyBuilder(actorType, ActorProxy.class, deferClose(runs.right.newActorClient())); logger.debug("Creating actorId"); ActorId actorId = new ActorId(UUID.randomUUID().toString()); logger.debug("Building proxy"); diff --git a/sdk-tests/src/test/java/io/dapr/it/actors/ActorTurnBasedConcurrencyIT.java b/sdk-tests/src/test/java/io/dapr/it/actors/ActorTurnBasedConcurrencyIT.java index adc7b36c3..dd021d98b 100644 --- a/sdk-tests/src/test/java/io/dapr/it/actors/ActorTurnBasedConcurrencyIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/actors/ActorTurnBasedConcurrencyIT.java @@ -80,7 +80,7 @@ public void cleanUpTestCase() { public void invokeOneActorMethodReminderAndTimer() throws Exception { System.out.println("Starting test 'actorTest1'"); - startDaprApp( + var run = startDaprApp( ActorTurnBasedConcurrencyIT.class.getSimpleName(), MyActorService.SUCCESS_MESSAGE, MyActorService.class, @@ -92,7 +92,7 @@ public void invokeOneActorMethodReminderAndTimer() throws Exception { logger.debug("Creating proxy builder"); ActorProxyBuilder proxyBuilder = - new ActorProxyBuilder(actorType, ActorProxy.class, newActorClient()); + new ActorProxyBuilder(actorType, ActorProxy.class, deferClose(run.newActorClient())); logger.debug("Creating actorId"); ActorId actorId1 = new ActorId(ACTOR_ID); logger.debug("Building proxy"); diff --git a/sdk-tests/src/test/java/io/dapr/it/api/ApiIT.java b/sdk-tests/src/test/java/io/dapr/it/api/ApiIT.java index 462d22927..8b37c5ad3 100644 --- a/sdk-tests/src/test/java/io/dapr/it/api/ApiIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/api/ApiIT.java @@ -19,7 +19,7 @@ public void testShutdownAPI() throws Exception { // TODO(artursouza): change this to wait for the sidecar to be healthy (new method needed in DaprClient). Thread.sleep(3000); - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = run.newDaprClientBuilder().build()) { logger.info("Sending shutdown request."); client.shutdown().block(); diff --git a/sdk-tests/src/test/java/io/dapr/it/binding/http/BindingIT.java b/sdk-tests/src/test/java/io/dapr/it/binding/http/BindingIT.java index 610a55820..8b104f0d8 100644 --- a/sdk-tests/src/test/java/io/dapr/it/binding/http/BindingIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/binding/http/BindingIT.java @@ -24,6 +24,7 @@ import java.util.Collections; import java.util.List; +import java.util.Map; import static io.dapr.it.Retry.callWithRetry; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -37,10 +38,10 @@ public class BindingIT extends BaseIT { @Test public void httpOutputBindingError() throws Exception { - startDaprApp( + var run = startDaprApp( this.getClass().getSimpleName() + "-httpoutputbinding-exception", 60000); - try(DaprClient client = new DaprClientBuilder().build()) { + try(DaprClient client = run.newDaprClientBuilder().build()) { // Validate error message callWithRetry(() -> { System.out.println("Checking exception handling for output binding ..."); @@ -60,10 +61,10 @@ public void httpOutputBindingError() throws Exception { @Test public void httpOutputBindingErrorIgnoredByComponent() throws Exception { - startDaprApp( + var run = startDaprApp( this.getClass().getSimpleName() + "-httpoutputbinding-ignore-error", 60000); - try(DaprClient client = new DaprClientBuilder().build()) { + try(DaprClient client = run.newDaprClientBuilder().build()) { // Validate error message callWithRetry(() -> { System.out.println("Checking exception handling for output binding ..."); @@ -92,7 +93,7 @@ public void inputOutputBinding() throws Exception { var bidingName = "sample123"; - try(DaprClient client = new DaprClientBuilder().build()) { + try(DaprClient client = daprRun.newDaprClientBuilder().build()) { callWithRetry(() -> { System.out.println("Checking if input binding is up before publishing events ..."); client.invokeBinding( @@ -115,14 +116,14 @@ public void inputOutputBinding() throws Exception { System.out.println("sending first message"); client.invokeBinding( - bidingName, "create", myClass, Collections.singletonMap("MyMetadata", "MyValue"), Void.class).block(); + bidingName, "create", myClass, Map.of("MyMetadata", "MyValue"), Void.class).block(); // This is an example of sending a plain string. The input binding will receive // cat final String m = "cat"; System.out.println("sending " + m); client.invokeBinding( - bidingName, "create", m, Collections.singletonMap("MyMetadata", "MyValue"), Void.class).block(); + bidingName, "create", m, Map.of("MyMetadata", "MyValue"), Void.class).block(); // Metadata is not used by Kafka component, so it is not possible to validate. callWithRetry(() -> { diff --git a/sdk-tests/src/test/java/io/dapr/it/configuration/ConfigurationClientIT.java b/sdk-tests/src/test/java/io/dapr/it/configuration/ConfigurationClientIT.java index 02c5798de..ee1343467 100644 --- a/sdk-tests/src/test/java/io/dapr/it/configuration/ConfigurationClientIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/configuration/ConfigurationClientIT.java @@ -68,7 +68,7 @@ public class ConfigurationClientIT extends BaseIT { @BeforeAll public static void init() throws Exception { daprRun = startDaprApp(ConfigurationClientIT.class.getSimpleName(), 5000); - daprClient = new DaprClientBuilder().build(); + daprClient = daprRun.newDaprClientBuilder().build(); } @AfterAll diff --git a/sdk-tests/src/test/java/io/dapr/it/methodinvoke/grpc/MethodInvokeIT.java b/sdk-tests/src/test/java/io/dapr/it/methodinvoke/grpc/MethodInvokeIT.java index ed9609cc9..ea94d2136 100644 --- a/sdk-tests/src/test/java/io/dapr/it/methodinvoke/grpc/MethodInvokeIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/methodinvoke/grpc/MethodInvokeIT.java @@ -49,7 +49,7 @@ public void init() throws Exception { @Test public void testInvoke() throws Exception { - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { client.waitForSidecar(10000).block(); daprRun.waitForAppHealth(10000); @@ -81,7 +81,7 @@ public void testInvoke() throws Exception { @Test public void testInvokeTimeout() throws Exception { - try (DaprClient client = new DaprClientBuilder().withResiliencyOptions(RESILIENCY_OPTIONS).build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().withResiliencyOptions(RESILIENCY_OPTIONS).build()) { client.waitForSidecar(10000).block(); daprRun.waitForAppHealth(10000); @@ -99,7 +99,7 @@ public void testInvokeTimeout() throws Exception { @Test public void testInvokeException() throws Exception { - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { client.waitForSidecar(10000).block(); daprRun.waitForAppHealth(10000); diff --git a/sdk-tests/src/test/java/io/dapr/it/methodinvoke/http/MethodInvokeIT.java b/sdk-tests/src/test/java/io/dapr/it/methodinvoke/http/MethodInvokeIT.java index 1b5ab7577..ba7b485ca 100644 --- a/sdk-tests/src/test/java/io/dapr/it/methodinvoke/http/MethodInvokeIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/methodinvoke/http/MethodInvokeIT.java @@ -48,7 +48,7 @@ public void testInvoke() throws Exception { // At this point, it is guaranteed that the service above is running and all ports being listened to. - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { client.waitForSidecar(10000).block(); for (int i = 0; i < NUM_MESSAGES; i++) { String message = String.format("This is message #%d", i); @@ -74,7 +74,7 @@ public void testInvoke() throws Exception { @Test public void testInvokeWithObjects() throws Exception { - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { client.waitForSidecar(10000).block(); for (int i = 0; i < NUM_MESSAGES; i++) { Person person = new Person(); @@ -110,7 +110,7 @@ public void testInvokeWithObjects() throws Exception { @Test public void testInvokeTimeout() throws Exception { - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { client.waitForSidecar(10000).block(); long started = System.currentTimeMillis(); String message = assertThrows(IllegalStateException.class, () -> { @@ -127,7 +127,7 @@ public void testInvokeTimeout() throws Exception { @Test public void testInvokeException() throws Exception { - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { client.waitForSidecar(10000).block(); MethodInvokeServiceProtos.SleepRequest req = MethodInvokeServiceProtos.SleepRequest.newBuilder().setSeconds(-9).build(); DaprException exception = assertThrows(DaprException.class, () -> diff --git a/sdk-tests/src/test/java/io/dapr/it/pubsub/http/PubSubIT.java b/sdk-tests/src/test/java/io/dapr/it/pubsub/http/PubSubIT.java index 347bd64de..57b9485e9 100644 --- a/sdk-tests/src/test/java/io/dapr/it/pubsub/http/PubSubIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/pubsub/http/PubSubIT.java @@ -43,6 +43,7 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Random; import java.util.Set; @@ -104,7 +105,7 @@ public void publishPubSubNotFound() throws Exception { this.getClass().getSimpleName(), 60000)); - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { assertThrowsDaprExceptionWithReason( "INVALID_ARGUMENT", "INVALID_ARGUMENT: pubsub unknown pubsub is not found", @@ -119,7 +120,7 @@ public void testBulkPublishPubSubNotFound() throws Exception { this.getClass().getSimpleName(), 60000)); - try (DaprPreviewClient client = new DaprClientBuilder().buildPreviewClient()) { + try (DaprPreviewClient client = daprRun.newDaprClientBuilder().buildPreviewClient()) { assertThrowsDaprException( "INVALID_ARGUMENT", "INVALID_ARGUMENT: pubsub unknown pubsub is not found", @@ -151,8 +152,8 @@ public String getContentType() { return "application/json"; } }; - try (DaprClient client = new DaprClientBuilder().withObjectSerializer(serializer).build(); - DaprPreviewClient previewClient = new DaprClientBuilder().withObjectSerializer(serializer).buildPreviewClient()) { + try (DaprClient client = daprRun.newDaprClientBuilder().withObjectSerializer(serializer).build(); + DaprPreviewClient previewClient = daprRun.newDaprClientBuilder().withObjectSerializer(serializer).buildPreviewClient()) { // Only for the gRPC test // Send a multiple messages on one topic in messagebus pubsub via publishEvents API. List messages = new ArrayList<>(); @@ -282,7 +283,7 @@ public String getContentType() { }; // Send a batch of messages on one topic - try (DaprClient client = new DaprClientBuilder().withObjectSerializer(serializer).build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().withObjectSerializer(serializer).build()) { for (int i = 0; i < NUM_MESSAGES; i++) { String message = String.format("This is message #%d on topic %s", i, TOPIC_NAME); //Publishing messages @@ -494,7 +495,7 @@ public String getContentType() { return "application/octet-stream"; } }; - try (DaprClient client = new DaprClientBuilder().withObjectSerializer(serializer).build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().withObjectSerializer(serializer).build()) { client.publishEvent( PUBSUB_NAME, BINARY_TOPIC_NAME, @@ -504,7 +505,7 @@ public String getContentType() { Thread.sleep(3000); - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { callWithRetry(() -> { System.out.println("Checking results for topic " + BINARY_TOPIC_NAME); final List messages = client.invokeMethod( @@ -526,7 +527,7 @@ public void testPubSubTTLMetadata() throws Exception { 60000)); // Send a batch of messages on one topic, all to be expired in 1 second. - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { for (int i = 0; i < NUM_MESSAGES; i++) { String message = String.format("This is message #%d on topic %s", i, TTL_TOPIC_NAME); //Publishing messages @@ -534,7 +535,7 @@ public void testPubSubTTLMetadata() throws Exception { PUBSUB_NAME, TTL_TOPIC_NAME, message, - Collections.singletonMap(Metadata.TTL_IN_SECONDS, "1")).block(); + Map.of(Metadata.TTL_IN_SECONDS, "1")).block(); System.out.println(String.format("Published message: '%s' to topic '%s' pubsub_name '%s'", message, TOPIC_NAME, PUBSUB_NAME)); } } @@ -555,7 +556,7 @@ public void testPubSubTTLMetadata() throws Exception { Thread.sleep(5000); final String appId = daprRun.getAppName(); - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { callWithRetry(() -> { System.out.println("Checking results for topic " + TTL_TOPIC_NAME); final List messages = client.invokeMethod(appId, "messages/" + TTL_TOPIC_NAME, null, HttpExtension.GET, List.class).block(); @@ -576,7 +577,7 @@ public void testPubSubBulkSubscribe() throws Exception { 60000)); // Send a batch of messages on one topic. - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { for (int i = 0; i < NUM_MESSAGES; i++) { String message = String.format("This is message #%d on topic %s", i, BULK_SUB_TOPIC_NAME); // Publishing messages @@ -590,7 +591,7 @@ public void testPubSubBulkSubscribe() throws Exception { Thread.sleep(5000); final String appId = daprRun.getAppName(); - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { callWithRetry(() -> { System.out.println("Checking results for topic " + BULK_SUB_TOPIC_NAME); @@ -643,7 +644,7 @@ public void testLongValues() throws Exception { values.add(val); } Iterator valuesIt = values.iterator(); - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { for (int i = 0; i < NUM_MESSAGES; i++) { ConvertToLong value = valuesIt.next(); System.out.println("The long value sent " + value.getValue()); @@ -652,7 +653,7 @@ public void testLongValues() throws Exception { PUBSUB_NAME, LONG_TOPIC_NAME, value, - Collections.singletonMap(Metadata.TTL_IN_SECONDS, "30")).block(); + Map.of(Metadata.TTL_IN_SECONDS, "30")).block(); try { Thread.sleep((long) (1000 * Math.random())); @@ -665,7 +666,7 @@ public void testLongValues() throws Exception { } Set actual = new HashSet<>(); - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { callWithRetry(() -> { System.out.println("Checking results for topic " + LONG_TOPIC_NAME); final List> messages = client.invokeMethod( diff --git a/sdk-tests/src/test/java/io/dapr/it/resiliency/SdkResiliencyIT.java b/sdk-tests/src/test/java/io/dapr/it/resiliency/SdkResiliencyIT.java index c3bdc7195..723e40b2a 100644 --- a/sdk-tests/src/test/java/io/dapr/it/resiliency/SdkResiliencyIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/resiliency/SdkResiliencyIT.java @@ -63,22 +63,21 @@ public class SdkResiliencyIT extends BaseIT { @BeforeAll public static void init() throws Exception { DaprRun daprRun = startDaprApp(SdkResiliencyIT.class.getSimpleName(), 5000); - daprClient = new DaprClientBuilder().build(); + daprClient = daprRun.newDaprClientBuilder().build(); daprClient.waitForSidecar(8000).block(); toxiProxyRun = new ToxiProxyRun(daprRun, LATENCY, JITTER); toxiProxyRun.start(); - toxiProxyRun.use(); - daprToxiClient = new DaprClientBuilder() + daprToxiClient = toxiProxyRun.newDaprClientBuilder() .withResiliencyOptions( new ResiliencyOptions().setTimeout(TIMEOUT)) .build(); - daprResilientClient = new DaprClientBuilder() + daprResilientClient = toxiProxyRun.newDaprClientBuilder() .withResiliencyOptions( new ResiliencyOptions().setTimeout(TIMEOUT).setMaxRetries(MAX_RETRIES)) .build(); - daprRetriesOnceClient = new DaprClientBuilder() + daprRetriesOnceClient = toxiProxyRun.newDaprClientBuilder() .withResiliencyOptions( new ResiliencyOptions().setTimeout(TIMEOUT).setMaxRetries(1)) .build(); diff --git a/sdk-tests/src/test/java/io/dapr/it/secrets/SecretsClientIT.java b/sdk-tests/src/test/java/io/dapr/it/secrets/SecretsClientIT.java index fc6260a68..23f05957b 100644 --- a/sdk-tests/src/test/java/io/dapr/it/secrets/SecretsClientIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/secrets/SecretsClientIT.java @@ -75,7 +75,7 @@ public static void init() throws Exception { @BeforeEach public void setup() { - this.daprClient = new DaprClientBuilder().build(); + this.daprClient = daprRun.newDaprClientBuilder().build(); } @AfterEach diff --git a/sdk-tests/src/test/java/io/dapr/it/spring/data/DaprKeyValueRepositoryIT.java b/sdk-tests/src/test/java/io/dapr/it/spring/data/DaprKeyValueRepositoryIT.java index c2a09e9e6..60d4b8854 100644 --- a/sdk-tests/src/test/java/io/dapr/it/spring/data/DaprKeyValueRepositoryIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/spring/data/DaprKeyValueRepositoryIT.java @@ -51,7 +51,7 @@ public class DaprKeyValueRepositoryIT { "host=postgres-repository user=postgres password=password port=5432 connect_timeout=10 database=dapr_db_repository"; private static final Map STATE_STORE_PROPERTIES = createStateStoreProperties(); - private static final Map BINDING_PROPERTIES = Collections.singletonMap("connectionString", CONNECTION_STRING); + private static final Map BINDING_PROPERTIES = Map.of("connectionString", CONNECTION_STRING); private static final Network DAPR_NETWORK = Network.newNetwork(); diff --git a/sdk-tests/src/test/java/io/dapr/it/spring/data/MySQLDaprKeyValueTemplateIT.java b/sdk-tests/src/test/java/io/dapr/it/spring/data/MySQLDaprKeyValueTemplateIT.java index f5d145b90..d365685d9 100644 --- a/sdk-tests/src/test/java/io/dapr/it/spring/data/MySQLDaprKeyValueTemplateIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/spring/data/MySQLDaprKeyValueTemplateIT.java @@ -63,7 +63,7 @@ public class MySQLDaprKeyValueTemplateIT { private static final String BINDING_DSN = "mysql:password@tcp(mysql:3306)/dapr_db"; private static final Map STATE_STORE_PROPERTIES = createStateStoreProperties(); - private static final Map BINDING_PROPERTIES = Collections.singletonMap("url", BINDING_DSN); + private static final Map BINDING_PROPERTIES = Map.of("url", BINDING_DSN); private static final Network DAPR_NETWORK = Network.newNetwork(); @@ -114,7 +114,7 @@ private static Map createStateStoreProperties() { */ @AfterEach public void tearDown() { - var meta = Collections.singletonMap("sql", "delete from state"); + var meta = Map.of("sql", "delete from state"); daprClient.invokeBinding(BINDING_NAME, "exec", null, meta).block(); } diff --git a/sdk-tests/src/test/java/io/dapr/it/spring/data/PostgreSQLDaprKeyValueTemplateIT.java b/sdk-tests/src/test/java/io/dapr/it/spring/data/PostgreSQLDaprKeyValueTemplateIT.java index 1d2ef4de8..b055d3aef 100644 --- a/sdk-tests/src/test/java/io/dapr/it/spring/data/PostgreSQLDaprKeyValueTemplateIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/spring/data/PostgreSQLDaprKeyValueTemplateIT.java @@ -54,7 +54,7 @@ public class PostgreSQLDaprKeyValueTemplateIT { "host=postgres user=postgres password=password port=5432 connect_timeout=10 database=dapr_db"; private static final Map STATE_STORE_PROPERTIES = createStateStoreProperties(); - private static final Map BINDING_PROPERTIES = Collections.singletonMap("connectionString", CONNECTION_STRING); + private static final Map BINDING_PROPERTIES = Map.of("connectionString", CONNECTION_STRING); private static final Network DAPR_NETWORK = Network.newNetwork(); @@ -96,7 +96,7 @@ private static Map createStateStoreProperties() { @BeforeEach public void setUp() { - var meta = Collections.singletonMap("sql", "delete from state"); + var meta = Map.of("sql", "delete from state"); daprClient.invokeBinding(BINDING_NAME, "exec", null, meta).block(); } diff --git a/sdk-tests/src/test/java/io/dapr/it/spring/messaging/DaprSpringMessagingIT.java b/sdk-tests/src/test/java/io/dapr/it/spring/messaging/DaprSpringMessagingIT.java index 50d5b9114..fc03f4412 100644 --- a/sdk-tests/src/test/java/io/dapr/it/spring/messaging/DaprSpringMessagingIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/spring/messaging/DaprSpringMessagingIT.java @@ -23,6 +23,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Disabled; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -86,6 +87,7 @@ public void beforeEach() throws InterruptedException { } @Test + @Disabled("Test is flaky due to global state in the spring test application.") public void testDaprMessagingTemplate() throws InterruptedException { for (int i = 0; i < 10; i++) { var msg = "ProduceAndReadWithPrimitiveMessageType:" + i; diff --git a/sdk-tests/src/test/java/io/dapr/it/state/GRPCStateClientIT.java b/sdk-tests/src/test/java/io/dapr/it/state/GRPCStateClientIT.java index 1c77d93d2..5254e0b06 100644 --- a/sdk-tests/src/test/java/io/dapr/it/state/GRPCStateClientIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/state/GRPCStateClientIT.java @@ -37,7 +37,7 @@ public class GRPCStateClientIT extends AbstractStateClientIT { @BeforeAll public static void init() throws Exception { daprRun = startDaprApp(GRPCStateClientIT.class.getSimpleName(), 5000); - daprClient = new DaprClientBuilder().build(); + daprClient = daprRun.newDaprClientBuilder().build(); } @AfterAll diff --git a/sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprContainerIT.java b/sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprContainerIT.java index bdafff9a7..d565be86f 100644 --- a/sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprContainerIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprContainerIT.java @@ -46,7 +46,6 @@ import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; import static com.github.tomakehurst.wiremock.client.WireMock.verify; -import static java.util.Collections.singletonMap; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -166,7 +165,7 @@ public void testPubSub() throws Exception { try (DaprClient client = (builder).build()) { String message = "message content"; - Map metadata = singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS); + Map metadata = Map.of(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS); client.publishEvent(PUBSUB_NAME, PUBSUB_TOPIC_NAME, message, metadata).block(); } diff --git a/sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprWorkflowsIT.java b/sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprWorkflowsIT.java index 91ccfa7ce..f0c39ed80 100644 --- a/sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprWorkflowsIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprWorkflowsIT.java @@ -37,6 +37,7 @@ import java.time.Duration; import java.util.ArrayList; import java.util.Collections; +import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -59,7 +60,7 @@ public class DaprWorkflowsIT { .withAppName("workflow-dapr-app") .withNetwork(DAPR_NETWORK) .withComponent(new Component("kvstore", "state.in-memory", "v1", - Collections.singletonMap("actorStateStore", "true"))) + Map.of("actorStateStore", "true"))) .withComponent(new Component("pubsub", "pubsub.in-memory", "v1", Collections.emptyMap())) .withDaprLogLevel(DaprLogLevel.DEBUG) .withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String())) diff --git a/sdk-tests/src/test/java/io/dapr/it/tracing/grpc/TracingIT.java b/sdk-tests/src/test/java/io/dapr/it/tracing/grpc/TracingIT.java index 76df40044..256347398 100644 --- a/sdk-tests/src/test/java/io/dapr/it/tracing/grpc/TracingIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/tracing/grpc/TracingIT.java @@ -47,7 +47,7 @@ public void testInvoke() throws Exception { String spanName = UUID.randomUUID().toString(); Span span = tracer.spanBuilder(spanName).setSpanKind(SpanKind.CLIENT).startSpan(); - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { client.waitForSidecar(10000).block(); try (Scope scope = span.makeCurrent()) { SleepRequest req = SleepRequest.newBuilder().setSeconds(1).build(); diff --git a/sdk-tests/src/test/java/io/dapr/it/tracing/http/TracingIT.java b/sdk-tests/src/test/java/io/dapr/it/tracing/http/TracingIT.java index a4c75d140..3285d2ca8 100644 --- a/sdk-tests/src/test/java/io/dapr/it/tracing/http/TracingIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/tracing/http/TracingIT.java @@ -46,7 +46,7 @@ public void testInvoke() throws Exception { String spanName = UUID.randomUUID().toString(); Span span = tracer.spanBuilder(spanName).setSpanKind(SpanKind.CLIENT).startSpan(); - try (DaprClient client = new DaprClientBuilder().build()) { + try (DaprClient client = daprRun.newDaprClientBuilder().build()) { client.waitForSidecar(10000).block(); try (Scope scope = span.makeCurrent()) { client.invokeMethod(daprRun.getAppName(), "sleep", 1, HttpExtension.POST) diff --git a/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java b/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java index a31276b9d..ad67eed8c 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java @@ -117,12 +117,24 @@ public DaprClientBuilder withResiliencyOptions(ResiliencyOptions options) { * @param property that we want to override * @param value the value of such property * @return an instance of the setup Client - */ + */ public DaprClientBuilder withPropertyOverride(Property property, String value) { this.propertyOverrides.put(property.getName(), value); return this; } + /** + * Allow to set up properties override for static properties. + * @param overrides properties to override + * @return an instance of the setup Client + */ + public DaprClientBuilder withPropertyOverrides(Map, String> overrides) { + for (final Map.Entry, String> override : overrides.entrySet()) { + this.propertyOverrides.put(override.getKey().getName(), override.getValue()); + } + return this; + } + /** * Build an instance of the Client based on the provided setup. * diff --git a/sdk/src/main/java/io/dapr/client/DaprHttp.java b/sdk/src/main/java/io/dapr/client/DaprHttp.java index 01485e433..d2478d87f 100644 --- a/sdk/src/main/java/io/dapr/client/DaprHttp.java +++ b/sdk/src/main/java/io/dapr/client/DaprHttp.java @@ -71,8 +71,7 @@ public class DaprHttp implements AutoCloseable { /** * Context entries allowed to be in HTTP Headers. */ - private static final Set ALLOWED_CONTEXT_IN_HEADERS = - Collections.unmodifiableSet(new HashSet<>(Arrays.asList("grpc-trace-bin", "traceparent", "tracestate"))); + private static final Set ALLOWED_CONTEXT_IN_HEADERS = Set.of("grpc-trace-bin", "traceparent", "tracestate"); /** * Object mapper to parse DaprError with or without details. diff --git a/sdk/src/main/java/io/dapr/client/DaprTracingInterceptor.java b/sdk/src/main/java/io/dapr/client/DaprTracingInterceptor.java deleted file mode 100644 index 20f6d1c06..000000000 --- a/sdk/src/main/java/io/dapr/client/DaprTracingInterceptor.java +++ /dev/null @@ -1,48 +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.client; - -import io.grpc.CallOptions; -import io.grpc.Channel; -import io.grpc.ClientCall; -import io.grpc.ClientInterceptor; -import io.grpc.MethodDescriptor; -import reactor.util.context.ContextView; - -/** - * Injects tracing headers to gRPC metadata. - */ -public class DaprTracingInterceptor implements ClientInterceptor { - - private final io.dapr.internal.grpc.interceptors.DaprTracingInterceptor delegate; - - /** - * Creates an instance of the injector for gRPC context from Reactor's context. - * @param context Reactor's context - */ - public DaprTracingInterceptor(ContextView context) { - this.delegate = new io.dapr.internal.grpc.interceptors.DaprTracingInterceptor(context); - } - - /** - * {@inheritDoc} - */ - @Override - public ClientCall interceptCall( - MethodDescriptor methodDescriptor, - CallOptions callOptions, - Channel channel) { - return this.delegate.interceptCall(methodDescriptor, callOptions, channel); - } -} diff --git a/sdk/src/main/java/io/dapr/client/domain/BulkPublishEntry.java b/sdk/src/main/java/io/dapr/client/domain/BulkPublishEntry.java index da8a05ffb..57ba82609 100644 --- a/sdk/src/main/java/io/dapr/client/domain/BulkPublishEntry.java +++ b/sdk/src/main/java/io/dapr/client/domain/BulkPublishEntry.java @@ -55,7 +55,7 @@ public BulkPublishEntry(String entryId, T event, String contentType) { this.entryId = entryId; this.event = event; this.contentType = contentType; - this.metadata = Collections.unmodifiableMap(new HashMap<>()); + this.metadata = Map.of(); } /** @@ -70,8 +70,7 @@ public BulkPublishEntry(String entryId, T event, String contentType, Map()) : - Collections.unmodifiableMap(metadata); + this.metadata = metadata == null ? Map.of() : Collections.unmodifiableMap(metadata); } public String getEntryId() { diff --git a/sdk/src/main/java/io/dapr/client/domain/BulkPublishRequest.java b/sdk/src/main/java/io/dapr/client/domain/BulkPublishRequest.java index 4c6a3dee8..e4b29daff 100644 --- a/sdk/src/main/java/io/dapr/client/domain/BulkPublishRequest.java +++ b/sdk/src/main/java/io/dapr/client/domain/BulkPublishRequest.java @@ -72,10 +72,8 @@ public BulkPublishRequest(String pubsubName, String topic, List metadata) { this.pubsubName = pubsubName; this.topic = topic; - this.entries = entries == null ? Collections.unmodifiableList(new ArrayList<>()) : - Collections.unmodifiableList(entries); - this.metadata = metadata == null ? Collections.unmodifiableMap(new HashMap<>()) : - Collections.unmodifiableMap(metadata); + this.entries = entries == null ? List.of() : Collections.unmodifiableList(entries); + this.metadata = metadata == null ? Map.of() : Collections.unmodifiableMap(metadata); } public String getPubsubName() { diff --git a/sdk/src/main/java/io/dapr/config/Properties.java b/sdk/src/main/java/io/dapr/config/Properties.java index 38a88b767..14b0a4fb2 100644 --- a/sdk/src/main/java/io/dapr/config/Properties.java +++ b/sdk/src/main/java/io/dapr/config/Properties.java @@ -18,8 +18,8 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.time.Duration; -import java.util.Collections; import java.util.Map; +import java.util.stream.Collectors; /** * Global properties for Dapr's SDK, using Supplier so they are dynamically resolved. @@ -189,10 +189,17 @@ public Properties() { /** * Creates a new instance to handle Properties per instance. - * @param overrides to override static properties - */ - public Properties(Map overrides) { - this.overrides = Collections.unmodifiableMap(overrides); + * @param overridesInput to override static properties + */ + public Properties(Map overridesInput) { + this.overrides = overridesInput == null ? Map.of() : + Map.copyOf(overridesInput.entrySet().stream() + .filter(e -> e.getKey() != null) + .filter(e -> e.getValue() != null) + .collect(Collectors.toMap( + entry -> entry.getKey().toString(), + entry -> entry.getValue() + ))); } /** diff --git a/sdk/src/main/java/io/dapr/config/Property.java b/sdk/src/main/java/io/dapr/config/Property.java index f0e54f2e7..c24ab5384 100644 --- a/sdk/src/main/java/io/dapr/config/Property.java +++ b/sdk/src/main/java/io/dapr/config/Property.java @@ -58,6 +58,14 @@ public String getName() { return this.name; } + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return this.name; + } + /** * Gets the environment variable's name. * @return Name. diff --git a/sdk/src/main/java/io/dapr/exceptions/DaprErrorDetails.java b/sdk/src/main/java/io/dapr/exceptions/DaprErrorDetails.java index f78dc31e6..ee1d182f8 100644 --- a/sdk/src/main/java/io/dapr/exceptions/DaprErrorDetails.java +++ b/sdk/src/main/java/io/dapr/exceptions/DaprErrorDetails.java @@ -33,20 +33,18 @@ public class DaprErrorDetails { static final DaprErrorDetails EMPTY_INSTANCE = new DaprErrorDetails((Status) null); private static final Map, ErrorDetailType> SUPPORTED_ERROR_TYPES = - Collections.unmodifiableMap(new HashMap<>() { - { - put(com.google.rpc.ErrorInfo.class, ErrorDetailType.ERROR_INFO); - put(com.google.rpc.RetryInfo.class, ErrorDetailType.RETRY_INFO); - put(com.google.rpc.DebugInfo.class, ErrorDetailType.DEBUG_INFO); - put(com.google.rpc.QuotaFailure.class, ErrorDetailType.QUOTA_FAILURE); - put(com.google.rpc.PreconditionFailure.class, ErrorDetailType.PRECONDITION_FAILURE); - put(com.google.rpc.BadRequest.class, ErrorDetailType.BAD_REQUEST); - put(com.google.rpc.RequestInfo.class, ErrorDetailType.REQUEST_INFO); - put(com.google.rpc.ResourceInfo.class, ErrorDetailType.RESOURCE_INFO); - put(com.google.rpc.Help.class, ErrorDetailType.HELP); - put(com.google.rpc.LocalizedMessage.class, ErrorDetailType.LOCALIZED_MESSAGE); - } - }); + Map.of( + com.google.rpc.ErrorInfo.class, ErrorDetailType.ERROR_INFO, + com.google.rpc.RetryInfo.class, ErrorDetailType.RETRY_INFO, + com.google.rpc.DebugInfo.class, ErrorDetailType.DEBUG_INFO, + com.google.rpc.QuotaFailure.class, ErrorDetailType.QUOTA_FAILURE, + com.google.rpc.PreconditionFailure.class, ErrorDetailType.PRECONDITION_FAILURE, + com.google.rpc.BadRequest.class, ErrorDetailType.BAD_REQUEST, + com.google.rpc.RequestInfo.class, ErrorDetailType.REQUEST_INFO, + com.google.rpc.ResourceInfo.class, ErrorDetailType.RESOURCE_INFO, + com.google.rpc.Help.class, ErrorDetailType.HELP, + com.google.rpc.LocalizedMessage.class, ErrorDetailType.LOCALIZED_MESSAGE + ); private static final Map> ERROR_TYPES_FQN_REVERSE_LOOKUP = SUPPORTED_ERROR_TYPES.keySet().stream().collect(Collectors.toMap( diff --git a/sdk/src/test/java/io/dapr/client/DaprClientBuilderTest.java b/sdk/src/test/java/io/dapr/client/DaprClientBuilderTest.java index 4a972a019..dbc8ffae1 100644 --- a/sdk/src/test/java/io/dapr/client/DaprClientBuilderTest.java +++ b/sdk/src/test/java/io/dapr/client/DaprClientBuilderTest.java @@ -41,7 +41,6 @@ public void build() { @Test public void buildWithOverrideSidecarIP() { - DaprClientBuilder daprClientBuilder = new DaprClientBuilder(); daprClientBuilder.withPropertyOverride(Properties.SIDECAR_IP, "unknown-host"); DaprClient daprClient = daprClientBuilder.build(); diff --git a/sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java b/sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java index fae617c69..c09a00124 100644 --- a/sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java +++ b/sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java @@ -124,7 +124,6 @@ public void waitForSidecarTimeOutHealthCheck() throws Exception { @Test public void waitForSidecarBadHealthCheck() throws Exception { int port = findFreePort(); - System.setProperty(Properties.HTTP_PORT.getName(), Integer.toString(port)); daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), port, daprApiToken, okHttpClient); DaprClient daprClientHttp = buildDaprClient(daprHttp); @@ -149,7 +148,6 @@ public void waitForSidecarBadHealthCheck() throws Exception { @Test public void waitForSidecarSlowSuccessfulHealthCheck() throws Exception { int port = findFreePort(); - System.setProperty(Properties.HTTP_PORT.getName(), Integer.toString(port)); daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), port, daprApiToken, okHttpClient); DaprClient daprClientHttp = buildDaprClient(daprHttp); @@ -178,7 +176,6 @@ public void waitForSidecarSlowSuccessfulHealthCheck() throws Exception { @Test public void waitForSidecarOK() throws Exception { int port = findFreePort(); - System.setProperty(Properties.HTTP_PORT.getName(), Integer.toString(port)); daprHttp = new DaprHttp(sidecarIp, port, daprApiToken, okHttpClient); DaprClient daprClientHttp = buildDaprClient(daprHttp); @@ -201,7 +198,6 @@ public void waitForSidecarTimeoutOK() throws Exception { .respond(204); try (ServerSocket serverSocket = new ServerSocket(0)) { final int port = serverSocket.getLocalPort(); - System.setProperty(Properties.HTTP_PORT.getName(), Integer.toString(port)); Thread t = new Thread(() -> { try { try (Socket socket = serverSocket.accept()) { diff --git a/sdk/src/test/java/io/dapr/client/DaprHttpTest.java b/sdk/src/test/java/io/dapr/client/DaprHttpTest.java index b65270ba5..bf21e6419 100644 --- a/sdk/src/test/java/io/dapr/client/DaprHttpTest.java +++ b/sdk/src/test/java/io/dapr/client/DaprHttpTest.java @@ -122,9 +122,7 @@ public void invokeMethod() throws IOException { @Test public void invokeMethodIPv6() throws IOException { - String prevSidecarIp = sidecarIp; - System.setProperty(Properties.SIDECAR_IP.getName(), "2001:db8:3333:4444:5555:6666:7777:8888"); - sidecarIp = formatIpAddress(Properties.SIDECAR_IP.get()); + sidecarIp = formatIpAddress("2001:db8:3333:4444:5555:6666:7777:8888"); Map headers = new HashMap<>(); headers.put("content-type", "text/html"); headers.put("header1", "value1"); @@ -132,7 +130,6 @@ public void invokeMethodIPv6() throws IOException { .post("http://" + sidecarIp + ":3500/v1.0/state") .respond(serializer.serialize(EXPECTED_RESULT)); DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, okHttpClient); - System.setProperty(Properties.SIDECAR_IP.getName(), prevSidecarIp); Mono mono = daprHttp.invokeApi("POST", "v1.0/state".split("/"), null, (byte[]) null, headers, Context.empty()); DaprHttp.Response response = mono.block(); diff --git a/sdk/src/test/java/io/dapr/utils/NetworkUtilsTest.java b/sdk/src/test/java/io/dapr/utils/NetworkUtilsTest.java index 878d249f6..d90697f28 100644 --- a/sdk/src/test/java/io/dapr/utils/NetworkUtilsTest.java +++ b/sdk/src/test/java/io/dapr/utils/NetworkUtilsTest.java @@ -8,19 +8,25 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.HashMap; +import java.util.Map; + public class NetworkUtilsTest { private final int defaultGrpcPort = 4000; private final String defaultSidecarIP = "127.0.0.1"; private ManagedChannel channel; - private Properties properties = new Properties(); + private Map propertiesOverride; @BeforeEach public void setUp() { - System.setProperty(Properties.GRPC_PORT.getName(), Integer.toString(defaultGrpcPort)); - System.setProperty(Properties.SIDECAR_IP.getName(), defaultSidecarIP); - System.setProperty(Properties.GRPC_ENDPOINT.getName(), ""); + // Must be mutable for some test scenarios here. + propertiesOverride = new HashMap<>(Map.of( + Properties.GRPC_PORT.getName(), Integer.toString(defaultGrpcPort), + Properties.SIDECAR_IP.getName(), defaultSidecarIP, + Properties.GRPC_ENDPOINT.getName(), "" + )); } @AfterEach @@ -32,7 +38,7 @@ public void tearDown() { @Test public void testBuildGrpcManagedChannel() { - channel = NetworkUtils.buildGrpcManagedChannel(properties); + channel = NetworkUtils.buildGrpcManagedChannel(new Properties(propertiesOverride)); String expectedAuthority = String.format("%s:%s", defaultSidecarIP, defaultGrpcPort); Assertions.assertEquals(expectedAuthority, channel.authority()); @@ -40,8 +46,8 @@ public void testBuildGrpcManagedChannel() { @Test public void testBuildGrpcManagedChannel_httpEndpointNoPort() { - System.setProperty(Properties.GRPC_ENDPOINT.getName(), "http://example.com"); - channel = NetworkUtils.buildGrpcManagedChannel(properties); + propertiesOverride.put(Properties.GRPC_ENDPOINT.getName(), "http://example.com"); + channel = NetworkUtils.buildGrpcManagedChannel(new Properties(propertiesOverride)); String expectedAuthority = "example.com:80"; Assertions.assertEquals(expectedAuthority, channel.authority()); @@ -49,8 +55,8 @@ public void testBuildGrpcManagedChannel_httpEndpointNoPort() { @Test public void testBuildGrpcManagedChannel_httpEndpointWithPort() { - System.setProperty(Properties.GRPC_ENDPOINT.getName(), "http://example.com:3000"); - channel = NetworkUtils.buildGrpcManagedChannel(properties); + propertiesOverride.put(Properties.GRPC_ENDPOINT.getName(), "http://example.com:3000"); + channel = NetworkUtils.buildGrpcManagedChannel(new Properties(propertiesOverride)); String expectedAuthority = "example.com:3000"; Assertions.assertEquals(expectedAuthority, channel.authority()); @@ -58,8 +64,8 @@ public void testBuildGrpcManagedChannel_httpEndpointWithPort() { @Test public void testBuildGrpcManagedChannel_httpsEndpointNoPort() { - System.setProperty(Properties.GRPC_ENDPOINT.getName(), "https://example.com"); - channel = NetworkUtils.buildGrpcManagedChannel(properties); + propertiesOverride.put(Properties.GRPC_ENDPOINT.getName(), "https://example.com"); + channel = NetworkUtils.buildGrpcManagedChannel(new Properties(propertiesOverride)); String expectedAuthority = "example.com:443"; Assertions.assertEquals(expectedAuthority, channel.authority()); @@ -67,8 +73,8 @@ public void testBuildGrpcManagedChannel_httpsEndpointNoPort() { @Test public void testBuildGrpcManagedChannel_httpsEndpointWithPort() { - System.setProperty(Properties.GRPC_ENDPOINT.getName(), "https://example.com:3000"); - channel = NetworkUtils.buildGrpcManagedChannel(properties); + propertiesOverride.put(Properties.GRPC_ENDPOINT.getName(), "https://example.com:3000"); + channel = NetworkUtils.buildGrpcManagedChannel(new Properties(propertiesOverride)); String expectedAuthority = "example.com:3000"; Assertions.assertEquals(expectedAuthority, channel.authority()); @@ -138,8 +144,8 @@ private static void testGrpcEndpointParsingScenario( String expectedEndpoint, boolean expectSecure ) { - System.setProperty(Properties.GRPC_ENDPOINT.getName(), grpcEndpointEnvValue); - var settings = NetworkUtils.GrpcEndpointSettings.parse(new Properties()); + var override = Map.of(Properties.GRPC_ENDPOINT.getName(), grpcEndpointEnvValue); + var settings = NetworkUtils.GrpcEndpointSettings.parse(new Properties(override)); Assertions.assertEquals(expectedEndpoint, settings.endpoint); Assertions.assertEquals(expectSecure, settings.secure); @@ -147,8 +153,8 @@ private static void testGrpcEndpointParsingScenario( private static void testGrpcEndpointParsingErrorScenario(String grpcEndpointEnvValue) { try { - System.setProperty(Properties.GRPC_ENDPOINT.getName(), grpcEndpointEnvValue); - NetworkUtils.GrpcEndpointSettings.parse(new Properties()); + var override = Map.of(Properties.GRPC_ENDPOINT.getName(), grpcEndpointEnvValue); + NetworkUtils.GrpcEndpointSettings.parse(new Properties(override)); Assert.fail(); } catch (IllegalArgumentException e) { // Expected