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.v1p3beta1; 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.api.gax.rpc.StatusCode; 27 import com.google.longrunning.Operation; 28 import com.google.protobuf.AbstractMessage; 29 import com.google.protobuf.Any; 30 import io.grpc.StatusRuntimeException; 31 import java.io.IOException; 32 import java.util.ArrayList; 33 import java.util.Arrays; 34 import java.util.List; 35 import java.util.UUID; 36 import java.util.concurrent.ExecutionException; 37 import javax.annotation.Generated; 38 import org.junit.After; 39 import org.junit.AfterClass; 40 import org.junit.Assert; 41 import org.junit.Before; 42 import org.junit.BeforeClass; 43 import org.junit.Test; 44 45 @Generated("by gapic-generator-java") 46 public class ImageAnnotatorClientTest { 47 private static MockImageAnnotator mockImageAnnotator; 48 private static MockServiceHelper mockServiceHelper; 49 private LocalChannelProvider channelProvider; 50 private ImageAnnotatorClient client; 51 52 @BeforeClass startStaticServer()53 public static void startStaticServer() { 54 mockImageAnnotator = new MockImageAnnotator(); 55 mockServiceHelper = 56 new MockServiceHelper( 57 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockImageAnnotator)); 58 mockServiceHelper.start(); 59 } 60 61 @AfterClass stopServer()62 public static void stopServer() { 63 mockServiceHelper.stop(); 64 } 65 66 @Before setUp()67 public void setUp() throws IOException { 68 mockServiceHelper.reset(); 69 channelProvider = mockServiceHelper.createChannelProvider(); 70 ImageAnnotatorSettings settings = 71 ImageAnnotatorSettings.newBuilder() 72 .setTransportChannelProvider(channelProvider) 73 .setCredentialsProvider(NoCredentialsProvider.create()) 74 .build(); 75 client = ImageAnnotatorClient.create(settings); 76 } 77 78 @After tearDown()79 public void tearDown() throws Exception { 80 client.close(); 81 } 82 83 @Test batchAnnotateImagesTest()84 public void batchAnnotateImagesTest() throws Exception { 85 BatchAnnotateImagesResponse expectedResponse = 86 BatchAnnotateImagesResponse.newBuilder() 87 .addAllResponses(new ArrayList<AnnotateImageResponse>()) 88 .build(); 89 mockImageAnnotator.addResponse(expectedResponse); 90 91 List<AnnotateImageRequest> requests = new ArrayList<>(); 92 93 BatchAnnotateImagesResponse actualResponse = client.batchAnnotateImages(requests); 94 Assert.assertEquals(expectedResponse, actualResponse); 95 96 List<AbstractMessage> actualRequests = mockImageAnnotator.getRequests(); 97 Assert.assertEquals(1, actualRequests.size()); 98 BatchAnnotateImagesRequest actualRequest = ((BatchAnnotateImagesRequest) actualRequests.get(0)); 99 100 Assert.assertEquals(requests, actualRequest.getRequestsList()); 101 Assert.assertTrue( 102 channelProvider.isHeaderSent( 103 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 104 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 105 } 106 107 @Test batchAnnotateImagesExceptionTest()108 public void batchAnnotateImagesExceptionTest() throws Exception { 109 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 110 mockImageAnnotator.addException(exception); 111 112 try { 113 List<AnnotateImageRequest> requests = new ArrayList<>(); 114 client.batchAnnotateImages(requests); 115 Assert.fail("No exception raised"); 116 } catch (InvalidArgumentException e) { 117 // Expected exception. 118 } 119 } 120 121 @Test asyncBatchAnnotateFilesTest()122 public void asyncBatchAnnotateFilesTest() throws Exception { 123 AsyncBatchAnnotateFilesResponse expectedResponse = 124 AsyncBatchAnnotateFilesResponse.newBuilder() 125 .addAllResponses(new ArrayList<AsyncAnnotateFileResponse>()) 126 .build(); 127 Operation resultOperation = 128 Operation.newBuilder() 129 .setName("asyncBatchAnnotateFilesTest") 130 .setDone(true) 131 .setResponse(Any.pack(expectedResponse)) 132 .build(); 133 mockImageAnnotator.addResponse(resultOperation); 134 135 List<AsyncAnnotateFileRequest> requests = new ArrayList<>(); 136 137 AsyncBatchAnnotateFilesResponse actualResponse = 138 client.asyncBatchAnnotateFilesAsync(requests).get(); 139 Assert.assertEquals(expectedResponse, actualResponse); 140 141 List<AbstractMessage> actualRequests = mockImageAnnotator.getRequests(); 142 Assert.assertEquals(1, actualRequests.size()); 143 AsyncBatchAnnotateFilesRequest actualRequest = 144 ((AsyncBatchAnnotateFilesRequest) actualRequests.get(0)); 145 146 Assert.assertEquals(requests, actualRequest.getRequestsList()); 147 Assert.assertTrue( 148 channelProvider.isHeaderSent( 149 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 150 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 151 } 152 153 @Test asyncBatchAnnotateFilesExceptionTest()154 public void asyncBatchAnnotateFilesExceptionTest() throws Exception { 155 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 156 mockImageAnnotator.addException(exception); 157 158 try { 159 List<AsyncAnnotateFileRequest> requests = new ArrayList<>(); 160 client.asyncBatchAnnotateFilesAsync(requests).get(); 161 Assert.fail("No exception raised"); 162 } catch (ExecutionException e) { 163 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 164 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 165 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 166 } 167 } 168 } 169