Wrapping in a 'Data' json object not needed for actor method data (#192)

* Wrapping in a 'Data' json object not needed for actor method data

* cleanup

Co-authored-by: Artur Souza <artursouza.ms@outlook.com>
This commit is contained in:
Leon Mai 2020-01-31 16:32:31 -08:00 committed by GitHub
parent b2083187df
commit 969a3cb72d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 19 additions and 89 deletions

View File

@ -71,9 +71,9 @@ class ActorProxyImpl implements ActorProxy {
*/
@Override
public <T> Mono<T> invokeActorMethod(String methodName, Object data, Class<T> clazz) {
return this.daprClient.invokeActorMethod(actorType, actorId.toString(), methodName, this.wrap(data))
return this.daprClient.invokeActorMethod(actorType, actorId.toString(), methodName, this.serialize(data))
.filter(s -> s.length > 0)
.map(s -> unwrap(s, clazz));
.map(s -> deserialize(s, clazz));
}
/**
@ -83,7 +83,7 @@ class ActorProxyImpl implements ActorProxy {
public <T> Mono<T> invokeActorMethod(String methodName, Class<T> clazz) {
return this.daprClient.invokeActorMethod(actorType, actorId.toString(), methodName, null)
.filter(s -> s.length > 0)
.map(s -> unwrap(s, clazz));
.map(s -> deserialize(s, clazz));
}
/**
@ -99,7 +99,7 @@ class ActorProxyImpl implements ActorProxy {
*/
@Override
public Mono<Void> invokeActorMethod(String methodName, Object data) {
return this.daprClient.invokeActorMethod(actorType, actorId.toString(), methodName, this.wrap(data)).then();
return this.daprClient.invokeActorMethod(actorType, actorId.toString(), methodName, this.serialize(data)).then();
}
/**
@ -111,9 +111,9 @@ class ActorProxyImpl implements ActorProxy {
* @return Response object or null.
* @throws RuntimeException In case it cannot generate Object.
*/
private <T> T unwrap(final byte[] response, Class<T> clazz) {
private <T> T deserialize(final byte[] response, Class<T> clazz) {
try {
return this.serializer.deserialize(INTERNAL_SERIALIZER.unwrapData(response), clazz);
return this.serializer.deserialize(response, clazz);
} catch (IOException e) {
throw new RuntimeException(e);
}
@ -126,9 +126,9 @@ class ActorProxyImpl implements ActorProxy {
* @return Payload to be sent to Dapr's API.
* @throws RuntimeException In case it cannot generate payload.
*/
private byte[] wrap(final Object request) {
private byte[] serialize(final Object request) {
try {
return INTERNAL_SERIALIZER.wrapData(this.serializer.serialize(request));
return this.serializer.serialize(request);
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@ -234,8 +234,7 @@ public class ActorRuntime {
*/
public Mono<byte[]> invoke(String actorTypeName, String actorId, String actorMethodName, byte[] payload) {
return Mono.fromSupplier(() -> this.getActorManager(actorTypeName))
.flatMap(m -> m.invokeMethod(new ActorId(actorId), actorMethodName, unwrap(payload)))
.map(response -> wrap((byte[]) response));
.flatMap(m -> m.invokeMethod(new ActorId(actorId), actorMethodName, payload));
}
/**
@ -283,34 +282,4 @@ public class ActorRuntime {
return actorManager;
}
/**
* Extracts the data as String from the Actor's method result.
*
* @param payload String returned by API.
* @return data or null.
* @throws RuntimeException In case it cannot extract data.
*/
private byte[] unwrap(final byte[] payload) {
try {
return INTERNAL_SERIALIZER.unwrapData(payload);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Builds the request to invoke an API for Actors.
*
* @param data Data to be wrapped in the request.
* @return Payload to be sent to Dapr's API.
* @throws RuntimeException In case it cannot generate payload.
*/
private byte[] wrap(final byte[] data) {
try {
return INTERNAL_SERIALIZER.wrapData(data);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -30,9 +30,9 @@ public class ActorProxyImplTest {
@Test()
public void invokeActorMethodWithoutDataWithReturnType() {
final DaprClient daprClient = mock(DaprClient.class);
Mono<byte[]> daprResponse =
Mono.just("{\n\t\"data\": \"ewoJCSJwcm9wZXJ0eUEiOiAidmFsdWVBIiwKCQkicHJvcGVydHlCIjogInZhbHVlQiIKCX0=\"\n}"
.getBytes());
Mono<byte[]> daprResponse = Mono.just(
"{\n\t\t\"propertyA\": \"valueA\",\n\t\t\"propertyB\": \"valueB\"\n\t}".getBytes());
when(daprClient.invokeActorMethod(anyString(), anyString(), anyString(), Mockito.isNull()))
.thenReturn(daprResponse);
@ -93,8 +93,7 @@ public class ActorProxyImplTest {
final DaprClient daprClient = mock(DaprClient.class);
when(daprClient.invokeActorMethod(anyString(), anyString(), anyString(), Mockito.isNotNull()))
.thenReturn(
Mono.just("{\n\t\"data\": \"ewoJCSJwcm9wZXJ0eUEiOiAidmFsdWVBIiwKCQkicHJvcGVydHlCIjogInZhbHVlQiIKCX0=\"\n}"
.getBytes()));
Mono.just("{\n\t\t\"propertyA\": \"valueA\",\n\t\t\"propertyB\": \"valueB\"\n\t}".getBytes()));
final ActorProxy actorProxy = new ActorProxyImpl(
"myActorType",

View File

@ -138,14 +138,7 @@ public class ActorCustomSerializerTest {
this.manager.invokeMethod(
new ActorId(invocationOnMock.getArgument(1, String.class)),
invocationOnMock.getArgument(2, String.class),
INTERNAL_SERIALIZER.unwrapData(invocationOnMock.getArgument(3, byte[].class)))
.map(s -> {
try {
return INTERNAL_SERIALIZER.wrapData(s);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
invocationOnMock.getArgument(3, byte[].class)));
this.manager.activateActor(actorId).block();

View File

@ -181,14 +181,7 @@ public class ActorNoStateTest {
this.manager.invokeMethod(
new ActorId(invocationOnMock.getArgument(1, String.class)),
invocationOnMock.getArgument(2, String.class),
INTERNAL_SERIALIZER.unwrapData(invocationOnMock.getArgument(3, byte[].class)))
.map(s -> {
try {
return INTERNAL_SERIALIZER.wrapData(s);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
invocationOnMock.getArgument(3, byte[].class)));
this.manager.activateActor(actorId).block();

View File

@ -85,7 +85,7 @@ public class ActorRuntimeTest {
this.runtime.activate(ACTOR_NAME, actorId).block();
byte[] response = this.runtime.invoke(ACTOR_NAME, actorId, "say", null).block();
String message = ACTOR_STATE_SERIALIZER.deserialize(ACTOR_STATE_SERIALIZER.unwrapData(response), String.class);
String message = ACTOR_STATE_SERIALIZER.deserialize(response, String.class);
Assert.assertEquals("Nothing to say.", message);
}

View File

@ -612,15 +612,7 @@ public class ActorStatefulTest {
this.manager.invokeMethod(
new ActorId(invocationOnMock.getArgument(1, String.class)),
invocationOnMock.getArgument(2, String.class),
INTERNAL_SERIALIZER.unwrapData(
invocationOnMock.getArgument(3, byte[].class)))
.map(s -> {
try {
return INTERNAL_SERIALIZER.wrapData(s);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
invocationOnMock.getArgument(3, byte[].class)));
this.manager.activateActor(actorId).block();

View File

@ -335,15 +335,7 @@ public class DerivedActorTest {
this.manager.invokeMethod(
new ActorId(invocationOnMock.getArgument(1, String.class)),
invocationOnMock.getArgument(2, String.class),
INTERNAL_SERIALIZER.unwrapData(
invocationOnMock.getArgument(3, byte[].class)))
.map(s -> {
try {
return INTERNAL_SERIALIZER.wrapData(s);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
invocationOnMock.getArgument(3, byte[].class)));
this.manager.activateActor(actorId).block();

View File

@ -154,15 +154,7 @@ public class ThrowFromPreAndPostActorMethodsTest {
this.manager.invokeMethod(
new ActorId(invocationOnMock.getArgument(1, String.class)),
invocationOnMock.getArgument(2, String.class),
INTERNAL_SERIALIZER.unwrapData(
invocationOnMock.getArgument(3, byte[].class)))
.map(s -> {
try {
return INTERNAL_SERIALIZER.wrapData(s);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
invocationOnMock.getArgument(3, byte[].class)));
this.manager.activateActor(actorId).block();