77
88import com .fasterxml .jackson .databind .ObjectMapper ;
99import io .dapr .actors .ActorId ;
10+ import io .dapr .client .ObjectSerializer ;
1011import io .dapr .serializer .DaprObjectSerializer ;
1112import io .dapr .serializer .DefaultObjectSerializer ;
1213import io .dapr .utils .TypeRef ;
1314import org .junit .Assert ;
1415import org .junit .Test ;
1516import reactor .core .publisher .Mono ;
1617
18+ import java .io .IOException ;
1719import java .util .Arrays ;
1820import java .util .Objects ;
1921
@@ -27,10 +29,29 @@ public class DaprStateAsyncProviderTest {
2729
2830 private static final DaprObjectSerializer SERIALIZER = new DefaultObjectSerializer ();
2931
30- private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
32+
3133
3234 private static final double EPSILON = 1e-10 ;
3335
36+ class CustomJsonSerializer implements DaprObjectSerializer {
37+ private final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
38+
39+ @ Override
40+ public byte [] serialize (Object o ) throws IOException {
41+ return OBJECT_MAPPER .writeValueAsBytes (o );
42+ }
43+
44+ @ Override
45+ public <T > T deserialize (byte [] data , TypeRef <T > type ) throws IOException {
46+ return OBJECT_MAPPER .readValue (data , OBJECT_MAPPER .constructType (type .getType ()));
47+ }
48+
49+ @ Override
50+ public String getContentType () {
51+ return "application/json" ;
52+ }
53+ }
54+
3455 /**
3556 * Class used to test JSON serialization.
3657 */
@@ -136,6 +157,49 @@ public void happyCaseApply() {
136157 verify (daprClient ).saveStateTransactionally (eq ("MyActor" ), eq ("123" ), any ());
137158 }
138159
160+ @ Test
161+ public void happyCaseApplyWithCustomJsonSerializer () {
162+ DaprClient daprClient = mock (DaprClient .class );
163+ when (daprClient
164+ .saveStateTransactionally (
165+ eq ("MyActor" ),
166+ eq ("123" ),
167+ argThat (operations -> {
168+ if (operations == null ) {
169+ return false ;
170+ }
171+
172+ if (operations .size () != 1 ) {
173+ return false ;
174+ }
175+ ActorStateOperation operation = operations .get (0 );
176+ if (operation .getOperationType () == null ) {
177+ return false ;
178+ }
179+ if (operation .getKey () == null ) {
180+ return false ;
181+ }
182+
183+ String opName = operation .getOperationType ();
184+ String key = operation .getKey ();
185+ Object value = operation .getValue ();
186+
187+ return "upsert" .equals (opName ) &&
188+ "object" .equals (key ) &&
189+ "{\" id\" :1000,\" name\" :\" Roxane\" }" .equals (value );
190+ })))
191+ .thenReturn (Mono .empty ());
192+
193+ DaprStateAsyncProvider provider = new DaprStateAsyncProvider (daprClient , new CustomJsonSerializer ());
194+ provider .apply ("MyActor" ,
195+ new ActorId ("123" ),
196+ createInsertChange ("object" , new Customer ().setId (1000 ).setName ("Roxane" )))
197+ .block ();
198+
199+ verify (daprClient ).saveStateTransactionally (eq ("MyActor" ), eq ("123" ), any ());
200+ }
201+
202+
139203 @ Test
140204 public void happyCaseLoad () throws Exception {
141205 DaprClient daprClient = mock (DaprClient .class );
0 commit comments