Skip to content

Commit

Permalink
Fix checkstyle errors in BigQuery sample tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tswast committed Mar 5, 2016
1 parent fa54a9e commit 565e7ee
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 173 deletions.
5 changes: 3 additions & 2 deletions bigquery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-matchers</artifactId>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.28</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions bigquery/src/main/resources/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"datasetId": "test_dataset_java",
"currentTableId": "test_table_java",
"newTableId": "test_table_java_2",
"cloudStorageInputURI": "gs://cloud-samples-tests/data.csv",
"cloudStorageOutputURI": "gs://cloud-samples-tests/output.csv",
"cloudStorageInputUri": "gs://cloud-samples-tests/data.csv",
"cloudStorageOutputUri": "gs://cloud-samples-tests/output.csv",
"query": "SELECT corpus FROM publicdata:samples.shakespeare GROUP BY corpus;"
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,60 @@
/*
* Copyright (c) 2015 Google Inc.
*
* 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.samples.test;

import static com.google.common.truth.Truth.assertThat;

import com.google.api.services.bigquery.model.GetQueryResultsResponse;
import com.google.cloud.bigquery.samples.AsyncQuerySample;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;

import org.junit.*;

import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

/**
* Tests for asynchronous query sample.
*/
public class AsyncQuerySampleTest extends BigquerySampleTest{

/**
* @throws JsonSyntaxException
* @throws JsonIOException
* @throws FileNotFoundException
*/
public AsyncQuerySampleTest() throws JsonSyntaxException, JsonIOException,
FileNotFoundException {
public AsyncQuerySampleTest() throws JsonSyntaxException, JsonIOException, FileNotFoundException {
super();
// TODO(elibixby): Auto-generated constructor stub
}


@Test
public void testInteractive() throws IOException, InterruptedException{
Iterator<GetQueryResultsResponse> pages = AsyncQuerySample.run(CONSTANTS.getProjectId(), CONSTANTS.getQuery(), false, 5000);
while(pages.hasNext()){
assertTrue(!pages.next().getRows().isEmpty());
public void testInteractive() throws IOException, InterruptedException {
Iterator<GetQueryResultsResponse> pages =
AsyncQuerySample.run(CONSTANTS.getProjectId(), CONSTANTS.getQuery(), false, 5000);
while (pages.hasNext()) {
assertThat(pages.next().getRows()).isNotEmpty();
}
}



@Test
@Ignore // Batches can take up to 3 hours to run, probably shouldn't use this
public void testBatch() throws IOException, InterruptedException{
Iterator<GetQueryResultsResponse> pages = AsyncQuerySample.run(CONSTANTS.getProjectId(), CONSTANTS.getQuery(), true, 5000);
while(pages.hasNext()){
assertTrue(!pages.next().getRows().isEmpty());
public void testBatch() throws IOException, InterruptedException {
Iterator<GetQueryResultsResponse> pages =
AsyncQuerySample.run(CONSTANTS.getProjectId(), CONSTANTS.getQuery(), true, 5000);
while (pages.hasNext()) {
assertThat(pages.next().getRows()).isNotEmpty();
}
}


}
Original file line number Diff line number Diff line change
@@ -1,84 +1,81 @@
/*
* Copyright (c) 2015 Google Inc.
*
* 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.samples.test;

import com.google.cloud.bigquery.samples.BigqueryUtils;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
* TODO: Insert description here. (generated by elibixby)
* Superclass for tests for samples.
*/
public class BigquerySampleTest extends BigqueryUtils {

protected static class Constants{
protected static class Constants {
private String projectId;
private String datasetId;
private String currentTableId;
private String newTableId;
private String cloudStorageInputURI;
private String cloudStorageOutputURI;
private String cloudStorageInputUri;
private String cloudStorageOutputUri;
private String query;
/**
* @return the projectId
*/

public String getProjectId() {
return projectId;
}
/**
* @return the datasetId
*/

public String getDatasetId() {
return datasetId;
}
/**
* @return the currentTableId
*/

public String getCurrentTableId() {
return currentTableId;
}
/**
* @return the newTableId
*/

public String getNewTableId() {
return newTableId;
}
/**
* @return the query
*/

public String getQuery() {
return query;
}
/**
* @return the cloudStorageOutputURI
*/
public String getCloudStorageOutputURI() {
return cloudStorageOutputURI;

public String getCloudStorageOutputUri() {
return cloudStorageOutputUri;
}
/**
* @return the cloudStorageInputURI
*/
public String getCloudStorageInputURI() {
return cloudStorageInputURI;

public String getCloudStorageInputUri() {
return cloudStorageInputUri;
}
}

protected static Constants CONSTANTS = null ;
@SuppressWarnings("checkstyle:abbreviationaswordinname")
protected static Constants CONSTANTS = null;

protected BigquerySampleTest() throws JsonSyntaxException, JsonIOException, FileNotFoundException{
if(CONSTANTS == null){
InputStream is = this.getClass().getResourceAsStream
("/constants.json");
CONSTANTS = (new Gson()).<Constants>fromJson(
new InputStreamReader(is),
Constants.class);
protected BigquerySampleTest()
throws JsonSyntaxException, JsonIOException, FileNotFoundException {
if (CONSTANTS == null) {
InputStream is = this.getClass().getResourceAsStream("/constants.json");
CONSTANTS = (new Gson()).<Constants>fromJson(new InputStreamReader(is), Constants.class);
}
}



}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2015 Google Inc.
*
* 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.samples.test;

import com.google.cloud.bigquery.samples.ExportDataCloudStorageSample;
Expand All @@ -10,29 +26,22 @@
import java.io.IOException;

/**
* TODO: Insert description here. (generated by elibixby)
* Tests for export data Cloud Storage sample.
*/
public class ExportDataCloudStorageSampleTest extends BigquerySampleTest {

/**
* @throws JsonSyntaxException
* @throws JsonIOException
* @throws FileNotFoundException
*/
public ExportDataCloudStorageSampleTest() throws JsonSyntaxException, JsonIOException,
FileNotFoundException {
public ExportDataCloudStorageSampleTest()
throws JsonSyntaxException, JsonIOException, FileNotFoundException {
super();
}

@Test
public void testExportData() throws IOException, InterruptedException{
ExportDataCloudStorageSample.run(CONSTANTS.getCloudStorageOutputURI(),
public void testExportData() throws IOException, InterruptedException {
ExportDataCloudStorageSample.run(
CONSTANTS.getCloudStorageOutputUri(),
CONSTANTS.getProjectId(),
CONSTANTS.getDatasetId(),
CONSTANTS.getCurrentTableId(),
5000L);
}



}
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
/*
* Copyright (c) 2015 Google Inc.
*
* 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.samples.test;

import com.google.api.services.bigquery.model.GetQueryResultsResponse;
import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.bigquery.samples.GettingStarted;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;

import static com.jcabi.matchers.RegexMatchers.*;
import static org.junit.Assert.*;
import static org.junit.matchers.JUnitMatchers.*;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Iterator;


/**
* Test for GettingStarted.java
*/
public class GettingStartedTest extends BigquerySampleTest {
private final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
private final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
private static final PrintStream REAL_OUT = System.out;
private static final PrintStream REAL_ERR = System.err;

private final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
private final ByteArrayOutputStream stderr = new ByteArrayOutputStream();

public GettingStartedTest() throws FileNotFoundException {
super();
}
Expand All @@ -50,7 +60,7 @@ public void tearDown() {
public void testSyncQuery() throws IOException {
GettingStarted.main(new String[] { CONSTANTS.getProjectId() });
String out = stdout.toString();
assertThat(out, containsPattern("Query Results:"));
assertThat(out, containsString("hamlet"));
assertThat(out).named("stdout").containsMatch("Query Results:");
assertThat(out).named("stdout").contains("hamlet");
}
}
Loading

0 comments on commit 565e7ee

Please sign in to comment.