forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobTest.java
More file actions
371 lines (339 loc) · 14.3 KB
/
JobTest.java
File metadata and controls
371 lines (339 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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 com.google.cloud.bigquery;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import com.google.cloud.Clock;
import com.google.cloud.WaitForOption;
import com.google.cloud.bigquery.JobStatistics.CopyStatistics;
import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class JobTest {
private static final JobId JOB_ID = JobId.of("project", "job");
private static final TableId TABLE_ID1 = TableId.of("dataset", "table1");
private static final TableId TABLE_ID2 = TableId.of("dataset", "table2");
private static final String ETAG = "etag";
private static final String GENERATED_ID = "id";
private static final String SELF_LINK = "selfLink";
private static final String EMAIL = "email";
private static final JobStatus JOB_STATUS = new JobStatus(JobStatus.State.DONE);
private static final JobStatistics COPY_JOB_STATISTICS = CopyStatistics.builder()
.creationTime(1L)
.endTime(3L)
.startTime(2L)
.build();
private static final CopyJobConfiguration COPY_CONFIGURATION =
CopyJobConfiguration.of(TABLE_ID1, TABLE_ID2);
private static final JobInfo JOB_INFO = JobInfo.builder(COPY_CONFIGURATION)
.jobId(JOB_ID)
.statistics(COPY_JOB_STATISTICS)
.jobId(JOB_ID)
.etag(ETAG)
.generatedId(GENERATED_ID)
.selfLink(SELF_LINK)
.userEmail(EMAIL)
.status(JOB_STATUS)
.build();
private BigQuery serviceMockReturnsOptions = createStrictMock(BigQuery.class);
private BigQueryOptions mockOptions = createMock(BigQueryOptions.class);
private BigQuery bigquery;
private Job expectedJob;
private Job job;
@Rule
public final ExpectedException thrown = ExpectedException.none();
private void initializeExpectedJob(int optionsCalls) {
expect(serviceMockReturnsOptions.options()).andReturn(mockOptions).times(optionsCalls);
replay(serviceMockReturnsOptions);
bigquery = createStrictMock(BigQuery.class);
expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(JOB_INFO));
}
private void initializeJob() {
job = new Job(bigquery, new JobInfo.BuilderImpl(JOB_INFO));
}
@After
public void tearDown() throws Exception {
verify(bigquery, serviceMockReturnsOptions);
}
@Test
public void testBuilder() {
initializeExpectedJob(2);
replay(bigquery);
Job builtJob = new Job.Builder(serviceMockReturnsOptions, COPY_CONFIGURATION)
.jobId(JOB_ID)
.statistics(COPY_JOB_STATISTICS)
.jobId(JOB_ID)
.etag(ETAG)
.generatedId(GENERATED_ID)
.selfLink(SELF_LINK)
.userEmail(EMAIL)
.status(JOB_STATUS)
.build();
assertEquals(ETAG, builtJob.etag());
assertEquals(GENERATED_ID, builtJob.generatedId());
assertEquals(SELF_LINK, builtJob.selfLink());
assertEquals(EMAIL, builtJob.userEmail());
assertEquals(JOB_ID, builtJob.jobId());
assertEquals(JOB_STATUS, builtJob.status());
assertEquals(COPY_CONFIGURATION, builtJob.configuration());
assertEquals(COPY_JOB_STATISTICS, builtJob.statistics());
assertSame(serviceMockReturnsOptions, builtJob.bigquery());
}
@Test
public void testToBuilder() {
initializeExpectedJob(4);
replay(bigquery);
compareJob(expectedJob, expectedJob.toBuilder().build());
}
@Test
public void testExists_True() throws Exception {
initializeExpectedJob(1);
BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields()};
expect(bigquery.options()).andReturn(mockOptions);
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(expectedJob);
replay(bigquery);
initializeJob();
assertTrue(job.exists());
}
@Test
public void testExists_False() throws Exception {
initializeExpectedJob(1);
BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields()};
expect(bigquery.options()).andReturn(mockOptions);
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(null);
replay(bigquery);
initializeJob();
assertFalse(job.exists());
}
@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.state()).andReturn(JobStatus.State.DONE);
expect(bigquery.options()).andReturn(mockOptions);
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions))
.andReturn(expectedJob.toBuilder().status(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.state()).andReturn(JobStatus.State.RUNNING);
expect(bigquery.options()).andReturn(mockOptions);
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions))
.andReturn(expectedJob.toBuilder().status(status).build());
replay(status, bigquery);
initializeJob();
assertFalse(job.isDone());
verify(status);
}
@Test
public void testIsDone_NotExists() throws Exception {
initializeExpectedJob(1);
BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)};
expect(bigquery.options()).andReturn(mockOptions);
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(null);
replay(bigquery);
initializeJob();
assertTrue(job.isDone());
}
@Test
public void testWaitFor() throws InterruptedException, TimeoutException {
initializeExpectedJob(2);
BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)};
JobStatus status = createStrictMock(JobStatus.class);
expect(status.state()).andReturn(JobStatus.State.DONE);
expect(bigquery.options()).andReturn(mockOptions);
expect(mockOptions.clock()).andReturn(Clock.defaultClock());
Job completedJob = expectedJob.toBuilder().status(status).build();
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(completedJob);
expect(bigquery.getJob(JOB_INFO.jobId())).andReturn(completedJob);
replay(status, bigquery, mockOptions);
initializeJob();
assertSame(completedJob, job.waitFor());
verify(status, mockOptions);
}
@Test
public void testWaitFor_Null() throws InterruptedException, TimeoutException {
initializeExpectedJob(1);
BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)};
expect(bigquery.options()).andReturn(mockOptions);
expect(mockOptions.clock()).andReturn(Clock.defaultClock());
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(null);
expect(bigquery.getJob(JOB_INFO.jobId())).andReturn(null);
replay(bigquery, mockOptions);
initializeJob();
assertNull(job.waitFor());
verify(mockOptions);
}
@Test
public void testWaitForWithCheckingPeriod() throws InterruptedException, TimeoutException {
initializeExpectedJob(3);
BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)};
TimeUnit timeUnit = createStrictMock(TimeUnit.class);
timeUnit.sleep(42);
EasyMock.expectLastCall();
JobStatus status = createStrictMock(JobStatus.class);
expect(status.state()).andReturn(JobStatus.State.RUNNING);
expect(status.state()).andReturn(JobStatus.State.DONE);
expect(bigquery.options()).andReturn(mockOptions);
expect(mockOptions.clock()).andReturn(Clock.defaultClock());
Job runningJob = expectedJob.toBuilder().status(status).build();
Job completedJob = expectedJob.toBuilder().status(status).build();
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(runningJob);
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(completedJob);
expect(bigquery.getJob(JOB_INFO.jobId())).andReturn(completedJob);
replay(status, bigquery, timeUnit, mockOptions);
initializeJob();
assertSame(completedJob, job.waitFor(WaitForOption.checkEvery(42, timeUnit)));
verify(status, timeUnit, mockOptions);
}
@Test
public void testWaitForWithCheckingPeriod_Null() throws InterruptedException, TimeoutException {
initializeExpectedJob(2);
BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)};
TimeUnit timeUnit = createStrictMock(TimeUnit.class);
timeUnit.sleep(42);
EasyMock.expectLastCall();
expect(bigquery.options()).andReturn(mockOptions);
expect(mockOptions.clock()).andReturn(Clock.defaultClock());
Job runningJob = expectedJob.toBuilder().status(new JobStatus(JobStatus.State.RUNNING)).build();
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(runningJob);
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(null);
expect(bigquery.getJob(JOB_INFO.jobId())).andReturn(null);
replay(bigquery, timeUnit, mockOptions);
initializeJob();
assertNull(job.waitFor(WaitForOption.checkEvery(42, timeUnit)));
verify(bigquery, timeUnit, mockOptions);
}
@Test
public void testWaitForWithTimeout() throws InterruptedException, TimeoutException {
initializeExpectedJob(2);
BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)};
TimeUnit timeUnit = createStrictMock(TimeUnit.class);
timeUnit.sleep(1);
EasyMock.expectLastCall();
Clock clock = createStrictMock(Clock.class);
expect(clock.millis()).andReturn(0L);
expect(clock.millis()).andReturn(1L);
expect(clock.millis()).andReturn(3L);
JobStatus status = createStrictMock(JobStatus.class);
expect(status.state()).andReturn(JobStatus.State.RUNNING);
expect(status.state()).andReturn(JobStatus.State.RUNNING);
expect(bigquery.options()).andReturn(mockOptions);
expect(mockOptions.clock()).andReturn(clock);
Job runningJob = expectedJob.toBuilder().status(status).build();
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(runningJob);
expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(runningJob);
replay(status, bigquery, timeUnit, clock, mockOptions);
initializeJob();
thrown.expect(TimeoutException.class);
job.waitFor(WaitForOption.checkEvery(1, timeUnit),
WaitForOption.timeout(3, TimeUnit.MILLISECONDS));
verify(status, timeUnit, clock, mockOptions);
}
@Test
public void testReload() throws Exception {
initializeExpectedJob(4);
JobInfo updatedInfo = JOB_INFO.toBuilder().etag("etag").build();
Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(updatedInfo));
expect(bigquery.options()).andReturn(mockOptions);
expect(bigquery.getJob(JOB_INFO.jobId())).andReturn(expectedJob);
replay(bigquery);
initializeJob();
Job updatedJob = job.reload();
compareJob(expectedJob, updatedJob);
}
@Test
public void testReloadNull() throws Exception {
initializeExpectedJob(1);
expect(bigquery.options()).andReturn(mockOptions);
expect(bigquery.getJob(JOB_INFO.jobId())).andReturn(null);
replay(bigquery);
initializeJob();
assertNull(job.reload());
}
@Test
public void testReloadWithOptions() throws Exception {
initializeExpectedJob(4);
JobInfo updatedInfo = JOB_INFO.toBuilder().etag("etag").build();
Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(updatedInfo));
expect(bigquery.options()).andReturn(mockOptions);
expect(bigquery.getJob(JOB_INFO.jobId(), BigQuery.JobOption.fields()))
.andReturn(expectedJob);
replay(bigquery);
initializeJob();
Job updatedJob = job.reload(BigQuery.JobOption.fields());
compareJob(expectedJob, updatedJob);
}
@Test
public void testCancel() throws Exception {
initializeExpectedJob(1);
expect(bigquery.options()).andReturn(mockOptions);
expect(bigquery.cancel(JOB_INFO.jobId())).andReturn(true);
replay(bigquery);
initializeJob();
assertTrue(job.cancel());
}
@Test
public void testBigquery() {
initializeExpectedJob(1);
replay(bigquery);
assertSame(serviceMockReturnsOptions, expectedJob.bigquery());
}
@Test
public void testToAndFromPb() {
initializeExpectedJob(4);
replay(bigquery);
compareJob(expectedJob, Job.fromPb(serviceMockReturnsOptions, expectedJob.toPb()));
}
private void compareJob(Job expected, Job value) {
assertEquals(expected, value);
compareJobInfo(expected, value);
assertEquals(expected.bigquery().options(), value.bigquery().options());
}
private void compareJobInfo(JobInfo expected, JobInfo value) {
assertEquals(expected, value);
assertEquals(expected.hashCode(), value.hashCode());
assertEquals(expected.toString(), value.toString());
assertEquals(expected.etag(), value.etag());
assertEquals(expected.generatedId(), value.generatedId());
assertEquals(expected.jobId(), value.jobId());
assertEquals(expected.selfLink(), value.selfLink());
assertEquals(expected.status(), value.status());
assertEquals(expected.statistics(), value.statistics());
assertEquals(expected.userEmail(), value.userEmail());
assertEquals(expected.configuration(), value.configuration());
}
}