1717import com .google .protobuf .Any ;
1818import com .google .protobuf .ByteString ;
1919import com .google .protobuf .Empty ;
20- import com . google . protobuf . GeneratedMessageV3 ;
20+ import io . dapr . v1 . DaprActorsProtos ;
2121import io .dapr .v1 .DaprGrpc ;
22- import io .dapr .v1 .DaprProtos ;
2322import io .grpc .ManagedChannel ;
2423import io .grpc .Status ;
2524import io .grpc .StatusException ;
@@ -178,17 +177,83 @@ public void unregisterActorTimer() {
178177 }
179178
180179
181- private class CustomDaprClient extends DaprGrpc .DaprImplBase {
180+ private static class OperationsMatcher {
181+
182+ private final List <ActorStateOperation > operations ;
183+
184+ OperationsMatcher (List <ActorStateOperation > operations ) {
185+ this .operations = operations ;
186+ }
187+
188+ private static boolean nullableEquals (Object one , Any another ) {
189+ if (one == null ) {
190+ return another .getValue ().isEmpty ();
191+ }
192+
193+ if ((one == null ) ^ (another == null )) {
194+ return false ;
195+ }
196+
197+ try {
198+ Any oneAny = getAny (one );
199+ return oneAny .getValue ().equals (another .getValue ());
200+ } catch (IOException e ) {
201+ e .printStackTrace ();
202+ return false ;
203+ }
204+ }
205+
206+ private static Any getAny (Object value ) throws IOException {
207+ if (value instanceof byte []) {
208+ String base64 = OBJECT_MAPPER .writeValueAsString (value );
209+ return Any .newBuilder ().setValue (ByteString .copyFrom (base64 .getBytes ())).build ();
210+ } else if (value instanceof String ) {
211+ return Any .newBuilder ().setValue (ByteString .copyFrom (((String ) value ).getBytes ())).build ();
212+ }
213+
214+ throw new IllegalArgumentException ("Must be byte[] or String" );
215+ }
216+
217+ public boolean matches (DaprActorsProtos .ExecuteActorStateTransactionRequest argument ) {
218+ if (argument == null ) {
219+ return false ;
220+ }
221+
222+ if (operations .size () != argument .getOperationsCount ()) {
223+ return false ;
224+ }
225+
226+ for (ActorStateOperation operation : operations ) {
227+ boolean found = false ;
228+ for (DaprActorsProtos .TransactionalActorStateOperation grpcOperation : argument .getOperationsList ()) {
229+ if (operation .getKey ().equals (grpcOperation .getKey ())
230+ && operation .getOperationType ().equals (grpcOperation .getOperationType ())
231+ && nullableEquals (operation .getValue (), grpcOperation .getValue ())) {
232+ found = true ;
233+ break ;
234+ }
235+ }
236+
237+ if (!found ) {
238+ return false ;
239+ }
240+ }
241+
242+ return true ;
243+ }
244+ }
245+
246+ private class CustomDaprClient extends DaprGrpc .DaprImplBase {
182247
183248 @ Override
184- public void getActorState (DaprProtos .GetActorStateRequest request ,
185- StreamObserver <DaprProtos .GetActorStateResponse > responseObserver ) {
249+ public void getActorState (DaprActorsProtos .GetActorStateRequest request ,
250+ StreamObserver <DaprActorsProtos .GetActorStateResponse > responseObserver ) {
186251 assertEquals (ACTOR_TYPE , request .getActorType ());
187252 assertEquals (KEY , request .getKey ());
188253 assertEquals (ACTOR_ID , request .getActorId ());
189254 switch (request .getActorId ()) {
190255 case ACTOR_ID :
191- populateObserver (responseObserver , DaprProtos .GetActorStateResponse .newBuilder ().setData (ByteString .copyFrom (RESPONSE_PAYLOAD ))
256+ populateObserver (responseObserver , DaprActorsProtos .GetActorStateResponse .newBuilder ().setData (ByteString .copyFrom (RESPONSE_PAYLOAD ))
192257 .build ());
193258 return ;
194259
@@ -199,7 +264,7 @@ public void getActorState(DaprProtos.GetActorStateRequest request,
199264 super .getActorState (request , responseObserver );
200265 }
201266
202- public void executeActorStateTransaction (io . dapr . v1 . DaprProtos .ExecuteActorStateTransactionRequest request ,
267+ public void executeActorStateTransaction (DaprActorsProtos .ExecuteActorStateTransactionRequest request ,
203268 io .grpc .stub .StreamObserver <com .google .protobuf .Empty > responseObserver ) {
204269 assertEquals (ACTOR_TYPE , request .getActorType ());
205270 assertEquals (ACTOR_ID , request .getActorId ());
@@ -217,7 +282,7 @@ public void executeActorStateTransaction(io.dapr.v1.DaprProtos.ExecuteActorState
217282 }
218283
219284 @ Override
220- public void registerActorReminder (io . dapr . v1 . DaprProtos .RegisterActorReminderRequest request ,
285+ public void registerActorReminder (DaprActorsProtos .RegisterActorReminderRequest request ,
221286 io .grpc .stub .StreamObserver <com .google .protobuf .Empty > responseObserver ) {
222287 assertEquals (REMINDER_NAME , request .getName ());
223288 assertEquals ("0h0m1s0ms" , request .getDueTime ());
@@ -236,7 +301,7 @@ public void registerActorReminder(io.dapr.v1.DaprProtos.RegisterActorReminderReq
236301 super .registerActorReminder (request , responseObserver );
237302 }
238303
239- public void registerActorTimer (io . dapr . v1 . DaprProtos .RegisterActorTimerRequest request ,
304+ public void registerActorTimer (DaprActorsProtos .RegisterActorTimerRequest request ,
240305 io .grpc .stub .StreamObserver <com .google .protobuf .Empty > responseObserver ) {
241306 assertEquals (ACTOR_TYPE , request .getActorType ());
242307 assertEquals (ACTOR_ID , request .getActorId ());
@@ -261,7 +326,7 @@ public void registerActorTimer(io.dapr.v1.DaprProtos.RegisterActorTimerRequest r
261326 * Unregister an actor timer.
262327 * </pre>
263328 */
264- public void unregisterActorTimer (io . dapr . v1 . DaprProtos .UnregisterActorTimerRequest request ,
329+ public void unregisterActorTimer (DaprActorsProtos .UnregisterActorTimerRequest request ,
265330 io .grpc .stub .StreamObserver <com .google .protobuf .Empty > responseObserver ) {
266331 assertEquals (ACTOR_TYPE , request .getActorType ());
267332 assertEquals (ACTOR_ID , request .getActorId ());
@@ -278,7 +343,7 @@ public void unregisterActorTimer(io.dapr.v1.DaprProtos.UnregisterActorTimerReque
278343 super .unregisterActorTimer (request , responseObserver );
279344 }
280345
281- public void unregisterActorReminder (io . dapr . v1 . DaprProtos .UnregisterActorReminderRequest request ,
346+ public void unregisterActorReminder (DaprActorsProtos .UnregisterActorReminderRequest request ,
282347 io .grpc .stub .StreamObserver <com .google .protobuf .Empty > responseObserver ) {
283348 assertEquals (ACTOR_TYPE , request .getActorType ());
284349 assertEquals (ACTOR_ID , request .getActorId ());
@@ -306,70 +371,4 @@ private <T extends com.google.protobuf.Message> void populateObserver(StreamObse
306371 responseObserver .onCompleted ();
307372 }
308373 }
309-
310- private static class OperationsMatcher {
311-
312- private final List <ActorStateOperation > operations ;
313-
314- OperationsMatcher (List <ActorStateOperation > operations ) {
315- this .operations = operations ;
316- }
317-
318- public boolean matches (DaprProtos .ExecuteActorStateTransactionRequest argument ) {
319- if (argument == null ) {
320- return false ;
321- }
322-
323- if (operations .size () != argument .getOperationsCount ()) {
324- return false ;
325- }
326-
327- for (ActorStateOperation operation : operations ) {
328- boolean found = false ;
329- for (DaprProtos .TransactionalActorStateOperation grpcOperation : argument .getOperationsList ()) {
330- if (operation .getKey ().equals (grpcOperation .getKey ())
331- && operation .getOperationType ().equals (grpcOperation .getOperationType ())
332- && nullableEquals (operation .getValue (), grpcOperation .getValue ())) {
333- found = true ;
334- break ;
335- }
336- }
337-
338- if (!found ) {
339- return false ;
340- }
341- }
342-
343- return true ;
344- }
345-
346- private static boolean nullableEquals (Object one , Any another ) {
347- if (one == null ) {
348- return another .getValue ().isEmpty ();
349- }
350-
351- if ((one == null ) ^ (another == null )) {
352- return false ;
353- }
354-
355- try {
356- Any oneAny = getAny (one );
357- return oneAny .getValue ().equals (another .getValue ());
358- } catch (IOException e ) {
359- e .printStackTrace ();
360- return false ;
361- }
362- }
363-
364- private static Any getAny (Object value ) throws IOException {
365- if (value instanceof byte []) {
366- String base64 = OBJECT_MAPPER .writeValueAsString (value );
367- return Any .newBuilder ().setValue (ByteString .copyFrom (base64 .getBytes ())).build ();
368- } else if (value instanceof String ) {
369- return Any .newBuilder ().setValue (ByteString .copyFrom (((String )value ).getBytes ())).build ();
370- }
371-
372- throw new IllegalArgumentException ("Must be byte[] or String" );
373- }
374- }
375374}
0 commit comments