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.vision.v1p1beta1; 18 19 import com.google.api.gax.core.NoCredentialsProvider; 20 import com.google.api.gax.grpc.GaxGrpcProperties; 21 import com.google.api.gax.grpc.testing.LocalChannelProvider; 22 import com.google.api.gax.grpc.testing.MockGrpcService; 23 import com.google.api.gax.grpc.testing.MockServiceHelper; 24 import com.google.api.gax.rpc.ApiClientHeaderProvider; 25 import com.google.api.gax.rpc.InvalidArgumentException; 26 import com.google.protobuf.AbstractMessage; 27 import io.grpc.StatusRuntimeException; 28 import java.io.IOException; 29 import java.util.ArrayList; 30 import java.util.Arrays; 31 import java.util.List; 32 import java.util.UUID; 33 import javax.annotation.Generated; 34 import org.junit.After; 35 import org.junit.AfterClass; 36 import org.junit.Assert; 37 import org.junit.Before; 38 import org.junit.BeforeClass; 39 import org.junit.Test; 40 41 @Generated("by gapic-generator-java") 42 public class ImageAnnotatorClientTest { 43 private static MockImageAnnotator mockImageAnnotator; 44 private static MockServiceHelper mockServiceHelper; 45 private LocalChannelProvider channelProvider; 46 private ImageAnnotatorClient client; 47 48 @BeforeClass startStaticServer()49 public static void startStaticServer() { 50 mockImageAnnotator = new MockImageAnnotator(); 51 mockServiceHelper = 52 new MockServiceHelper( 53 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockImageAnnotator)); 54 mockServiceHelper.start(); 55 } 56 57 @AfterClass stopServer()58 public static void stopServer() { 59 mockServiceHelper.stop(); 60 } 61 62 @Before setUp()63 public void setUp() throws IOException { 64 mockServiceHelper.reset(); 65 channelProvider = mockServiceHelper.createChannelProvider(); 66 ImageAnnotatorSettings settings = 67 ImageAnnotatorSettings.newBuilder() 68 .setTransportChannelProvider(channelProvider) 69 .setCredentialsProvider(NoCredentialsProvider.create()) 70 .build(); 71 client = ImageAnnotatorClient.create(settings); 72 } 73 74 @After tearDown()75 public void tearDown() throws Exception { 76 client.close(); 77 } 78 79 @Test batchAnnotateImagesTest()80 public void batchAnnotateImagesTest() throws Exception { 81 BatchAnnotateImagesResponse expectedResponse = 82 BatchAnnotateImagesResponse.newBuilder() 83 .addAllResponses(new ArrayList<AnnotateImageResponse>()) 84 .build(); 85 mockImageAnnotator.addResponse(expectedResponse); 86 87 List<AnnotateImageRequest> requests = new ArrayList<>(); 88 89 BatchAnnotateImagesResponse actualResponse = client.batchAnnotateImages(requests); 90 Assert.assertEquals(expectedResponse, actualResponse); 91 92 List<AbstractMessage> actualRequests = mockImageAnnotator.getRequests(); 93 Assert.assertEquals(1, actualRequests.size()); 94 BatchAnnotateImagesRequest actualRequest = ((BatchAnnotateImagesRequest) actualRequests.get(0)); 95 96 Assert.assertEquals(requests, actualRequest.getRequestsList()); 97 Assert.assertTrue( 98 channelProvider.isHeaderSent( 99 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 100 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 101 } 102 103 @Test batchAnnotateImagesExceptionTest()104 public void batchAnnotateImagesExceptionTest() throws Exception { 105 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 106 mockImageAnnotator.addException(exception); 107 108 try { 109 List<AnnotateImageRequest> requests = new ArrayList<>(); 110 client.batchAnnotateImages(requests); 111 Assert.fail("No exception raised"); 112 } catch (InvalidArgumentException e) { 113 // Expected exception. 114 } 115 } 116 } 117