mirror of https://github.com/dapr/java-sdk.git
Makes service method API invocations over HTTP by default. (#450)
* Makes service method API invocations over HTTP by default. * Add logic to avoid building managedchannel in case of http API for actor runtime.
This commit is contained in:
parent
38b899d8b8
commit
befed4a01b
|
@ -7,6 +7,7 @@ package io.dapr.actors.client;
|
|||
|
||||
import io.dapr.actors.ActorId;
|
||||
import io.dapr.actors.ActorUtils;
|
||||
import io.dapr.client.DaprApiProtocol;
|
||||
import io.dapr.client.DaprHttpBuilder;
|
||||
import io.dapr.config.Properties;
|
||||
import io.dapr.serializer.DaprObjectSerializer;
|
||||
|
@ -26,7 +27,7 @@ public class ActorProxyBuilder<T> implements Closeable {
|
|||
/**
|
||||
* Determine if this builder will create GRPC clients instead of HTTP clients.
|
||||
*/
|
||||
private final boolean useGrpc;
|
||||
private final DaprApiProtocol apiProtocol;
|
||||
|
||||
/**
|
||||
* Actor's type.
|
||||
|
@ -73,6 +74,19 @@ public class ActorProxyBuilder<T> implements Closeable {
|
|||
* @param actorTypeClass Actor's type class.
|
||||
*/
|
||||
public ActorProxyBuilder(String actorType, Class<T> actorTypeClass) {
|
||||
this(actorType, actorTypeClass, Properties.API_PROTOCOL.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new builder for a given Actor type, using {@link DefaultObjectSerializer} by default.
|
||||
*
|
||||
* {@link DefaultObjectSerializer} is not recommended for production scenarios.
|
||||
*
|
||||
* @param actorType Actor's type.
|
||||
* @param actorTypeClass Actor's type class.
|
||||
* @param apiProtocol Dapr's API protocol.
|
||||
*/
|
||||
private ActorProxyBuilder(String actorType, Class<T> actorTypeClass, DaprApiProtocol apiProtocol) {
|
||||
if ((actorType == null) || actorType.isEmpty()) {
|
||||
throw new IllegalArgumentException("ActorType is required.");
|
||||
}
|
||||
|
@ -80,12 +94,12 @@ public class ActorProxyBuilder<T> implements Closeable {
|
|||
throw new IllegalArgumentException("ActorTypeClass is required.");
|
||||
}
|
||||
|
||||
this.useGrpc = Properties.USE_GRPC.get();
|
||||
this.apiProtocol = apiProtocol;
|
||||
this.actorType = actorType;
|
||||
this.objectSerializer = new DefaultObjectSerializer();
|
||||
this.clazz = actorTypeClass;
|
||||
this.daprHttpBuilder = new DaprHttpBuilder();
|
||||
this.channel = buildManagedChannel();
|
||||
this.channel = buildManagedChannel(apiProtocol);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -138,11 +152,11 @@ public class ActorProxyBuilder<T> implements Closeable {
|
|||
* @throws java.lang.IllegalStateException if any required field is missing
|
||||
*/
|
||||
private DaprClient buildDaprClient() {
|
||||
if (this.useGrpc) {
|
||||
return new DaprGrpcClient(DaprGrpc.newFutureStub(this.channel));
|
||||
switch (this.apiProtocol) {
|
||||
case GRPC: return new DaprGrpcClient(DaprGrpc.newFutureStub(this.channel));
|
||||
case HTTP: return new DaprHttpClient(daprHttpBuilder.build());
|
||||
default: throw new IllegalStateException("Unsupported protocol: " + this.apiProtocol.name());
|
||||
}
|
||||
|
||||
return new DaprHttpClient(daprHttpBuilder.build());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -158,10 +172,11 @@ public class ActorProxyBuilder<T> implements Closeable {
|
|||
/**
|
||||
* Creates a GRPC managed channel (or null, if not applicable).
|
||||
*
|
||||
* @param apiProtocol Dapr's API protocol.
|
||||
* @return GRPC managed channel or null.
|
||||
*/
|
||||
private static ManagedChannel buildManagedChannel() {
|
||||
if (!Properties.USE_GRPC.get()) {
|
||||
private static ManagedChannel buildManagedChannel(DaprApiProtocol apiProtocol) {
|
||||
if (apiProtocol != DaprApiProtocol.GRPC) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ package io.dapr.actors.runtime;
|
|||
|
||||
import io.dapr.actors.ActorId;
|
||||
import io.dapr.actors.ActorTrace;
|
||||
import io.dapr.client.DaprApiProtocol;
|
||||
import io.dapr.client.DaprHttpBuilder;
|
||||
import io.dapr.config.Properties;
|
||||
import io.dapr.serializer.DaprObjectSerializer;
|
||||
|
@ -307,7 +308,7 @@ public class ActorRuntime implements Closeable {
|
|||
* @throws java.lang.IllegalStateException if any required field is missing
|
||||
*/
|
||||
private static DaprClient buildDaprClient(ManagedChannel channel) {
|
||||
if (Properties.USE_GRPC.get()) {
|
||||
if (Properties.API_PROTOCOL.get() == DaprApiProtocol.GRPC) {
|
||||
return new DaprGrpcClient(channel);
|
||||
}
|
||||
|
||||
|
@ -320,7 +321,7 @@ public class ActorRuntime implements Closeable {
|
|||
* @return GRPC managed channel or null.
|
||||
*/
|
||||
private static ManagedChannel buildManagedChannel() {
|
||||
if (!Properties.USE_GRPC.get()) {
|
||||
if (Properties.API_PROTOCOL.get() != DaprApiProtocol.GRPC) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
package io.dapr.it;
|
||||
|
||||
import io.dapr.client.DaprApiProtocol;
|
||||
import io.dapr.config.Properties;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -19,7 +20,7 @@ import static io.dapr.it.Retry.callWithRetry;
|
|||
public class AppRun implements Stoppable {
|
||||
|
||||
private static final String APP_COMMAND =
|
||||
"mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=test -D exec.args=\"%s\" -D dapr.grpc.enabled=%b";
|
||||
"mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=test -D exec.args=\"%s\" -D %s=%s -D %s=%s";
|
||||
|
||||
private final DaprPorts ports;
|
||||
|
||||
|
@ -31,10 +32,10 @@ public class AppRun implements Stoppable {
|
|||
String successMessage,
|
||||
Class serviceClass,
|
||||
int maxWaitMilliseconds,
|
||||
boolean useGRPC) {
|
||||
DaprApiProtocol protocol) {
|
||||
this.command = new Command(
|
||||
successMessage,
|
||||
buildCommand(serviceClass, ports, useGRPC),
|
||||
buildCommand(serviceClass, ports, protocol),
|
||||
new HashMap<>() {{
|
||||
put("DAPR_HTTP_PORT", ports.getHttpPort().toString());
|
||||
put("DAPR_GRPC_PORT", ports.getGrpcPort().toString());
|
||||
|
@ -72,10 +73,11 @@ public class AppRun implements Stoppable {
|
|||
}
|
||||
}
|
||||
|
||||
private static String buildCommand(Class serviceClass, DaprPorts ports, boolean useGRPC) {
|
||||
private static String buildCommand(Class serviceClass, DaprPorts ports, DaprApiProtocol protocol) {
|
||||
return String.format(APP_COMMAND, serviceClass.getCanonicalName(),
|
||||
ports.getAppPort() != null ? ports.getAppPort().toString() : "",
|
||||
useGRPC);
|
||||
Properties.API_PROTOCOL.getName(), protocol,
|
||||
Properties.API_METHOD_INVOCATION_PROTOCOL.getName(), protocol);
|
||||
}
|
||||
|
||||
private static void assertListeningOnPort(int port) {
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
package io.dapr.it;
|
||||
|
||||
import io.dapr.client.DaprApiProtocol;
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.junit.AfterClass;
|
||||
|
||||
|
@ -30,7 +31,7 @@ public abstract class BaseIT {
|
|||
Class serviceClass,
|
||||
Boolean useAppPort,
|
||||
int maxWaitMilliseconds) throws Exception {
|
||||
return startDaprApp(testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, true);
|
||||
return startDaprApp(testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, DaprApiProtocol.GRPC);
|
||||
}
|
||||
|
||||
protected static DaprRun startDaprApp(
|
||||
|
@ -39,14 +40,15 @@ public abstract class BaseIT {
|
|||
Class serviceClass,
|
||||
Boolean useAppPort,
|
||||
int maxWaitMilliseconds,
|
||||
boolean useGRPC) throws Exception {
|
||||
return startDaprApp(testName, successMessage, serviceClass, useAppPort, true, maxWaitMilliseconds, useGRPC);
|
||||
DaprApiProtocol protocol) throws Exception {
|
||||
return startDaprApp(testName, successMessage, serviceClass, useAppPort, true, maxWaitMilliseconds, protocol);
|
||||
}
|
||||
|
||||
protected static DaprRun startDaprApp(
|
||||
String testName,
|
||||
int maxWaitMilliseconds) throws Exception {
|
||||
return startDaprApp(testName, "You're up and running!", null, false, true, maxWaitMilliseconds, true);
|
||||
return startDaprApp(
|
||||
testName, "You're up and running!", null, false, true, maxWaitMilliseconds, DaprApiProtocol.GRPC);
|
||||
}
|
||||
|
||||
protected static DaprRun startDaprApp(
|
||||
|
@ -56,13 +58,13 @@ public abstract class BaseIT {
|
|||
Boolean useAppPort,
|
||||
Boolean useDaprPorts,
|
||||
int maxWaitMilliseconds,
|
||||
boolean useGRPC) throws Exception {
|
||||
DaprApiProtocol protocol) throws Exception {
|
||||
DaprRun.Builder builder = new DaprRun.Builder(
|
||||
testName,
|
||||
() -> DaprPorts.build(useAppPort, useDaprPorts, useDaprPorts),
|
||||
successMessage,
|
||||
maxWaitMilliseconds,
|
||||
useGRPC).withServiceClass(serviceClass);
|
||||
protocol).withServiceClass(serviceClass);
|
||||
DaprRun run = builder.build();
|
||||
TO_BE_STOPPED.add(run);
|
||||
DAPR_RUN_BUILDERS.put(run.getAppName(), builder);
|
||||
|
@ -77,7 +79,8 @@ public abstract class BaseIT {
|
|||
Class serviceClass,
|
||||
Boolean useAppPort,
|
||||
int maxWaitMilliseconds) throws Exception {
|
||||
return startSplitDaprAndApp(testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, true);
|
||||
return startSplitDaprAndApp(
|
||||
testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, DaprApiProtocol.GRPC);
|
||||
}
|
||||
|
||||
protected static ImmutablePair<AppRun, DaprRun> startSplitDaprAndApp(
|
||||
|
@ -86,13 +89,13 @@ public abstract class BaseIT {
|
|||
Class serviceClass,
|
||||
Boolean useAppPort,
|
||||
int maxWaitMilliseconds,
|
||||
boolean useGRPC) throws Exception {
|
||||
DaprApiProtocol protocol) throws Exception {
|
||||
DaprRun.Builder builder = new DaprRun.Builder(
|
||||
testName,
|
||||
() -> DaprPorts.build(useAppPort, true, true),
|
||||
successMessage,
|
||||
maxWaitMilliseconds,
|
||||
useGRPC).withServiceClass(serviceClass);
|
||||
protocol).withServiceClass(serviceClass);
|
||||
ImmutablePair<AppRun, DaprRun> runs = builder.splitBuild();
|
||||
TO_BE_STOPPED.add(runs.left);
|
||||
TO_BE_STOPPED.add(runs.right);
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
package io.dapr.it;
|
||||
|
||||
import io.dapr.client.DaprApiProtocol;
|
||||
import io.dapr.config.Properties;
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
|
||||
|
@ -23,7 +24,7 @@ public class DaprRun implements Stoppable {
|
|||
|
||||
// the arg in -Dexec.args is the app's port
|
||||
private static final String DAPR_COMMAND =
|
||||
" -- mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=test -D exec.args=\"%s\" -D dapr.grpc.enabled=%s";
|
||||
" -- mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=test -D exec.args=\"%s\" -D %s=%s -D %s=%s";
|
||||
|
||||
private final DaprPorts ports;
|
||||
|
||||
|
@ -44,11 +45,11 @@ public class DaprRun implements Stoppable {
|
|||
String successMessage,
|
||||
Class serviceClass,
|
||||
int maxWaitMilliseconds,
|
||||
boolean useGRPC) {
|
||||
DaprApiProtocol protocol) {
|
||||
// The app name needs to be deterministic since we depend on it to kill previous runs.
|
||||
this.appName = serviceClass == null ? testName : String.format("%s_%s", testName, serviceClass.getSimpleName());
|
||||
this.startCommand =
|
||||
new Command(successMessage, buildDaprCommand(this.appName, serviceClass, ports, useGRPC));
|
||||
new Command(successMessage, buildDaprCommand(this.appName, serviceClass, ports, protocol));
|
||||
this.listCommand = new Command(
|
||||
this.appName,
|
||||
"dapr list");
|
||||
|
@ -132,20 +133,24 @@ public class DaprRun implements Stoppable {
|
|||
|
||||
public void use() {
|
||||
if (this.ports.getHttpPort() != null) {
|
||||
System.getProperties().setProperty("dapr.http.port", String.valueOf(this.ports.getHttpPort()));
|
||||
System.getProperties().setProperty(Properties.HTTP_PORT.getName(), String.valueOf(this.ports.getHttpPort()));
|
||||
}
|
||||
if (this.ports.getGrpcPort() != null) {
|
||||
System.getProperties().setProperty("dapr.grpc.port", String.valueOf(this.ports.getGrpcPort()));
|
||||
System.getProperties().setProperty(Properties.GRPC_PORT.getName(), String.valueOf(this.ports.getGrpcPort()));
|
||||
}
|
||||
System.getProperties().setProperty("dapr.grpc.enabled", Boolean.TRUE.toString());
|
||||
System.getProperties().setProperty(Properties.API_PROTOCOL.getName(), DaprApiProtocol.GRPC.name());
|
||||
}
|
||||
|
||||
public void switchToGRPC() {
|
||||
System.getProperties().setProperty("dapr.grpc.enabled", Boolean.TRUE.toString());
|
||||
System.getProperties().setProperty(Properties.API_PROTOCOL.getName(), DaprApiProtocol.GRPC.name());
|
||||
}
|
||||
|
||||
public void switchToHTTP() {
|
||||
System.getProperties().setProperty("dapr.grpc.enabled", Boolean.FALSE.toString());
|
||||
System.getProperties().setProperty(Properties.API_PROTOCOL.getName(), DaprApiProtocol.HTTP.name());
|
||||
}
|
||||
|
||||
public void switchToProtocol(DaprApiProtocol protocol) {
|
||||
System.getProperties().setProperty(Properties.API_PROTOCOL.getName(), protocol.name());
|
||||
}
|
||||
|
||||
public int getGrpcPort() {
|
||||
|
@ -164,14 +169,17 @@ public class DaprRun implements Stoppable {
|
|||
return appName;
|
||||
}
|
||||
|
||||
private static String buildDaprCommand(String appName, Class serviceClass, DaprPorts ports, boolean useGRPC) {
|
||||
private static String buildDaprCommand(
|
||||
String appName, Class serviceClass, DaprPorts ports, DaprApiProtocol protocol) {
|
||||
StringBuilder stringBuilder = new StringBuilder(String.format(DAPR_RUN, appName))
|
||||
.append(ports.getAppPort() != null ? " --app-port " + ports.getAppPort() : "")
|
||||
.append(ports.getHttpPort() != null ? " --dapr-http-port " + ports.getHttpPort() : "")
|
||||
.append(ports.getGrpcPort() != null ? " --dapr-grpc-port " + ports.getGrpcPort() : "")
|
||||
.append(serviceClass == null ? "" :
|
||||
String.format(DAPR_COMMAND, serviceClass.getCanonicalName(),
|
||||
ports.getAppPort() != null ? ports.getAppPort().toString() : "", useGRPC));
|
||||
ports.getAppPort() != null ? ports.getAppPort().toString() : "",
|
||||
Properties.API_PROTOCOL.getName(), protocol,
|
||||
Properties.API_METHOD_INVOCATION_PROTOCOL.getName(), protocol));
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
|
@ -200,19 +208,19 @@ public class DaprRun implements Stoppable {
|
|||
|
||||
private Class serviceClass;
|
||||
|
||||
private boolean useGRPC;
|
||||
private DaprApiProtocol protocol;
|
||||
|
||||
Builder(
|
||||
String testName,
|
||||
Supplier<DaprPorts> portsSupplier,
|
||||
String successMessage,
|
||||
int maxWaitMilliseconds,
|
||||
boolean useGRPC) {
|
||||
DaprApiProtocol protocol) {
|
||||
this.testName = testName;
|
||||
this.portsSupplier = portsSupplier;
|
||||
this.successMessage = successMessage;
|
||||
this.maxWaitMilliseconds = maxWaitMilliseconds;
|
||||
this.useGRPC = useGRPC;
|
||||
this.protocol = protocol;
|
||||
}
|
||||
|
||||
public Builder withServiceClass(Class serviceClass) {
|
||||
|
@ -227,7 +235,7 @@ public class DaprRun implements Stoppable {
|
|||
this.successMessage,
|
||||
this.serviceClass,
|
||||
this.maxWaitMilliseconds,
|
||||
this.useGRPC);
|
||||
this.protocol);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -241,7 +249,7 @@ public class DaprRun implements Stoppable {
|
|||
this.successMessage,
|
||||
this.serviceClass,
|
||||
this.maxWaitMilliseconds,
|
||||
this.useGRPC);
|
||||
this.protocol);
|
||||
|
||||
DaprRun daprRun = new DaprRun(
|
||||
this.testName,
|
||||
|
@ -249,7 +257,7 @@ public class DaprRun implements Stoppable {
|
|||
DAPR_SUCCESS_MESSAGE,
|
||||
null,
|
||||
this.maxWaitMilliseconds,
|
||||
this.useGRPC);
|
||||
this.protocol);
|
||||
|
||||
return new ImmutablePair<>(appRun, daprRun);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ package io.dapr.it.actors;
|
|||
import io.dapr.actors.ActorId;
|
||||
import io.dapr.actors.client.ActorProxy;
|
||||
import io.dapr.actors.client.ActorProxyBuilder;
|
||||
import io.dapr.client.DaprApiProtocol;
|
||||
import io.dapr.it.BaseIT;
|
||||
import io.dapr.it.DaprRun;
|
||||
import io.dapr.it.actors.services.springboot.StatefulActor;
|
||||
|
@ -38,14 +39,19 @@ public class ActorStateIT extends BaseIT {
|
|||
*/
|
||||
@Parameterized.Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][] { { false, false }, { false, true }, { true, false }, { true, true } });
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ DaprApiProtocol.HTTP, DaprApiProtocol.HTTP },
|
||||
{ DaprApiProtocol.HTTP, DaprApiProtocol.GRPC },
|
||||
{ DaprApiProtocol.GRPC, DaprApiProtocol.HTTP },
|
||||
{ DaprApiProtocol.GRPC, DaprApiProtocol.GRPC },
|
||||
});
|
||||
}
|
||||
|
||||
@Parameterized.Parameter(0)
|
||||
public boolean useGrpc;
|
||||
public DaprApiProtocol daprClientProtocol;
|
||||
|
||||
@Parameterized.Parameter(1)
|
||||
public boolean useGrpcInService;
|
||||
public DaprApiProtocol serviceAppProtocol;
|
||||
|
||||
@Test
|
||||
public void writeReadState() throws Exception {
|
||||
|
@ -57,19 +63,15 @@ public class ActorStateIT extends BaseIT {
|
|||
StatefulActorService.class,
|
||||
true,
|
||||
60000,
|
||||
useGrpcInService);
|
||||
serviceAppProtocol);
|
||||
|
||||
if (this.useGrpc) {
|
||||
runtime.switchToGRPC();
|
||||
} else {
|
||||
runtime.switchToHTTP();
|
||||
}
|
||||
runtime.switchToProtocol(this.daprClientProtocol);
|
||||
|
||||
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-%b", System.currentTimeMillis(), this.useGrpc, this.useGrpcInService));
|
||||
String.format("%d-%b-%b", System.currentTimeMillis(), this.daprClientProtocol, this.serviceAppProtocol));
|
||||
String actorType = "StatefulActorTest";
|
||||
logger.debug("Building proxy ...");
|
||||
ActorProxyBuilder<ActorProxy> proxyBuilder = deferClose(new ActorProxyBuilder(actorType, ActorProxy.class));
|
||||
|
@ -153,13 +155,9 @@ public class ActorStateIT extends BaseIT {
|
|||
StatefulActorService.class,
|
||||
true,
|
||||
60000,
|
||||
useGrpcInService);
|
||||
serviceAppProtocol);
|
||||
|
||||
if (this.useGrpc) {
|
||||
run2.switchToGRPC();
|
||||
} else {
|
||||
run2.switchToHTTP();
|
||||
}
|
||||
runtime.switchToProtocol(this.daprClientProtocol);
|
||||
|
||||
// Need new proxy builder because the proxy builder holds the channel.
|
||||
proxyBuilder = deferClose(new ActorProxyBuilder(actorType, ActorProxy.class));
|
||||
|
|
|
@ -9,8 +9,6 @@ import com.bettercloud.vault.Vault;
|
|||
import com.bettercloud.vault.VaultConfig;
|
||||
import io.dapr.client.DaprClient;
|
||||
import io.dapr.client.DaprClientBuilder;
|
||||
import io.dapr.client.DaprClientGrpc;
|
||||
import io.dapr.client.DaprClientHttp;
|
||||
import io.dapr.it.BaseIT;
|
||||
import io.dapr.it.DaprRun;
|
||||
import org.junit.After;
|
||||
|
@ -79,7 +77,7 @@ public class SecretsClientIT extends BaseIT {
|
|||
}
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
public void setup() {
|
||||
if (this.useGrpc) {
|
||||
daprRun.switchToGRPC();
|
||||
} else {
|
||||
|
@ -87,11 +85,6 @@ public class SecretsClientIT extends BaseIT {
|
|||
}
|
||||
|
||||
this.daprClient = new DaprClientBuilder().build();
|
||||
if (this.useGrpc) {
|
||||
assertEquals(DaprClientGrpc.class, this.daprClient.getClass());
|
||||
} else {
|
||||
assertEquals(DaprClientHttp.class, this.daprClient.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
|
|
|
@ -7,7 +7,6 @@ package io.dapr.it.state;
|
|||
|
||||
import io.dapr.client.DaprClient;
|
||||
import io.dapr.client.DaprClientBuilder;
|
||||
import io.dapr.client.DaprClientGrpc;
|
||||
import io.dapr.client.domain.State;
|
||||
import io.dapr.it.DaprRun;
|
||||
import org.junit.AfterClass;
|
||||
|
@ -17,7 +16,6 @@ import org.junit.Test;
|
|||
import java.util.Collections;
|
||||
|
||||
import static io.dapr.it.TestUtils.assertThrowsDaprException;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Test State GRPC DAPR capabilities using a DAPR instance with an empty service running
|
||||
|
@ -33,8 +31,6 @@ public class GRPCStateClientIT extends AbstractStateClientIT {
|
|||
daprRun = startDaprApp(GRPCStateClientIT.class.getSimpleName(), 5000);
|
||||
daprRun.switchToGRPC();
|
||||
daprClient = new DaprClientBuilder().build();
|
||||
|
||||
assertTrue(daprClient instanceof DaprClientGrpc);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
|
||||
package io.dapr.client;
|
||||
|
||||
/**
|
||||
* Transport protocol for Dapr's API.
|
||||
*/
|
||||
public enum DaprApiProtocol {
|
||||
|
||||
GRPC,
|
||||
HTTP
|
||||
|
||||
}
|
|
@ -24,7 +24,12 @@ public class DaprClientBuilder {
|
|||
/**
|
||||
* Determine if this builder will create GRPC clients instead of HTTP clients.
|
||||
*/
|
||||
private final boolean useGrpc;
|
||||
private final DaprApiProtocol apiProtocol;
|
||||
|
||||
/**
|
||||
* Determine if this builder will use HTTP client for service method invocation APIs.
|
||||
*/
|
||||
private final DaprApiProtocol methodInvocationApiProtocol;
|
||||
|
||||
/**
|
||||
* Builder for Dapr's HTTP Client.
|
||||
|
@ -50,7 +55,8 @@ public class DaprClientBuilder {
|
|||
public DaprClientBuilder() {
|
||||
this.objectSerializer = new DefaultObjectSerializer();
|
||||
this.stateSerializer = new DefaultObjectSerializer();
|
||||
this.useGrpc = Properties.USE_GRPC.get();
|
||||
this.apiProtocol = Properties.API_PROTOCOL.get();
|
||||
this.methodInvocationApiProtocol = Properties.API_METHOD_INVOCATION_PROTOCOL.get();
|
||||
this.daprHttpBuilder = new DaprHttpBuilder();
|
||||
}
|
||||
|
||||
|
@ -97,11 +103,30 @@ public class DaprClientBuilder {
|
|||
* @throws java.lang.IllegalStateException if any required field is missing
|
||||
*/
|
||||
public DaprClient build() {
|
||||
if (this.useGrpc) {
|
||||
return buildDaprClientGrpc();
|
||||
if (this.apiProtocol != this.methodInvocationApiProtocol) {
|
||||
return new DaprClientProxy(buildDaprClient(this.apiProtocol), buildDaprClient(this.methodInvocationApiProtocol));
|
||||
}
|
||||
|
||||
return buildDaprClientHttp();
|
||||
return buildDaprClient(this.apiProtocol);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of a Dapr Client based on the chosen protocol.
|
||||
*
|
||||
* @param protocol Dapr API's protocol.
|
||||
* @return the GRPC Client.
|
||||
* @throws java.lang.IllegalStateException if either host is missing or if port is missing or a negative number.
|
||||
*/
|
||||
private DaprClient buildDaprClient(DaprApiProtocol protocol) {
|
||||
if (protocol == null) {
|
||||
throw new IllegalStateException("Protocol is required.");
|
||||
}
|
||||
|
||||
switch (protocol) {
|
||||
case GRPC: return buildDaprClientGrpc();
|
||||
case HTTP: return buildDaprClientHttp();
|
||||
default: throw new IllegalStateException("Unsupported protocol: " + protocol.name());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,478 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
|
||||
package io.dapr.client;
|
||||
|
||||
import io.dapr.client.domain.DeleteStateRequest;
|
||||
import io.dapr.client.domain.ExecuteStateTransactionRequest;
|
||||
import io.dapr.client.domain.GetBulkStateRequest;
|
||||
import io.dapr.client.domain.GetSecretRequest;
|
||||
import io.dapr.client.domain.GetStateRequest;
|
||||
import io.dapr.client.domain.HttpExtension;
|
||||
import io.dapr.client.domain.InvokeBindingRequest;
|
||||
import io.dapr.client.domain.InvokeMethodRequest;
|
||||
import io.dapr.client.domain.PublishEventRequest;
|
||||
import io.dapr.client.domain.Response;
|
||||
import io.dapr.client.domain.SaveStateRequest;
|
||||
import io.dapr.client.domain.State;
|
||||
import io.dapr.client.domain.StateOptions;
|
||||
import io.dapr.client.domain.TransactionalStateOperation;
|
||||
import io.dapr.utils.TypeRef;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Class that delegates to other implementations.
|
||||
*
|
||||
* @see DaprClient
|
||||
* @see DaprClientGrpc
|
||||
* @see DaprClientHttp
|
||||
*/
|
||||
class DaprClientProxy implements DaprClient {
|
||||
|
||||
/**
|
||||
* Client for all API invocations.
|
||||
*/
|
||||
private final DaprClient client;
|
||||
|
||||
/**
|
||||
* Client to override Dapr's service invocation APIs.
|
||||
*/
|
||||
private final DaprClient methodInvocationOverrideClient;
|
||||
|
||||
/**
|
||||
* Constructor with delegate client.
|
||||
*
|
||||
* @param client Client for all API invocations.
|
||||
* @see DaprClientBuilder
|
||||
*/
|
||||
DaprClientProxy(DaprClient client) {
|
||||
this(client, client);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with delegate client and override client for Dapr's method invocation APIs.
|
||||
*
|
||||
* @param client Client for all API invocations, except override below.
|
||||
* @param methodInvocationOverrideClient Client to override Dapr's service invocation APIs.
|
||||
* @see DaprClientBuilder
|
||||
*/
|
||||
DaprClientProxy(
|
||||
DaprClient client,
|
||||
DaprClient methodInvocationOverrideClient) {
|
||||
this.client = client;
|
||||
this.methodInvocationOverrideClient = methodInvocationOverrideClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> waitForSidecar(int timeoutInMilliseconds) {
|
||||
return client.waitForSidecar(timeoutInMilliseconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> publishEvent(String pubsubName, String topicName, Object data) {
|
||||
return client.publishEvent(pubsubName, topicName, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> publishEvent(String pubsubName, String topicName, Object data, Map<String, String> metadata) {
|
||||
return client.publishEvent(pubsubName, topicName, data, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Response<Void>> publishEvent(PublishEventRequest request) {
|
||||
return client.publishEvent(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<T> invokeMethod(String appId,
|
||||
String methodName,
|
||||
Object data,
|
||||
HttpExtension httpExtension,
|
||||
Map<String, String> metadata,
|
||||
TypeRef<T> type) {
|
||||
return methodInvocationOverrideClient.invokeMethod(appId, methodName, data, httpExtension, metadata, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<T> invokeMethod(String appId,
|
||||
String methodName,
|
||||
Object request,
|
||||
HttpExtension httpExtension,
|
||||
Map<String, String> metadata,
|
||||
Class<T> clazz) {
|
||||
return methodInvocationOverrideClient.invokeMethod(appId, methodName, request, httpExtension, metadata, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<T> invokeMethod(String appId,
|
||||
String methodName,
|
||||
Object request,
|
||||
HttpExtension httpExtension,
|
||||
TypeRef<T> type) {
|
||||
return methodInvocationOverrideClient.invokeMethod(appId, methodName, request, httpExtension, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<T> invokeMethod(String appId,
|
||||
String methodName,
|
||||
Object request,
|
||||
HttpExtension httpExtension,
|
||||
Class<T> clazz) {
|
||||
return methodInvocationOverrideClient.invokeMethod(appId, methodName, request, httpExtension, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<T> invokeMethod(String appId,
|
||||
String methodName,
|
||||
HttpExtension httpExtension,
|
||||
Map<String, String> metadata,
|
||||
TypeRef<T> type) {
|
||||
return methodInvocationOverrideClient.invokeMethod(appId, methodName, httpExtension, metadata, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<T> invokeMethod(String appId,
|
||||
String methodName,
|
||||
HttpExtension httpExtension,
|
||||
Map<String, String> metadata,
|
||||
Class<T> clazz) {
|
||||
return methodInvocationOverrideClient.invokeMethod(appId, methodName, httpExtension, metadata, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> invokeMethod(String appId,
|
||||
String methodName,
|
||||
Object request,
|
||||
HttpExtension httpExtension,
|
||||
Map<String, String> metadata) {
|
||||
return methodInvocationOverrideClient.invokeMethod(appId, methodName, request, httpExtension, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> invokeMethod(String appId, String methodName, Object request, HttpExtension httpExtension) {
|
||||
return methodInvocationOverrideClient.invokeMethod(appId, methodName, request, httpExtension);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> invokeMethod(String appId,
|
||||
String methodName,
|
||||
HttpExtension httpExtension,
|
||||
Map<String, String> metadata) {
|
||||
return methodInvocationOverrideClient.invokeMethod(appId, methodName, httpExtension, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<byte[]> invokeMethod(String appId,
|
||||
String methodName,
|
||||
byte[] request,
|
||||
HttpExtension httpExtension,
|
||||
Map<String, String> metadata) {
|
||||
return methodInvocationOverrideClient.invokeMethod(appId, methodName, request, httpExtension, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<Response<T>> invokeMethod(InvokeMethodRequest invokeMethodRequest, TypeRef<T> type) {
|
||||
return methodInvocationOverrideClient.invokeMethod(invokeMethodRequest, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> invokeBinding(String bindingName, String operation, Object data) {
|
||||
return client.invokeBinding(bindingName, operation, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<byte[]> invokeBinding(String bindingName, String operation, byte[] data, Map<String, String> metadata) {
|
||||
return client.invokeBinding(bindingName, operation, data, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<T> invokeBinding(String bindingName, String operation, Object data, TypeRef<T> type) {
|
||||
return client.invokeBinding(bindingName, operation, data, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<T> invokeBinding(String bindingName, String operation, Object data, Class<T> clazz) {
|
||||
return client.invokeBinding(bindingName, operation, data, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<T> invokeBinding(String bindingName,
|
||||
String operation,
|
||||
Object data,
|
||||
Map<String, String> metadata,
|
||||
TypeRef<T> type) {
|
||||
return client.invokeBinding(bindingName, operation, data, metadata, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<T> invokeBinding(String bindingName,
|
||||
String operation,
|
||||
Object data,
|
||||
Map<String, String> metadata,
|
||||
Class<T> clazz) {
|
||||
return client.invokeBinding(bindingName, operation, data, metadata, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<Response<T>> invokeBinding(InvokeBindingRequest request, TypeRef<T> type) {
|
||||
return client.invokeBinding(request, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<State<T>> getState(String storeName, State<T> state, TypeRef<T> type) {
|
||||
return client.getState(storeName, state, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<State<T>> getState(String storeName, State<T> state, Class<T> clazz) {
|
||||
return client.getState(storeName, state, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<State<T>> getState(String storeName, String key, TypeRef<T> type) {
|
||||
return client.getState(storeName, key, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<State<T>> getState(String storeName, String key, Class<T> clazz) {
|
||||
return client.getState(storeName, key, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<State<T>> getState(String storeName, String key, StateOptions options, TypeRef<T> type) {
|
||||
return client.getState(storeName, key, options, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<State<T>> getState(String storeName, String key, StateOptions options, Class<T> clazz) {
|
||||
return client.getState(storeName, key, options, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<Response<State<T>>> getState(GetStateRequest request, TypeRef<T> type) {
|
||||
return client.getState(request, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<List<State<T>>> getBulkState(String storeName, List<String> keys, TypeRef<T> type) {
|
||||
return client.getBulkState(storeName, keys, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<List<State<T>>> getBulkState(String storeName, List<String> keys, Class<T> clazz) {
|
||||
return client.getBulkState(storeName, keys, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<Response<List<State<T>>>> getBulkState(GetBulkStateRequest request, TypeRef<T> type) {
|
||||
return client.getBulkState(request, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> executeStateTransaction(String storeName, List<TransactionalStateOperation<?>> operations) {
|
||||
return client.executeStateTransaction(storeName, operations);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Response<Void>> executeStateTransaction(ExecuteStateTransactionRequest request) {
|
||||
return client.executeStateTransaction(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> saveBulkState(String storeName, List<State<?>> states) {
|
||||
return client.saveBulkState(storeName, states);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Response<Void>> saveBulkState(SaveStateRequest request) {
|
||||
return client.saveBulkState(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> saveState(String storeName, String key, Object value) {
|
||||
return client.saveState(storeName, key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> saveState(String storeName, String key, String etag, Object value, StateOptions options) {
|
||||
return client.saveState(storeName, key, etag, value, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> deleteState(String storeName, String key) {
|
||||
return client.deleteState(storeName, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> deleteState(String storeName, String key, String etag, StateOptions options) {
|
||||
return client.deleteState(storeName, key, etag, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Response<Void>> deleteState(DeleteStateRequest request) {
|
||||
return client.deleteState(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Map<String, String>> getSecret(String storeName, String secretName, Map<String, String> metadata) {
|
||||
return client.getSecret(storeName, secretName, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Map<String, String>> getSecret(String storeName, String secretName) {
|
||||
return client.getSecret(storeName, secretName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Response<Map<String, String>>> getSecret(GetSecretRequest request) {
|
||||
return client.getSecret(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
client.close();
|
||||
if (client != methodInvocationOverrideClient) {
|
||||
methodInvocationOverrideClient.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,7 +29,7 @@ public class DaprHttpBuilder {
|
|||
/**
|
||||
* Read timeout used to build object.
|
||||
*/
|
||||
private Duration readTimeout = Duration.ofSeconds(Properties.HTTP_CLIENT_READTIMEOUTSECONDS.get());
|
||||
private Duration readTimeout = Duration.ofSeconds(Properties.HTTP_CLIENT_READ_TIMEOUT_SECONDS.get());
|
||||
|
||||
/**
|
||||
* Sets the read timeout duration for the instance to be built.
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
|
||||
package io.dapr.config;
|
||||
|
||||
import io.dapr.client.DaprApiProtocol;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
|
@ -14,7 +16,7 @@ import java.nio.charset.StandardCharsets;
|
|||
public class Properties {
|
||||
|
||||
/**
|
||||
* Dapr's default IP for HTTP and GRPC communication.
|
||||
* Dapr's default IP for HTTP and gRPC communication.
|
||||
*/
|
||||
private static final String DEFAULT_SIDECAR_IP = "127.0.0.1";
|
||||
|
||||
|
@ -24,14 +26,19 @@ public class Properties {
|
|||
private static final Integer DEFAULT_HTTP_PORT = 3500;
|
||||
|
||||
/**
|
||||
* Dapr's default GRPC port.
|
||||
* Dapr's default gRPC port.
|
||||
*/
|
||||
private static final Integer DEFAULT_GRPC_PORT = 50001;
|
||||
|
||||
/**
|
||||
* Dapr's default GRPC port.
|
||||
* Dapr's default use of gRPC or HTTP.
|
||||
*/
|
||||
private static final Boolean DEFAULT_GRPC_ENABLED = true;
|
||||
private static final DaprApiProtocol DEFAULT_API_PROTOCOL = DaprApiProtocol.GRPC;
|
||||
|
||||
/**
|
||||
* Dapr's default use of gRPC or HTTP for Dapr's method invocation APIs.
|
||||
*/
|
||||
private static final DaprApiProtocol DEFAULT_API_METHOD_INVOCATION_PROTOCOL = DaprApiProtocol.HTTP;
|
||||
|
||||
/**
|
||||
* Dapr's default String encoding: UTF-8.
|
||||
|
@ -68,12 +75,22 @@ public class Properties {
|
|||
DEFAULT_GRPC_PORT);
|
||||
|
||||
/**
|
||||
* Determines if Dapr client will use GRPC to talk to Dapr's side car.
|
||||
* Determines if Dapr client will use gRPC or HTTP to talk to Dapr's side car.
|
||||
*/
|
||||
public static final Property<Boolean> USE_GRPC = new BooleanProperty(
|
||||
"dapr.grpc.enabled",
|
||||
"DAPR_GRPC_ENABLED",
|
||||
DEFAULT_GRPC_ENABLED);
|
||||
public static final Property<DaprApiProtocol> API_PROTOCOL = new GenericProperty<>(
|
||||
"dapr.api.protocol",
|
||||
"DAPR_API_PROTOCOL",
|
||||
DEFAULT_API_PROTOCOL,
|
||||
(s) -> DaprApiProtocol.valueOf(s.toUpperCase()));
|
||||
|
||||
/**
|
||||
* Determines if Dapr client should use gRPC or HTTP for Dapr's service method invocation APIs.
|
||||
*/
|
||||
public static final Property<DaprApiProtocol> API_METHOD_INVOCATION_PROTOCOL = new GenericProperty<>(
|
||||
"dapr.api.methodInvocation.protocol",
|
||||
"DAPR_API_METHOD_INVOCATION_PROTOCOL",
|
||||
DEFAULT_API_METHOD_INVOCATION_PROTOCOL,
|
||||
(s) -> DaprApiProtocol.valueOf(s.toUpperCase()));
|
||||
|
||||
/**
|
||||
* API token for authentication between App and Dapr's side car.
|
||||
|
@ -95,8 +112,8 @@ public class Properties {
|
|||
/**
|
||||
* Dapr's timeout in seconds for HTTP client reads.
|
||||
*/
|
||||
public static final Property<Integer> HTTP_CLIENT_READTIMEOUTSECONDS = new IntegerProperty(
|
||||
"dapr.http.client.readtimeoutseconds",
|
||||
"DAPR_HTTP_CLIENT_READTIMEOUTSECONDS",
|
||||
public static final Property<Integer> HTTP_CLIENT_READ_TIMEOUT_SECONDS = new IntegerProperty(
|
||||
"dapr.http.client.readTimeoutSeconds",
|
||||
"DAPR_HTTP_CLIENT_READ_TIMEOUT_SECONDS",
|
||||
DEFAULT_HTTP_CLIENT_READTIMEOUTSECONDS);
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -54,7 +54,7 @@ public class DaprClientHttpTest {
|
|||
private final String EXPECTED_RESULT =
|
||||
"{\"data\":\"ewoJCSJwcm9wZXJ0eUEiOiAidmFsdWVBIiwKCQkicHJvcGVydHlCIjogInZhbHVlQiIKCX0=\"}";
|
||||
|
||||
private DaprClientHttp daprClientHttp;
|
||||
private DaprClient daprClientHttp;
|
||||
|
||||
private DaprHttp daprHttp;
|
||||
|
||||
|
@ -66,6 +66,8 @@ public class DaprClientHttpTest {
|
|||
public void setUp() {
|
||||
mockInterceptor = new MockInterceptor(Behavior.UNORDERED);
|
||||
okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientProxy(new DaprClientHttp(daprHttp));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -114,8 +116,7 @@ public class DaprClientHttpTest {
|
|||
.post("http://127.0.0.1:3000/v1.0/publish/mypubsubname/A")
|
||||
.respond(EXPECTED_RESULT);
|
||||
String event = "{ \"message\": \"This is a test\" }";
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.publishEvent("mypubsubname","A", event);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -123,8 +124,7 @@ public class DaprClientHttpTest {
|
|||
@Test
|
||||
public void publishEventIfTopicIsNullOrEmpty() {
|
||||
String event = "{ \"message\": \"This is a test\" }";
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
daprClientHttp.publishEvent("mypubsubname", null, event).block());
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
|
@ -137,8 +137,7 @@ public class DaprClientHttpTest {
|
|||
.post("http://127.0.0.1:3000/v1.0/publish/mypubsubname/A")
|
||||
.respond(EXPECTED_RESULT);
|
||||
String event = "{ \"message\": \"This is a test\" }";
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
daprClientHttp.publishEvent("mypubsubname", "", event);
|
||||
// Should not throw exception because did not call block() on mono above.
|
||||
}
|
||||
|
@ -149,8 +148,7 @@ public class DaprClientHttpTest {
|
|||
.post("http://127.0.0.1:3000/v1.0/publish/A")
|
||||
.respond(EXPECTED_RESULT);
|
||||
String event = "{ \"message\": \"This is a test\" }";
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
daprClientHttp.invokeMethod(null, "", "", null, null, (Class)null).block());
|
||||
}
|
||||
|
@ -160,8 +158,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/invoke/41/method/badorder")
|
||||
.respond("INVALID JSON");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
// null HttpMethod
|
||||
daprClientHttp.invokeMethod("1", "2", "3", new HttpExtension(null), null, (Class)null).block();
|
||||
|
@ -199,8 +196,7 @@ public class DaprClientHttpTest {
|
|||
.post("http://127.0.0.1:3000/v1.0/publish/A")
|
||||
.respond(EXPECTED_RESULT);
|
||||
String event = "{ \"message\": \"This is a test\" }";
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
daprClientHttp.invokeMethod("1", "", null, HttpExtension.POST, null, (Class)null).block());
|
||||
}
|
||||
|
@ -210,8 +206,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder")
|
||||
.respond("\"hello world\"");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<String> mono = daprClientHttp.invokeMethod("41", "neworder", null, HttpExtension.GET, null, String.class);
|
||||
assertEquals("hello world", mono.block());
|
||||
}
|
||||
|
@ -221,8 +216,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder")
|
||||
.respond(new byte[0]);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<String> mono = daprClientHttp.invokeMethod("41", "neworder", null, HttpExtension.GET, null, String.class);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -232,8 +226,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<byte[]> mono = daprClientHttp.invokeMethod("41", "neworder", null, HttpExtension.GET, byte[].class);
|
||||
assertEquals(new String(mono.block()), EXPECTED_RESULT);
|
||||
}
|
||||
|
@ -244,8 +237,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<byte[]> mono = daprClientHttp.invokeMethod("41", "neworder", (byte[]) null, HttpExtension.GET, map);
|
||||
String monoString = new String(mono.block());
|
||||
assertEquals(monoString, EXPECTED_RESULT);
|
||||
|
@ -257,8 +249,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.invokeMethod("41", "neworder", HttpExtension.GET, map);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -269,8 +260,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.invokeMethod("41", "neworder", "", HttpExtension.GET, map);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -281,8 +271,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder?test=1")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Map<String, String> queryString = new HashMap<>();
|
||||
queryString.put("test", "1");
|
||||
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET, queryString, null);
|
||||
|
@ -296,8 +285,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder")
|
||||
.respond(500);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
daprClientHttp.invokeMethod("41", "neworder", "", HttpExtension.GET, map);
|
||||
// No exception should be thrown because did not call block() on mono above.
|
||||
}
|
||||
|
@ -308,8 +296,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond("");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.invokeBinding("sample-topic", "myoperation", "");
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -320,8 +307,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond("");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.invokeBinding("sample-topic", "myoperation", null);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -331,8 +317,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond("NOT VALID JSON");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
daprClientHttp.invokeBinding(null, "myoperation", "").block();
|
||||
});
|
||||
|
@ -356,8 +341,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond(new byte[0]);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<String> mono = daprClientHttp.invokeBinding("sample-topic", "myoperation", "", null, String.class);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -368,8 +352,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond("\"OK\"");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<String> mono = daprClientHttp.invokeBinding("sample-topic", "myoperation", "", null, String.class);
|
||||
assertEquals("OK", mono.block());
|
||||
}
|
||||
|
@ -380,8 +363,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond("1.5");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Double> mono = daprClientHttp.invokeBinding("sample-topic", "myoperation", "", map, double.class);
|
||||
assertEquals(1.5, mono.block(), 0.0001);
|
||||
}
|
||||
|
@ -392,8 +374,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond("1.5");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Float> mono = daprClientHttp.invokeBinding("sample-topic", "myoperation", "", null, float.class);
|
||||
assertEquals(1.5, mono.block(), 0.0001);
|
||||
}
|
||||
|
@ -404,8 +385,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond("\"a\"");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Character> mono = daprClientHttp.invokeBinding("sample-topic", "myoperation", "", null, char.class);
|
||||
assertEquals('a', (char)mono.block());
|
||||
}
|
||||
|
@ -416,8 +396,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond("\"2\"");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Byte> mono = daprClientHttp.invokeBinding("sample-topic", "myoperation", "", null, byte.class);
|
||||
assertEquals((byte)0x2, (byte)mono.block());
|
||||
}
|
||||
|
@ -428,8 +407,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond("1");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Long> mono = daprClientHttp.invokeBinding("sample-topic", "myoperation", "", null, long.class);
|
||||
assertEquals(1, (long)mono.block());
|
||||
}
|
||||
|
@ -440,8 +418,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond("1");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Integer> mono = daprClientHttp.invokeBinding("sample-topic", "myoperation", "", null, int.class);
|
||||
assertEquals(1, (int)mono.block());
|
||||
}
|
||||
|
@ -452,8 +429,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
daprClientHttp.invokeBinding(null, "myoperation", "").block());
|
||||
}
|
||||
|
@ -464,8 +440,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
daprClientHttp.invokeBinding("sample-topic", null, "").block());
|
||||
}
|
||||
|
@ -476,8 +451,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/bindings/sample-topic")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
daprClientHttp.invokeBinding(null, "", "");
|
||||
// No exception is thrown because did not call block() on mono above.
|
||||
}
|
||||
|
@ -487,8 +461,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore/bulk")
|
||||
.respond("NOT VALID JSON");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
daprClientHttp.getBulkState(STATE_STORE_NAME, null, String.class).block();
|
||||
});
|
||||
|
@ -519,8 +492,7 @@ public class DaprClientHttpTest {
|
|||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore/bulk")
|
||||
.respond("[{\"key\": \"100\", \"data\": \"hello world\", \"etag\": \"1\"}," +
|
||||
"{\"key\": \"200\", \"error\": \"not found\"}]");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
List<State<String>> result =
|
||||
daprClientHttp.getBulkState(STATE_STORE_NAME, Arrays.asList("100", "200"), String.class).block();
|
||||
assertEquals(2, result.size());
|
||||
|
@ -540,8 +512,7 @@ public class DaprClientHttpTest {
|
|||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore/bulk")
|
||||
.respond("[{\"key\": \"100\", \"data\": 1234, \"etag\": \"1\"}," +
|
||||
"{\"key\": \"200\", \"error\": \"not found\"}]");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
List<State<Integer>> result =
|
||||
daprClientHttp.getBulkState(STATE_STORE_NAME, Arrays.asList("100", "200"), int.class).block();
|
||||
assertEquals(2, result.size());
|
||||
|
@ -562,8 +533,7 @@ public class DaprClientHttpTest {
|
|||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore/bulk")
|
||||
.respond("[{\"key\": \"100\", \"data\": true, \"etag\": \"1\"}," +
|
||||
"{\"key\": \"200\", \"error\": \"not found\"}]");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
List<State<Boolean>> result =
|
||||
daprClientHttp.getBulkState(STATE_STORE_NAME, Arrays.asList("100", "200"), boolean.class).block();
|
||||
assertNotNull(result);
|
||||
|
@ -586,8 +556,7 @@ public class DaprClientHttpTest {
|
|||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore/bulk")
|
||||
.respond("[{\"key\": \"100\", \"data\": \"" + base64Value + "\", \"etag\": \"1\"}," +
|
||||
"{\"key\": \"200\", \"error\": \"not found\"}]");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
// JSON cannot differentiate if data returned is String or byte[], it is ambiguous. So we get base64 encoded back.
|
||||
// So, users should use String instead of byte[].
|
||||
List<State<String>> result =
|
||||
|
@ -611,8 +580,7 @@ public class DaprClientHttpTest {
|
|||
.respond("[{\"key\": \"100\", \"data\": " +
|
||||
"{ \"id\": \"" + object.id + "\", \"value\": \"" + object.value + "\"}, \"etag\": \"1\"}," +
|
||||
"{\"key\": \"200\", \"error\": \"not found\"}]");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
// JSON cannot differentiate if data returned is String or byte[], it is ambiguous. So we get base64 encoded back.
|
||||
// So, users should use String instead of byte[].
|
||||
List<State<MyObject>> result =
|
||||
|
@ -641,8 +609,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/state/MyStateStore/keyBadPayload")
|
||||
.respond("NOT VALID");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
daprClientHttp.getState(STATE_STORE_NAME, stateKeyNull, String.class).block();
|
||||
});
|
||||
|
@ -670,8 +637,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond("\"" + EXPECTED_RESULT + "\"");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
State<String> monoEmptyEtag = daprClientHttp.getState(STATE_STORE_NAME, stateEmptyEtag, String.class).block();
|
||||
assertEquals(monoEmptyEtag.getKey(), "key");
|
||||
assertNull(monoEmptyEtag.getEtag());
|
||||
|
@ -684,8 +650,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/state/MyStateStore/key?metadata.key_1=val_1")
|
||||
.respond("\"" + EXPECTED_RESULT + "\"");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
GetStateRequestBuilder builder = new GetStateRequestBuilder(STATE_STORE_NAME, "key");
|
||||
builder.withMetadata(metadata);
|
||||
Mono<Response<State<String>>> monoMetadata = daprClientHttp.getState(builder.build(), TypeRef.get(String.class));
|
||||
|
@ -698,8 +663,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond("\"" + EXPECTED_RESULT + "\"");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
State<String> monoNullEtag = daprClientHttp.getState(STATE_STORE_NAME, stateNullEtag, String.class).block();
|
||||
assertEquals(monoNullEtag.getKey(), "key");
|
||||
assertNull(monoNullEtag.getEtag());
|
||||
|
@ -711,8 +675,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond(500);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
daprClientHttp.getState(STATE_STORE_NAME, stateNullEtag, String.class);
|
||||
// No exception should be thrown since did not call block() on mono above.
|
||||
}
|
||||
|
@ -724,16 +687,14 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.saveBulkState(STATE_STORE_NAME, stateKeyValueList);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveStatesErrors() {
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
daprClientHttp.saveBulkState(null, null).block());
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
|
@ -743,8 +704,7 @@ public class DaprClientHttpTest {
|
|||
@Test
|
||||
public void saveStatesNull() {
|
||||
List<State<?>> stateKeyValueList = new ArrayList<>();
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.saveBulkState(STATE_STORE_NAME, null);
|
||||
assertNull(mono.block());
|
||||
Mono<Void> mono1 = daprClientHttp.saveBulkState(STATE_STORE_NAME, stateKeyValueList);
|
||||
|
@ -758,8 +718,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono1 = daprClientHttp.saveBulkState(STATE_STORE_NAME, stateKeyValueList);
|
||||
assertNull(mono1.block());
|
||||
}
|
||||
|
@ -771,8 +730,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.saveBulkState(STATE_STORE_NAME, stateKeyValueList);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -784,8 +742,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.saveBulkState(STATE_STORE_NAME, stateKeyValueList);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -796,8 +753,7 @@ public class DaprClientHttpTest {
|
|||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore")
|
||||
.respond(EXPECTED_RESULT);
|
||||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.saveState(STATE_STORE_NAME, "key", "etag", "value", stateOptions);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -808,8 +764,7 @@ public class DaprClientHttpTest {
|
|||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore")
|
||||
.respond(500);
|
||||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
daprClientHttp.saveState(STATE_STORE_NAME, "key", "etag", "value", stateOptions);
|
||||
// No exception should be thrown because we did not call block() on the mono above.
|
||||
}
|
||||
|
@ -823,8 +778,7 @@ public class DaprClientHttpTest {
|
|||
String key = "key1";
|
||||
String data = "my data";
|
||||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
|
||||
State<String> stateKey = new State<>(data, key, etag, stateOptions);
|
||||
TransactionalStateOperation<String> upsertOperation = new TransactionalStateOperation<>(
|
||||
|
@ -847,8 +801,7 @@ public class DaprClientHttpTest {
|
|||
String key = "key1";
|
||||
String data = "my data";
|
||||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
|
||||
State<String> stateKey = new State<>(data, key, etag, stateOptions);
|
||||
TransactionalStateOperation<String> upsertOperation = new TransactionalStateOperation<>(
|
||||
|
@ -871,8 +824,7 @@ public class DaprClientHttpTest {
|
|||
String key = "key1";
|
||||
String data = "my data";
|
||||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
|
||||
State<String> stateKey = new State<>(data, key, etag, stateOptions);
|
||||
TransactionalStateOperation<String> upsertOperation = new TransactionalStateOperation<>(
|
||||
|
@ -895,8 +847,7 @@ public class DaprClientHttpTest {
|
|||
String key = "key1";
|
||||
String data = "my data";
|
||||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
|
||||
State<String> stateKey = new State<>(data, key, etag, stateOptions);
|
||||
TransactionalStateOperation<String> upsertOperation = new TransactionalStateOperation<>(
|
||||
|
@ -918,8 +869,7 @@ public class DaprClientHttpTest {
|
|||
|
||||
@Test
|
||||
public void executeTransactionErrors() {
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
daprClientHttp.executeStateTransaction(null, null).block());
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
|
@ -931,8 +881,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore/transaction")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.executeStateTransaction(STATE_STORE_NAME, null);
|
||||
assertNull(mono.block());
|
||||
mono = daprClientHttp.executeStateTransaction(STATE_STORE_NAME, Collections.emptyList());
|
||||
|
@ -946,8 +895,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.delete("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.deleteState(STATE_STORE_NAME, stateKeyValue.getKey(), stateKeyValue.getEtag(), stateOptions);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -961,8 +909,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.delete("http://127.0.0.1:3000/v1.0/state/MyStateStore/key?metadata.key_1=val_1")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
DeleteStateRequestBuilder builder = new DeleteStateRequestBuilder(STATE_STORE_NAME, stateKeyValue.getKey());
|
||||
builder.withMetadata(metadata).withEtag(stateKeyValue.getEtag()).withStateOptions(stateOptions);
|
||||
Mono<Response<Void>> monoMetadata = daprClientHttp.deleteState(builder.build());
|
||||
|
@ -976,8 +923,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.delete("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond(500);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
daprClientHttp.deleteState(STATE_STORE_NAME, stateKeyValue.getKey(), stateKeyValue.getEtag(), stateOptions);
|
||||
// No exception should be thrown because we did not call block() on the mono above.
|
||||
}
|
||||
|
@ -988,8 +934,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.delete("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.deleteState(STATE_STORE_NAME, stateKeyValue.getKey(), stateKeyValue.getEtag(), null);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -1000,8 +945,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.delete("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
Mono<Void> mono = daprClientHttp.deleteState(STATE_STORE_NAME, stateKeyValue.getKey(), stateKeyValue.getEtag(), null);
|
||||
assertNull(mono.block());
|
||||
}
|
||||
|
@ -1013,8 +957,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.delete("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond(EXPECTED_RESULT);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
daprClientHttp.deleteState(STATE_STORE_NAME, null, null, null).block();
|
||||
});
|
||||
|
@ -1040,8 +983,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/secrets/MySecretStore/key")
|
||||
.respond("{ \"mysecretkey\": \"mysecretvalue\"}");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
daprClientHttp.getSecret(SECRET_STORE_NAME, null).block();
|
||||
});
|
||||
|
@ -1056,8 +998,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/secrets/MySecretStore/key%2Fone")
|
||||
.respond("{ \"mysecretkey\": \"mysecretvalue\"}");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
daprClientHttp.getSecret(SECRET_STORE_NAME, null).block();
|
||||
});
|
||||
|
@ -1072,8 +1013,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/secrets/MySecretStore/key")
|
||||
.respond("");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
daprClientHttp.getSecret(SECRET_STORE_NAME, null).block();
|
||||
});
|
||||
|
@ -1087,8 +1027,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/secrets/MySecretStore/key")
|
||||
.respond(404);
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrowsDaprException("UNKNOWN", () ->
|
||||
daprClientHttp.getSecret(SECRET_STORE_NAME, "key").block()
|
||||
);
|
||||
|
@ -1102,8 +1041,7 @@ public class DaprClientHttpTest {
|
|||
ResponseBody.create("" +
|
||||
"{\"errorCode\":\"ERR_SECRET_STORE_NOT_FOUND\"," +
|
||||
"\"message\":\"error message\"}", MediaTypes.MEDIATYPE_JSON));
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrowsDaprException("ERR_SECRET_STORE_NOT_FOUND", "ERR_SECRET_STORE_NOT_FOUND: error message", () ->
|
||||
daprClientHttp.getSecret(SECRET_STORE_NAME, "key").block()
|
||||
);
|
||||
|
@ -1114,8 +1052,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/secrets/MySecretStore/key")
|
||||
.respond("INVALID JSON");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
daprClientHttp.getSecret(null, "key").block());
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
|
@ -1139,8 +1076,7 @@ public class DaprClientHttpTest {
|
|||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/secrets/MySecretStore/key?metadata.metakey=metavalue")
|
||||
.respond("{ \"mysecretkey2\": \"mysecretvalue2\"}");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
|
||||
{
|
||||
Map<String, String> secret = daprClientHttp.getSecret(
|
||||
SECRET_STORE_NAME,
|
||||
|
@ -1173,7 +1109,7 @@ public class DaprClientHttpTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void close() {
|
||||
public void close() throws Exception {
|
||||
DaprHttp daprHttp = Mockito.mock(DaprHttp.class);
|
||||
Mockito.doNothing().when(daprHttp).close();
|
||||
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
package io.dapr.client;
|
||||
|
||||
import io.dapr.client.domain.HttpExtension;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
public class DaprClientProxyTest {
|
||||
|
||||
@Test
|
||||
public void stateAPI() {
|
||||
DaprClient client1 = Mockito.mock(DaprClient.class);
|
||||
DaprClient client2 = Mockito.mock(DaprClient.class);
|
||||
|
||||
Mockito.when(client1.saveState("state", "key", "value")).thenReturn(Mono.empty());
|
||||
Mockito.when(client2.saveState("state", "key", "value")).thenReturn(Mono.empty());
|
||||
|
||||
DaprClient proxy = new DaprClientProxy(client1, client2);
|
||||
proxy.saveState("state", "key", "value").block();
|
||||
|
||||
Mockito.verify(client1, times(1)).saveState("state", "key", "value");
|
||||
Mockito.verify(client2, times(0)).saveState("state", "key", "value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void methodInvocationAPI() {
|
||||
DaprClient client1 = Mockito.mock(DaprClient.class);
|
||||
DaprClient client2 = Mockito.mock(DaprClient.class);
|
||||
|
||||
Mockito.when(client1.invokeMethod("appId", "methodName", "body", HttpExtension.POST))
|
||||
.thenReturn(Mono.empty());
|
||||
Mockito.when(client2.invokeMethod("appId", "methodName", "body", HttpExtension.POST))
|
||||
.thenReturn(Mono.empty());
|
||||
|
||||
DaprClient proxy = new DaprClientProxy(client1, client2);
|
||||
proxy.invokeMethod("appId", "methodName", "body", HttpExtension.POST).block();
|
||||
|
||||
Mockito.verify(client1, times(0))
|
||||
.invokeMethod("appId", "methodName", "body", HttpExtension.POST);
|
||||
Mockito.verify(client2, times(1))
|
||||
.invokeMethod("appId", "methodName", "body", HttpExtension.POST);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closeAllClients() throws Exception {
|
||||
DaprClient client1 = Mockito.mock(DaprClient.class);
|
||||
DaprClient client2 = Mockito.mock(DaprClient.class);
|
||||
|
||||
DaprClient proxy = new DaprClientProxy(client1, client2);
|
||||
proxy.close();
|
||||
|
||||
Mockito.verify(client1, times(1)).close();
|
||||
Mockito.verify(client2, times(1)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closeSingleClient() throws Exception {
|
||||
DaprClient client1 = Mockito.mock(DaprClient.class);
|
||||
|
||||
DaprClient proxy = new DaprClientProxy(client1);
|
||||
proxy.close();
|
||||
|
||||
Mockito.verify(client1, times(1)).close();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue