diff --git a/gcloud-java-core/pom.xml b/gcloud-java-core/pom.xml index 0a426d50a21b..50155634bd11 100644 --- a/gcloud-java-core/pom.xml +++ b/gcloud-java-core/pom.xml @@ -106,7 +106,7 @@ com.google.api gax - 0.0.14 + 0.0.16 com.google.api.grpc diff --git a/gcloud-java-vision/pom.xml b/gcloud-java-vision/pom.xml new file mode 100644 index 000000000000..4cd176d1e86e --- /dev/null +++ b/gcloud-java-vision/pom.xml @@ -0,0 +1,126 @@ + + + 4.0.0 + gcloud-java-vision + jar + GCloud Java Vision + https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-vision + + Java idiomatic client for Google Cloud Vision API. + + + com.google.cloud + gcloud-java-pom + 0.2.9-SNAPSHOT + + + gcloud-java-vision + + + + io.netty + netty-tcnative-boringssl-static + 1.1.33.Fork17 + + + ${project.groupId} + gcloud-java-core + ${project.version} + + + com.google.api.grpc + grpc-google-cloud-vision-v1 + 0.0.7 + + + io.grpc + grpc-all + 0.15.0 + + + com.google.auto.value + auto-value + 1.1 + + + ${project.groupId} + gcloud-java-core + ${project.version} + test-jar + test + + + junit + junit + 4.12 + test + + + org.easymock + easymock + 3.4 + test + + + + + doclint-java8-disable + + [1.8,) + + + + -Xdoclint:none + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.9.1 + + + generate-sources + add-source + + + generated/src/main/java + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.3 + + + attach-javadocs + + jar + + + ${javadoc.opts} + + + + + + maven-compiler-plugin + + + + 3.1 + + 1.7 + 1.7 + UTF-8 + -Xlint:unchecked + + + + + diff --git a/gcloud-java-vision/src/main/java/com/google/cloud/vision/spi/v1/ImageAnnotatorApi.java b/gcloud-java-vision/src/main/java/com/google/cloud/vision/spi/v1/ImageAnnotatorApi.java new file mode 100644 index 000000000000..ed0fedf62399 --- /dev/null +++ b/gcloud-java-vision/src/main/java/com/google/cloud/vision/spi/v1/ImageAnnotatorApi.java @@ -0,0 +1,222 @@ +/* + * Copyright 2016 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.vision.spi.v1; + +import com.google.api.gax.grpc.ApiCallable; +import com.google.api.gax.protobuf.PathTemplate; +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import io.grpc.ManagedChannel; +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; + +// AUTO-GENERATED DOCUMENTATION AND SERVICE +/** + * Service Description: Service that performs Google Cloud Vision API detection tasks, such as face, + * landmark, logo, label, and text detection, over client images, and returns + * detected entities from the images. + * + *

This class provides the ability to make remote calls to the backing service through method + * calls that map to API methods. Sample code to get started: + * + *

+ * 
+ * try (ImageAnnotatorApi imageAnnotatorApi = ImageAnnotatorApi.create()) {
+ *   List<AnnotateImageRequest> requests = new ArrayList<>();
+ *   BatchAnnotateImagesResponse response = imageAnnotatorApi.batchAnnotateImages(requests);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the imageAnnotatorApi object to clean up resources such + * as threads. In the example above, try-with-resources is used, which automatically calls + * close(). + * + *

The surface of this class includes several types of Java methods for each of the API's methods: + * + *

    + *
  1. A "flattened" method. With this type of method, the fields of the request type have been + * converted into function parameters. It may be the case that not all fields are available + * as parameters, and not every API method will have a flattened method entry point. + *
  2. A "request object" method. This type of method only takes one parameter, a request + * object, which must be constructed before the call. Not every API method will have a request + * object method. + *
  3. A "callable" method. This type of method takes no parameters and returns an immutable + * ApiCallable object, which can be used to initiate calls to the service. + *
+ * + *

See the individual methods for example code. + * + *

Many parameters require resource names to be formatted in a particular way. To assist + * with these names, this class includes a format method for each type of name, and additionally + * a parse method to extract the individual identifiers contained within names that are + * returned. + * + *

This class can be customized by passing in a custom instance of ImageAnnotatorSettings to + * create(). For example: + * + *

+ * 
+ * ImageAnnotatorSettings imageAnnotatorSettings = ImageAnnotatorSettings.defaultBuilder()
+ *     .provideChannelWith(myCredentials)
+ *     .build();
+ * ImageAnnotatorApi imageAnnotatorApi = ImageAnnotatorApi.create(imageAnnotatorSettings);
+ * 
+ * 
+ */ +@javax.annotation.Generated("by GAPIC") +public class ImageAnnotatorApi implements AutoCloseable { + private final ImageAnnotatorSettings settings; + private final ManagedChannel channel; + private final ScheduledExecutorService executor; + private final List closeables = new ArrayList<>(); + + private final ApiCallable + batchAnnotateImagesCallable; + + public final ImageAnnotatorSettings getSettings() { + return settings; + } + + /** + * Constructs an instance of ImageAnnotatorApi with default settings. + */ + public static final ImageAnnotatorApi create() throws IOException { + return create(ImageAnnotatorSettings.defaultBuilder().build()); + } + + /** + * Constructs an instance of ImageAnnotatorApi, using the given settings. + * The channels are created based on the settings passed in, or defaults for any + * settings that are not set. + */ + public static final ImageAnnotatorApi create(ImageAnnotatorSettings settings) throws IOException { + return new ImageAnnotatorApi(settings); + } + + /** + * Constructs an instance of ImageAnnotatorApi, using the given settings. + * This is protected so that it easy to make a subclass, but otherwise, the static + * factory methods should be preferred. + */ + protected ImageAnnotatorApi(ImageAnnotatorSettings settings) throws IOException { + this.settings = settings; + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); + + this.batchAnnotateImagesCallable = + ApiCallable.create(settings.batchAnnotateImagesSettings(), this.channel, this.executor); + + if (settings.getChannelProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + channel.shutdown(); + } + }); + } + if (settings.getExecutorProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + executor.shutdown(); + } + }); + } + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Run image detection and annotation for a batch of images. + * + * Sample code: + *

+   * try (ImageAnnotatorApi imageAnnotatorApi = ImageAnnotatorApi.create()) {
+   *   List<AnnotateImageRequest> requests = new ArrayList<>();
+   *   BatchAnnotateImagesResponse response = imageAnnotatorApi.batchAnnotateImages(requests);
+   * }
+   * 
+ * + * @param requests Individual image annotation requests for this batch. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final BatchAnnotateImagesResponse batchAnnotateImages( + List requests) { + BatchAnnotateImagesRequest request = + BatchAnnotateImagesRequest.newBuilder().addAllRequests(requests).build(); + return batchAnnotateImages(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Run image detection and annotation for a batch of images. + * + * Sample code: + *

+   * try (ImageAnnotatorApi imageAnnotatorApi = ImageAnnotatorApi.create()) {
+   *   List<AnnotateImageRequest> requests = new ArrayList<>();
+   *   BatchAnnotateImagesRequest request = BatchAnnotateImagesRequest.newBuilder()
+   *     .addAllRequests(requests)
+   *     .build();
+   *   BatchAnnotateImagesResponse response = imageAnnotatorApi.batchAnnotateImages(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + private final BatchAnnotateImagesResponse batchAnnotateImages( + BatchAnnotateImagesRequest request) { + return batchAnnotateImagesCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Run image detection and annotation for a batch of images. + * + * Sample code: + *

+   * try (ImageAnnotatorApi imageAnnotatorApi = ImageAnnotatorApi.create()) {
+   *   List<AnnotateImageRequest> requests = new ArrayList<>();
+   *   BatchAnnotateImagesRequest request = BatchAnnotateImagesRequest.newBuilder()
+   *     .addAllRequests(requests)
+   *     .build();
+   *   ListenableFuture<BatchAnnotateImagesResponse> future = imageAnnotatorApi.batchAnnotateImagesCallable().futureCall(request);
+   *   // Do something
+   *   BatchAnnotateImagesResponse response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable + batchAnnotateImagesCallable() { + return batchAnnotateImagesCallable; + } + + /** + * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately + * cancelled. + */ + @Override + public final void close() throws Exception { + for (AutoCloseable closeable : closeables) { + closeable.close(); + } + } +} diff --git a/gcloud-java-vision/src/main/java/com/google/cloud/vision/spi/v1/ImageAnnotatorSettings.java b/gcloud-java-vision/src/main/java/com/google/cloud/vision/spi/v1/ImageAnnotatorSettings.java new file mode 100644 index 000000000000..3828d661041a --- /dev/null +++ b/gcloud-java-vision/src/main/java/com/google/cloud/vision/spi/v1/ImageAnnotatorSettings.java @@ -0,0 +1,298 @@ +/* + * Copyright 2016 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.vision.spi.v1; + +import com.google.api.gax.core.ConnectionSettings; +import com.google.api.gax.core.RetrySettings; +import com.google.api.gax.grpc.ApiCallSettings; +import com.google.api.gax.grpc.PageStreamingDescriptor; +import com.google.api.gax.grpc.ServiceApiSettings; +import com.google.api.gax.grpc.SimpleCallSettings; +import com.google.auth.Credentials; +import com.google.cloud.vision.v1.BatchAnnotateImagesRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.ImageAnnotatorGrpc; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import io.grpc.ManagedChannel; +import io.grpc.Status; +import java.io.IOException; +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; +import org.joda.time.Duration; + +// AUTO-GENERATED DOCUMENTATION AND CLASS +/** + * Settings class to configure an instance of {@link ImageAnnotatorApi}. + * + *

The default instance has everything set to sensible defaults: + * + *

    + *
  • The default service address (vision.googleapis.com) and default port (443) + * are used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ * + *

The builder of this class is recursive, so contained classes are themselves builders. + * When build() is called, the tree of builders is called to create the complete settings + * object. For example, to set the total timeout of batchAnnotateImages to 30 seconds: + * + *

+ * 
+ * ImageAnnotatorSettings.Builder imageAnnotatorSettingsBuilder =
+ *     ImageAnnotatorSettings.defaultBuilder();
+ * imageAnnotatorSettingsBuilder.batchAnnotateImagesSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * ImageAnnotatorSettings imageAnnotatorSettings = imageAnnotatorSettingsBuilder.build();
+ * 
+ * 
+ */ +@javax.annotation.Generated("by GAPIC") +public class ImageAnnotatorSettings extends ServiceApiSettings { + /** + * The default address of the service. + */ + private static final String DEFAULT_SERVICE_ADDRESS = "vision.googleapis.com"; + + /** + * The default port of the service. + */ + private static final int DEFAULT_SERVICE_PORT = 443; + + /** + * The default scopes of the service. + */ + private static final ImmutableList DEFAULT_SERVICE_SCOPES = + ImmutableList.builder().add("https://www.googleapis.com/auth/cloud-platform").build(); + + /** + * The default connection settings of the service. + */ + public static final ConnectionSettings DEFAULT_CONNECTION_SETTINGS = + ConnectionSettings.newBuilder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build(); + + private final SimpleCallSettings + batchAnnotateImagesSettings; + + /** + * Returns the object with the settings used for calls to batchAnnotateImages. + */ + public SimpleCallSettings + batchAnnotateImagesSettings() { + return batchAnnotateImagesSettings; + } + + /** + * Returns the default service address. + */ + public static String getDefaultServiceAddress() { + return DEFAULT_SERVICE_ADDRESS; + } + + /** + * Returns the default service port. + */ + public static int getDefaultServicePort() { + return DEFAULT_SERVICE_PORT; + } + + /** + * Returns the default service scopes. + */ + public static ImmutableList getDefaultServiceScopes() { + return DEFAULT_SERVICE_SCOPES; + } + + /** + * Returns a builder for this class with recommended defaults. + */ + public static Builder defaultBuilder() { + return Builder.createDefault(); + } + + /** + * Returns a new builder for this class. + */ + public static Builder newBuilder() { + return new Builder(); + } + + /** + * Returns a builder containing all the values of this settings class. + */ + public Builder toBuilder() { + return new Builder(this); + } + + private ImageAnnotatorSettings(Builder settingsBuilder) throws IOException { + super( + settingsBuilder.getChannelProvider(), + settingsBuilder.getExecutorProvider(), + settingsBuilder.getGeneratorName(), + settingsBuilder.getGeneratorVersion(), + settingsBuilder.getClientLibName(), + settingsBuilder.getClientLibVersion()); + + batchAnnotateImagesSettings = settingsBuilder.batchAnnotateImagesSettings().build(); + } + + /** + * Builder for ImageAnnotatorSettings. + */ + public static class Builder extends ServiceApiSettings.Builder { + private final ImmutableList methodSettingsBuilders; + + private SimpleCallSettings.Builder + batchAnnotateImagesSettings; + + private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; + + static { + ImmutableMap.Builder> definitions = ImmutableMap.builder(); + definitions.put( + "idempotent", + Sets.immutableEnumSet( + Lists.newArrayList( + Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); + definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); + RETRYABLE_CODE_DEFINITIONS = definitions.build(); + } + + private static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetrySettings.Builder settingsBuilder = null; + settingsBuilder = + RetrySettings.newBuilder() + .setInitialRetryDelay(Duration.millis(100L)) + .setRetryDelayMultiplier(1.3) + .setMaxRetryDelay(Duration.millis(60000L)) + .setInitialRpcTimeout(Duration.millis(60000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.millis(60000L)) + .setTotalTimeout(Duration.millis(600000L)); + definitions.put("default", settingsBuilder); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private Builder() { + super(DEFAULT_CONNECTION_SETTINGS); + + batchAnnotateImagesSettings = + SimpleCallSettings.newBuilder(ImageAnnotatorGrpc.METHOD_BATCH_ANNOTATE_IMAGES); + + methodSettingsBuilders = + ImmutableList.of(batchAnnotateImagesSettings); + } + + private static Builder createDefault() { + Builder builder = new Builder(); + + builder + .batchAnnotateImagesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + + private Builder(ImageAnnotatorSettings settings) { + super(settings); + + batchAnnotateImagesSettings = settings.batchAnnotateImagesSettings.toBuilder(); + + methodSettingsBuilders = + ImmutableList.of(batchAnnotateImagesSettings); + } + + @Override + protected ConnectionSettings getDefaultConnectionSettings() { + return DEFAULT_CONNECTION_SETTINGS; + } + + @Override + public Builder provideExecutorWith(ScheduledExecutorService executor, boolean shouldAutoClose) { + super.provideExecutorWith(executor, shouldAutoClose); + return this; + } + + @Override + public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { + super.provideChannelWith(channel, shouldAutoClose); + return this; + } + + @Override + public Builder provideChannelWith(ConnectionSettings settings) { + super.provideChannelWith(settings); + return this; + } + + @Override + public Builder provideChannelWith(Credentials credentials) { + super.provideChannelWith(credentials); + return this; + } + + @Override + public Builder provideChannelWith(List scopes) { + super.provideChannelWith(scopes); + return this; + } + + @Override + public Builder setGeneratorHeader(String name, String version) { + super.setGeneratorHeader(name, version); + return this; + } + + @Override + public Builder setClientLibHeader(String name, String version) { + super.setClientLibHeader(name, version); + return this; + } + + /** + * Applies the given settings to all of the API methods in this service. Only + * values that are non-null will be applied, so this method is not capable + * of un-setting any values. + */ + public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { + super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); + return this; + } + + /** + * Returns the builder for the settings used for calls to batchAnnotateImages. + */ + public SimpleCallSettings.Builder + batchAnnotateImagesSettings() { + return batchAnnotateImagesSettings; + } + + @Override + public ImageAnnotatorSettings build() throws IOException { + return new ImageAnnotatorSettings(this); + } + } +} diff --git a/gcloud-java-vision/src/main/java/com/google/cloud/vision/spi/v1/package-info.java b/gcloud-java-vision/src/main/java/com/google/cloud/vision/spi/v1/package-info.java new file mode 100644 index 000000000000..99f77552a7b2 --- /dev/null +++ b/gcloud-java-vision/src/main/java/com/google/cloud/vision/spi/v1/package-info.java @@ -0,0 +1,39 @@ +/* + * Copyright 2016 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 client to Google Cloud Vision API. + * + * The interfaces provided are listed below, along with a usage sample + * + * ================= + * ImageAnnotatorApi + * ================= + * + * Service Description: Service that performs Google Cloud Vision API detection tasks, such as face, + * landmark, logo, label, and text detection, over client images, and returns + * detected entities from the images. + * + * Sample for ImageAnnotatorApi: + *
+ * 
+ * try (ImageAnnotatorApi imageAnnotatorApi = ImageAnnotatorApi.create()) {
+ *   List<AnnotateImageRequest> requests = new ArrayList<>();
+ *   BatchAnnotateImagesResponse response = imageAnnotatorApi.batchAnnotateImages(requests);
+ * }
+ * 
+ * 
+ * + */ +package com.google.cloud.vision.spi.v1; diff --git a/gcloud-java-vision/src/test/java/com/google/cloud/vision/spi/v1/ImageAnnotatorTest.java b/gcloud-java-vision/src/test/java/com/google/cloud/vision/spi/v1/ImageAnnotatorTest.java new file mode 100644 index 000000000000..f25bc08b6036 --- /dev/null +++ b/gcloud-java-vision/src/test/java/com/google/cloud/vision/spi/v1/ImageAnnotatorTest.java @@ -0,0 +1,89 @@ +/* + * Copyright 2016 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.vision.spi.v1; + +import com.google.api.gax.core.PageAccessor; +import com.google.api.gax.testing.MockGrpcService; +import com.google.api.gax.testing.MockServiceHelper; +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.common.collect.Lists; +import com.google.protobuf.GeneratedMessage; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +@javax.annotation.Generated("by GAPIC") +public class ImageAnnotatorTest { + private static MockImageAnnotator mockImageAnnotator; + private static MockServiceHelper serviceHelper; + private ImageAnnotatorApi api; + + @BeforeClass + public static void startStaticServer() { + mockImageAnnotator = new MockImageAnnotator(); + serviceHelper = + new MockServiceHelper("in-process-1", Arrays.asList(mockImageAnnotator)); + serviceHelper.start(); + } + + @AfterClass + public static void stopServer() { + serviceHelper.stop(); + } + + @Before + public void setUp() throws IOException { + serviceHelper.reset(); + ImageAnnotatorSettings settings = + ImageAnnotatorSettings.defaultBuilder() + .provideChannelWith(serviceHelper.createChannel(), true) + .build(); + api = ImageAnnotatorApi.create(settings); + } + + @After + public void tearDown() throws Exception { + api.close(); + } + + @Test + @SuppressWarnings("all") + public void batchAnnotateImagesTest() { + BatchAnnotateImagesResponse expectedResponse = BatchAnnotateImagesResponse.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockImageAnnotator.setResponses(expectedResponses); + + List requests = new ArrayList<>(); + + BatchAnnotateImagesResponse actualResponse = api.batchAnnotateImages(requests); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockImageAnnotator.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + BatchAnnotateImagesRequest actualRequest = (BatchAnnotateImagesRequest) actualRequests.get(0); + + Assert.assertEquals(requests, actualRequest.getRequestsList()); + } +} diff --git a/gcloud-java-vision/src/test/java/com/google/cloud/vision/spi/v1/MockImageAnnotator.java b/gcloud-java-vision/src/test/java/com/google/cloud/vision/spi/v1/MockImageAnnotator.java new file mode 100644 index 000000000000..e308fef89443 --- /dev/null +++ b/gcloud-java-vision/src/test/java/com/google/cloud/vision/spi/v1/MockImageAnnotator.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016 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.vision.spi.v1; + +import com.google.api.gax.testing.MockGrpcService; +import com.google.cloud.vision.v1.ImageAnnotatorGrpc; +import com.google.protobuf.GeneratedMessage; +import io.grpc.ServerServiceDefinition; +import java.util.List; + +@javax.annotation.Generated("by GAPIC") +public class MockImageAnnotator implements MockGrpcService { + private MockImageAnnotatorImpl serviceImpl; + + public MockImageAnnotator() { + serviceImpl = new MockImageAnnotatorImpl(); + } + + @Override + public List getRequests() { + return serviceImpl.getRequests(); + } + + @Override + public void setResponses(List responses) { + serviceImpl.setResponses(responses); + } + + @Override + public ServerServiceDefinition getServiceDefinition() { + return ImageAnnotatorGrpc.bindService(serviceImpl); + } + + @Override + public void reset() { + serviceImpl.reset(); + } +} diff --git a/gcloud-java-vision/src/test/java/com/google/cloud/vision/spi/v1/MockImageAnnotatorImpl.java b/gcloud-java-vision/src/test/java/com/google/cloud/vision/spi/v1/MockImageAnnotatorImpl.java new file mode 100644 index 000000000000..845ab36db57f --- /dev/null +++ b/gcloud-java-vision/src/test/java/com/google/cloud/vision/spi/v1/MockImageAnnotatorImpl.java @@ -0,0 +1,60 @@ +/* + * Copyright 2016 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.vision.spi.v1; + +import com.google.cloud.vision.v1.BatchAnnotateImagesRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.ImageAnnotatorGrpc.ImageAnnotator; +import com.google.common.collect.Lists; +import com.google.protobuf.GeneratedMessage; +import io.grpc.stub.StreamObserver; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +@javax.annotation.Generated("by GAPIC") +public class MockImageAnnotatorImpl implements ImageAnnotator { + private ArrayList requests; + private Queue responses; + + public MockImageAnnotatorImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + public List getRequests() { + return requests; + } + + public void setResponses(List responses) { + this.responses = Lists.newLinkedList(responses); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + @Override + public void batchAnnotateImages( + BatchAnnotateImagesRequest request, + StreamObserver responseObserver) { + BatchAnnotateImagesResponse response = (BatchAnnotateImagesResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } +} diff --git a/pom.xml b/pom.xml index 8002ac3338d1..138a7e38b0b4 100644 --- a/pom.xml +++ b/pom.xml @@ -106,6 +106,7 @@ gcloud-java-resourcemanager gcloud-java-storage gcloud-java-translate + gcloud-java-vision