/** Example of getting a job. */ // [TARGET getJob(String, JobOption...)] // [VARIABLE "my_job_name"] public Job getJob(String jobName) { // [START ] Job job = bigquery.getJob(jobName); if (job == null) { // job was not found } // [END ] return job; }
@Override public void run(BigQuery bigquery, JobId jobId) { System.out.println("Job info: " + bigquery.getJob(jobId)); } }
/** Example of getting a job. */ // [TARGET getJob(JobId, JobOption...)] // [VARIABLE "my_job_name"] public Job getJobFromId(String jobName) { // [START ] JobId jobIdObject = JobId.of(jobName); Job job = bigquery.getJob(jobIdObject); if (job == null) { // job was not found } // [END ] return job; }
@Override public Job call() throws Exception { return bigquery.getJob(getJobId(), JobOption.fields(BigQuery.JobField.STATUS)); } },
@Test public void testReloadNull() throws Exception { initializeExpectedJob(1); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getJob(JOB_INFO.getJobId())).andReturn(null); replay(bigquery); initializeJob(); assertNull(job.reload()); }
@Test public void testExists_True() throws Exception { initializeExpectedJob(1); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields()}; expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(expectedJob); replay(bigquery); initializeJob(); assertTrue(job.exists()); }
@Test public void testWaitFor_Null() throws InterruptedException { initializeExpectedJob(1); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; expect(bigquery.getOptions()).andReturn(mockOptions); expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(null); replay(bigquery, mockOptions); initializeJob(); assertNull(job.waitFor(TEST_RETRY_OPTIONS)); verify(mockOptions); }
@Test public void testExists_False() throws Exception { initializeExpectedJob(1); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields()}; expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(null); replay(bigquery); initializeJob(); assertFalse(job.exists()); }
@Test public void testIsDone_NotExists() throws Exception { initializeExpectedJob(1); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(null); replay(bigquery); initializeJob(); assertTrue(job.isDone()); }
@Test public void testWaitForWithCheckingPeriod_Null() throws InterruptedException { initializeExpectedJob(2); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; expect(bigquery.getOptions()).andReturn(mockOptions); expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); Job runningJob = expectedJob.toBuilder().setStatus(new JobStatus(JobStatus.State.RUNNING)).build(); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(runningJob); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(null); replay(bigquery, mockOptions); initializeJob(); assertNull(job.waitFor(TEST_RETRY_OPTIONS)); verify(bigquery, mockOptions); }
@Test public void testIsDone_True() throws Exception { initializeExpectedJob(2); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; JobStatus status = createStrictMock(JobStatus.class); expect(status.getState()).andReturn(JobStatus.State.DONE); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)) .andReturn(expectedJob.toBuilder().setStatus(status).build()); replay(status, bigquery); initializeJob(); assertTrue(job.isDone()); verify(status); }
@Test public void testIsDone_False() throws Exception { initializeExpectedJob(2); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; JobStatus status = createStrictMock(JobStatus.class); expect(status.getState()).andReturn(JobStatus.State.RUNNING); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)) .andReturn(expectedJob.toBuilder().setStatus(status).build()); replay(status, bigquery); initializeJob(); assertFalse(job.isDone()); verify(status); }
@Test public void testGetJob() { EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) .andReturn(COMPLETE_COPY_JOB.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Job job = bigquery.getJob(JOB); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); }
@Test public void testGetJobFromJobId() { EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) .andReturn(COMPLETE_COPY_JOB.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Job job = bigquery.getJob(JobId.of(JOB)); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); }
@Test public void testGetJobWithLocation() { EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS)) .andReturn(COMPLETE_COPY_JOB.toPb()); EasyMock.replay(bigqueryRpcMock); BigQueryOptions options = createBigQueryOptionsForProjectWithLocation(PROJECT, rpcFactoryMock); bigquery = options.getService(); Job job = bigquery.getJob(JOB); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); }
@Test public void testGetJobFromJobIdWithLocation() { EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS)) .andReturn(COMPLETE_COPY_JOB.toPb()); EasyMock.replay(bigqueryRpcMock); BigQueryOptions options = createBigQueryOptionsForProjectWithLocation(PROJECT, rpcFactoryMock); bigquery = options.getService(); Job job = bigquery.getJob(JobId.of(JOB)); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); }
@Test public void testReload() throws Exception { initializeExpectedJob(4); JobInfo updatedInfo = JOB_INFO.toBuilder().setEtag("etag").build(); Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(updatedInfo)); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getJob(JOB_INFO.getJobId())).andReturn(expectedJob); replay(bigquery); initializeJob(); Job updatedJob = job.reload(); compareJob(expectedJob, updatedJob); }
@Test public void testGetJobFromJobIdWithProject() { JobId jobId = JobId.of(OTHER_PROJECT, JOB); JobInfo jobInfo = COPY_JOB.setProjectId(OTHER_PROJECT); EasyMock.expect(bigqueryRpcMock.getJob(OTHER_PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) .andReturn(jobInfo.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Job job = bigquery.getJob(jobId); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(jobInfo)), job); }
@Test public void testGetJobFromJobIdWithProjectWithLocation() { JobId jobId = JobId.of(OTHER_PROJECT, JOB); JobInfo jobInfo = COPY_JOB.setProjectId(OTHER_PROJECT); EasyMock.expect(bigqueryRpcMock.getJob(OTHER_PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS)) .andReturn(jobInfo.toPb()); EasyMock.replay(bigqueryRpcMock); BigQueryOptions options = createBigQueryOptionsForProjectWithLocation(PROJECT, rpcFactoryMock); bigquery = options.getService(); Job job = bigquery.getJob(jobId); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(jobInfo)), job); }
@Test public void testReloadWithOptions() throws Exception { initializeExpectedJob(4); JobInfo updatedInfo = JOB_INFO.toBuilder().setEtag("etag").build(); Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(updatedInfo)); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getJob(JOB_INFO.getJobId(), BigQuery.JobOption.fields())) .andReturn(expectedJob); replay(bigquery); initializeJob(); Job updatedJob = job.reload(BigQuery.JobOption.fields()); compareJob(expectedJob, updatedJob); }