Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration test and example jar. #714

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.google.common.collect.Sets;
import com.google.gcloud.Page;
import com.google.gcloud.Service;
import com.google.gcloud.spi.BigQueryRpc;
import com.google.gcloud.bigquery.spi.BigQueryRpc;

import java.util.List;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.google.gcloud.PageImpl.NextPageFetcher;
import com.google.gcloud.RetryHelper;
import com.google.gcloud.bigquery.InsertAllRequest.RowToInsert;
import com.google.gcloud.spi.BigQueryRpc;
import com.google.gcloud.bigquery.spi.BigQueryRpc;

import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import com.google.common.collect.ImmutableSet;
import com.google.gcloud.ServiceOptions;
import com.google.gcloud.spi.BigQueryRpc;
import com.google.gcloud.spi.BigQueryRpcFactory;
import com.google.gcloud.spi.DefaultBigQueryRpc;
import com.google.gcloud.bigquery.spi.BigQueryRpc;
import com.google.gcloud.bigquery.spi.BigQueryRpcFactory;
import com.google.gcloud.bigquery.spi.DefaultBigQueryRpc;

import java.util.Set;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.MoreObjects;
import com.google.gcloud.spi.BigQueryRpc;
import com.google.gcloud.bigquery.spi.BigQueryRpc;

import java.io.Serializable;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.google.gcloud.spi;
package com.google.gcloud.bigquery.spi;

import com.google.api.services.bigquery.model.Dataset;
import com.google.api.services.bigquery.model.GetQueryResultsResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
*/

package com.google.gcloud.spi;
package com.google.gcloud.bigquery.spi;

import com.google.gcloud.bigquery.BigQueryOptions;
import com.google.gcloud.spi.ServiceRpcFactory;

/**
* An interface for BigQuery RPC factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
* the License.
*/

package com.google.gcloud.spi;
package com.google.gcloud.bigquery.spi;

import static com.google.gcloud.spi.BigQueryRpc.Option.DELETE_CONTENTS;
import static com.google.gcloud.spi.BigQueryRpc.Option.FIELDS;
import static com.google.gcloud.spi.BigQueryRpc.Option.MAX_RESULTS;
import static com.google.gcloud.spi.BigQueryRpc.Option.PAGE_TOKEN;
import static com.google.gcloud.spi.BigQueryRpc.Option.START_INDEX;
import static com.google.gcloud.spi.BigQueryRpc.Option.TIMEOUT;
import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.ALL_DATASETS;
import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.ALL_USERS;
import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.DELETE_CONTENTS;
import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.FIELDS;
import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.MAX_RESULTS;
import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.PAGE_TOKEN;
import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.START_INDEX;
import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.STATE_FILTER;
import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.TIMEOUT;
import static java.net.HttpURLConnection.HTTP_CREATED;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_OK;
Expand Down Expand Up @@ -110,9 +113,10 @@ public Tuple<String, Iterable<Dataset>> listDatasets(Map<Option, ?> options) {
try {
DatasetList datasetsList = bigquery.datasets()
.list(this.options.projectId())
.setAll(Option.ALL_DATASETS.getBoolean(options))
.setAll(ALL_DATASETS.getBoolean(options))
.setMaxResults(MAX_RESULTS.getLong(options))
.setPageToken(PAGE_TOKEN.getString(options))
.setPageToken(PAGE_TOKEN.getString(options))
.execute();
Iterable<DatasetList.Datasets> datasets = datasetsList.getDatasets();
return Tuple.of(datasetsList.getNextPageToken(),
Expand Down Expand Up @@ -322,9 +326,9 @@ public Tuple<String, Iterable<Job>> listJobs(Map<Option, ?> options) {
try {
JobList jobsList = bigquery.jobs()
.list(this.options.projectId())
.setAllUsers(Option.ALL_USERS.getBoolean(options))
.setFields(Option.FIELDS.getString(options))
.setStateFilter(Option.STATE_FILTER.<List<String>>get(options))
.setAllUsers(ALL_USERS.getBoolean(options))
.setFields(FIELDS.getString(options))
.setStateFilter(STATE_FILTER.<List<String>>get(options))
.setMaxResults(MAX_RESULTS.getLong(options))
.setPageToken(PAGE_TOKEN.getString(options))
.setProjection(DEFAULT_PROJECTION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
import com.google.gcloud.RetryParams;
import com.google.gcloud.WriteChannel;
import com.google.gcloud.bigquery.InsertAllRequest.RowToInsert;
import com.google.gcloud.spi.BigQueryRpc;
import com.google.gcloud.spi.BigQueryRpc.Tuple;
import com.google.gcloud.spi.BigQueryRpcFactory;
import com.google.gcloud.bigquery.spi.BigQueryRpc;
import com.google.gcloud.bigquery.spi.BigQueryRpc.Tuple;
import com.google.gcloud.bigquery.spi.BigQueryRpcFactory;

import org.easymock.Capture;
import org.easymock.EasyMock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static org.junit.Assert.assertEquals;

import com.google.gcloud.spi.BigQueryRpc;
import com.google.gcloud.bigquery.spi.BigQueryRpc;

import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@

import com.google.gcloud.RestorableState;
import com.google.gcloud.WriteChannel;
import com.google.gcloud.spi.BigQueryRpc;
import com.google.gcloud.spi.BigQueryRpcFactory;
import com.google.gcloud.bigquery.spi.BigQueryRpc;
import com.google.gcloud.bigquery.spi.BigQueryRpcFactory;

import org.easymock.Capture;
import org.easymock.CaptureType;
Expand Down
58 changes: 36 additions & 22 deletions gcloud-java-contrib/gcloud-java-nio/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
<parent>
<groupId>com.google.gcloud</groupId>
<artifactId>gcloud-java-contrib</artifactId>
<version>0.1.4-SNAPSHOT</version>
<version>0.1.6-SNAPSHOT</version>
</parent>
<properties>
<site.installationModule>nio</site.installationModule>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gcloud-java</artifactId>
<artifactId>gcloud-java-storage</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand Down Expand Up @@ -92,26 +92,40 @@
<compilerArgument>-Xlint:unchecked</compilerArgument>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<addMavenDescriptor>true</addMavenDescriptor>
<index>true</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<buildNumber>${buildNumber}</buildNumber>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!-- shade the jar so we can demo adding the NIO jar to add functionality. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<relocations>
<relocation>
<pattern>com</pattern>
<shadedPattern>shaded.gcloud-nio.com</shadedPattern>
<excludes>
<exclude>com.google.gcloud.**</exclude>
</excludes>
</relocation>
<relocation>
<pattern>org</pattern>
<shadedPattern>shaded.gcloud-nio.org</shadedPattern>
</relocation>
<relocation>
<pattern>google</pattern>

This comment was marked as spam.

This comment was marked as spam.

<shadedPattern>shaded.gcloud-nio.google</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public CloudStorageConfiguration build() {
Builder() {}
}

static final CloudStorageConfiguration DEFAULT = builder().build();
public static final CloudStorageConfiguration DEFAULT = builder().build();

static CloudStorageConfiguration fromMap(Map<String, ?> env) {
Builder builder = builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ImmutableSet;
import com.google.gcloud.storage.StorageOptions;

import java.io.IOException;
import java.net.URI;
Expand All @@ -18,6 +19,7 @@
import java.util.Objects;
import java.util.Set;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
Expand All @@ -44,7 +46,7 @@ public final class CloudStorageFileSystem extends FileSystem {
* provides a simpler alternative.
*
* @see #forBucket(String, CloudStorageConfiguration)
* @see java.nio.file.FileSystems#getFileSystem(java.net.URI)
* @see java.nio.file.FileSystems#getFileSystem(URI)
*/
public static CloudStorageFileSystem forBucket(String bucket) {
return forBucket(bucket, CloudStorageConfiguration.DEFAULT);
Expand All @@ -56,10 +58,21 @@ public static CloudStorageFileSystem forBucket(String bucket) {
* @see #forBucket(String)

This comment was marked as spam.

This comment was marked as spam.

*/
public static CloudStorageFileSystem forBucket(String bucket, CloudStorageConfiguration config) {
checkArgument(
!bucket.startsWith(URI_SCHEME + ":"), "Bucket name must not have schema: %s", bucket);
return new CloudStorageFileSystem(
new CloudStorageFileSystemProvider(), bucket, checkNotNull(config));
return forBucket(bucket, config, null);
}

/**
* Creates a new filesystem for a particular bucket, with customizable settings and storage
* options.
*
* @see #forBucket(String)
*/
public static CloudStorageFileSystem forBucket(String bucket, CloudStorageConfiguration config,
@Nullable StorageOptions storageOptions) {
checkArgument(!bucket.startsWith(URI_SCHEME + ":"),
"Bucket name must not have schema: %s", bucket);
return new CloudStorageFileSystem(new CloudStorageFileSystemProvider(storageOptions),
bucket, checkNotNull(config));
}

public static final String URI_SCHEME = "gs";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ public final class CloudStorageFileSystemProvider extends FileSystemProvider {
private final Storage storage;

// used only when we create a new instance of CloudStorageFileSystemProvider.
private static StorageOptions storageOptions;
private static StorageOptions defaultStorageOptions;

This comment was marked as spam.


/**
* Sets options that are only used by the constructor.
* Sets default options that are only used by the constructor.
*/
@VisibleForTesting
public static void setGCloudOptions(StorageOptions newStorageOptions) {
storageOptions = newStorageOptions;
defaultStorageOptions = newStorageOptions;

This comment was marked as spam.

This comment was marked as spam.

}

/**
Expand All @@ -83,14 +83,19 @@ public static void setGCloudOptions(StorageOptions newStorageOptions) {
* @see CloudStorageFileSystem#forBucket(String)
*/
public CloudStorageFileSystemProvider() {
this(storageOptions);
this(defaultStorageOptions);
}

private CloudStorageFileSystemProvider(@Nullable StorageOptions gcsStorageOptions) {
if (gcsStorageOptions == null) {
this.storage = StorageOptions.defaultInstance().service();
CloudStorageFileSystemProvider(@Nullable StorageOptions explicitOptions) {
// explicit options have priority over default options.
if (explicitOptions == null) {
if (defaultStorageOptions == null) {
this.storage = StorageOptions.defaultInstance().service();
} else {
this.storage = defaultStorageOptions.service();
}
} else {
this.storage = gcsStorageOptions.service();
this.storage = explicitOptions.service();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
import com.google.gcloud.storage.StorageOptions;
import com.google.gcloud.storage.testing.LocalGcsHelper;

import org.junit.Before;
Expand Down Expand Up @@ -111,7 +112,8 @@ public void testNullness() throws IOException, NoSuchMethodException, SecurityEx
NullPointerTester tester =
new NullPointerTester()
.ignore(CloudStorageFileSystem.class.getMethod("equals", Object.class))
.setDefault(CloudStorageConfiguration.class, CloudStorageConfiguration.DEFAULT);
.setDefault(CloudStorageConfiguration.class, CloudStorageConfiguration.DEFAULT)
.setDefault(StorageOptions.class, LocalGcsHelper.options());
tester.testAllPublicStaticMethods(CloudStorageFileSystem.class);
tester.testAllPublicInstanceMethods(fs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.mockito.Mockito.when;

import com.google.gcloud.ReadChannel;
import com.google.gcloud.storage.Blob;
import com.google.gcloud.storage.BlobId;
import com.google.gcloud.storage.BlobInfo;
import com.google.gcloud.storage.Storage;
Expand Down Expand Up @@ -39,11 +40,12 @@ public class CloudStorageReadChannelTest {

private final Storage gcsStorage = mock(Storage.class);
private final BlobId file = BlobId.of("blob", "attack");
private final BlobInfo metadata = BlobInfo.builder(file).size(42L).build();
private Blob metadata = mock(Blob.class);
private final ReadChannel gcsChannel = mock(ReadChannel.class);

@Before
public void before() throws IOException {
when(metadata.size()).thenReturn(42L);
when(gcsStorage.get(file)).thenReturn(metadata);
when(gcsStorage.reader(eq(file))).thenReturn(gcsChannel);
when(gcsChannel.isOpen()).thenReturn(true);
Expand All @@ -61,7 +63,6 @@ public void testRead() throws IOException {
assertThat(chan.position()).isEqualTo(1L);
verify(gcsChannel).read(any(ByteBuffer.class));
verify(gcsChannel, times(3)).isOpen();
verifyNoMoreInteractions(gcsStorage, gcsChannel);
}

@Test
Expand Down Expand Up @@ -91,15 +92,13 @@ public void testIsOpen() throws IOException {
assertThat(chan.isOpen()).isFalse();
verify(gcsChannel, times(2)).isOpen();
verify(gcsChannel).close();
verifyNoMoreInteractions(gcsStorage, gcsChannel);
}

@Test
public void testSize() throws IOException {
assertThat(chan.size()).isEqualTo(42L);
verify(gcsChannel).isOpen();
verifyZeroInteractions(gcsChannel);
verifyNoMoreInteractions(gcsStorage);
}

@Test
Expand Down Expand Up @@ -139,6 +138,5 @@ public void testSetPosition() throws IOException {
assertThat(chan.size()).isEqualTo(42L);
verify(gcsChannel).seek(1);
verify(gcsChannel, times(5)).isOpen();
verifyNoMoreInteractions(gcsStorage, gcsChannel);
}
}
Loading