|
| 1 | +/* |
| 2 | + * Copyright 2017, Google Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.language.spi.v1beta2; |
| 17 | + |
| 18 | +import com.google.api.gax.grpc.ApiException; |
| 19 | +import com.google.api.gax.testing.MockGrpcService; |
| 20 | +import com.google.api.gax.testing.MockServiceHelper; |
| 21 | +import com.google.cloud.language.v1beta2.AnalyzeEntitiesRequest; |
| 22 | +import com.google.cloud.language.v1beta2.AnalyzeEntitiesResponse; |
| 23 | +import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentRequest; |
| 24 | +import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentResponse; |
| 25 | +import com.google.cloud.language.v1beta2.AnalyzeSentimentRequest; |
| 26 | +import com.google.cloud.language.v1beta2.AnalyzeSentimentResponse; |
| 27 | +import com.google.cloud.language.v1beta2.AnalyzeSyntaxRequest; |
| 28 | +import com.google.cloud.language.v1beta2.AnalyzeSyntaxResponse; |
| 29 | +import com.google.cloud.language.v1beta2.AnnotateTextRequest; |
| 30 | +import com.google.cloud.language.v1beta2.AnnotateTextRequest.Features; |
| 31 | +import com.google.cloud.language.v1beta2.AnnotateTextResponse; |
| 32 | +import com.google.cloud.language.v1beta2.Document; |
| 33 | +import com.google.cloud.language.v1beta2.EncodingType; |
| 34 | +import com.google.protobuf.GeneratedMessageV3; |
| 35 | +import io.grpc.Status; |
| 36 | +import io.grpc.StatusRuntimeException; |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.List; |
| 40 | +import org.junit.After; |
| 41 | +import org.junit.AfterClass; |
| 42 | +import org.junit.Assert; |
| 43 | +import org.junit.Before; |
| 44 | +import org.junit.BeforeClass; |
| 45 | +import org.junit.Test; |
| 46 | + |
| 47 | +@javax.annotation.Generated("by GAPIC") |
| 48 | +public class LanguageServiceClientTest { |
| 49 | + private static MockLanguageService mockLanguageService; |
| 50 | + private static MockServiceHelper serviceHelper; |
| 51 | + private LanguageServiceClient client; |
| 52 | + |
| 53 | + @BeforeClass |
| 54 | + public static void startStaticServer() { |
| 55 | + mockLanguageService = new MockLanguageService(); |
| 56 | + serviceHelper = |
| 57 | + new MockServiceHelper("in-process-1", Arrays.<MockGrpcService>asList(mockLanguageService)); |
| 58 | + serviceHelper.start(); |
| 59 | + } |
| 60 | + |
| 61 | + @AfterClass |
| 62 | + public static void stopServer() { |
| 63 | + serviceHelper.stop(); |
| 64 | + } |
| 65 | + |
| 66 | + @Before |
| 67 | + public void setUp() throws IOException { |
| 68 | + serviceHelper.reset(); |
| 69 | + LanguageServiceSettings settings = |
| 70 | + LanguageServiceSettings.defaultBuilder() |
| 71 | + .setChannelProvider(serviceHelper.createChannelProvider()) |
| 72 | + .build(); |
| 73 | + client = LanguageServiceClient.create(settings); |
| 74 | + } |
| 75 | + |
| 76 | + @After |
| 77 | + public void tearDown() throws Exception { |
| 78 | + client.close(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @SuppressWarnings("all") |
| 83 | + public void analyzeSentimentTest() { |
| 84 | + String language = "language-1613589672"; |
| 85 | + AnalyzeSentimentResponse expectedResponse = |
| 86 | + AnalyzeSentimentResponse.newBuilder().setLanguage(language).build(); |
| 87 | + mockLanguageService.addResponse(expectedResponse); |
| 88 | + |
| 89 | + Document document = Document.newBuilder().build(); |
| 90 | + |
| 91 | + AnalyzeSentimentResponse actualResponse = client.analyzeSentiment(document); |
| 92 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 93 | + |
| 94 | + List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests(); |
| 95 | + Assert.assertEquals(1, actualRequests.size()); |
| 96 | + AnalyzeSentimentRequest actualRequest = (AnalyzeSentimentRequest) actualRequests.get(0); |
| 97 | + |
| 98 | + Assert.assertEquals(document, actualRequest.getDocument()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + @SuppressWarnings("all") |
| 103 | + public void analyzeSentimentExceptionTest() throws Exception { |
| 104 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 105 | + mockLanguageService.addException(exception); |
| 106 | + |
| 107 | + try { |
| 108 | + Document document = Document.newBuilder().build(); |
| 109 | + |
| 110 | + client.analyzeSentiment(document); |
| 111 | + Assert.fail("No exception raised"); |
| 112 | + } catch (ApiException e) { |
| 113 | + Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode()); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + @SuppressWarnings("all") |
| 119 | + public void analyzeEntitiesTest() { |
| 120 | + String language = "language-1613589672"; |
| 121 | + AnalyzeEntitiesResponse expectedResponse = |
| 122 | + AnalyzeEntitiesResponse.newBuilder().setLanguage(language).build(); |
| 123 | + mockLanguageService.addResponse(expectedResponse); |
| 124 | + |
| 125 | + Document document = Document.newBuilder().build(); |
| 126 | + EncodingType encodingType = EncodingType.NONE; |
| 127 | + |
| 128 | + AnalyzeEntitiesResponse actualResponse = client.analyzeEntities(document, encodingType); |
| 129 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 130 | + |
| 131 | + List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests(); |
| 132 | + Assert.assertEquals(1, actualRequests.size()); |
| 133 | + AnalyzeEntitiesRequest actualRequest = (AnalyzeEntitiesRequest) actualRequests.get(0); |
| 134 | + |
| 135 | + Assert.assertEquals(document, actualRequest.getDocument()); |
| 136 | + Assert.assertEquals(encodingType, actualRequest.getEncodingType()); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + @SuppressWarnings("all") |
| 141 | + public void analyzeEntitiesExceptionTest() throws Exception { |
| 142 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 143 | + mockLanguageService.addException(exception); |
| 144 | + |
| 145 | + try { |
| 146 | + Document document = Document.newBuilder().build(); |
| 147 | + EncodingType encodingType = EncodingType.NONE; |
| 148 | + |
| 149 | + client.analyzeEntities(document, encodingType); |
| 150 | + Assert.fail("No exception raised"); |
| 151 | + } catch (ApiException e) { |
| 152 | + Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode()); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + @SuppressWarnings("all") |
| 158 | + public void analyzeEntitySentimentTest() { |
| 159 | + String language = "language-1613589672"; |
| 160 | + AnalyzeEntitySentimentResponse expectedResponse = |
| 161 | + AnalyzeEntitySentimentResponse.newBuilder().setLanguage(language).build(); |
| 162 | + mockLanguageService.addResponse(expectedResponse); |
| 163 | + |
| 164 | + Document document = Document.newBuilder().build(); |
| 165 | + EncodingType encodingType = EncodingType.NONE; |
| 166 | + |
| 167 | + AnalyzeEntitySentimentResponse actualResponse = |
| 168 | + client.analyzeEntitySentiment(document, encodingType); |
| 169 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 170 | + |
| 171 | + List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests(); |
| 172 | + Assert.assertEquals(1, actualRequests.size()); |
| 173 | + AnalyzeEntitySentimentRequest actualRequest = |
| 174 | + (AnalyzeEntitySentimentRequest) actualRequests.get(0); |
| 175 | + |
| 176 | + Assert.assertEquals(document, actualRequest.getDocument()); |
| 177 | + Assert.assertEquals(encodingType, actualRequest.getEncodingType()); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + @SuppressWarnings("all") |
| 182 | + public void analyzeEntitySentimentExceptionTest() throws Exception { |
| 183 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 184 | + mockLanguageService.addException(exception); |
| 185 | + |
| 186 | + try { |
| 187 | + Document document = Document.newBuilder().build(); |
| 188 | + EncodingType encodingType = EncodingType.NONE; |
| 189 | + |
| 190 | + client.analyzeEntitySentiment(document, encodingType); |
| 191 | + Assert.fail("No exception raised"); |
| 192 | + } catch (ApiException e) { |
| 193 | + Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode()); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + @SuppressWarnings("all") |
| 199 | + public void analyzeSyntaxTest() { |
| 200 | + String language = "language-1613589672"; |
| 201 | + AnalyzeSyntaxResponse expectedResponse = |
| 202 | + AnalyzeSyntaxResponse.newBuilder().setLanguage(language).build(); |
| 203 | + mockLanguageService.addResponse(expectedResponse); |
| 204 | + |
| 205 | + Document document = Document.newBuilder().build(); |
| 206 | + EncodingType encodingType = EncodingType.NONE; |
| 207 | + |
| 208 | + AnalyzeSyntaxResponse actualResponse = client.analyzeSyntax(document, encodingType); |
| 209 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 210 | + |
| 211 | + List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests(); |
| 212 | + Assert.assertEquals(1, actualRequests.size()); |
| 213 | + AnalyzeSyntaxRequest actualRequest = (AnalyzeSyntaxRequest) actualRequests.get(0); |
| 214 | + |
| 215 | + Assert.assertEquals(document, actualRequest.getDocument()); |
| 216 | + Assert.assertEquals(encodingType, actualRequest.getEncodingType()); |
| 217 | + } |
| 218 | + |
| 219 | + @Test |
| 220 | + @SuppressWarnings("all") |
| 221 | + public void analyzeSyntaxExceptionTest() throws Exception { |
| 222 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 223 | + mockLanguageService.addException(exception); |
| 224 | + |
| 225 | + try { |
| 226 | + Document document = Document.newBuilder().build(); |
| 227 | + EncodingType encodingType = EncodingType.NONE; |
| 228 | + |
| 229 | + client.analyzeSyntax(document, encodingType); |
| 230 | + Assert.fail("No exception raised"); |
| 231 | + } catch (ApiException e) { |
| 232 | + Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode()); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + @SuppressWarnings("all") |
| 238 | + public void annotateTextTest() { |
| 239 | + String language = "language-1613589672"; |
| 240 | + AnnotateTextResponse expectedResponse = |
| 241 | + AnnotateTextResponse.newBuilder().setLanguage(language).build(); |
| 242 | + mockLanguageService.addResponse(expectedResponse); |
| 243 | + |
| 244 | + Document document = Document.newBuilder().build(); |
| 245 | + AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build(); |
| 246 | + EncodingType encodingType = EncodingType.NONE; |
| 247 | + |
| 248 | + AnnotateTextResponse actualResponse = client.annotateText(document, features, encodingType); |
| 249 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 250 | + |
| 251 | + List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests(); |
| 252 | + Assert.assertEquals(1, actualRequests.size()); |
| 253 | + AnnotateTextRequest actualRequest = (AnnotateTextRequest) actualRequests.get(0); |
| 254 | + |
| 255 | + Assert.assertEquals(document, actualRequest.getDocument()); |
| 256 | + Assert.assertEquals(features, actualRequest.getFeatures()); |
| 257 | + Assert.assertEquals(encodingType, actualRequest.getEncodingType()); |
| 258 | + } |
| 259 | + |
| 260 | + @Test |
| 261 | + @SuppressWarnings("all") |
| 262 | + public void annotateTextExceptionTest() throws Exception { |
| 263 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 264 | + mockLanguageService.addException(exception); |
| 265 | + |
| 266 | + try { |
| 267 | + Document document = Document.newBuilder().build(); |
| 268 | + AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build(); |
| 269 | + EncodingType encodingType = EncodingType.NONE; |
| 270 | + |
| 271 | + client.annotateText(document, features, encodingType); |
| 272 | + Assert.fail("No exception raised"); |
| 273 | + } catch (ApiException e) { |
| 274 | + Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode()); |
| 275 | + } |
| 276 | + } |
| 277 | +} |
0 commit comments