Skip to content

Commit 595526c

Browse files
committed
---
yaml --- r: 2557 b: refs/heads/update-datastore c: 6bc4f2c h: refs/heads/master i: 2555: 304d63a
1 parent 7fc356b commit 595526c

6 files changed

Lines changed: 1094 additions & 2 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/gh-pages: 4e0561bb4504bf647db669a14417b2b2c87ba45d
55
refs/heads/bigquery: 762fa5830e6c398c0396177e3e7fd243bd62cfc3
66
refs/heads/pubsub-alpha: 1a0e970f265af871e02274085b9662b3fe29058b
77
refs/heads/resource-manager: ebf4adc5ee835cd2086c4ac5b4e78d01a5a005a7
8-
refs/heads/update-datastore: ec115cbbcedb37ea2c14d80e70202662e9355d69
8+
refs/heads/update-datastore: 6bc4f2c26bfd873864ac5fc436e8152cd304595f
99
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444
1010
refs/tags/v0.0.10: 207ebd2a3472fddee69fe1298eb90429e3306efd
1111
refs/tags/v0.0.11: ffbfba48a6426ff63c08ff2117e58681f251fbf2

branches/update-datastore/gcloud-java-bigquery/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@
2222
<artifactId>gcloud-java-core</artifactId>
2323
<version>${project.version}</version>
2424
</dependency>
25+
<dependency>
26+
<groupId>${project.groupId}</groupId>
27+
<artifactId>gcloud-java-storage</artifactId>
28+
<version>${project.version}</version>
29+
<scope>test</scope>
30+
</dependency>
2531
<dependency>
2632
<groupId>com.google.apis</groupId>
2733
<artifactId>google-api-services-bigquery</artifactId>
28-
<version>v2-rev244-1.20.0</version>
34+
<version>v2-rev254-1.21.0</version>
2935
<scope>compile</scope>
3036
<exclusions>
3137
<exclusion>
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.bigquery.testing;
18+
19+
import com.google.gcloud.AuthCredentials;
20+
import com.google.gcloud.RetryParams;
21+
import com.google.gcloud.bigquery.BigQuery;
22+
import com.google.gcloud.bigquery.BigQueryException;
23+
import com.google.gcloud.bigquery.BigQueryOptions;
24+
25+
import java.io.FileInputStream;
26+
import java.io.FileNotFoundException;
27+
import java.io.IOException;
28+
import java.io.InputStream;
29+
import java.util.UUID;
30+
import java.util.logging.Level;
31+
import java.util.logging.Logger;
32+
33+
/**
34+
* Utility to create a remote BigQuery configuration for testing.
35+
*/
36+
public class RemoteBigQueryHelper {
37+
38+
private static final Logger log = Logger.getLogger(RemoteBigQueryHelper.class.getName());
39+
private static final String DATASET_NAME_PREFIX = "gcloud_test_dataset_temp_";
40+
private final BigQueryOptions options;
41+
42+
private RemoteBigQueryHelper(BigQueryOptions options) {
43+
this.options = options;
44+
}
45+
46+
/**
47+
* Returns a {@link BigQueryOptions} object to be used for testing.
48+
*/
49+
public BigQueryOptions options() {
50+
return options;
51+
}
52+
53+
/**
54+
* Deletes a dataset, even if non-empty.
55+
*
56+
* @param bigquery the BigQuery service to be used to issue the delete request
57+
* @param dataset the dataset to be deleted
58+
* @return {@code true} if deletion succeeded, {@code false} if the dataset was not found.
59+
* @throws BigQueryException upon failure
60+
*/
61+
public static boolean forceDelete(BigQuery bigquery, String dataset) {
62+
return bigquery.delete(dataset, BigQuery.DatasetDeleteOption.deleteContents());
63+
}
64+
65+
/**
66+
* Returns a dataset name generated using a random UUID.
67+
*/
68+
public static String generateDatasetName() {
69+
return DATASET_NAME_PREFIX + UUID.randomUUID().toString().replace('-', '_');
70+
}
71+
72+
/**
73+
* Creates a {@code RemoteBigQueryHelper} object for the given project id and JSON key input
74+
* stream.
75+
*
76+
* @param projectId id of the project to be used for running the tests
77+
* @param keyStream input stream for a JSON key
78+
* @return A {@code RemoteBigQueryHelper} object for the provided options.
79+
* @throws BigQueryHelperException if {@code keyStream} is not a valid JSON key stream
80+
*/
81+
public static RemoteBigQueryHelper create(String projectId, InputStream keyStream)
82+
throws BigQueryHelperException {
83+
try {
84+
BigQueryOptions bigqueryOptions = BigQueryOptions.builder()
85+
.authCredentials(AuthCredentials.createForJson(keyStream))
86+
.projectId(projectId)
87+
.retryParams(retryParams())
88+
.connectTimeout(60000)
89+
.readTimeout(60000)
90+
.build();
91+
return new RemoteBigQueryHelper(bigqueryOptions);
92+
} catch (IOException ex) {
93+
if (log.isLoggable(Level.WARNING)) {
94+
log.log(Level.WARNING, ex.getMessage());
95+
}
96+
throw BigQueryHelperException.translate(ex);
97+
}
98+
}
99+
100+
/**
101+
* Creates a {@code RemoteBigQueryHelper} object for the given project id and JSON key path.
102+
*
103+
* @param projectId id of the project to be used for running the tests
104+
* @param keyPath path to the JSON key to be used for running the tests
105+
* @return A {@code RemoteBigQueryHelper} object for the provided options.
106+
* @throws BigQueryHelperException if the file pointed by {@code keyPath} does not exist
107+
*/
108+
public static RemoteBigQueryHelper create(String projectId, String keyPath)
109+
throws BigQueryHelperException {
110+
try {
111+
InputStream keyFileStream = new FileInputStream(keyPath);
112+
return create(projectId, keyFileStream);
113+
} catch (FileNotFoundException ex) {
114+
if (log.isLoggable(Level.WARNING)) {
115+
log.log(Level.WARNING, ex.getMessage());
116+
}
117+
throw BigQueryHelperException.translate(ex);
118+
}
119+
}
120+
121+
/**
122+
* Creates a {@code RemoteBigQueryHelper} object using default project id and authentication
123+
* credentials.
124+
*/
125+
public static RemoteBigQueryHelper create() {
126+
BigQueryOptions bigqueryOptions = BigQueryOptions.builder()
127+
.retryParams(retryParams())
128+
.connectTimeout(60000)
129+
.readTimeout(60000)
130+
.build();
131+
return new RemoteBigQueryHelper(bigqueryOptions);
132+
}
133+
134+
private static RetryParams retryParams() {
135+
return RetryParams.builder()
136+
.retryMaxAttempts(10)
137+
.retryMinAttempts(6)
138+
.maxRetryDelayMillis(30000)
139+
.totalRetryPeriodMillis(120000)
140+
.initialRetryDelayMillis(250)
141+
.build();
142+
}
143+
144+
public static class BigQueryHelperException extends RuntimeException {
145+
146+
private static final long serialVersionUID = 3984993496060055562L;
147+
148+
public BigQueryHelperException(String message, Throwable cause) {
149+
super(message, cause);
150+
}
151+
152+
public static BigQueryHelperException translate(Exception ex) {
153+
return new BigQueryHelperException(ex.getMessage(), ex);
154+
}
155+
}
156+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* A testing helper for Google BigQuery.
19+
*
20+
* <p>A simple usage example:
21+
* <p>Before the test:
22+
* <pre> {@code
23+
* RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create();
24+
* BigQuery bigquery = bigqueryHelper.options().service();
25+
* String dataset = RemoteBigQueryHelper.generateDatasetName();
26+
* bigquery.create(DatasetInfo.builder(dataset).build());
27+
* } </pre>
28+
*
29+
* <p>After the test:
30+
* <pre> {@code
31+
* RemoteBigQueryHelper.forceDelete(bigquery, DATASET);
32+
* }</pre>
33+
*
34+
* @see <a href="https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-bigquery">
35+
* gcloud-java tools for testing</a>
36+
*/
37+
package com.google.gcloud.bigquery.testing;

0 commit comments

Comments
 (0)