Skip to content

Commit

Permalink
updating test with new serialization to follow postgresql format
Browse files Browse the repository at this point in the history
  • Loading branch information
salaboy committed Jul 9, 2024
1 parent 33dc23f commit eb2a94b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
14 changes: 10 additions & 4 deletions sdk/src/main/java/io/dapr/client/ObjectSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package io.dapr.client;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -23,7 +24,7 @@
import io.dapr.utils.TypeRef;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -151,10 +152,15 @@ private <T> T deserialize(byte[] content, JavaType javaType) throws IOException
* @return Object of type T.
* @throws IOException In case content cannot be deserialized.
*/
@SuppressWarnings("unchecked")
public <T> List<T> deserializeList(byte[] content, Class<T> clazz) throws IOException {
Class<? extends Object[]> clazzArray = ((T[]) java.lang.reflect.Array.newInstance(clazz, 1)).getClass();
return Arrays.asList(deserialize(content, OBJECT_MAPPER.constructType(clazzArray)));
List<List<String>> deserialized = deserialize(content,
OBJECT_MAPPER.constructType(new TypeReference<List<List<String>>>(){}));
List<T> results = new ArrayList<>(deserialized.size());
for (List<String> l : deserialized) {
results.add(OBJECT_MAPPER.readValue(l.get(0), clazz));
}

return results;
}

/**
Expand Down
59 changes: 50 additions & 9 deletions sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

package io.dapr.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;

import io.dapr.client.domain.AppConnectionPropertiesHealthMetadata;
import io.dapr.client.domain.AppConnectionPropertiesMetadata;
import io.dapr.client.domain.ComponentMetadata;
Expand Down Expand Up @@ -52,13 +54,15 @@
import io.grpc.StatusRuntimeException;
import io.grpc.protobuf.StatusProto;
import io.grpc.stub.StreamObserver;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatcher;
import org.mockito.ArgumentMatchers;
import org.mockito.stubbing.Answer;

import reactor.core.publisher.Mono;

import java.io.IOException;
Expand All @@ -75,6 +79,7 @@
import java.util.stream.Collectors;

import static io.dapr.utils.TestUtils.assertThrowsDaprException;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -90,6 +95,8 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.core.type.TypeReference;

public class DaprClientGrpcTest {

private static final String STATE_STORE_NAME = "MyStateStore";
Expand Down Expand Up @@ -406,25 +413,59 @@ public void invokeBindingResponseObjectTypeRefTest() throws IOException {
assertEquals("OK", result.block());
}


@Test
public void invokeBindingListResponseObjectTypeRefTest() throws IOException {
List<MyObject> list = new ArrayList<>();
MyObject obj1 = new MyObject(1, "Object1");
MyObject obj2 = new MyObject(2, "Objet2");
list.add(obj1);
list.add(obj2);
public void invokeBindingResponseListObjectTypeRefTest() throws IOException {
//The output binding needs to be an array of arrays containing strings intead of JSON objects if we want to be able to parse it
ByteString listOfObjects = ByteString.copyFromUtf8("[[\"{ \\\"id\\\": 1,\\\"value\\\": \\\"Object1\\\"}\"],[\"{ \\\"id\\\": 2,\\\"value\\\": \\\"Object2\\\"}\"]]");
DaprProtos.InvokeBindingResponse.Builder responseBuilder =
DaprProtos.InvokeBindingResponse.newBuilder().setData(listOfObjects);
doAnswer((Answer<Void>) invocation -> {
StreamObserver<DaprProtos.InvokeBindingResponse> observer = (StreamObserver<DaprProtos.InvokeBindingResponse>) invocation.getArguments()[1];
observer.onNext(responseBuilder.build());
observer.onCompleted();
return null;
}).when(daprStub).invokeBinding(any(DaprProtos.InvokeBindingRequest.class), any());

MyObject event = new MyObject(1, "Event");
List<List<String>> result = client.invokeBinding("BindingName", "MyOperation", event, new TypeRef<List<List<String>>>(){}).block();
ObjectMapper mapper = new ObjectMapper();
List<MyObject> myObjects = new ArrayList<>(result.size());
for(List<String> l : result){
myObjects.add(mapper.readValue(l.get(0), MyObject.class));
}

assertEquals(2, myObjects.size());
assertEquals(1, myObjects.get(0).getId());
assertEquals("Object1", myObjects.get(0).getValue());
assertEquals(2, myObjects.get(1).getId());
assertEquals("Object2", myObjects.get(1).getValue());

}

@Test
public void invokeBindingListResponseWeirdArrayTypeRefTest() throws IOException {

// The binding must return the values list only, the keys are not needed, as the Keys for Java are the field inside the object
// The content of the objects should be provided as a string with internal quotes
ByteString listOfObjects = ByteString.copyFromUtf8("[[\"{ \\\"id\\\": 1,\\\"value\\\": \\\"Object1\\\"}\"],[\"{ \\\"id\\\": 2,\\\"value\\\": \\\"Object2\\\"}\"]]");
System.out.println(listOfObjects.toStringUtf8());
DaprProtos.InvokeBindingResponse.Builder responseBuilder =
DaprProtos.InvokeBindingResponse.newBuilder().setData(serialize(list));
DaprProtos.InvokeBindingResponse.newBuilder().setData(listOfObjects);
doAnswer((Answer<Void>) invocation -> {
StreamObserver<DaprProtos.InvokeBindingResponse> observer = (StreamObserver<DaprProtos.InvokeBindingResponse>) invocation.getArguments()[1];
observer.onNext(responseBuilder.build());
observer.onCompleted();
return null;
}).when(daprStub).invokeBinding(any(DaprProtos.InvokeBindingRequest.class), any());

Mono<List<MyObject>> result = client.invokeBindingList("BindingName", "MyOperation", null, null, TypeRef.get(MyObject.class));
List<MyObject> myObjects = client.invokeBindingList("BindingName", "MyOperation", null, null, TypeRef.get(MyObject.class)).block();

assertEquals(list, result.block());
assertEquals(2, myObjects.size());
assertEquals(1, myObjects.get(0).getId());
assertEquals("Object1", myObjects.get(0).getValue());
assertEquals(2, myObjects.get(1).getId());
assertEquals("Object2", myObjects.get(1).getValue());
}

@Test
Expand Down

0 comments on commit eb2a94b

Please sign in to comment.