diff --git a/google-cloud-language/pom.xml b/google-cloud-language/pom.xml index d733ff4e25f6..533bd990411c 100644 --- a/google-cloud-language/pom.xml +++ b/google-cloud-language/pom.xml @@ -32,7 +32,18 @@ com.google.api.grpc grpc-google-cloud-language-v1 - 0.1.5 + 0.1.6 + + + io.grpc + grpc-all + + + + + com.google.api.grpc + grpc-google-cloud-language-v1beta2 + 0.1.6 io.grpc diff --git a/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1/LanguageServiceClient.java b/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1/LanguageServiceClient.java index d36a394e2fc1..e52b83183e25 100644 --- a/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1/LanguageServiceClient.java +++ b/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1/LanguageServiceClient.java @@ -238,8 +238,8 @@ private final AnalyzeSentimentResponse analyzeSentiment(AnalyzeSentimentRequest // AUTO-GENERATED DOCUMENTATION AND METHOD /** - * Finds named entities (currently finds proper names) in the text, entity types, salience, - * mentions for each entity, and other properties. + * Finds named entities (currently proper names and common nouns) in the text along with entity + * types, salience, mentions for each entity, and other properties. * *

Sample code: * @@ -268,8 +268,8 @@ public final AnalyzeEntitiesResponse analyzeEntities( // AUTO-GENERATED DOCUMENTATION AND METHOD /** - * Finds named entities (currently finds proper names) in the text, entity types, salience, - * mentions for each entity, and other properties. + * Finds named entities (currently proper names and common nouns) in the text along with entity + * types, salience, mentions for each entity, and other properties. * *

Sample code: * @@ -294,8 +294,8 @@ public final AnalyzeEntitiesResponse analyzeEntities(AnalyzeEntitiesRequest requ // AUTO-GENERATED DOCUMENTATION AND METHOD /** - * Finds named entities (currently finds proper names) in the text, entity types, salience, - * mentions for each entity, and other properties. + * Finds named entities (currently proper names and common nouns) in the text along with entity + * types, salience, mentions for each entity, and other properties. * *

Sample code: * diff --git a/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1beta2/LanguageServiceClient.java b/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1beta2/LanguageServiceClient.java new file mode 100644 index 000000000000..06ae5526a0f1 --- /dev/null +++ b/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1beta2/LanguageServiceClient.java @@ -0,0 +1,589 @@ +/* + * Copyright 2017, 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.language.spi.v1beta2; + +import com.google.api.gax.grpc.ChannelAndExecutor; +import com.google.api.gax.grpc.UnaryCallable; +import com.google.cloud.language.v1beta2.AnalyzeEntitiesRequest; +import com.google.cloud.language.v1beta2.AnalyzeEntitiesResponse; +import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentRequest; +import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentResponse; +import com.google.cloud.language.v1beta2.AnalyzeSentimentRequest; +import com.google.cloud.language.v1beta2.AnalyzeSentimentResponse; +import com.google.cloud.language.v1beta2.AnalyzeSyntaxRequest; +import com.google.cloud.language.v1beta2.AnalyzeSyntaxResponse; +import com.google.cloud.language.v1beta2.AnnotateTextRequest; +import com.google.cloud.language.v1beta2.AnnotateTextRequest.Features; +import com.google.cloud.language.v1beta2.AnnotateTextResponse; +import com.google.cloud.language.v1beta2.Document; +import com.google.cloud.language.v1beta2.EncodingType; +import com.google.protobuf.ExperimentalApi; +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; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND SERVICE +/** + * Service Description: Provides text analysis operations such as sentiment analysis and entity + * recognition. + * + *

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 (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+ *   Document document = Document.newBuilder().build();
+ *   AnalyzeSentimentResponse response = languageServiceClient.analyzeSentiment(document);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the languageServiceClient 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 API + * callable 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 LanguageServiceSettings to + * create(). For example: + * + *

+ * 
+ * InstantiatingChannelProvider channelProvider =
+ *     LanguageServiceSettings.defaultChannelProviderBuilder()
+ *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
+ *         .build();
+ * LanguageServiceSettings languageServiceSettings =
+ *     LanguageServiceSettings.defaultBuilder().setChannelProvider(channelProvider).build();
+ * LanguageServiceClient languageServiceClient =
+ *     LanguageServiceClient.create(languageServiceSettings);
+ * 
+ * 
+ */ +@Generated("by GAPIC") +@ExperimentalApi +public class LanguageServiceClient implements AutoCloseable { + private final LanguageServiceSettings settings; + private final ScheduledExecutorService executor; + private final ManagedChannel channel; + private final List closeables = new ArrayList<>(); + + private final UnaryCallable + analyzeSentimentCallable; + private final UnaryCallable + analyzeEntitiesCallable; + private final UnaryCallable + analyzeEntitySentimentCallable; + private final UnaryCallable analyzeSyntaxCallable; + private final UnaryCallable annotateTextCallable; + + /** Constructs an instance of LanguageServiceClient with default settings. */ + public static final LanguageServiceClient create() throws IOException { + return create(LanguageServiceSettings.defaultBuilder().build()); + } + + /** + * Constructs an instance of LanguageServiceClient, 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 LanguageServiceClient create(LanguageServiceSettings settings) + throws IOException { + return new LanguageServiceClient(settings); + } + + /** + * Constructs an instance of LanguageServiceClient, 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 LanguageServiceClient(LanguageServiceSettings settings) throws IOException { + this.settings = settings; + ChannelAndExecutor channelAndExecutor = settings.getChannelAndExecutor(); + this.executor = channelAndExecutor.getExecutor(); + this.channel = channelAndExecutor.getChannel(); + + this.analyzeSentimentCallable = + UnaryCallable.create(settings.analyzeSentimentSettings(), this.channel, this.executor); + this.analyzeEntitiesCallable = + UnaryCallable.create(settings.analyzeEntitiesSettings(), this.channel, this.executor); + this.analyzeEntitySentimentCallable = + UnaryCallable.create( + settings.analyzeEntitySentimentSettings(), this.channel, this.executor); + this.analyzeSyntaxCallable = + UnaryCallable.create(settings.analyzeSyntaxSettings(), this.channel, this.executor); + this.annotateTextCallable = + UnaryCallable.create(settings.annotateTextSettings(), 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(); + } + }); + } + } + + public final LanguageServiceSettings getSettings() { + return settings; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Analyzes the sentiment of the provided text. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   AnalyzeSentimentResponse response = languageServiceClient.analyzeSentiment(document);
+   * }
+   * 
+ * + * @param document Input document. Currently, `analyzeSentiment` only supports English text + * ([Document.language][google.cloud.language.v1beta2.Document.language]="EN"). + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final AnalyzeSentimentResponse analyzeSentiment(Document document) { + + AnalyzeSentimentRequest request = + AnalyzeSentimentRequest.newBuilder().setDocument(document).build(); + return analyzeSentiment(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Analyzes the sentiment of the provided text. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   AnalyzeSentimentRequest request = AnalyzeSentimentRequest.newBuilder()
+   *     .setDocument(document)
+   *     .build();
+   *   AnalyzeSentimentResponse response = languageServiceClient.analyzeSentiment(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 AnalyzeSentimentResponse analyzeSentiment(AnalyzeSentimentRequest request) { + return analyzeSentimentCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Analyzes the sentiment of the provided text. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   AnalyzeSentimentRequest request = AnalyzeSentimentRequest.newBuilder()
+   *     .setDocument(document)
+   *     .build();
+   *   ApiFuture<AnalyzeSentimentResponse> future = languageServiceClient.analyzeSentimentCallable().futureCall(request);
+   *   // Do something
+   *   AnalyzeSentimentResponse response = future.get();
+   * }
+   * 
+ */ + public final UnaryCallable + analyzeSentimentCallable() { + return analyzeSentimentCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Finds named entities (currently proper names and common nouns) in the text along with entity + * types, salience, mentions for each entity, and other properties. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   EncodingType encodingType = EncodingType.NONE;
+   *   AnalyzeEntitiesResponse response = languageServiceClient.analyzeEntities(document, encodingType);
+   * }
+   * 
+ * + * @param document Input document. + * @param encodingType The encoding type used by the API to calculate offsets. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final AnalyzeEntitiesResponse analyzeEntities( + Document document, EncodingType encodingType) { + + AnalyzeEntitiesRequest request = + AnalyzeEntitiesRequest.newBuilder() + .setDocument(document) + .setEncodingType(encodingType) + .build(); + return analyzeEntities(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Finds named entities (currently proper names and common nouns) in the text along with entity + * types, salience, mentions for each entity, and other properties. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   EncodingType encodingType = EncodingType.NONE;
+   *   AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder()
+   *     .setDocument(document)
+   *     .setEncodingType(encodingType)
+   *     .build();
+   *   AnalyzeEntitiesResponse response = languageServiceClient.analyzeEntities(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 + */ + public final AnalyzeEntitiesResponse analyzeEntities(AnalyzeEntitiesRequest request) { + return analyzeEntitiesCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Finds named entities (currently proper names and common nouns) in the text along with entity + * types, salience, mentions for each entity, and other properties. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   EncodingType encodingType = EncodingType.NONE;
+   *   AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder()
+   *     .setDocument(document)
+   *     .setEncodingType(encodingType)
+   *     .build();
+   *   ApiFuture<AnalyzeEntitiesResponse> future = languageServiceClient.analyzeEntitiesCallable().futureCall(request);
+   *   // Do something
+   *   AnalyzeEntitiesResponse response = future.get();
+   * }
+   * 
+ */ + public final UnaryCallable + analyzeEntitiesCallable() { + return analyzeEntitiesCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Finds entities, similar to + * [AnalyzeEntities][google.cloud.language.v1beta2.LanguageService.AnalyzeEntities] in the text + * and analyzes sentiment associated with each entity and its mentions. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   EncodingType encodingType = EncodingType.NONE;
+   *   AnalyzeEntitySentimentResponse response = languageServiceClient.analyzeEntitySentiment(document, encodingType);
+   * }
+   * 
+ * + * @param document Input document. + * @param encodingType The encoding type used by the API to calculate offsets. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final AnalyzeEntitySentimentResponse analyzeEntitySentiment( + Document document, EncodingType encodingType) { + + AnalyzeEntitySentimentRequest request = + AnalyzeEntitySentimentRequest.newBuilder() + .setDocument(document) + .setEncodingType(encodingType) + .build(); + return analyzeEntitySentiment(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Finds entities, similar to + * [AnalyzeEntities][google.cloud.language.v1beta2.LanguageService.AnalyzeEntities] in the text + * and analyzes sentiment associated with each entity and its mentions. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   EncodingType encodingType = EncodingType.NONE;
+   *   AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder()
+   *     .setDocument(document)
+   *     .setEncodingType(encodingType)
+   *     .build();
+   *   AnalyzeEntitySentimentResponse response = languageServiceClient.analyzeEntitySentiment(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 + */ + public final AnalyzeEntitySentimentResponse analyzeEntitySentiment( + AnalyzeEntitySentimentRequest request) { + return analyzeEntitySentimentCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Finds entities, similar to + * [AnalyzeEntities][google.cloud.language.v1beta2.LanguageService.AnalyzeEntities] in the text + * and analyzes sentiment associated with each entity and its mentions. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   EncodingType encodingType = EncodingType.NONE;
+   *   AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder()
+   *     .setDocument(document)
+   *     .setEncodingType(encodingType)
+   *     .build();
+   *   ApiFuture<AnalyzeEntitySentimentResponse> future = languageServiceClient.analyzeEntitySentimentCallable().futureCall(request);
+   *   // Do something
+   *   AnalyzeEntitySentimentResponse response = future.get();
+   * }
+   * 
+ */ + public final UnaryCallable + analyzeEntitySentimentCallable() { + return analyzeEntitySentimentCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Analyzes the syntax of the text and provides sentence boundaries and tokenization along with + * part of speech tags, dependency trees, and other properties. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   EncodingType encodingType = EncodingType.NONE;
+   *   AnalyzeSyntaxResponse response = languageServiceClient.analyzeSyntax(document, encodingType);
+   * }
+   * 
+ * + * @param document Input document. + * @param encodingType The encoding type used by the API to calculate offsets. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final AnalyzeSyntaxResponse analyzeSyntax(Document document, EncodingType encodingType) { + + AnalyzeSyntaxRequest request = + AnalyzeSyntaxRequest.newBuilder() + .setDocument(document) + .setEncodingType(encodingType) + .build(); + return analyzeSyntax(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Analyzes the syntax of the text and provides sentence boundaries and tokenization along with + * part of speech tags, dependency trees, and other properties. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   EncodingType encodingType = EncodingType.NONE;
+   *   AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder()
+   *     .setDocument(document)
+   *     .setEncodingType(encodingType)
+   *     .build();
+   *   AnalyzeSyntaxResponse response = languageServiceClient.analyzeSyntax(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 + */ + public final AnalyzeSyntaxResponse analyzeSyntax(AnalyzeSyntaxRequest request) { + return analyzeSyntaxCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Analyzes the syntax of the text and provides sentence boundaries and tokenization along with + * part of speech tags, dependency trees, and other properties. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   EncodingType encodingType = EncodingType.NONE;
+   *   AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder()
+   *     .setDocument(document)
+   *     .setEncodingType(encodingType)
+   *     .build();
+   *   ApiFuture<AnalyzeSyntaxResponse> future = languageServiceClient.analyzeSyntaxCallable().futureCall(request);
+   *   // Do something
+   *   AnalyzeSyntaxResponse response = future.get();
+   * }
+   * 
+ */ + public final UnaryCallable analyzeSyntaxCallable() { + return analyzeSyntaxCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * A convenience method that provides all syntax, sentiment, and entity features in one call. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build();
+   *   EncodingType encodingType = EncodingType.NONE;
+   *   AnnotateTextResponse response = languageServiceClient.annotateText(document, features, encodingType);
+   * }
+   * 
+ * + * @param document Input document. + * @param features The enabled features. + * @param encodingType The encoding type used by the API to calculate offsets. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final AnnotateTextResponse annotateText( + Document document, AnnotateTextRequest.Features features, EncodingType encodingType) { + + AnnotateTextRequest request = + AnnotateTextRequest.newBuilder() + .setDocument(document) + .setFeatures(features) + .setEncodingType(encodingType) + .build(); + return annotateText(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * A convenience method that provides all syntax, sentiment, and entity features in one call. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build();
+   *   EncodingType encodingType = EncodingType.NONE;
+   *   AnnotateTextRequest request = AnnotateTextRequest.newBuilder()
+   *     .setDocument(document)
+   *     .setFeatures(features)
+   *     .setEncodingType(encodingType)
+   *     .build();
+   *   AnnotateTextResponse response = languageServiceClient.annotateText(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 + */ + public final AnnotateTextResponse annotateText(AnnotateTextRequest request) { + return annotateTextCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * A convenience method that provides all syntax, sentiment, and entity features in one call. + * + *

Sample code: + * + *


+   * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+   *   Document document = Document.newBuilder().build();
+   *   AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build();
+   *   EncodingType encodingType = EncodingType.NONE;
+   *   AnnotateTextRequest request = AnnotateTextRequest.newBuilder()
+   *     .setDocument(document)
+   *     .setFeatures(features)
+   *     .setEncodingType(encodingType)
+   *     .build();
+   *   ApiFuture<AnnotateTextResponse> future = languageServiceClient.annotateTextCallable().futureCall(request);
+   *   // Do something
+   *   AnnotateTextResponse response = future.get();
+   * }
+   * 
+ */ + public final UnaryCallable annotateTextCallable() { + return annotateTextCallable; + } + + /** + * 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/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1beta2/LanguageServiceSettings.java b/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1beta2/LanguageServiceSettings.java new file mode 100644 index 000000000000..a734059a9412 --- /dev/null +++ b/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1beta2/LanguageServiceSettings.java @@ -0,0 +1,388 @@ +/* + * Copyright 2017, 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.language.spi.v1beta2; + +import com.google.api.gax.core.GoogleCredentialsProvider; +import com.google.api.gax.core.PropertiesProvider; +import com.google.api.gax.core.RetrySettings; +import com.google.api.gax.grpc.ChannelProvider; +import com.google.api.gax.grpc.ClientSettings; +import com.google.api.gax.grpc.ExecutorProvider; +import com.google.api.gax.grpc.InstantiatingChannelProvider; +import com.google.api.gax.grpc.InstantiatingExecutorProvider; +import com.google.api.gax.grpc.SimpleCallSettings; +import com.google.api.gax.grpc.UnaryCallSettings; +import com.google.cloud.language.v1beta2.AnalyzeEntitiesRequest; +import com.google.cloud.language.v1beta2.AnalyzeEntitiesResponse; +import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentRequest; +import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentResponse; +import com.google.cloud.language.v1beta2.AnalyzeSentimentRequest; +import com.google.cloud.language.v1beta2.AnalyzeSentimentResponse; +import com.google.cloud.language.v1beta2.AnalyzeSyntaxRequest; +import com.google.cloud.language.v1beta2.AnalyzeSyntaxResponse; +import com.google.cloud.language.v1beta2.AnnotateTextRequest; +import com.google.cloud.language.v1beta2.AnnotateTextResponse; +import com.google.cloud.language.v1beta2.LanguageServiceGrpc; +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 com.google.protobuf.ExperimentalApi; +import io.grpc.Status; +import java.io.IOException; +import javax.annotation.Generated; +import org.joda.time.Duration; + +// AUTO-GENERATED DOCUMENTATION AND CLASS +/** + * Settings class to configure an instance of {@link LanguageServiceClient}. + * + *

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

    + *
  • The default service address (language.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 analyzeSentiment to 30 seconds: + * + *

+ * 
+ * LanguageServiceSettings.Builder languageServiceSettingsBuilder =
+ *     LanguageServiceSettings.defaultBuilder();
+ * languageServiceSettingsBuilder.analyzeSentimentSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * LanguageServiceSettings languageServiceSettings = languageServiceSettingsBuilder.build();
+ * 
+ * 
+ */ +@Generated("by GAPIC v0.0.5") +@ExperimentalApi +public class LanguageServiceSettings extends ClientSettings { + /** The default address of the service. */ + private static final String DEFAULT_SERVICE_ADDRESS = "language.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(); + + private static final String DEFAULT_GAPIC_NAME = "gapic"; + private static final String DEFAULT_GAPIC_VERSION = ""; + + private static final String PROPERTIES_FILE = "/project.properties"; + private static final String META_VERSION_KEY = "artifact.version"; + + private static String gapicVersion; + + private final SimpleCallSettings + analyzeSentimentSettings; + private final SimpleCallSettings + analyzeEntitiesSettings; + private final SimpleCallSettings + analyzeEntitySentimentSettings; + private final SimpleCallSettings + analyzeSyntaxSettings; + private final SimpleCallSettings annotateTextSettings; + + /** Returns the object with the settings used for calls to analyzeSentiment. */ + public SimpleCallSettings + analyzeSentimentSettings() { + return analyzeSentimentSettings; + } + + /** Returns the object with the settings used for calls to analyzeEntities. */ + public SimpleCallSettings + analyzeEntitiesSettings() { + return analyzeEntitiesSettings; + } + + /** Returns the object with the settings used for calls to analyzeEntitySentiment. */ + public SimpleCallSettings + analyzeEntitySentimentSettings() { + return analyzeEntitySentimentSettings; + } + + /** Returns the object with the settings used for calls to analyzeSyntax. */ + public SimpleCallSettings analyzeSyntaxSettings() { + return analyzeSyntaxSettings; + } + + /** Returns the object with the settings used for calls to annotateText. */ + public SimpleCallSettings annotateTextSettings() { + return annotateTextSettings; + } + + /** Returns a builder for the default ExecutorProvider for this service. */ + public static InstantiatingExecutorProvider.Builder defaultExecutorProviderBuilder() { + return InstantiatingExecutorProvider.newBuilder(); + } + + /** 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 the default credentials for this service. */ + public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilder() { + return GoogleCredentialsProvider.newBuilder().setScopesToApply(DEFAULT_SERVICE_SCOPES); + } + + /** Returns a builder for the default ChannelProvider for this service. */ + public static InstantiatingChannelProvider.Builder defaultChannelProviderBuilder() { + return InstantiatingChannelProvider.newBuilder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .setGeneratorHeader(DEFAULT_GAPIC_NAME, getGapicVersion()) + .setCredentialsProvider(defaultCredentialsProviderBuilder().build()); + } + + private static String getGapicVersion() { + if (gapicVersion == null) { + gapicVersion = + PropertiesProvider.loadProperty( + LanguageServiceSettings.class, PROPERTIES_FILE, META_VERSION_KEY); + gapicVersion = gapicVersion == null ? DEFAULT_GAPIC_VERSION : gapicVersion; + } + return gapicVersion; + } + + /** 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 LanguageServiceSettings(Builder settingsBuilder) throws IOException { + super(settingsBuilder.getExecutorProvider(), settingsBuilder.getChannelProvider()); + + analyzeSentimentSettings = settingsBuilder.analyzeSentimentSettings().build(); + analyzeEntitiesSettings = settingsBuilder.analyzeEntitiesSettings().build(); + analyzeEntitySentimentSettings = settingsBuilder.analyzeEntitySentimentSettings().build(); + analyzeSyntaxSettings = settingsBuilder.analyzeSyntaxSettings().build(); + annotateTextSettings = settingsBuilder.annotateTextSettings().build(); + } + + /** Builder for LanguageServiceSettings. */ + public static class Builder extends ClientSettings.Builder { + private final ImmutableList unaryMethodSettingsBuilders; + + private final SimpleCallSettings.Builder + analyzeSentimentSettings; + private final SimpleCallSettings.Builder + analyzeEntitiesSettings; + private final SimpleCallSettings.Builder< + AnalyzeEntitySentimentRequest, AnalyzeEntitySentimentResponse> + analyzeEntitySentimentSettings; + private final SimpleCallSettings.Builder + analyzeSyntaxSettings; + private final SimpleCallSettings.Builder + annotateTextSettings; + + 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(Status.Code.UNAVAILABLE))); + 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(defaultChannelProviderBuilder().build()); + + analyzeSentimentSettings = + SimpleCallSettings.newBuilder(LanguageServiceGrpc.METHOD_ANALYZE_SENTIMENT); + + analyzeEntitiesSettings = + SimpleCallSettings.newBuilder(LanguageServiceGrpc.METHOD_ANALYZE_ENTITIES); + + analyzeEntitySentimentSettings = + SimpleCallSettings.newBuilder(LanguageServiceGrpc.METHOD_ANALYZE_ENTITY_SENTIMENT); + + analyzeSyntaxSettings = + SimpleCallSettings.newBuilder(LanguageServiceGrpc.METHOD_ANALYZE_SYNTAX); + + annotateTextSettings = + SimpleCallSettings.newBuilder(LanguageServiceGrpc.METHOD_ANNOTATE_TEXT); + + unaryMethodSettingsBuilders = + ImmutableList.of( + analyzeSentimentSettings, + analyzeEntitiesSettings, + analyzeEntitySentimentSettings, + analyzeSyntaxSettings, + annotateTextSettings); + } + + private static Builder createDefault() { + Builder builder = new Builder(); + + builder + .analyzeSentimentSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .analyzeEntitiesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .analyzeEntitySentimentSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .analyzeSyntaxSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .annotateTextSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + + private Builder(LanguageServiceSettings settings) { + super(settings); + + analyzeSentimentSettings = settings.analyzeSentimentSettings.toBuilder(); + analyzeEntitiesSettings = settings.analyzeEntitiesSettings.toBuilder(); + analyzeEntitySentimentSettings = settings.analyzeEntitySentimentSettings.toBuilder(); + analyzeSyntaxSettings = settings.analyzeSyntaxSettings.toBuilder(); + annotateTextSettings = settings.annotateTextSettings.toBuilder(); + + unaryMethodSettingsBuilders = + ImmutableList.of( + analyzeSentimentSettings, + analyzeEntitiesSettings, + analyzeEntitySentimentSettings, + analyzeSyntaxSettings, + annotateTextSettings); + } + + @Override + public Builder setExecutorProvider(ExecutorProvider executorProvider) { + super.setExecutorProvider(executorProvider); + return this; + } + + @Override + public Builder setChannelProvider(ChannelProvider channelProvider) { + super.setChannelProvider(channelProvider); + return this; + } + + /** + * Applies the given settings to all of the unary 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. + * + *

Note: This method does not support applying settings to streaming methods. + */ + public Builder applyToAllUnaryMethods(UnaryCallSettings.Builder unaryCallSettings) + throws Exception { + super.applyToAllUnaryMethods(unaryMethodSettingsBuilders, unaryCallSettings); + return this; + } + + /** Returns the builder for the settings used for calls to analyzeSentiment. */ + public SimpleCallSettings.Builder + analyzeSentimentSettings() { + return analyzeSentimentSettings; + } + + /** Returns the builder for the settings used for calls to analyzeEntities. */ + public SimpleCallSettings.Builder + analyzeEntitiesSettings() { + return analyzeEntitiesSettings; + } + + /** Returns the builder for the settings used for calls to analyzeEntitySentiment. */ + public SimpleCallSettings.Builder + analyzeEntitySentimentSettings() { + return analyzeEntitySentimentSettings; + } + + /** Returns the builder for the settings used for calls to analyzeSyntax. */ + public SimpleCallSettings.Builder + analyzeSyntaxSettings() { + return analyzeSyntaxSettings; + } + + /** Returns the builder for the settings used for calls to annotateText. */ + public SimpleCallSettings.Builder + annotateTextSettings() { + return annotateTextSettings; + } + + @Override + public LanguageServiceSettings build() throws IOException { + return new LanguageServiceSettings(this); + } + } +} diff --git a/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1beta2/package-info.java b/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1beta2/package-info.java new file mode 100644 index 000000000000..39e9f44eb781 --- /dev/null +++ b/google-cloud-language/src/main/java/com/google/cloud/language/spi/v1beta2/package-info.java @@ -0,0 +1,38 @@ +/* + * Copyright 2017, 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 Natural Language API. + * + *

The interfaces provided are listed below, along with usage samples. + * + *

===================== LanguageServiceClient ===================== + * + *

Service Description: Provides text analysis operations such as sentiment analysis and entity + * recognition. + * + *

Sample for LanguageServiceClient: + * + *

+ * 
+ * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
+ *   Document document = Document.newBuilder().build();
+ *   AnalyzeSentimentResponse response = languageServiceClient.analyzeSentiment(document);
+ * }
+ * 
+ * 
+ */ +package com.google.cloud.language.spi.v1beta2; diff --git a/google-cloud-language/src/test/java/com/google/cloud/language/spi/v1beta2/LanguageServiceClientTest.java b/google-cloud-language/src/test/java/com/google/cloud/language/spi/v1beta2/LanguageServiceClientTest.java new file mode 100644 index 000000000000..b0c4c5969c4b --- /dev/null +++ b/google-cloud-language/src/test/java/com/google/cloud/language/spi/v1beta2/LanguageServiceClientTest.java @@ -0,0 +1,277 @@ +/* + * Copyright 2017, 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.language.spi.v1beta2; + +import com.google.api.gax.grpc.ApiException; +import com.google.api.gax.testing.MockGrpcService; +import com.google.api.gax.testing.MockServiceHelper; +import com.google.cloud.language.v1beta2.AnalyzeEntitiesRequest; +import com.google.cloud.language.v1beta2.AnalyzeEntitiesResponse; +import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentRequest; +import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentResponse; +import com.google.cloud.language.v1beta2.AnalyzeSentimentRequest; +import com.google.cloud.language.v1beta2.AnalyzeSentimentResponse; +import com.google.cloud.language.v1beta2.AnalyzeSyntaxRequest; +import com.google.cloud.language.v1beta2.AnalyzeSyntaxResponse; +import com.google.cloud.language.v1beta2.AnnotateTextRequest; +import com.google.cloud.language.v1beta2.AnnotateTextRequest.Features; +import com.google.cloud.language.v1beta2.AnnotateTextResponse; +import com.google.cloud.language.v1beta2.Document; +import com.google.cloud.language.v1beta2.EncodingType; +import com.google.protobuf.GeneratedMessageV3; +import io.grpc.Status; +import io.grpc.StatusRuntimeException; +import java.io.IOException; +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 LanguageServiceClientTest { + private static MockLanguageService mockLanguageService; + private static MockServiceHelper serviceHelper; + private LanguageServiceClient client; + + @BeforeClass + public static void startStaticServer() { + mockLanguageService = new MockLanguageService(); + serviceHelper = + new MockServiceHelper("in-process-1", Arrays.asList(mockLanguageService)); + serviceHelper.start(); + } + + @AfterClass + public static void stopServer() { + serviceHelper.stop(); + } + + @Before + public void setUp() throws IOException { + serviceHelper.reset(); + LanguageServiceSettings settings = + LanguageServiceSettings.defaultBuilder() + .setChannelProvider(serviceHelper.createChannelProvider()) + .build(); + client = LanguageServiceClient.create(settings); + } + + @After + public void tearDown() throws Exception { + client.close(); + } + + @Test + @SuppressWarnings("all") + public void analyzeSentimentTest() { + String language = "language-1613589672"; + AnalyzeSentimentResponse expectedResponse = + AnalyzeSentimentResponse.newBuilder().setLanguage(language).build(); + mockLanguageService.addResponse(expectedResponse); + + Document document = Document.newBuilder().build(); + + AnalyzeSentimentResponse actualResponse = client.analyzeSentiment(document); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLanguageService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + AnalyzeSentimentRequest actualRequest = (AnalyzeSentimentRequest) actualRequests.get(0); + + Assert.assertEquals(document, actualRequest.getDocument()); + } + + @Test + @SuppressWarnings("all") + public void analyzeSentimentExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); + mockLanguageService.addException(exception); + + try { + Document document = Document.newBuilder().build(); + + client.analyzeSentiment(document); + Assert.fail("No exception raised"); + } catch (ApiException e) { + Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode()); + } + } + + @Test + @SuppressWarnings("all") + public void analyzeEntitiesTest() { + String language = "language-1613589672"; + AnalyzeEntitiesResponse expectedResponse = + AnalyzeEntitiesResponse.newBuilder().setLanguage(language).build(); + mockLanguageService.addResponse(expectedResponse); + + Document document = Document.newBuilder().build(); + EncodingType encodingType = EncodingType.NONE; + + AnalyzeEntitiesResponse actualResponse = client.analyzeEntities(document, encodingType); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLanguageService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + AnalyzeEntitiesRequest actualRequest = (AnalyzeEntitiesRequest) actualRequests.get(0); + + Assert.assertEquals(document, actualRequest.getDocument()); + Assert.assertEquals(encodingType, actualRequest.getEncodingType()); + } + + @Test + @SuppressWarnings("all") + public void analyzeEntitiesExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); + mockLanguageService.addException(exception); + + try { + Document document = Document.newBuilder().build(); + EncodingType encodingType = EncodingType.NONE; + + client.analyzeEntities(document, encodingType); + Assert.fail("No exception raised"); + } catch (ApiException e) { + Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode()); + } + } + + @Test + @SuppressWarnings("all") + public void analyzeEntitySentimentTest() { + String language = "language-1613589672"; + AnalyzeEntitySentimentResponse expectedResponse = + AnalyzeEntitySentimentResponse.newBuilder().setLanguage(language).build(); + mockLanguageService.addResponse(expectedResponse); + + Document document = Document.newBuilder().build(); + EncodingType encodingType = EncodingType.NONE; + + AnalyzeEntitySentimentResponse actualResponse = + client.analyzeEntitySentiment(document, encodingType); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLanguageService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + AnalyzeEntitySentimentRequest actualRequest = + (AnalyzeEntitySentimentRequest) actualRequests.get(0); + + Assert.assertEquals(document, actualRequest.getDocument()); + Assert.assertEquals(encodingType, actualRequest.getEncodingType()); + } + + @Test + @SuppressWarnings("all") + public void analyzeEntitySentimentExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); + mockLanguageService.addException(exception); + + try { + Document document = Document.newBuilder().build(); + EncodingType encodingType = EncodingType.NONE; + + client.analyzeEntitySentiment(document, encodingType); + Assert.fail("No exception raised"); + } catch (ApiException e) { + Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode()); + } + } + + @Test + @SuppressWarnings("all") + public void analyzeSyntaxTest() { + String language = "language-1613589672"; + AnalyzeSyntaxResponse expectedResponse = + AnalyzeSyntaxResponse.newBuilder().setLanguage(language).build(); + mockLanguageService.addResponse(expectedResponse); + + Document document = Document.newBuilder().build(); + EncodingType encodingType = EncodingType.NONE; + + AnalyzeSyntaxResponse actualResponse = client.analyzeSyntax(document, encodingType); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLanguageService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + AnalyzeSyntaxRequest actualRequest = (AnalyzeSyntaxRequest) actualRequests.get(0); + + Assert.assertEquals(document, actualRequest.getDocument()); + Assert.assertEquals(encodingType, actualRequest.getEncodingType()); + } + + @Test + @SuppressWarnings("all") + public void analyzeSyntaxExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); + mockLanguageService.addException(exception); + + try { + Document document = Document.newBuilder().build(); + EncodingType encodingType = EncodingType.NONE; + + client.analyzeSyntax(document, encodingType); + Assert.fail("No exception raised"); + } catch (ApiException e) { + Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode()); + } + } + + @Test + @SuppressWarnings("all") + public void annotateTextTest() { + String language = "language-1613589672"; + AnnotateTextResponse expectedResponse = + AnnotateTextResponse.newBuilder().setLanguage(language).build(); + mockLanguageService.addResponse(expectedResponse); + + Document document = Document.newBuilder().build(); + AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build(); + EncodingType encodingType = EncodingType.NONE; + + AnnotateTextResponse actualResponse = client.annotateText(document, features, encodingType); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLanguageService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + AnnotateTextRequest actualRequest = (AnnotateTextRequest) actualRequests.get(0); + + Assert.assertEquals(document, actualRequest.getDocument()); + Assert.assertEquals(features, actualRequest.getFeatures()); + Assert.assertEquals(encodingType, actualRequest.getEncodingType()); + } + + @Test + @SuppressWarnings("all") + public void annotateTextExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); + mockLanguageService.addException(exception); + + try { + Document document = Document.newBuilder().build(); + AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build(); + EncodingType encodingType = EncodingType.NONE; + + client.annotateText(document, features, encodingType); + Assert.fail("No exception raised"); + } catch (ApiException e) { + Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode()); + } + } +} diff --git a/google-cloud-language/src/test/java/com/google/cloud/language/spi/v1beta2/MockLanguageService.java b/google-cloud-language/src/test/java/com/google/cloud/language/spi/v1beta2/MockLanguageService.java new file mode 100644 index 000000000000..21146ba5d120 --- /dev/null +++ b/google-cloud-language/src/test/java/com/google/cloud/language/spi/v1beta2/MockLanguageService.java @@ -0,0 +1,59 @@ +/* + * Copyright 2017, 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.language.spi.v1beta2; + +import com.google.api.gax.testing.MockGrpcService; +import com.google.protobuf.GeneratedMessageV3; +import io.grpc.ServerServiceDefinition; +import java.util.List; + +@javax.annotation.Generated("by GAPIC") +public class MockLanguageService implements MockGrpcService { + private final MockLanguageServiceImpl serviceImpl; + + public MockLanguageService() { + serviceImpl = new MockLanguageServiceImpl(); + } + + @Override + public List getRequests() { + return serviceImpl.getRequests(); + } + + @Override + public void addResponse(GeneratedMessageV3 response) { + serviceImpl.addResponse(response); + } + + @Override + public void addException(Exception exception) { + serviceImpl.addException(exception); + } + + public void setResponses(List responses) { + serviceImpl.setResponses(responses); + } + + @Override + public ServerServiceDefinition getServiceDefinition() { + return serviceImpl.bindService(); + } + + @Override + public void reset() { + serviceImpl.reset(); + } +} diff --git a/google-cloud-language/src/test/java/com/google/cloud/language/spi/v1beta2/MockLanguageServiceImpl.java b/google-cloud-language/src/test/java/com/google/cloud/language/spi/v1beta2/MockLanguageServiceImpl.java new file mode 100644 index 000000000000..766c4bb5f53b --- /dev/null +++ b/google-cloud-language/src/test/java/com/google/cloud/language/spi/v1beta2/MockLanguageServiceImpl.java @@ -0,0 +1,142 @@ +/* + * Copyright 2017, 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.language.spi.v1beta2; + +import com.google.cloud.language.v1beta2.AnalyzeEntitiesRequest; +import com.google.cloud.language.v1beta2.AnalyzeEntitiesResponse; +import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentRequest; +import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentResponse; +import com.google.cloud.language.v1beta2.AnalyzeSentimentRequest; +import com.google.cloud.language.v1beta2.AnalyzeSentimentResponse; +import com.google.cloud.language.v1beta2.AnalyzeSyntaxRequest; +import com.google.cloud.language.v1beta2.AnalyzeSyntaxResponse; +import com.google.cloud.language.v1beta2.AnnotateTextRequest; +import com.google.cloud.language.v1beta2.AnnotateTextResponse; +import com.google.cloud.language.v1beta2.LanguageServiceGrpc.LanguageServiceImplBase; +import com.google.protobuf.GeneratedMessageV3; +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 MockLanguageServiceImpl extends LanguageServiceImplBase { + private ArrayList requests; + private Queue responses; + + public MockLanguageServiceImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + public List getRequests() { + return requests; + } + + public void addResponse(GeneratedMessageV3 response) { + responses.add(response); + } + + public void setResponses(List responses) { + this.responses = new LinkedList(responses); + } + + public void addException(Exception exception) { + responses.add(exception); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + @Override + public void analyzeSentiment( + AnalyzeSentimentRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof AnalyzeSentimentResponse) { + requests.add(request); + responseObserver.onNext((AnalyzeSentimentResponse) response); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError((Exception) response); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } + + @Override + public void analyzeEntities( + AnalyzeEntitiesRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof AnalyzeEntitiesResponse) { + requests.add(request); + responseObserver.onNext((AnalyzeEntitiesResponse) response); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError((Exception) response); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } + + @Override + public void analyzeEntitySentiment( + AnalyzeEntitySentimentRequest request, + StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof AnalyzeEntitySentimentResponse) { + requests.add(request); + responseObserver.onNext((AnalyzeEntitySentimentResponse) response); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError((Exception) response); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } + + @Override + public void analyzeSyntax( + AnalyzeSyntaxRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof AnalyzeSyntaxResponse) { + requests.add(request); + responseObserver.onNext((AnalyzeSyntaxResponse) response); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError((Exception) response); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } + + @Override + public void annotateText( + AnnotateTextRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof AnnotateTextResponse) { + requests.add(request); + responseObserver.onNext((AnnotateTextResponse) response); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError((Exception) response); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } +} diff --git a/pom.xml b/pom.xml index 327b52909132..6620d988926b 100644 --- a/pom.xml +++ b/pom.xml @@ -92,7 +92,7 @@ github 0.6.0 1.0.3 - 0.8.0 + 0.8.1 0.1.5 0.11.3-alpha-SNAPSHOT 0.11.3-beta-SNAPSHOT @@ -442,7 +442,9 @@ http://googleapis.github.io/gax-java/${gax.version}/apidocs/ http://googleapis.github.io/googleapis/java/grpc-google-iam-v1/${generatedProto.version}/apidocs/ http://googleapis.github.io/googleapis/java/grpc-google-cloud-error-reporting-v1beta1/${generatedProto.version}/apidocs/ - http://googleapis.github.io/googleapis/java/grpc-google-cloud-language-v1/${generatedProto.version}/apidocs/ + + http://googleapis.github.io/googleapis/java/grpc-google-cloud-language-v1/0.1.6/apidocs/ + http://googleapis.github.io/googleapis/java/grpc-google-cloud-language-v1beta2/0.1.6/apidocs/ http://googleapis.github.io/googleapis/java/grpc-google-cloud-logging-v2/${generatedProto.version}/apidocs/ http://googleapis.github.io/googleapis/java/grpc-google-cloud-monitoring-v3/${generatedProto.version}/apidocs/ http://googleapis.github.io/googleapis/java/grpc-google-cloud-pubsub-v1/${generatedProto.version}/apidocs/