1 /* 2 * Copyright 2022 Google LLC 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 * https://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 17 package com.google.cloud.language.v1; 18 19 import com.google.api.gax.core.NoCredentialsProvider; 20 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 21 import com.google.api.gax.httpjson.testing.MockHttpService; 22 import com.google.api.gax.rpc.ApiClientHeaderProvider; 23 import com.google.api.gax.rpc.ApiException; 24 import com.google.api.gax.rpc.ApiExceptionFactory; 25 import com.google.api.gax.rpc.InvalidArgumentException; 26 import com.google.api.gax.rpc.StatusCode; 27 import com.google.api.gax.rpc.testing.FakeStatusCode; 28 import com.google.cloud.language.v1.stub.HttpJsonLanguageServiceStub; 29 import java.io.IOException; 30 import java.util.ArrayList; 31 import java.util.List; 32 import javax.annotation.Generated; 33 import org.junit.After; 34 import org.junit.AfterClass; 35 import org.junit.Assert; 36 import org.junit.Before; 37 import org.junit.BeforeClass; 38 import org.junit.Test; 39 40 @Generated("by gapic-generator-java") 41 public class LanguageServiceClientHttpJsonTest { 42 private static MockHttpService mockService; 43 private static LanguageServiceClient client; 44 45 @BeforeClass startStaticServer()46 public static void startStaticServer() throws IOException { 47 mockService = 48 new MockHttpService( 49 HttpJsonLanguageServiceStub.getMethodDescriptors(), 50 LanguageServiceSettings.getDefaultEndpoint()); 51 LanguageServiceSettings settings = 52 LanguageServiceSettings.newHttpJsonBuilder() 53 .setTransportChannelProvider( 54 LanguageServiceSettings.defaultHttpJsonTransportProviderBuilder() 55 .setHttpTransport(mockService) 56 .build()) 57 .setCredentialsProvider(NoCredentialsProvider.create()) 58 .build(); 59 client = LanguageServiceClient.create(settings); 60 } 61 62 @AfterClass stopServer()63 public static void stopServer() { 64 client.close(); 65 } 66 67 @Before setUp()68 public void setUp() {} 69 70 @After tearDown()71 public void tearDown() throws Exception { 72 mockService.reset(); 73 } 74 75 @Test analyzeSentimentTest()76 public void analyzeSentimentTest() throws Exception { 77 AnalyzeSentimentResponse expectedResponse = 78 AnalyzeSentimentResponse.newBuilder() 79 .setDocumentSentiment(Sentiment.newBuilder().build()) 80 .setLanguage("language-1613589672") 81 .addAllSentences(new ArrayList<Sentence>()) 82 .build(); 83 mockService.addResponse(expectedResponse); 84 85 Document document = Document.newBuilder().build(); 86 87 AnalyzeSentimentResponse actualResponse = client.analyzeSentiment(document); 88 Assert.assertEquals(expectedResponse, actualResponse); 89 90 List<String> actualRequests = mockService.getRequestPaths(); 91 Assert.assertEquals(1, actualRequests.size()); 92 93 String apiClientHeaderKey = 94 mockService 95 .getRequestHeaders() 96 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 97 .iterator() 98 .next(); 99 Assert.assertTrue( 100 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 101 .matcher(apiClientHeaderKey) 102 .matches()); 103 } 104 105 @Test analyzeSentimentExceptionTest()106 public void analyzeSentimentExceptionTest() throws Exception { 107 ApiException exception = 108 ApiExceptionFactory.createException( 109 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 110 mockService.addException(exception); 111 112 try { 113 Document document = Document.newBuilder().build(); 114 client.analyzeSentiment(document); 115 Assert.fail("No exception raised"); 116 } catch (InvalidArgumentException e) { 117 // Expected exception. 118 } 119 } 120 121 @Test analyzeSentimentTest2()122 public void analyzeSentimentTest2() throws Exception { 123 AnalyzeSentimentResponse expectedResponse = 124 AnalyzeSentimentResponse.newBuilder() 125 .setDocumentSentiment(Sentiment.newBuilder().build()) 126 .setLanguage("language-1613589672") 127 .addAllSentences(new ArrayList<Sentence>()) 128 .build(); 129 mockService.addResponse(expectedResponse); 130 131 Document document = Document.newBuilder().build(); 132 EncodingType encodingType = EncodingType.forNumber(0); 133 134 AnalyzeSentimentResponse actualResponse = client.analyzeSentiment(document, encodingType); 135 Assert.assertEquals(expectedResponse, actualResponse); 136 137 List<String> actualRequests = mockService.getRequestPaths(); 138 Assert.assertEquals(1, actualRequests.size()); 139 140 String apiClientHeaderKey = 141 mockService 142 .getRequestHeaders() 143 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 144 .iterator() 145 .next(); 146 Assert.assertTrue( 147 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 148 .matcher(apiClientHeaderKey) 149 .matches()); 150 } 151 152 @Test analyzeSentimentExceptionTest2()153 public void analyzeSentimentExceptionTest2() throws Exception { 154 ApiException exception = 155 ApiExceptionFactory.createException( 156 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 157 mockService.addException(exception); 158 159 try { 160 Document document = Document.newBuilder().build(); 161 EncodingType encodingType = EncodingType.forNumber(0); 162 client.analyzeSentiment(document, encodingType); 163 Assert.fail("No exception raised"); 164 } catch (InvalidArgumentException e) { 165 // Expected exception. 166 } 167 } 168 169 @Test analyzeEntitiesTest()170 public void analyzeEntitiesTest() throws Exception { 171 AnalyzeEntitiesResponse expectedResponse = 172 AnalyzeEntitiesResponse.newBuilder() 173 .addAllEntities(new ArrayList<Entity>()) 174 .setLanguage("language-1613589672") 175 .build(); 176 mockService.addResponse(expectedResponse); 177 178 Document document = Document.newBuilder().build(); 179 180 AnalyzeEntitiesResponse actualResponse = client.analyzeEntities(document); 181 Assert.assertEquals(expectedResponse, actualResponse); 182 183 List<String> actualRequests = mockService.getRequestPaths(); 184 Assert.assertEquals(1, actualRequests.size()); 185 186 String apiClientHeaderKey = 187 mockService 188 .getRequestHeaders() 189 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 190 .iterator() 191 .next(); 192 Assert.assertTrue( 193 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 194 .matcher(apiClientHeaderKey) 195 .matches()); 196 } 197 198 @Test analyzeEntitiesExceptionTest()199 public void analyzeEntitiesExceptionTest() throws Exception { 200 ApiException exception = 201 ApiExceptionFactory.createException( 202 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 203 mockService.addException(exception); 204 205 try { 206 Document document = Document.newBuilder().build(); 207 client.analyzeEntities(document); 208 Assert.fail("No exception raised"); 209 } catch (InvalidArgumentException e) { 210 // Expected exception. 211 } 212 } 213 214 @Test analyzeEntitiesTest2()215 public void analyzeEntitiesTest2() throws Exception { 216 AnalyzeEntitiesResponse expectedResponse = 217 AnalyzeEntitiesResponse.newBuilder() 218 .addAllEntities(new ArrayList<Entity>()) 219 .setLanguage("language-1613589672") 220 .build(); 221 mockService.addResponse(expectedResponse); 222 223 Document document = Document.newBuilder().build(); 224 EncodingType encodingType = EncodingType.forNumber(0); 225 226 AnalyzeEntitiesResponse actualResponse = client.analyzeEntities(document, encodingType); 227 Assert.assertEquals(expectedResponse, actualResponse); 228 229 List<String> actualRequests = mockService.getRequestPaths(); 230 Assert.assertEquals(1, actualRequests.size()); 231 232 String apiClientHeaderKey = 233 mockService 234 .getRequestHeaders() 235 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 236 .iterator() 237 .next(); 238 Assert.assertTrue( 239 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 240 .matcher(apiClientHeaderKey) 241 .matches()); 242 } 243 244 @Test analyzeEntitiesExceptionTest2()245 public void analyzeEntitiesExceptionTest2() throws Exception { 246 ApiException exception = 247 ApiExceptionFactory.createException( 248 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 249 mockService.addException(exception); 250 251 try { 252 Document document = Document.newBuilder().build(); 253 EncodingType encodingType = EncodingType.forNumber(0); 254 client.analyzeEntities(document, encodingType); 255 Assert.fail("No exception raised"); 256 } catch (InvalidArgumentException e) { 257 // Expected exception. 258 } 259 } 260 261 @Test analyzeEntitySentimentTest()262 public void analyzeEntitySentimentTest() throws Exception { 263 AnalyzeEntitySentimentResponse expectedResponse = 264 AnalyzeEntitySentimentResponse.newBuilder() 265 .addAllEntities(new ArrayList<Entity>()) 266 .setLanguage("language-1613589672") 267 .build(); 268 mockService.addResponse(expectedResponse); 269 270 Document document = Document.newBuilder().build(); 271 272 AnalyzeEntitySentimentResponse actualResponse = client.analyzeEntitySentiment(document); 273 Assert.assertEquals(expectedResponse, actualResponse); 274 275 List<String> actualRequests = mockService.getRequestPaths(); 276 Assert.assertEquals(1, actualRequests.size()); 277 278 String apiClientHeaderKey = 279 mockService 280 .getRequestHeaders() 281 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 282 .iterator() 283 .next(); 284 Assert.assertTrue( 285 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 286 .matcher(apiClientHeaderKey) 287 .matches()); 288 } 289 290 @Test analyzeEntitySentimentExceptionTest()291 public void analyzeEntitySentimentExceptionTest() throws Exception { 292 ApiException exception = 293 ApiExceptionFactory.createException( 294 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 295 mockService.addException(exception); 296 297 try { 298 Document document = Document.newBuilder().build(); 299 client.analyzeEntitySentiment(document); 300 Assert.fail("No exception raised"); 301 } catch (InvalidArgumentException e) { 302 // Expected exception. 303 } 304 } 305 306 @Test analyzeEntitySentimentTest2()307 public void analyzeEntitySentimentTest2() throws Exception { 308 AnalyzeEntitySentimentResponse expectedResponse = 309 AnalyzeEntitySentimentResponse.newBuilder() 310 .addAllEntities(new ArrayList<Entity>()) 311 .setLanguage("language-1613589672") 312 .build(); 313 mockService.addResponse(expectedResponse); 314 315 Document document = Document.newBuilder().build(); 316 EncodingType encodingType = EncodingType.forNumber(0); 317 318 AnalyzeEntitySentimentResponse actualResponse = 319 client.analyzeEntitySentiment(document, encodingType); 320 Assert.assertEquals(expectedResponse, actualResponse); 321 322 List<String> actualRequests = mockService.getRequestPaths(); 323 Assert.assertEquals(1, actualRequests.size()); 324 325 String apiClientHeaderKey = 326 mockService 327 .getRequestHeaders() 328 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 329 .iterator() 330 .next(); 331 Assert.assertTrue( 332 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 333 .matcher(apiClientHeaderKey) 334 .matches()); 335 } 336 337 @Test analyzeEntitySentimentExceptionTest2()338 public void analyzeEntitySentimentExceptionTest2() throws Exception { 339 ApiException exception = 340 ApiExceptionFactory.createException( 341 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 342 mockService.addException(exception); 343 344 try { 345 Document document = Document.newBuilder().build(); 346 EncodingType encodingType = EncodingType.forNumber(0); 347 client.analyzeEntitySentiment(document, encodingType); 348 Assert.fail("No exception raised"); 349 } catch (InvalidArgumentException e) { 350 // Expected exception. 351 } 352 } 353 354 @Test analyzeSyntaxTest()355 public void analyzeSyntaxTest() throws Exception { 356 AnalyzeSyntaxResponse expectedResponse = 357 AnalyzeSyntaxResponse.newBuilder() 358 .addAllSentences(new ArrayList<Sentence>()) 359 .addAllTokens(new ArrayList<Token>()) 360 .setLanguage("language-1613589672") 361 .build(); 362 mockService.addResponse(expectedResponse); 363 364 Document document = Document.newBuilder().build(); 365 366 AnalyzeSyntaxResponse actualResponse = client.analyzeSyntax(document); 367 Assert.assertEquals(expectedResponse, actualResponse); 368 369 List<String> actualRequests = mockService.getRequestPaths(); 370 Assert.assertEquals(1, actualRequests.size()); 371 372 String apiClientHeaderKey = 373 mockService 374 .getRequestHeaders() 375 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 376 .iterator() 377 .next(); 378 Assert.assertTrue( 379 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 380 .matcher(apiClientHeaderKey) 381 .matches()); 382 } 383 384 @Test analyzeSyntaxExceptionTest()385 public void analyzeSyntaxExceptionTest() throws Exception { 386 ApiException exception = 387 ApiExceptionFactory.createException( 388 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 389 mockService.addException(exception); 390 391 try { 392 Document document = Document.newBuilder().build(); 393 client.analyzeSyntax(document); 394 Assert.fail("No exception raised"); 395 } catch (InvalidArgumentException e) { 396 // Expected exception. 397 } 398 } 399 400 @Test analyzeSyntaxTest2()401 public void analyzeSyntaxTest2() throws Exception { 402 AnalyzeSyntaxResponse expectedResponse = 403 AnalyzeSyntaxResponse.newBuilder() 404 .addAllSentences(new ArrayList<Sentence>()) 405 .addAllTokens(new ArrayList<Token>()) 406 .setLanguage("language-1613589672") 407 .build(); 408 mockService.addResponse(expectedResponse); 409 410 Document document = Document.newBuilder().build(); 411 EncodingType encodingType = EncodingType.forNumber(0); 412 413 AnalyzeSyntaxResponse actualResponse = client.analyzeSyntax(document, encodingType); 414 Assert.assertEquals(expectedResponse, actualResponse); 415 416 List<String> actualRequests = mockService.getRequestPaths(); 417 Assert.assertEquals(1, actualRequests.size()); 418 419 String apiClientHeaderKey = 420 mockService 421 .getRequestHeaders() 422 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 423 .iterator() 424 .next(); 425 Assert.assertTrue( 426 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 427 .matcher(apiClientHeaderKey) 428 .matches()); 429 } 430 431 @Test analyzeSyntaxExceptionTest2()432 public void analyzeSyntaxExceptionTest2() throws Exception { 433 ApiException exception = 434 ApiExceptionFactory.createException( 435 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 436 mockService.addException(exception); 437 438 try { 439 Document document = Document.newBuilder().build(); 440 EncodingType encodingType = EncodingType.forNumber(0); 441 client.analyzeSyntax(document, encodingType); 442 Assert.fail("No exception raised"); 443 } catch (InvalidArgumentException e) { 444 // Expected exception. 445 } 446 } 447 448 @Test classifyTextTest()449 public void classifyTextTest() throws Exception { 450 ClassifyTextResponse expectedResponse = 451 ClassifyTextResponse.newBuilder() 452 .addAllCategories(new ArrayList<ClassificationCategory>()) 453 .build(); 454 mockService.addResponse(expectedResponse); 455 456 Document document = Document.newBuilder().build(); 457 458 ClassifyTextResponse actualResponse = client.classifyText(document); 459 Assert.assertEquals(expectedResponse, actualResponse); 460 461 List<String> actualRequests = mockService.getRequestPaths(); 462 Assert.assertEquals(1, actualRequests.size()); 463 464 String apiClientHeaderKey = 465 mockService 466 .getRequestHeaders() 467 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 468 .iterator() 469 .next(); 470 Assert.assertTrue( 471 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 472 .matcher(apiClientHeaderKey) 473 .matches()); 474 } 475 476 @Test classifyTextExceptionTest()477 public void classifyTextExceptionTest() throws Exception { 478 ApiException exception = 479 ApiExceptionFactory.createException( 480 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 481 mockService.addException(exception); 482 483 try { 484 Document document = Document.newBuilder().build(); 485 client.classifyText(document); 486 Assert.fail("No exception raised"); 487 } catch (InvalidArgumentException e) { 488 // Expected exception. 489 } 490 } 491 492 @Test annotateTextTest()493 public void annotateTextTest() throws Exception { 494 AnnotateTextResponse expectedResponse = 495 AnnotateTextResponse.newBuilder() 496 .addAllSentences(new ArrayList<Sentence>()) 497 .addAllTokens(new ArrayList<Token>()) 498 .addAllEntities(new ArrayList<Entity>()) 499 .setDocumentSentiment(Sentiment.newBuilder().build()) 500 .setLanguage("language-1613589672") 501 .addAllCategories(new ArrayList<ClassificationCategory>()) 502 .build(); 503 mockService.addResponse(expectedResponse); 504 505 Document document = Document.newBuilder().build(); 506 AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build(); 507 508 AnnotateTextResponse actualResponse = client.annotateText(document, features); 509 Assert.assertEquals(expectedResponse, actualResponse); 510 511 List<String> actualRequests = mockService.getRequestPaths(); 512 Assert.assertEquals(1, actualRequests.size()); 513 514 String apiClientHeaderKey = 515 mockService 516 .getRequestHeaders() 517 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 518 .iterator() 519 .next(); 520 Assert.assertTrue( 521 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 522 .matcher(apiClientHeaderKey) 523 .matches()); 524 } 525 526 @Test annotateTextExceptionTest()527 public void annotateTextExceptionTest() throws Exception { 528 ApiException exception = 529 ApiExceptionFactory.createException( 530 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 531 mockService.addException(exception); 532 533 try { 534 Document document = Document.newBuilder().build(); 535 AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build(); 536 client.annotateText(document, features); 537 Assert.fail("No exception raised"); 538 } catch (InvalidArgumentException e) { 539 // Expected exception. 540 } 541 } 542 543 @Test annotateTextTest2()544 public void annotateTextTest2() throws Exception { 545 AnnotateTextResponse expectedResponse = 546 AnnotateTextResponse.newBuilder() 547 .addAllSentences(new ArrayList<Sentence>()) 548 .addAllTokens(new ArrayList<Token>()) 549 .addAllEntities(new ArrayList<Entity>()) 550 .setDocumentSentiment(Sentiment.newBuilder().build()) 551 .setLanguage("language-1613589672") 552 .addAllCategories(new ArrayList<ClassificationCategory>()) 553 .build(); 554 mockService.addResponse(expectedResponse); 555 556 Document document = Document.newBuilder().build(); 557 AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build(); 558 EncodingType encodingType = EncodingType.forNumber(0); 559 560 AnnotateTextResponse actualResponse = client.annotateText(document, features, encodingType); 561 Assert.assertEquals(expectedResponse, actualResponse); 562 563 List<String> actualRequests = mockService.getRequestPaths(); 564 Assert.assertEquals(1, actualRequests.size()); 565 566 String apiClientHeaderKey = 567 mockService 568 .getRequestHeaders() 569 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 570 .iterator() 571 .next(); 572 Assert.assertTrue( 573 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 574 .matcher(apiClientHeaderKey) 575 .matches()); 576 } 577 578 @Test annotateTextExceptionTest2()579 public void annotateTextExceptionTest2() throws Exception { 580 ApiException exception = 581 ApiExceptionFactory.createException( 582 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 583 mockService.addException(exception); 584 585 try { 586 Document document = Document.newBuilder().build(); 587 AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build(); 588 EncodingType encodingType = EncodingType.forNumber(0); 589 client.annotateText(document, features, encodingType); 590 Assert.fail("No exception raised"); 591 } catch (InvalidArgumentException e) { 592 // Expected exception. 593 } 594 } 595 } 596