/** * Creates a job. * * <p>Sample code: * * <pre><code> * try (CloudSchedulerClient cloudSchedulerClient = CloudSchedulerClient.create()) { * LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); * Job job = Job.newBuilder().build(); * Job response = cloudSchedulerClient.createJob(parent.toString(), job); * } * </code></pre> * * @param parent Required. * <p>The location name. For example: `projects/PROJECT_ID/locations/LOCATION_ID`. * @param job Required. * <p>The job to add. The user can optionally specify a name for the job in * [name][google.cloud.scheduler.v1beta1.Job.name]. * [name][google.cloud.scheduler.v1beta1.Job.name] cannot be the same as an existing job. If a * name is not specified then the system will generate a random unique name that will be * returned ([name][google.cloud.scheduler.v1beta1.Job.name]) in the response. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final Job createJob(String parent, Job job) { CreateJobRequest request = CreateJobRequest.newBuilder().setParent(parent).setJob(job).build(); return createJob(request); }
.setJob(job) .build(); return createJob(request);
@Test @SuppressWarnings("all") public void createJobExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockCloudScheduler.addException(exception); try { LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); Job job = Job.newBuilder().build(); client.createJob(parent, job); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void createJobTest() { JobName name = JobName.of("[PROJECT]", "[LOCATION]", "[JOB]"); String description = "description-1724546052"; String schedule = "schedule-697920873"; String timeZone = "timeZone36848094"; Job expectedResponse = Job.newBuilder() .setName(name.toString()) .setDescription(description) .setSchedule(schedule) .setTimeZone(timeZone) .build(); mockCloudScheduler.addResponse(expectedResponse); LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); Job job = Job.newBuilder().build(); Job actualResponse = client.createJob(parent, job); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockCloudScheduler.getRequests(); Assert.assertEquals(1, actualRequests.size()); CreateJobRequest actualRequest = (CreateJobRequest) actualRequests.get(0); Assert.assertEquals(parent, LocationName.parse(actualRequest.getParent())); Assert.assertEquals(job, actualRequest.getJob()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }