diff --git a/examples/src/main/java/io/dapr/examples/jobs/DemoJobsClient.java b/examples/src/main/java/io/dapr/examples/jobs/DemoJobsClient.java index 87ccf08016..ddc8ac78e0 100644 --- a/examples/src/main/java/io/dapr/examples/jobs/DemoJobsClient.java +++ b/examples/src/main/java/io/dapr/examples/jobs/DemoJobsClient.java @@ -13,6 +13,7 @@ package io.dapr.examples.jobs; +import io.dapr.client.DaprClient; import io.dapr.client.DaprClientBuilder; import io.dapr.client.DaprPreviewClient; import io.dapr.client.domain.GetJobRequest; @@ -35,7 +36,7 @@ public static void main(String[] args) throws Exception { Properties.GRPC_PORT, "51439" ); - try (DaprPreviewClient client = new DaprClientBuilder().withPropertyOverrides(overrides).buildPreviewClient()) { + try (DaprClient client = new DaprClientBuilder().withPropertyOverrides(overrides).build()) { // Schedule a job. System.out.println("**** Scheduling a Job with name dapr-jobs-1 *****"); diff --git a/examples/src/main/java/io/dapr/examples/jobs/README.md b/examples/src/main/java/io/dapr/examples/jobs/README.md index 4b899ac4a6..392c0969bb 100644 --- a/examples/src/main/java/io/dapr/examples/jobs/README.md +++ b/examples/src/main/java/io/dapr/examples/jobs/README.md @@ -64,7 +64,7 @@ export DAPR_API_TOKEN="your-dapr-api-token" This example uses the Java SDK Dapr client in order to **Schedule and Get** Jobs. `DemoJobsClient.java` is the example class demonstrating these features. -Kindly check [DaprPreviewClient.java](https://github.com/dapr/java-sdk/blob/master/sdk/src/main/java/io/dapr/client/DaprPreviewClient.java) for a detailed description of the supported APIs. +Kindly check [DaprClient.java](https://github.com/dapr/java-sdk/blob/master/sdk/src/main/java/io/dapr/client/DaprClient.java) for a detailed description of the supported APIs. ```java public class DemoJobsClient { @@ -77,7 +77,7 @@ public class DemoJobsClient { Properties.GRPC_PORT, "51439" ); - try (DaprPreviewClient client = new DaprClientBuilder().withPropertyOverrides(overrides).buildPreviewClient()) { + try (DaprClient client = new DaprClientBuilder().withPropertyOverrides(overrides).build()) { // Schedule a job. ScheduleJobRequest scheduleJobRequest = new ScheduleJobRequest("dapr-job-1", diff --git a/sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprClientConfiguration.java b/sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprClientConfiguration.java new file mode 100644 index 0000000000..80046d45ee --- /dev/null +++ b/sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprClientConfiguration.java @@ -0,0 +1,41 @@ +/* + * Copyright 2025 The Dapr Authors + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and +limitations under the License. +*/ + +package io.dapr.it.testcontainers; + +import io.dapr.client.DaprClient; +import io.dapr.client.DaprClientBuilder; +import io.dapr.client.DaprPreviewClient; +import io.dapr.config.Properties; +import io.dapr.config.Property; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.Map; + +@Configuration +public class DaprClientConfiguration { + @Bean + public DaprClient daprClient( + @Value("${dapr.http.endpoint}") String daprHttpEndpoint, + @Value("${dapr.grpc.endpoint}") String daprGrpcEndpoint + ){ + Map, String> overrides = Map.of( + Properties.HTTP_ENDPOINT, daprHttpEndpoint, + Properties.GRPC_ENDPOINT, daprGrpcEndpoint + ); + + return new DaprClientBuilder().withPropertyOverrides(overrides).build(); + } +} diff --git a/sdk-tests/src/test/java/io/dapr/it/testcontainers/jobs/DaprJobsIT.java b/sdk-tests/src/test/java/io/dapr/it/testcontainers/jobs/DaprJobsIT.java index ac2b4a71be..b17c834139 100644 --- a/sdk-tests/src/test/java/io/dapr/it/testcontainers/jobs/DaprJobsIT.java +++ b/sdk-tests/src/test/java/io/dapr/it/testcontainers/jobs/DaprJobsIT.java @@ -13,7 +13,7 @@ package io.dapr.it.testcontainers.jobs; -import io.dapr.client.DaprPreviewClient; +import io.dapr.client.DaprClient; import io.dapr.client.domain.ConstantFailurePolicy; import io.dapr.client.domain.DeleteJobRequest; import io.dapr.client.domain.DropFailurePolicy; @@ -22,13 +22,12 @@ import io.dapr.client.domain.GetJobResponse; import io.dapr.client.domain.JobSchedule; import io.dapr.client.domain.ScheduleJobRequest; -import io.dapr.it.testcontainers.DaprPreviewClientConfiguration; +import io.dapr.it.testcontainers.DaprClientConfiguration; import io.dapr.testcontainers.DaprContainer; import io.dapr.testcontainers.DaprLogLevel; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; -import org.junit.runner.notification.Failure; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -51,7 +50,7 @@ @SpringBootTest( webEnvironment = WebEnvironment.RANDOM_PORT, classes = { - DaprPreviewClientConfiguration.class, + DaprClientConfiguration.class, TestJobsApplication.class } ) @@ -85,7 +84,7 @@ static void daprProperties(DynamicPropertyRegistry registry) { } @Autowired - private DaprPreviewClient daprPreviewClient; + private DaprClient daprClient; @BeforeEach public void setUp(){ @@ -98,12 +97,12 @@ public void testJobScheduleCreationWithDueTime() { .withZone(ZoneOffset.UTC); Instant currentTime = Instant.now(); - daprPreviewClient.scheduleJob(new ScheduleJobRequest("Job", currentTime).setOverwrite(true)).block(); + daprClient.scheduleJob(new ScheduleJobRequest("Job", currentTime).setOverwrite(true)).block(); GetJobResponse getJobResponse = - daprPreviewClient.getJob(new GetJobRequest("Job")).block(); + daprClient.getJob(new GetJobRequest("Job")).block(); - daprPreviewClient.deleteJob(new DeleteJobRequest("Job")).block(); + daprClient.deleteJob(new DeleteJobRequest("Job")).block(); assertEquals(iso8601Formatter.format(currentTime), getJobResponse.getDueTime().toString()); assertEquals("Job", getJobResponse.getName()); @@ -115,13 +114,13 @@ public void testJobScheduleCreationWithSchedule() { .withZone(ZoneOffset.UTC); Instant currentTime = Instant.now(); - daprPreviewClient.scheduleJob(new ScheduleJobRequest("Job", JobSchedule.hourly()) + daprClient.scheduleJob(new ScheduleJobRequest("Job", JobSchedule.hourly()) .setDueTime(currentTime).setOverwrite(true)).block(); GetJobResponse getJobResponse = - daprPreviewClient.getJob(new GetJobRequest("Job")).block(); + daprClient.getJob(new GetJobRequest("Job")).block(); - daprPreviewClient.deleteJob(new DeleteJobRequest("Job")).block(); + daprClient.deleteJob(new DeleteJobRequest("Job")).block(); assertEquals(iso8601Formatter.format(currentTime), getJobResponse.getDueTime().toString()); assertEquals(JobSchedule.hourly().getExpression(), getJobResponse.getSchedule().getExpression()); @@ -136,7 +135,7 @@ public void testJobScheduleCreationWithAllParameters() { String cronExpression = "2 * 3 * * FRI"; - daprPreviewClient.scheduleJob(new ScheduleJobRequest("Job", currentTime) + daprClient.scheduleJob(new ScheduleJobRequest("Job", currentTime) .setTtl(currentTime.plus(2, ChronoUnit.HOURS)) .setData("Job data".getBytes()) .setRepeat(3) @@ -144,9 +143,9 @@ public void testJobScheduleCreationWithAllParameters() { .setSchedule(JobSchedule.fromString(cronExpression))).block(); GetJobResponse getJobResponse = - daprPreviewClient.getJob(new GetJobRequest("Job")).block(); + daprClient.getJob(new GetJobRequest("Job")).block(); - daprPreviewClient.deleteJob(new DeleteJobRequest("Job")).block(); + daprClient.deleteJob(new DeleteJobRequest("Job")).block(); assertEquals(iso8601Formatter.format(currentTime), getJobResponse.getDueTime().toString()); assertEquals("2 * 3 * * FRI", getJobResponse.getSchedule().getExpression()); @@ -165,7 +164,7 @@ public void testJobScheduleCreationWithDropFailurePolicy() { String cronExpression = "2 * 3 * * FRI"; - daprPreviewClient.scheduleJob(new ScheduleJobRequest("Job", currentTime) + daprClient.scheduleJob(new ScheduleJobRequest("Job", currentTime) .setTtl(currentTime.plus(2, ChronoUnit.HOURS)) .setData("Job data".getBytes()) .setRepeat(3) @@ -173,9 +172,9 @@ public void testJobScheduleCreationWithDropFailurePolicy() { .setSchedule(JobSchedule.fromString(cronExpression))).block(); GetJobResponse getJobResponse = - daprPreviewClient.getJob(new GetJobRequest("Job")).block(); + daprClient.getJob(new GetJobRequest("Job")).block(); - daprPreviewClient.deleteJob(new DeleteJobRequest("Job")).block(); + daprClient.deleteJob(new DeleteJobRequest("Job")).block(); assertEquals(FailurePolicyType.DROP, getJobResponse.getFailurePolicy().getFailurePolicyType()); } @@ -188,7 +187,7 @@ public void testJobScheduleCreationWithConstantFailurePolicy() { String cronExpression = "2 * 3 * * FRI"; - daprPreviewClient.scheduleJob(new ScheduleJobRequest("Job", currentTime) + daprClient.scheduleJob(new ScheduleJobRequest("Job", currentTime) .setTtl(currentTime.plus(2, ChronoUnit.HOURS)) .setData("Job data".getBytes()) .setRepeat(3) @@ -197,9 +196,9 @@ public void testJobScheduleCreationWithConstantFailurePolicy() { .setSchedule(JobSchedule.fromString(cronExpression))).block(); GetJobResponse getJobResponse = - daprPreviewClient.getJob(new GetJobRequest("Job")).block(); + daprClient.getJob(new GetJobRequest("Job")).block(); - daprPreviewClient.deleteJob(new DeleteJobRequest("Job")).block(); + daprClient.deleteJob(new DeleteJobRequest("Job")).block(); ConstantFailurePolicy jobFailurePolicyConstant = (ConstantFailurePolicy) getJobResponse.getFailurePolicy(); assertEquals(FailurePolicyType.CONSTANT, getJobResponse.getFailurePolicy().getFailurePolicyType()); @@ -214,13 +213,13 @@ public void testDeleteJobRequest() { String cronExpression = "2 * 3 * * FRI"; - daprPreviewClient.scheduleJob(new ScheduleJobRequest("Job", currentTime) + daprClient.scheduleJob(new ScheduleJobRequest("Job", currentTime) .setTtl(currentTime.plus(2, ChronoUnit.HOURS)) .setData("Job data".getBytes()) .setRepeat(3) .setOverwrite(true) .setSchedule(JobSchedule.fromString(cronExpression))).block(); - daprPreviewClient.deleteJob(new DeleteJobRequest("Job")).block(); + daprClient.deleteJob(new DeleteJobRequest("Job")).block(); } } diff --git a/sdk/src/main/java/io/dapr/client/DaprClient.java b/sdk/src/main/java/io/dapr/client/DaprClient.java index 6ac6086e76..f341344b3f 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClient.java +++ b/sdk/src/main/java/io/dapr/client/DaprClient.java @@ -15,11 +15,14 @@ import io.dapr.client.domain.ConfigurationItem; import io.dapr.client.domain.DaprMetadata; +import io.dapr.client.domain.DeleteJobRequest; import io.dapr.client.domain.DeleteStateRequest; import io.dapr.client.domain.ExecuteStateTransactionRequest; import io.dapr.client.domain.GetBulkSecretRequest; import io.dapr.client.domain.GetBulkStateRequest; import io.dapr.client.domain.GetConfigurationRequest; +import io.dapr.client.domain.GetJobRequest; +import io.dapr.client.domain.GetJobResponse; import io.dapr.client.domain.GetSecretRequest; import io.dapr.client.domain.GetStateRequest; import io.dapr.client.domain.HttpExtension; @@ -27,6 +30,7 @@ import io.dapr.client.domain.InvokeMethodRequest; import io.dapr.client.domain.PublishEventRequest; import io.dapr.client.domain.SaveStateRequest; +import io.dapr.client.domain.ScheduleJobRequest; import io.dapr.client.domain.State; import io.dapr.client.domain.StateOptions; import io.dapr.client.domain.SubscribeConfigurationRequest; @@ -702,6 +706,38 @@ Flux subscribeConfiguration(String storeName, Li */ Mono getMetadata(); + /** + * Schedules a job using the provided job request details. + * + * @param scheduleJobRequest The request containing the details of the job to schedule. + * Must include a name and optional schedule, data, and other related properties. + * @return A {@link Mono} that completes when the job scheduling operation is successful or raises an error. + * @throws IllegalArgumentException If the request or its required fields like name are null or empty. + */ + public Mono scheduleJob(ScheduleJobRequest scheduleJobRequest); + + /** + * Retrieves details of a specific job. + * + * @param getJobRequest The request containing the job name for which the details are to be fetched. + * The name property is mandatory. + * @return A {@link Mono} that emits the {@link GetJobResponse} containing job details or raises an + * error if the job is not found. + * @throws IllegalArgumentException If the request or its required fields like name are null or empty. + */ + + public Mono getJob(GetJobRequest getJobRequest); + + /** + * Deletes a job based on the given request. + * + * @param deleteJobRequest The request containing the job name to be deleted. + * The name property is mandatory. + * @return A {@link Mono} that completes when the job is successfully deleted or raises an error. + * @throws IllegalArgumentException If the request or its required fields like name are null or empty. + */ + public Mono deleteJob(DeleteJobRequest deleteJobRequest); + /** * Gracefully shutdown the dapr runtime. * diff --git a/sdk/src/main/java/io/dapr/client/DaprPreviewClient.java b/sdk/src/main/java/io/dapr/client/DaprPreviewClient.java index 545b8e5dc5..9d8192369d 100644 --- a/sdk/src/main/java/io/dapr/client/DaprPreviewClient.java +++ b/sdk/src/main/java/io/dapr/client/DaprPreviewClient.java @@ -291,38 +291,6 @@ Subscription subscribeToEvents( */ Flux> subscribeToEvents(String pubsubName, String topic, TypeRef type); - /** - * Schedules a job using the provided job request details. - * - * @param scheduleJobRequest The request containing the details of the job to schedule. - * Must include a name and optional schedule, data, and other related properties. - * @return A {@link Mono} that completes when the job scheduling operation is successful or raises an error. - * @throws IllegalArgumentException If the request or its required fields like name are null or empty. - */ - public Mono scheduleJob(ScheduleJobRequest scheduleJobRequest); - - /** - * Retrieves details of a specific job. - * - * @param getJobRequest The request containing the job name for which the details are to be fetched. - * The name property is mandatory. - * @return A {@link Mono} that emits the {@link GetJobResponse} containing job details or raises an - * error if the job is not found. - * @throws IllegalArgumentException If the request or its required fields like name are null or empty. - */ - - public Mono getJob(GetJobRequest getJobRequest); - - /** - * Deletes a job based on the given request. - * - * @param deleteJobRequest The request containing the job name to be deleted. - * The name property is mandatory. - * @return A {@link Mono} that completes when the job is successfully deleted or raises an error. - * @throws IllegalArgumentException If the request or its required fields like name are null or empty. - */ - public Mono deleteJob(DeleteJobRequest deleteJobRequest); - /* * Converse with an LLM. * diff --git a/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java b/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java index 7ac6ab3cf5..642c3b4f28 100644 --- a/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java +++ b/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java @@ -20,14 +20,21 @@ import io.dapr.client.domain.AppConnectionPropertiesMetadata; import io.dapr.client.domain.ComponentMetadata; import io.dapr.client.domain.ConfigurationItem; +import io.dapr.client.domain.ConstantFailurePolicy; import io.dapr.client.domain.DaprMetadata; +import io.dapr.client.domain.DeleteJobRequest; import io.dapr.client.domain.DeleteStateRequest; +import io.dapr.client.domain.DropFailurePolicy; import io.dapr.client.domain.ExecuteStateTransactionRequest; import io.dapr.client.domain.GetBulkStateRequest; +import io.dapr.client.domain.GetJobRequest; +import io.dapr.client.domain.GetJobResponse; import io.dapr.client.domain.GetStateRequest; import io.dapr.client.domain.InvokeBindingRequest; +import io.dapr.client.domain.JobSchedule; import io.dapr.client.domain.PublishEventRequest; import io.dapr.client.domain.RuleMetadata; +import io.dapr.client.domain.ScheduleJobRequest; import io.dapr.client.domain.State; import io.dapr.client.domain.StateOptions; import io.dapr.client.domain.SubscribeConfigurationResponse; @@ -53,17 +60,26 @@ import io.grpc.StatusRuntimeException; import io.grpc.protobuf.StatusProto; import io.grpc.stub.StreamObserver; +import org.junit.Assert; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; 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.Mockito; import org.mockito.stubbing.Answer; import reactor.core.publisher.Mono; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.time.Instant; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -73,10 +89,12 @@ import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; 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.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -88,6 +106,7 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -2298,6 +2317,634 @@ public void getMetadataTest() { assertEquals(healthProperties.getHealthThreshold(), healthMetadata.getHealthThreshold()); } + @Test + public void scheduleJobShouldSucceedWhenAllFieldsArePresentInRequest() { + DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") + .withZone(ZoneOffset.UTC); + + ScheduleJobRequest expectedScheduleJobRequest = new ScheduleJobRequest("testJob", + JobSchedule.fromString("*/5 * * * *")) + .setData("testData".getBytes()) + .setTtl(Instant.now().plus(1, ChronoUnit.DAYS)) + .setRepeat(5) + .setDueTime(Instant.now().plus(10, ChronoUnit.MINUTES)); + + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onCompleted(); // Simulate successful response + return null; + }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); + + assertDoesNotThrow(() -> client.scheduleJob(expectedScheduleJobRequest).block()); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); + + verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); + DaprProtos.ScheduleJobRequest actualScheduleJobReq = captor.getValue(); + + assertEquals("testJob", actualScheduleJobReq.getJob().getName()); + assertEquals("testData", + new String(actualScheduleJobReq.getJob().getData().getValue().toByteArray(), StandardCharsets.UTF_8)); + assertEquals("*/5 * * * *", actualScheduleJobReq.getJob().getSchedule()); + assertEquals(iso8601Formatter.format(expectedScheduleJobRequest.getTtl()), actualScheduleJobReq.getJob().getTtl()); + assertEquals(expectedScheduleJobRequest.getRepeats(), actualScheduleJobReq.getJob().getRepeats()); + assertEquals(iso8601Formatter.format(expectedScheduleJobRequest.getDueTime()), actualScheduleJobReq.getJob().getDueTime()); + } + + @Test + public void scheduleJobShouldSucceedWhenRequiredFieldsNameAndDueTimeArePresentInRequest() { + DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") + .withZone(ZoneOffset.UTC); + + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onCompleted(); // Simulate successful response + return null; + }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); + + ScheduleJobRequest expectedScheduleJobRequest = + new ScheduleJobRequest("testJob", Instant.now().plus(10, ChronoUnit.MINUTES)); + assertDoesNotThrow(() -> client.scheduleJob(expectedScheduleJobRequest).block()); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); + + verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); + DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); + DaprProtos.Job job = actualScheduleJobRequest.getJob(); + assertEquals("testJob", job.getName()); + assertFalse(job.hasData()); + assertFalse(job.hasSchedule()); + assertEquals(0, job.getRepeats()); + assertFalse(job.hasTtl()); + assertEquals(iso8601Formatter.format(expectedScheduleJobRequest.getDueTime()), + actualScheduleJobRequest.getJob().getDueTime()); + } + + @Test + public void scheduleJobShouldSucceedWhenRequiredFieldsNameAndScheduleArePresentInRequest() { + DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") + .withZone(ZoneOffset.UTC); + + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onCompleted(); // Simulate successful response + return null; + }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); + + ScheduleJobRequest expectedScheduleJobRequest = new ScheduleJobRequest("testJob", + JobSchedule.fromString("* * * * * *")); + assertDoesNotThrow(() -> client.scheduleJob(expectedScheduleJobRequest).block()); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); + + verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); + DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); + DaprProtos.Job job = actualScheduleJobRequest.getJob(); + assertEquals("testJob", job.getName()); + assertFalse(job.hasData()); + assertEquals( "* * * * * *", job.getSchedule()); + assertEquals(0, job.getRepeats()); + assertFalse(job.hasTtl()); + } + + @Test + public void scheduleJobShouldThrowWhenRequestIsNull() { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + client.scheduleJob(null).block(); + }); + assertEquals("scheduleJobRequest cannot be null", exception.getMessage()); + } + + @Test + public void scheduleJobShouldThrowWhenInvalidRequest() { + ScheduleJobRequest scheduleJobRequest = new ScheduleJobRequest(null, Instant.now()); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + client.scheduleJob(scheduleJobRequest).block(); + }); + assertEquals("Name in the request cannot be null or empty", exception.getMessage()); + } + + @Test + public void scheduleJobShouldThrowWhenNameInRequestIsEmpty() { + ScheduleJobRequest scheduleJobRequest = new ScheduleJobRequest("", Instant.now()); + + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + client.scheduleJob(scheduleJobRequest).block(); + }); + assertEquals("Name in the request cannot be null or empty", exception.getMessage()); + } + + @Test + public void scheduleJobShouldHavePolicyWhenPolicyIsSet() { + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onCompleted(); // Simulate successful response + return null; + }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); + + ScheduleJobRequest expectedScheduleJobRequest = new ScheduleJobRequest("testJob", + JobSchedule.fromString("* * * * * *")) + .setFailurePolicy(new DropFailurePolicy()); + + client.scheduleJob(expectedScheduleJobRequest).block(); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); + + verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); + DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); + DaprProtos.Job job = actualScheduleJobRequest.getJob(); + assertEquals("testJob", job.getName()); + assertFalse(job.hasData()); + assertEquals( "* * * * * *", job.getSchedule()); + assertEquals(0, job.getRepeats()); + assertFalse(job.hasTtl()); + Assertions.assertTrue(job.hasFailurePolicy()); + } + + @Test + public void scheduleJobShouldHaveConstantPolicyWithMaxRetriesWhenConstantPolicyIsSetWithMaxRetries() { + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onCompleted(); // Simulate successful response + return null; + }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); + + ScheduleJobRequest expectedScheduleJobRequest = new ScheduleJobRequest("testJob", + JobSchedule.fromString("* * * * * *")) + .setFailurePolicy(new ConstantFailurePolicy(2)); + + client.scheduleJob(expectedScheduleJobRequest).block(); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); + + verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); + DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); + DaprProtos.Job job = actualScheduleJobRequest.getJob(); + assertEquals("testJob", job.getName()); + assertFalse(job.hasData()); + assertEquals( "* * * * * *", job.getSchedule()); + assertEquals(0, job.getRepeats()); + assertFalse(job.hasTtl()); + Assertions.assertTrue(job.hasFailurePolicy()); + assertEquals(2, job.getFailurePolicy().getConstant().getMaxRetries()); + } + + @Test + public void scheduleJobShouldHaveConstantPolicyWithIntervalWhenConstantPolicyIsSetWithInterval() { + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onCompleted(); // Simulate successful response + return null; + }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); + + ScheduleJobRequest expectedScheduleJobRequest = new ScheduleJobRequest("testJob", + JobSchedule.fromString("* * * * * *")) + .setFailurePolicy(new ConstantFailurePolicy(Duration.of(2, ChronoUnit.SECONDS))); + + client.scheduleJob(expectedScheduleJobRequest).block(); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); + + verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); + DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); + DaprProtos.Job job = actualScheduleJobRequest.getJob(); + assertEquals("testJob", job.getName()); + assertFalse(job.hasData()); + assertEquals( "* * * * * *", job.getSchedule()); + assertEquals(0, job.getRepeats()); + assertFalse(job.hasTtl()); + Assertions.assertTrue(job.hasFailurePolicy()); + assertEquals(Duration.of(2, ChronoUnit.SECONDS).getNano(), + job.getFailurePolicy().getConstant().getInterval().getNanos()); + } + + @Test + public void scheduleJobShouldHaveBothRetiresAndIntervalWhenConstantPolicyIsSetWithRetriesAndInterval() { + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onCompleted(); // Simulate successful response + return null; + }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); + + ScheduleJobRequest expectedScheduleJobRequest = new ScheduleJobRequest("testJob", + JobSchedule.fromString("* * * * * *")) + .setFailurePolicy(new ConstantFailurePolicy(Duration.of(2, ChronoUnit.SECONDS)) + .setMaxRetries(10)); + + client.scheduleJob(expectedScheduleJobRequest).block(); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); + + verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); + DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); + DaprProtos.Job job = actualScheduleJobRequest.getJob(); + assertEquals("testJob", job.getName()); + assertFalse(job.hasData()); + assertEquals( "* * * * * *", job.getSchedule()); + assertEquals(0, job.getRepeats()); + assertFalse(job.hasTtl()); + Assertions.assertTrue(job.hasFailurePolicy()); + assertEquals(Duration.of(2, ChronoUnit.SECONDS).getNano(), + job.getFailurePolicy().getConstant().getInterval().getNanos()); + assertEquals(10, job.getFailurePolicy().getConstant().getMaxRetries()); + } + + @Test + public void scheduleJobShouldThrowWhenNameAlreadyExists() { + AtomicInteger callCount = new AtomicInteger(0); + + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + if (callCount.incrementAndGet() == 1) { + // First call succeeds + observer.onCompleted(); + } else { + // Second call fails with ALREADY_EXISTS + observer.onError(newStatusRuntimeException("ALREADY_EXISTS", "Job with name 'testJob' already exists")); + } + return null; + }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); + + // First call should succeed + ScheduleJobRequest firstRequest = new ScheduleJobRequest("testJob", Instant.now()); + assertDoesNotThrow(() -> client.scheduleJob(firstRequest).block()); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); + + verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); + DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); + DaprProtos.Job job = actualScheduleJobRequest.getJob(); + assertEquals("testJob", job.getName()); + assertFalse(job.hasData()); + assertEquals(0, job.getRepeats()); + assertFalse(job.hasTtl()); + + // Second call with same name should fail + ScheduleJobRequest secondRequest = new ScheduleJobRequest("testJob", Instant.now()); + + assertThrowsDaprException( + ExecutionException.class, + "ALREADY_EXISTS", + "ALREADY_EXISTS: Job with name 'testJob' already exists", + () -> client.scheduleJob(secondRequest).block()); + } + + @Test + public void scheduleJobShouldSucceedWhenNameAlreadyExistsWithOverwrite() { + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onCompleted(); // Simulate successful response for both calls + return null; + }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); + + // First call should succeed + ScheduleJobRequest firstRequest = new ScheduleJobRequest("testJob", Instant.now()); + assertDoesNotThrow(() -> client.scheduleJob(firstRequest).block()); + + // Second call with same name but overwrite=true should also succeed + ScheduleJobRequest secondRequest = new ScheduleJobRequest("testJob", Instant.now()) + .setOverwrite(true); + assertDoesNotThrow(() -> client.scheduleJob(secondRequest).block()); + + // Verify that both calls were made successfully + ArgumentCaptor captor = + ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); + verify(daprStub, times(2)).scheduleJobAlpha1(captor.capture(), any()); + + // Verify the first call doesn't have overwrite set + DaprProtos.ScheduleJobRequest firstActualRequest = captor.getAllValues().get(0); + assertFalse(firstActualRequest.getOverwrite()); + assertEquals("testJob", firstActualRequest.getJob().getName()); + + // Verify the second call has overwrite set to true + DaprProtos.ScheduleJobRequest secondActualRequest = captor.getAllValues().get(1); + Assert.assertTrue(secondActualRequest.getOverwrite()); + assertEquals("testJob", secondActualRequest.getJob().getName()); + } + + @Test + public void getJobShouldReturnResponseWhenAllFieldsArePresentInRequest() { + DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") + .withZone(ZoneOffset.UTC); + + GetJobRequest getJobRequest = new GetJobRequest("testJob"); + + DaprProtos.Job job = DaprProtos.Job.newBuilder() + .setName("testJob") + .setTtl(OffsetDateTime.now().format(iso8601Formatter)) + .setData(Any.newBuilder().setValue(ByteString.copyFrom("testData".getBytes())).build()) + .setSchedule("*/5 * * * *") + .setRepeats(5) + .setDueTime(iso8601Formatter.format(Instant.now().plus(10, ChronoUnit.MINUTES))) + .build(); + + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onNext(DaprProtos.GetJobResponse.newBuilder() + .setJob(job) + .build()); + observer.onCompleted(); + return null; + }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); + + Mono resultMono = client.getJob(getJobRequest); + + GetJobResponse response = resultMono.block(); + assertNotNull(response); + assertEquals("testJob", response.getName()); + assertEquals("testData", new String(response.getData(), StandardCharsets.UTF_8)); + assertEquals("*/5 * * * *", response.getSchedule().getExpression()); + assertEquals(5, response.getRepeats()); + assertEquals(job.getTtl(), iso8601Formatter.format(response.getTtl())); + assertEquals(job.getDueTime(), iso8601Formatter.format(response.getDueTime())); + } + + @Test + public void getJobShouldReturnResponseWithScheduleSetWhenResponseHasSchedule() { + GetJobRequest getJobRequest = new GetJobRequest("testJob"); + + DaprProtos.Job job = DaprProtos.Job.newBuilder() + .setName("testJob") + .setSchedule("0 0 0 1 1 *") + .build(); + + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onNext(DaprProtos.GetJobResponse.newBuilder() + .setJob(job) + .build()); + observer.onCompleted(); + return null; + }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); + + Mono resultMono = client.getJob(getJobRequest); + + GetJobResponse response = resultMono.block(); + assertNotNull(response); + assertEquals("testJob", response.getName()); + assertNull(response.getData()); + assertEquals("0 0 0 1 1 *", response.getSchedule().getExpression()); + assertNull(response.getRepeats()); + assertNull(response.getTtl()); + assertNull(response.getDueTime()); + } + + @Test + public void getJobShouldReturnResponseWithDueTimeSetWhenResponseHasDueTime() { + GetJobRequest getJobRequest = new GetJobRequest("testJob"); + + String datetime = OffsetDateTime.now().toString(); + DaprProtos.Job job = DaprProtos.Job.newBuilder() + .setName("testJob") + .setDueTime(datetime) + .build(); + + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onNext(DaprProtos.GetJobResponse.newBuilder() + .setJob(job) + .build()); + observer.onCompleted(); + return null; + }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); + + Mono resultMono = client.getJob(getJobRequest); + + GetJobResponse response = resultMono.block(); + assertNotNull(response); + assertEquals("testJob", response.getName()); + assertNull(response.getData()); + assertNull(response.getSchedule()); + assertNull(response.getRepeats()); + assertNull(response.getTtl()); + assertEquals(job.getDueTime(), datetime); + } + + @Test + public void getJobShouldReturnResponseWithDropFailurePolicySet() { + GetJobRequest getJobRequest = new GetJobRequest("testJob"); + + String datetime = OffsetDateTime.now().toString(); + DaprProtos.Job job = DaprProtos.Job.newBuilder() + .setName("testJob") + .setDueTime(datetime) + .setFailurePolicy(CommonProtos.JobFailurePolicy.newBuilder() + .setDrop(CommonProtos.JobFailurePolicyDrop.newBuilder().build()).build()) + .build(); + + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onNext(DaprProtos.GetJobResponse.newBuilder() + .setJob(job) + .build()); + observer.onCompleted(); + return null; + }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); + + Mono resultMono = client.getJob(getJobRequest); + + GetJobResponse response = resultMono.block(); + assertNotNull(response); + assertEquals("testJob", response.getName()); + assertNull(response.getData()); + assertNull(response.getSchedule()); + assertNull(response.getRepeats()); + assertNull(response.getTtl()); + assertEquals(job.getDueTime(), datetime); + Assert.assertTrue(job.hasFailurePolicy()); + Assert.assertTrue(job.getFailurePolicy().hasDrop()); + } + + @Test + public void getJobShouldReturnResponseWithConstantFailurePolicyAndMaxRetriesSet() { + GetJobRequest getJobRequest = new GetJobRequest("testJob"); + + String datetime = OffsetDateTime.now().toString(); + DaprProtos.Job job = DaprProtos.Job.newBuilder() + .setName("testJob") + .setDueTime(datetime) + .setFailurePolicy(CommonProtos.JobFailurePolicy.newBuilder() + .setConstant(CommonProtos.JobFailurePolicyConstant.newBuilder().setMaxRetries(2).build()).build()) + .build(); + + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onNext(DaprProtos.GetJobResponse.newBuilder() + .setJob(job) + .build()); + observer.onCompleted(); + return null; + }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); + + Mono resultMono = client.getJob(getJobRequest); + + GetJobResponse response = resultMono.block(); + assertNotNull(response); + assertEquals("testJob", response.getName()); + assertNull(response.getData()); + assertNull(response.getSchedule()); + assertNull(response.getRepeats()); + assertNull(response.getTtl()); + assertEquals(job.getDueTime(), datetime); + Assert.assertTrue(job.hasFailurePolicy()); + Assert.assertTrue(job.getFailurePolicy().hasConstant()); + assertEquals(2, job.getFailurePolicy().getConstant().getMaxRetries()); + } + + @Test + public void getJobShouldReturnResponseWithConstantFailurePolicyAndIntervalSet() { + GetJobRequest getJobRequest = new GetJobRequest("testJob"); + + String datetime = OffsetDateTime.now().toString(); + DaprProtos.Job job = DaprProtos.Job.newBuilder() + .setName("testJob") + .setDueTime(datetime) + .setFailurePolicy(CommonProtos.JobFailurePolicy.newBuilder() + .setConstant(CommonProtos.JobFailurePolicyConstant.newBuilder() + .setInterval(com.google.protobuf.Duration.newBuilder().setNanos(5).build()).build()).build()) + .build(); + + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onNext(DaprProtos.GetJobResponse.newBuilder() + .setJob(job) + .build()); + observer.onCompleted(); + return null; + }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); + + Mono resultMono = client.getJob(getJobRequest); + + GetJobResponse response = resultMono.block(); + assertNotNull(response); + assertEquals("testJob", response.getName()); + assertNull(response.getData()); + assertNull(response.getSchedule()); + assertNull(response.getRepeats()); + assertNull(response.getTtl()); + assertEquals(job.getDueTime(), datetime); + Assert.assertTrue(job.hasFailurePolicy()); + Assert.assertTrue(job.getFailurePolicy().hasConstant()); + assertEquals(5, job.getFailurePolicy().getConstant().getInterval().getNanos()); + } + + @Test + public void getJobShouldReturnResponseWithConstantFailurePolicyIntervalAndMaxRetriesSet() { + GetJobRequest getJobRequest = new GetJobRequest("testJob"); + + String datetime = OffsetDateTime.now().toString(); + DaprProtos.Job job = DaprProtos.Job.newBuilder() + .setName("testJob") + .setDueTime(datetime) + .setFailurePolicy(CommonProtos.JobFailurePolicy.newBuilder() + .setConstant(CommonProtos.JobFailurePolicyConstant.newBuilder() + .setMaxRetries(10) + .setInterval(com.google.protobuf.Duration.newBuilder().setNanos(5).build()).build()).build()) + .build(); + + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onNext(DaprProtos.GetJobResponse.newBuilder() + .setJob(job) + .build()); + observer.onCompleted(); + return null; + }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); + + Mono resultMono = client.getJob(getJobRequest); + + GetJobResponse response = resultMono.block(); + assertNotNull(response); + assertEquals("testJob", response.getName()); + assertNull(response.getData()); + assertNull(response.getSchedule()); + assertNull(response.getRepeats()); + assertNull(response.getTtl()); + assertEquals(job.getDueTime(), datetime); + Assert.assertTrue(job.hasFailurePolicy()); + Assert.assertTrue(job.getFailurePolicy().hasConstant()); + assertEquals(10, job.getFailurePolicy().getConstant().getMaxRetries()); + assertEquals(5, job.getFailurePolicy().getConstant().getInterval().getNanos()); + } + + + @Test + public void getJobShouldThrowWhenRequestIsNull() { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + client.getJob(null).block(); + }); + assertEquals("getJobRequest cannot be null", exception.getMessage()); + } + + @Test + public void getJobShouldThrowWhenNameIsNullRequest() { + GetJobRequest getJobRequest = new GetJobRequest(null); + + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + client.getJob(getJobRequest).block(); + }); + assertEquals("Name in the request cannot be null or empty", exception.getMessage()); + } + + @Test + public void getJobShouldThrowWhenNameIsEmptyRequest() { + GetJobRequest getJobRequest =new GetJobRequest("");; + + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + client.getJob(getJobRequest).block(); + }); + assertEquals("Name in the request cannot be null or empty", exception.getMessage()); + } + + @Test + public void deleteJobShouldSucceedWhenValidRequest() { + DeleteJobRequest deleteJobRequest = new DeleteJobRequest("testJob"); + + doAnswer(invocation -> { + StreamObserver observer = invocation.getArgument(1); + observer.onCompleted(); // Simulate successful response + return null; + }).when(daprStub).deleteJobAlpha1(any(DaprProtos.DeleteJobRequest.class), any()); + + Mono resultMono = client.deleteJob(deleteJobRequest); + + assertDoesNotThrow(() -> resultMono.block()); + } + + @Test + public void deleteJobShouldThrowRequestIsNull() { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + client.deleteJob(null).block(); + }); + assertEquals("deleteJobRequest cannot be null", exception.getMessage()); + } + + @Test + public void deleteJobShouldThrowWhenNameIsNullRequest() { + DeleteJobRequest deleteJobRequest = new DeleteJobRequest(null); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + client.deleteJob(deleteJobRequest).block(); + }); + assertEquals("Name in the request cannot be null or empty", exception.getMessage()); + } + + @Test + public void deleteJobShouldThrowWhenNameIsEmptyRequest() { + DeleteJobRequest deleteJobRequest = new DeleteJobRequest(""); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + client.deleteJob(deleteJobRequest).block(); + }); + assertEquals("Name in the request cannot be null or empty", exception.getMessage()); + } + @Test public void getMetadataExceptionTest() { doAnswer((Answer) invocation -> { diff --git a/sdk/src/test/java/io/dapr/client/DaprPreviewClientGrpcTest.java b/sdk/src/test/java/io/dapr/client/DaprPreviewClientGrpcTest.java index a42c4f946c..1566c7d2cd 100644 --- a/sdk/src/test/java/io/dapr/client/DaprPreviewClientGrpcTest.java +++ b/sdk/src/test/java/io/dapr/client/DaprPreviewClientGrpcTest.java @@ -16,8 +16,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.collect.Lists; -import com.google.protobuf.Any; import com.google.protobuf.ByteString; import io.dapr.client.domain.AssistantMessage; import io.dapr.client.domain.BulkPublishEntry; @@ -35,23 +33,13 @@ import io.dapr.client.domain.ConversationResultChoices; import io.dapr.client.domain.ConversationToolCalls; import io.dapr.client.domain.ConversationTools; -import io.dapr.client.domain.DeleteJobRequest; import io.dapr.client.domain.DeveloperMessage; -import io.dapr.client.domain.GetJobRequest; -import io.dapr.client.domain.GetJobResponse; -import io.dapr.client.domain.ConstantFailurePolicy; import io.dapr.client.domain.ConversationInput; import io.dapr.client.domain.ConversationRequest; import io.dapr.client.domain.ConversationResponse; -import io.dapr.client.domain.DeleteJobRequest; -import io.dapr.client.domain.DropFailurePolicy; -import io.dapr.client.domain.GetJobRequest; -import io.dapr.client.domain.GetJobResponse; -import io.dapr.client.domain.JobSchedule; import io.dapr.client.domain.QueryStateItem; import io.dapr.client.domain.QueryStateRequest; import io.dapr.client.domain.QueryStateResponse; -import io.dapr.client.domain.ScheduleJobRequest; import io.dapr.client.domain.SystemMessage; import io.dapr.client.domain.ToolMessage; import io.dapr.client.domain.UnlockResponseStatus; @@ -60,7 +48,6 @@ import io.dapr.serializer.DaprObjectSerializer; import io.dapr.serializer.DefaultObjectSerializer; import io.dapr.utils.TypeRef; -import io.dapr.v1.CommonProtos; import io.dapr.v1.DaprAppCallbackProtos; import io.dapr.v1.DaprGrpc; import io.dapr.v1.DaprProtos; @@ -78,13 +65,6 @@ import reactor.core.publisher.Mono; import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.time.Duration; -import java.time.Instant; -import java.time.OffsetDateTime; -import java.time.ZoneOffset; -import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -99,9 +79,7 @@ import static io.dapr.utils.TestUtils.assertThrowsDaprException; import static org.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -802,634 +780,6 @@ public void converseShouldReturnConversationResponseWhenRequiredAndOptionalInput response.getConversationOutputs().get(0).getResult()); } - @Test - public void scheduleJobShouldSucceedWhenAllFieldsArePresentInRequest() { - DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") - .withZone(ZoneOffset.UTC); - - ScheduleJobRequest expectedScheduleJobRequest = new ScheduleJobRequest("testJob", - JobSchedule.fromString("*/5 * * * *")) - .setData("testData".getBytes()) - .setTtl(Instant.now().plus(1, ChronoUnit.DAYS)) - .setRepeat(5) - .setDueTime(Instant.now().plus(10, ChronoUnit.MINUTES)); - - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onCompleted(); // Simulate successful response - return null; - }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); - - assertDoesNotThrow(() -> previewClient.scheduleJob(expectedScheduleJobRequest).block()); - - ArgumentCaptor captor = - ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); - - verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); - DaprProtos.ScheduleJobRequest actualScheduleJobReq = captor.getValue(); - - assertEquals("testJob", actualScheduleJobReq.getJob().getName()); - assertEquals("testData", - new String(actualScheduleJobReq.getJob().getData().getValue().toByteArray(), StandardCharsets.UTF_8)); - assertEquals("*/5 * * * *", actualScheduleJobReq.getJob().getSchedule()); - assertEquals(iso8601Formatter.format(expectedScheduleJobRequest.getTtl()), actualScheduleJobReq.getJob().getTtl()); - assertEquals(expectedScheduleJobRequest.getRepeats(), actualScheduleJobReq.getJob().getRepeats()); - assertEquals(iso8601Formatter.format(expectedScheduleJobRequest.getDueTime()), actualScheduleJobReq.getJob().getDueTime()); - } - - @Test - public void scheduleJobShouldSucceedWhenRequiredFieldsNameAndDueTimeArePresentInRequest() { - DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") - .withZone(ZoneOffset.UTC); - - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onCompleted(); // Simulate successful response - return null; - }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); - - ScheduleJobRequest expectedScheduleJobRequest = - new ScheduleJobRequest("testJob", Instant.now().plus(10, ChronoUnit.MINUTES)); - assertDoesNotThrow(() -> previewClient.scheduleJob(expectedScheduleJobRequest).block()); - - ArgumentCaptor captor = - ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); - - verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); - DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); - DaprProtos.Job job = actualScheduleJobRequest.getJob(); - assertEquals("testJob", job.getName()); - assertFalse(job.hasData()); - assertFalse(job.hasSchedule()); - assertEquals(0, job.getRepeats()); - assertFalse(job.hasTtl()); - assertEquals(iso8601Formatter.format(expectedScheduleJobRequest.getDueTime()), - actualScheduleJobRequest.getJob().getDueTime()); - } - - @Test - public void scheduleJobShouldSucceedWhenRequiredFieldsNameAndScheduleArePresentInRequest() { - DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") - .withZone(ZoneOffset.UTC); - - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onCompleted(); // Simulate successful response - return null; - }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); - - ScheduleJobRequest expectedScheduleJobRequest = new ScheduleJobRequest("testJob", - JobSchedule.fromString("* * * * * *")); - assertDoesNotThrow(() -> previewClient.scheduleJob(expectedScheduleJobRequest).block()); - - ArgumentCaptor captor = - ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); - - verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); - DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); - DaprProtos.Job job = actualScheduleJobRequest.getJob(); - assertEquals("testJob", job.getName()); - assertFalse(job.hasData()); - assertEquals( "* * * * * *", job.getSchedule()); - assertEquals(0, job.getRepeats()); - assertFalse(job.hasTtl()); - } - - @Test - public void scheduleJobShouldThrowWhenRequestIsNull() { - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - previewClient.scheduleJob(null).block(); - }); - assertEquals("scheduleJobRequest cannot be null", exception.getMessage()); - } - - @Test - public void scheduleJobShouldThrowWhenInvalidRequest() { - ScheduleJobRequest scheduleJobRequest = new ScheduleJobRequest(null, Instant.now()); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - previewClient.scheduleJob(scheduleJobRequest).block(); - }); - assertEquals("Name in the request cannot be null or empty", exception.getMessage()); - } - - @Test - public void scheduleJobShouldThrowWhenNameInRequestIsEmpty() { - ScheduleJobRequest scheduleJobRequest = new ScheduleJobRequest("", Instant.now()); - - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - previewClient.scheduleJob(scheduleJobRequest).block(); - }); - assertEquals("Name in the request cannot be null or empty", exception.getMessage()); - } - - @Test - public void scheduleJobShouldHavePolicyWhenPolicyIsSet() { - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onCompleted(); // Simulate successful response - return null; - }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); - - ScheduleJobRequest expectedScheduleJobRequest = new ScheduleJobRequest("testJob", - JobSchedule.fromString("* * * * * *")) - .setFailurePolicy(new DropFailurePolicy()); - - previewClient.scheduleJob(expectedScheduleJobRequest).block(); - - ArgumentCaptor captor = - ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); - - verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); - DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); - DaprProtos.Job job = actualScheduleJobRequest.getJob(); - assertEquals("testJob", job.getName()); - assertFalse(job.hasData()); - assertEquals( "* * * * * *", job.getSchedule()); - assertEquals(0, job.getRepeats()); - assertFalse(job.hasTtl()); - Assertions.assertTrue(job.hasFailurePolicy()); - } - - @Test - public void scheduleJobShouldHaveConstantPolicyWithMaxRetriesWhenConstantPolicyIsSetWithMaxRetries() { - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onCompleted(); // Simulate successful response - return null; - }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); - - ScheduleJobRequest expectedScheduleJobRequest = new ScheduleJobRequest("testJob", - JobSchedule.fromString("* * * * * *")) - .setFailurePolicy(new ConstantFailurePolicy(2)); - - previewClient.scheduleJob(expectedScheduleJobRequest).block(); - - ArgumentCaptor captor = - ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); - - verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); - DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); - DaprProtos.Job job = actualScheduleJobRequest.getJob(); - assertEquals("testJob", job.getName()); - assertFalse(job.hasData()); - assertEquals( "* * * * * *", job.getSchedule()); - assertEquals(0, job.getRepeats()); - assertFalse(job.hasTtl()); - Assertions.assertTrue(job.hasFailurePolicy()); - assertEquals(2, job.getFailurePolicy().getConstant().getMaxRetries()); - } - - @Test - public void scheduleJobShouldHaveConstantPolicyWithIntervalWhenConstantPolicyIsSetWithInterval() { - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onCompleted(); // Simulate successful response - return null; - }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); - - ScheduleJobRequest expectedScheduleJobRequest = new ScheduleJobRequest("testJob", - JobSchedule.fromString("* * * * * *")) - .setFailurePolicy(new ConstantFailurePolicy(Duration.of(2, ChronoUnit.SECONDS))); - - previewClient.scheduleJob(expectedScheduleJobRequest).block(); - - ArgumentCaptor captor = - ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); - - verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); - DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); - DaprProtos.Job job = actualScheduleJobRequest.getJob(); - assertEquals("testJob", job.getName()); - assertFalse(job.hasData()); - assertEquals( "* * * * * *", job.getSchedule()); - assertEquals(0, job.getRepeats()); - assertFalse(job.hasTtl()); - Assertions.assertTrue(job.hasFailurePolicy()); - assertEquals(Duration.of(2, ChronoUnit.SECONDS).getNano(), - job.getFailurePolicy().getConstant().getInterval().getNanos()); - } - - @Test - public void scheduleJobShouldHaveBothRetiresAndIntervalWhenConstantPolicyIsSetWithRetriesAndInterval() { - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onCompleted(); // Simulate successful response - return null; - }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); - - ScheduleJobRequest expectedScheduleJobRequest = new ScheduleJobRequest("testJob", - JobSchedule.fromString("* * * * * *")) - .setFailurePolicy(new ConstantFailurePolicy(Duration.of(2, ChronoUnit.SECONDS)) - .setMaxRetries(10)); - - previewClient.scheduleJob(expectedScheduleJobRequest).block(); - - ArgumentCaptor captor = - ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); - - verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); - DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); - DaprProtos.Job job = actualScheduleJobRequest.getJob(); - assertEquals("testJob", job.getName()); - assertFalse(job.hasData()); - assertEquals( "* * * * * *", job.getSchedule()); - assertEquals(0, job.getRepeats()); - assertFalse(job.hasTtl()); - Assertions.assertTrue(job.hasFailurePolicy()); - assertEquals(Duration.of(2, ChronoUnit.SECONDS).getNano(), - job.getFailurePolicy().getConstant().getInterval().getNanos()); - assertEquals(10, job.getFailurePolicy().getConstant().getMaxRetries()); - } - - @Test - public void scheduleJobShouldThrowWhenNameAlreadyExists() { - AtomicInteger callCount = new AtomicInteger(0); - - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - if (callCount.incrementAndGet() == 1) { - // First call succeeds - observer.onCompleted(); - } else { - // Second call fails with ALREADY_EXISTS - observer.onError(newStatusRuntimeException("ALREADY_EXISTS", "Job with name 'testJob' already exists")); - } - return null; - }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); - - // First call should succeed - ScheduleJobRequest firstRequest = new ScheduleJobRequest("testJob", Instant.now()); - assertDoesNotThrow(() -> previewClient.scheduleJob(firstRequest).block()); - - ArgumentCaptor captor = - ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); - - verify(daprStub, times(1)).scheduleJobAlpha1(captor.capture(), Mockito.any()); - DaprProtos.ScheduleJobRequest actualScheduleJobRequest = captor.getValue(); - DaprProtos.Job job = actualScheduleJobRequest.getJob(); - assertEquals("testJob", job.getName()); - assertFalse(job.hasData()); - assertEquals(0, job.getRepeats()); - assertFalse(job.hasTtl()); - - // Second call with same name should fail - ScheduleJobRequest secondRequest = new ScheduleJobRequest("testJob", Instant.now()); - - assertThrowsDaprException( - ExecutionException.class, - "ALREADY_EXISTS", - "ALREADY_EXISTS: Job with name 'testJob' already exists", - () -> previewClient.scheduleJob(secondRequest).block()); - } - - @Test - public void scheduleJobShouldSucceedWhenNameAlreadyExistsWithOverwrite() { - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onCompleted(); // Simulate successful response for both calls - return null; - }).when(daprStub).scheduleJobAlpha1(any(DaprProtos.ScheduleJobRequest.class), any()); - - // First call should succeed - ScheduleJobRequest firstRequest = new ScheduleJobRequest("testJob", Instant.now()); - assertDoesNotThrow(() -> previewClient.scheduleJob(firstRequest).block()); - - // Second call with same name but overwrite=true should also succeed - ScheduleJobRequest secondRequest = new ScheduleJobRequest("testJob", Instant.now()) - .setOverwrite(true); - assertDoesNotThrow(() -> previewClient.scheduleJob(secondRequest).block()); - - // Verify that both calls were made successfully - ArgumentCaptor captor = - ArgumentCaptor.forClass(DaprProtos.ScheduleJobRequest.class); - verify(daprStub, times(2)).scheduleJobAlpha1(captor.capture(), any()); - - // Verify the first call doesn't have overwrite set - DaprProtos.ScheduleJobRequest firstActualRequest = captor.getAllValues().get(0); - assertFalse(firstActualRequest.getOverwrite()); - assertEquals("testJob", firstActualRequest.getJob().getName()); - - // Verify the second call has overwrite set to true - DaprProtos.ScheduleJobRequest secondActualRequest = captor.getAllValues().get(1); - assertTrue(secondActualRequest.getOverwrite()); - assertEquals("testJob", secondActualRequest.getJob().getName()); - } - - @Test - public void getJobShouldReturnResponseWhenAllFieldsArePresentInRequest() { - DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") - .withZone(ZoneOffset.UTC); - - GetJobRequest getJobRequest = new GetJobRequest("testJob"); - - DaprProtos.Job job = DaprProtos.Job.newBuilder() - .setName("testJob") - .setTtl(OffsetDateTime.now().format(iso8601Formatter)) - .setData(Any.newBuilder().setValue(ByteString.copyFrom("testData".getBytes())).build()) - .setSchedule("*/5 * * * *") - .setRepeats(5) - .setDueTime(iso8601Formatter.format(Instant.now().plus(10, ChronoUnit.MINUTES))) - .build(); - - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onNext(DaprProtos.GetJobResponse.newBuilder() - .setJob(job) - .build()); - observer.onCompleted(); - return null; - }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); - - Mono resultMono = previewClient.getJob(getJobRequest); - - GetJobResponse response = resultMono.block(); - assertNotNull(response); - assertEquals("testJob", response.getName()); - assertEquals("testData", new String(response.getData(), StandardCharsets.UTF_8)); - assertEquals("*/5 * * * *", response.getSchedule().getExpression()); - assertEquals(5, response.getRepeats()); - assertEquals(job.getTtl(), iso8601Formatter.format(response.getTtl())); - assertEquals(job.getDueTime(), iso8601Formatter.format(response.getDueTime())); - } - - @Test - public void getJobShouldReturnResponseWithScheduleSetWhenResponseHasSchedule() { - GetJobRequest getJobRequest = new GetJobRequest("testJob"); - - DaprProtos.Job job = DaprProtos.Job.newBuilder() - .setName("testJob") - .setSchedule("0 0 0 1 1 *") - .build(); - - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onNext(DaprProtos.GetJobResponse.newBuilder() - .setJob(job) - .build()); - observer.onCompleted(); - return null; - }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); - - Mono resultMono = previewClient.getJob(getJobRequest); - - GetJobResponse response = resultMono.block(); - assertNotNull(response); - assertEquals("testJob", response.getName()); - assertNull(response.getData()); - assertEquals("0 0 0 1 1 *", response.getSchedule().getExpression()); - assertNull(response.getRepeats()); - assertNull(response.getTtl()); - assertNull(response.getDueTime()); - } - - @Test - public void getJobShouldReturnResponseWithDueTimeSetWhenResponseHasDueTime() { - GetJobRequest getJobRequest = new GetJobRequest("testJob"); - - String datetime = OffsetDateTime.now().toString(); - DaprProtos.Job job = DaprProtos.Job.newBuilder() - .setName("testJob") - .setDueTime(datetime) - .build(); - - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onNext(DaprProtos.GetJobResponse.newBuilder() - .setJob(job) - .build()); - observer.onCompleted(); - return null; - }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); - - Mono resultMono = previewClient.getJob(getJobRequest); - - GetJobResponse response = resultMono.block(); - assertNotNull(response); - assertEquals("testJob", response.getName()); - assertNull(response.getData()); - assertNull(response.getSchedule()); - assertNull(response.getRepeats()); - assertNull(response.getTtl()); - assertEquals(job.getDueTime(), datetime); - } - - @Test - public void getJobShouldReturnResponseWithDropFailurePolicySet() { - GetJobRequest getJobRequest = new GetJobRequest("testJob"); - - String datetime = OffsetDateTime.now().toString(); - DaprProtos.Job job = DaprProtos.Job.newBuilder() - .setName("testJob") - .setDueTime(datetime) - .setFailurePolicy(CommonProtos.JobFailurePolicy.newBuilder() - .setDrop(CommonProtos.JobFailurePolicyDrop.newBuilder().build()).build()) - .build(); - - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onNext(DaprProtos.GetJobResponse.newBuilder() - .setJob(job) - .build()); - observer.onCompleted(); - return null; - }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); - - Mono resultMono = previewClient.getJob(getJobRequest); - - GetJobResponse response = resultMono.block(); - assertNotNull(response); - assertEquals("testJob", response.getName()); - assertNull(response.getData()); - assertNull(response.getSchedule()); - assertNull(response.getRepeats()); - assertNull(response.getTtl()); - assertEquals(job.getDueTime(), datetime); - assertTrue(job.hasFailurePolicy()); - assertTrue(job.getFailurePolicy().hasDrop()); - } - - @Test - public void getJobShouldReturnResponseWithConstantFailurePolicyAndMaxRetriesSet() { - GetJobRequest getJobRequest = new GetJobRequest("testJob"); - - String datetime = OffsetDateTime.now().toString(); - DaprProtos.Job job = DaprProtos.Job.newBuilder() - .setName("testJob") - .setDueTime(datetime) - .setFailurePolicy(CommonProtos.JobFailurePolicy.newBuilder() - .setConstant(CommonProtos.JobFailurePolicyConstant.newBuilder().setMaxRetries(2).build()).build()) - .build(); - - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onNext(DaprProtos.GetJobResponse.newBuilder() - .setJob(job) - .build()); - observer.onCompleted(); - return null; - }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); - - Mono resultMono = previewClient.getJob(getJobRequest); - - GetJobResponse response = resultMono.block(); - assertNotNull(response); - assertEquals("testJob", response.getName()); - assertNull(response.getData()); - assertNull(response.getSchedule()); - assertNull(response.getRepeats()); - assertNull(response.getTtl()); - assertEquals(job.getDueTime(), datetime); - assertTrue(job.hasFailurePolicy()); - assertTrue(job.getFailurePolicy().hasConstant()); - assertEquals(2, job.getFailurePolicy().getConstant().getMaxRetries()); - } - - @Test - public void getJobShouldReturnResponseWithConstantFailurePolicyAndIntervalSet() { - GetJobRequest getJobRequest = new GetJobRequest("testJob"); - - String datetime = OffsetDateTime.now().toString(); - DaprProtos.Job job = DaprProtos.Job.newBuilder() - .setName("testJob") - .setDueTime(datetime) - .setFailurePolicy(CommonProtos.JobFailurePolicy.newBuilder() - .setConstant(CommonProtos.JobFailurePolicyConstant.newBuilder() - .setInterval(com.google.protobuf.Duration.newBuilder().setNanos(5).build()).build()).build()) - .build(); - - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onNext(DaprProtos.GetJobResponse.newBuilder() - .setJob(job) - .build()); - observer.onCompleted(); - return null; - }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); - - Mono resultMono = previewClient.getJob(getJobRequest); - - GetJobResponse response = resultMono.block(); - assertNotNull(response); - assertEquals("testJob", response.getName()); - assertNull(response.getData()); - assertNull(response.getSchedule()); - assertNull(response.getRepeats()); - assertNull(response.getTtl()); - assertEquals(job.getDueTime(), datetime); - assertTrue(job.hasFailurePolicy()); - assertTrue(job.getFailurePolicy().hasConstant()); - assertEquals(5, job.getFailurePolicy().getConstant().getInterval().getNanos()); - } - - @Test - public void getJobShouldReturnResponseWithConstantFailurePolicyIntervalAndMaxRetriesSet() { - GetJobRequest getJobRequest = new GetJobRequest("testJob"); - - String datetime = OffsetDateTime.now().toString(); - DaprProtos.Job job = DaprProtos.Job.newBuilder() - .setName("testJob") - .setDueTime(datetime) - .setFailurePolicy(CommonProtos.JobFailurePolicy.newBuilder() - .setConstant(CommonProtos.JobFailurePolicyConstant.newBuilder() - .setMaxRetries(10) - .setInterval(com.google.protobuf.Duration.newBuilder().setNanos(5).build()).build()).build()) - .build(); - - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onNext(DaprProtos.GetJobResponse.newBuilder() - .setJob(job) - .build()); - observer.onCompleted(); - return null; - }).when(daprStub).getJobAlpha1(any(DaprProtos.GetJobRequest.class), any()); - - Mono resultMono = previewClient.getJob(getJobRequest); - - GetJobResponse response = resultMono.block(); - assertNotNull(response); - assertEquals("testJob", response.getName()); - assertNull(response.getData()); - assertNull(response.getSchedule()); - assertNull(response.getRepeats()); - assertNull(response.getTtl()); - assertEquals(job.getDueTime(), datetime); - assertTrue(job.hasFailurePolicy()); - assertTrue(job.getFailurePolicy().hasConstant()); - assertEquals(10, job.getFailurePolicy().getConstant().getMaxRetries()); - assertEquals(5, job.getFailurePolicy().getConstant().getInterval().getNanos()); - } - - - @Test - public void getJobShouldThrowWhenRequestIsNull() { - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - previewClient.getJob(null).block(); - }); - assertEquals("getJobRequest cannot be null", exception.getMessage()); - } - - @Test - public void getJobShouldThrowWhenNameIsNullRequest() { - GetJobRequest getJobRequest = new GetJobRequest(null); - - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - previewClient.getJob(getJobRequest).block(); - }); - assertEquals("Name in the request cannot be null or empty", exception.getMessage()); - } - - @Test - public void getJobShouldThrowWhenNameIsEmptyRequest() { - GetJobRequest getJobRequest =new GetJobRequest("");; - - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - previewClient.getJob(getJobRequest).block(); - }); - assertEquals("Name in the request cannot be null or empty", exception.getMessage()); - } - - @Test - public void deleteJobShouldSucceedWhenValidRequest() { - DeleteJobRequest deleteJobRequest = new DeleteJobRequest("testJob"); - - doAnswer(invocation -> { - StreamObserver observer = invocation.getArgument(1); - observer.onCompleted(); // Simulate successful response - return null; - }).when(daprStub).deleteJobAlpha1(any(DaprProtos.DeleteJobRequest.class), any()); - - Mono resultMono = previewClient.deleteJob(deleteJobRequest); - - assertDoesNotThrow(() -> resultMono.block()); - } - - @Test - public void deleteJobShouldThrowRequestIsNull() { - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - previewClient.deleteJob(null).block(); - }); - assertEquals("deleteJobRequest cannot be null", exception.getMessage()); - } - - @Test - public void deleteJobShouldThrowWhenNameIsNullRequest() { - DeleteJobRequest deleteJobRequest = new DeleteJobRequest(null); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - previewClient.deleteJob(deleteJobRequest).block(); - }); - assertEquals("Name in the request cannot be null or empty", exception.getMessage()); - } - - @Test - public void deleteJobShouldThrowWhenNameIsEmptyRequest() { - DeleteJobRequest deleteJobRequest = new DeleteJobRequest(""); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - previewClient.deleteJob(deleteJobRequest).block(); - }); - assertEquals("Name in the request cannot be null or empty", exception.getMessage()); - } - @Test public void converseAlpha2ShouldThrowIllegalArgumentExceptionWhenNameIsNull() { List messages = new ArrayList<>();