Skip to content

Commit

Permalink
Merge pull request #492 from mziccard/bigquery
Browse files Browse the repository at this point in the history
Add RemoteBigQueryHelper and integration tests
  • Loading branch information
aozarov committed Dec 21, 2015
2 parents ec115cb + 7eee528 commit 3781aa7
Show file tree
Hide file tree
Showing 11 changed files with 1,069 additions and 9 deletions.
8 changes: 7 additions & 1 deletion gcloud-java-bigquery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@
<artifactId>gcloud-java-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gcloud-java-storage</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-bigquery</artifactId>
<version>v2-rev244-1.20.0</version>
<version>v2-rev254-1.21.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ Access toPb() {
}

/**
* Class for a BigQuery Group entity. Objects of this class represent a group to grante access to.
* A Group entity can be created given the group's email or can be a special group:
* Class for a BigQuery Group entity. Objects of this class represent a group to granted access
* to. A Group entity can be created given the group's email or can be a special group:
* {@link #ofProjectOwners()}, {@link #ofProjectReaders()}, {@link #ofProjectWriters()} or
* {@link #ofAllAuthenticatedUsers()}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public enum State {
/**
* Returns the state of the job. A {@link State#PENDING} job is waiting to be executed. A
* {@link State#RUNNING} is being executed. A {@link State#DONE} job has completed either
* suceeding or failing. If failed {@link #error()} will be non-null.
* succeeding or failing. If failed {@link #error()} will be non-null.
*/
public State state() {
return state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public Builder priority(Priority priority) {
* the query is allowed to create large results at a slight cost in performance. If {@code true}
* {@link Builder#destinationTable(TableId)} must be provided.
*
* @see <a href="https://cloud.google.com/bigquery/querying-data#largequeryresults>
* @see <a href="https://cloud.google.com/bigquery/querying-data#largequeryresults">
* Returning Large Query Results</a>
*/
public Builder allowLargeResults(Boolean allowLargeResults) {
Expand Down Expand Up @@ -309,7 +309,7 @@ private QueryJobInfo(Builder builder) {
* the query is allowed to create large results at a slight cost in performance.
* the query is allowed to create large results at a slight cost in performance.
*
* @see <a href="https://cloud.google.com/bigquery/querying-data#largequeryresults>
* @see <a href="https://cloud.google.com/bigquery/querying-data#largequeryresults">
* Returning Large Query Results</a>
*/
public Boolean allowLargeResults() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public static Builder builder(TableId tableId, Schema schema) {
* @param tableId table id
* @param schema the schema of the table
*/
public static BaseTableInfo of(TableId tableId, Schema schema) {
public static TableInfo of(TableId tableId, Schema schema) {
return builder(tableId, schema).build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* 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.gcloud.bigquery.testing;

import com.google.gcloud.AuthCredentials;
import com.google.gcloud.RetryParams;
import com.google.gcloud.bigquery.BigQuery;
import com.google.gcloud.bigquery.BigQueryException;
import com.google.gcloud.bigquery.BigQueryOptions;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Utility to create a remote BigQuery configuration for testing. BigQuery options can be obtained
* via the {@link #options()} method. Returned options have custom
* {@link BigQueryOptions#retryParams()}: {@link RetryParams#retryMaxAttempts()} is {@code 10},
* {@link RetryParams#retryMinAttempts()} is {@code 6}, {@link RetryParams#maxRetryDelayMillis()} is
* {@code 30000}, {@link RetryParams#totalRetryPeriodMillis()} is {@code 120000} and
* {@link RetryParams#initialRetryDelayMillis()} is {@code 250}.
* {@link BigQueryOptions#connectTimeout()} and {@link BigQueryOptions#readTimeout()} are both set
* to {@code 60000}.
*/
public class RemoteBigQueryHelper {

private static final Logger log = Logger.getLogger(RemoteBigQueryHelper.class.getName());
private static final String DATASET_NAME_PREFIX = "gcloud_test_dataset_temp_";
private final BigQueryOptions options;

private RemoteBigQueryHelper(BigQueryOptions options) {
this.options = options;
}

/**
* Returns a {@link BigQueryOptions} object to be used for testing.
*/
public BigQueryOptions options() {
return options;
}

/**
* Deletes a dataset, even if non-empty.
*
* @param bigquery the BigQuery service to be used to issue the delete request
* @param dataset the dataset to be deleted
* @return {@code true} if deletion succeeded, {@code false} if the dataset was not found.
* @throws BigQueryException upon failure
*/
public static boolean forceDelete(BigQuery bigquery, String dataset) {
return bigquery.delete(dataset, BigQuery.DatasetDeleteOption.deleteContents());
}

/**
* Returns a dataset name generated using a random UUID.
*/
public static String generateDatasetName() {
return DATASET_NAME_PREFIX + UUID.randomUUID().toString().replace('-', '_');
}

/**
* Creates a {@code RemoteBigQueryHelper} object for the given project id and JSON key input
* stream.
*
* @param projectId id of the project to be used for running the tests
* @param keyStream input stream for a JSON key
* @return A {@code RemoteBigQueryHelper} object for the provided options.
* @throws BigQueryHelperException if {@code keyStream} is not a valid JSON key stream
*/
public static RemoteBigQueryHelper create(String projectId, InputStream keyStream)
throws BigQueryHelperException {
try {
BigQueryOptions bigqueryOptions = BigQueryOptions.builder()
.authCredentials(AuthCredentials.createForJson(keyStream))
.projectId(projectId)
.retryParams(retryParams())
.connectTimeout(60000)
.readTimeout(60000)
.build();
return new RemoteBigQueryHelper(bigqueryOptions);
} catch (IOException ex) {
if (log.isLoggable(Level.WARNING)) {
log.log(Level.WARNING, ex.getMessage());
}
throw BigQueryHelperException.translate(ex);
}
}

/**
* Creates a {@code RemoteBigQueryHelper} object using default project id and authentication
* credentials.
*/
public static RemoteBigQueryHelper create() {
BigQueryOptions bigqueryOptions = BigQueryOptions.builder()
.retryParams(retryParams())
.connectTimeout(60000)
.readTimeout(60000)
.build();
return new RemoteBigQueryHelper(bigqueryOptions);
}

private static RetryParams retryParams() {
return RetryParams.builder()
.retryMaxAttempts(10)
.retryMinAttempts(6)
.maxRetryDelayMillis(30000)
.totalRetryPeriodMillis(120000)
.initialRetryDelayMillis(250)
.build();
}

public static class BigQueryHelperException extends RuntimeException {

private static final long serialVersionUID = 3984993496060055562L;

public BigQueryHelperException(String message, Throwable cause) {
super(message, cause);
}

public static BigQueryHelperException translate(Exception ex) {
return new BigQueryHelperException(ex.getMessage(), ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.
*/

/**
* A testing helper for Google BigQuery.
*
* <p>A simple usage example:
*
* <p>Before the test:
* <pre> {@code
* RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create();
* BigQuery bigquery = bigqueryHelper.options().service();
* String dataset = RemoteBigQueryHelper.generateDatasetName();
* bigquery.create(DatasetInfo.builder(dataset).build());
* } </pre>
*
* <p>After the test:
* <pre> {@code
* RemoteBigQueryHelper.forceDelete(bigquery, DATASET);
* }</pre>
*
* @see <a href="https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-bigquery">
* gcloud-java tools for testing</a>
*/
package com.google.gcloud.bigquery.testing;
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.gcloud.AuthCredentials;
import com.google.gcloud.Page;
import com.google.gcloud.RetryParams;
import com.google.gcloud.bigquery.InsertAllRequest.RowToInsert;
Expand Down Expand Up @@ -860,7 +859,6 @@ public Job apply(JobInfo jobInfo) {
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(jobList.toArray(), Iterables.toArray(page.values(), JobInfo.class));
String selector = (String) capturedOptions.getValue().get(JOB_OPTION_FIELDS.rpcOption());
System.out.println(selector);
assertTrue(selector.contains("etag,jobs("));
assertTrue(selector.contains("configuration"));
assertTrue(selector.contains("jobReference"));
Expand Down
Loading

0 comments on commit 3781aa7

Please sign in to comment.