@@ -833,6 +833,84 @@ public void scheduleJobShouldThrowWhenNameInRequestIsEmpty() {
833
833
assertEquals ("Name in the request cannot be null or empty" , exception .getMessage ());
834
834
}
835
835
836
+ @ Test
837
+ public void scheduleJobShouldThrowWhenNameAlreadyExists () {
838
+ AtomicInteger callCount = new AtomicInteger (0 );
839
+
840
+ doAnswer (invocation -> {
841
+ StreamObserver <DaprProtos .ScheduleJobResponse > observer = invocation .getArgument (1 );
842
+ if (callCount .incrementAndGet () == 1 ) {
843
+ // First call succeeds
844
+ observer .onCompleted ();
845
+ } else {
846
+ // Second call fails with ALREADY_EXISTS
847
+ observer .onError (newStatusRuntimeException ("ALREADY_EXISTS" , "Job with name 'testJob' already exists" ));
848
+ }
849
+ return null ;
850
+ }).when (daprStub ).scheduleJobAlpha1 (any (DaprProtos .ScheduleJobRequest .class ), any ());
851
+
852
+ // First call should succeed
853
+ ScheduleJobRequest firstRequest = new ScheduleJobRequest ("testJob" , Instant .now ());
854
+ assertDoesNotThrow (() -> previewClient .scheduleJob (firstRequest ).block ());
855
+
856
+ ArgumentCaptor <DaprProtos .ScheduleJobRequest > captor =
857
+ ArgumentCaptor .forClass (DaprProtos .ScheduleJobRequest .class );
858
+
859
+ verify (daprStub , times (1 )).scheduleJobAlpha1 (captor .capture (), Mockito .any ());
860
+ DaprProtos .ScheduleJobRequest actualScheduleJobRequest = captor .getValue ();
861
+ DaprProtos .Job job = actualScheduleJobRequest .getJob ();
862
+ assertEquals ("testJob" , job .getName ());
863
+ assertFalse (job .hasData ());
864
+ assertEquals (0 , job .getRepeats ());
865
+ assertFalse (job .hasTtl ());
866
+
867
+ // Second call with same name should fail
868
+ ScheduleJobRequest secondRequest = new ScheduleJobRequest ("testJob" , Instant .now ());
869
+
870
+ assertThrowsDaprException (
871
+ ExecutionException .class ,
872
+ "ALREADY_EXISTS" ,
873
+ "ALREADY_EXISTS: Job with name 'testJob' already exists" ,
874
+ () -> previewClient .scheduleJob (secondRequest ).block ());
875
+ }
876
+
877
+ @ Test
878
+ public void scheduleJobShouldSucceedWhenNameAlreadyExistsWithOverwrite () {
879
+ doAnswer (invocation -> {
880
+ StreamObserver <DaprProtos .ScheduleJobResponse > observer = invocation .getArgument (1 );
881
+ observer .onCompleted (); // Simulate successful response for both calls
882
+ return null ;
883
+ }).when (daprStub ).scheduleJobAlpha1 (any (DaprProtos .ScheduleJobRequest .class ), any ());
884
+
885
+ // First call should succeed
886
+ ScheduleJobRequest firstRequest = new ScheduleJobRequest ("testJob" , Instant .now ());
887
+ assertDoesNotThrow (() -> previewClient .scheduleJob (firstRequest ).block ());
888
+
889
+ // Second call with same name but overwrite=true should also succeed
890
+ ScheduleJobRequest secondRequest = new ScheduleJobRequest ("testJob" , Instant .now ())
891
+ .setOverwrite (true );
892
+ assertDoesNotThrow (() -> previewClient .scheduleJob (secondRequest ).block ());
893
+
894
+ // Verify that both calls were made successfully
895
+ ArgumentCaptor <DaprProtos .ScheduleJobRequest > captor =
896
+ ArgumentCaptor .forClass (DaprProtos .ScheduleJobRequest .class );
897
+ verify (daprStub , times (2 )).scheduleJobAlpha1 (captor .capture (), any ());
898
+
899
+ // Verify the first call doesn't have overwrite set
900
+ DaprProtos .ScheduleJobRequest firstActualRequest = captor .getAllValues ().get (0 );
901
+ assertFalse (firstActualRequest .getJob ().getOverwrite ());
902
+ assertEquals ("testJob" , firstActualRequest .getJob ().getName ());
903
+
904
+ // Verify the second call has overwrite set to true
905
+ DaprProtos .ScheduleJobRequest secondActualRequest = captor .getAllValues ().get (1 );
906
+ assertTrue (secondActualRequest .getJob ().getOverwrite ());
907
+ assertEquals ("testJob" , secondActualRequest .getJob ().getName ());
908
+ }
909
+
910
+
911
+
912
+
913
+
836
914
@ Test
837
915
public void getJobShouldReturnResponseWhenAllFieldsArePresentInRequest () {
838
916
DateTimeFormatter iso8601Formatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" )
0 commit comments