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.v1p4beta1; 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 batchAnnotateFilesTest()122 public void batchAnnotateFilesTest() throws Exception { 123 BatchAnnotateFilesResponse expectedResponse = 124 BatchAnnotateFilesResponse.newBuilder() 125 .addAllResponses(new ArrayList<AnnotateFileResponse>()) 126 .build(); 127 mockImageAnnotator.addResponse(expectedResponse); 128 129 List<AnnotateFileRequest> requests = new ArrayList<>(); 130 131 BatchAnnotateFilesResponse actualResponse = client.batchAnnotateFiles(requests); 132 Assert.assertEquals(expectedResponse, actualResponse); 133 134 List<AbstractMessage> actualRequests = mockImageAnnotator.getRequests(); 135 Assert.assertEquals(1, actualRequests.size()); 136 BatchAnnotateFilesRequest actualRequest = ((BatchAnnotateFilesRequest) actualRequests.get(0)); 137 138 Assert.assertEquals(requests, actualRequest.getRequestsList()); 139 Assert.assertTrue( 140 channelProvider.isHeaderSent( 141 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 142 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 143 } 144 145 @Test batchAnnotateFilesExceptionTest()146 public void batchAnnotateFilesExceptionTest() throws Exception { 147 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 148 mockImageAnnotator.addException(exception); 149 150 try { 151 List<AnnotateFileRequest> requests = new ArrayList<>(); 152 client.batchAnnotateFiles(requests); 153 Assert.fail("No exception raised"); 154 } catch (InvalidArgumentException e) { 155 // Expected exception. 156 } 157 } 158 159 @Test asyncBatchAnnotateImagesTest()160 public void asyncBatchAnnotateImagesTest() throws Exception { 161 AsyncBatchAnnotateImagesResponse expectedResponse = 162 AsyncBatchAnnotateImagesResponse.newBuilder() 163 .setOutputConfig(OutputConfig.newBuilder().build()) 164 .build(); 165 Operation resultOperation = 166 Operation.newBuilder() 167 .setName("asyncBatchAnnotateImagesTest") 168 .setDone(true) 169 .setResponse(Any.pack(expectedResponse)) 170 .build(); 171 mockImageAnnotator.addResponse(resultOperation); 172 173 List<AnnotateImageRequest> requests = new ArrayList<>(); 174 OutputConfig outputConfig = OutputConfig.newBuilder().build(); 175 176 AsyncBatchAnnotateImagesResponse actualResponse = 177 client.asyncBatchAnnotateImagesAsync(requests, outputConfig).get(); 178 Assert.assertEquals(expectedResponse, actualResponse); 179 180 List<AbstractMessage> actualRequests = mockImageAnnotator.getRequests(); 181 Assert.assertEquals(1, actualRequests.size()); 182 AsyncBatchAnnotateImagesRequest actualRequest = 183 ((AsyncBatchAnnotateImagesRequest) actualRequests.get(0)); 184 185 Assert.assertEquals(requests, actualRequest.getRequestsList()); 186 Assert.assertEquals(outputConfig, actualRequest.getOutputConfig()); 187 Assert.assertTrue( 188 channelProvider.isHeaderSent( 189 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 190 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 191 } 192 193 @Test asyncBatchAnnotateImagesExceptionTest()194 public void asyncBatchAnnotateImagesExceptionTest() throws Exception { 195 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 196 mockImageAnnotator.addException(exception); 197 198 try { 199 List<AnnotateImageRequest> requests = new ArrayList<>(); 200 OutputConfig outputConfig = OutputConfig.newBuilder().build(); 201 client.asyncBatchAnnotateImagesAsync(requests, outputConfig).get(); 202 Assert.fail("No exception raised"); 203 } catch (ExecutionException e) { 204 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 205 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 206 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 207 } 208 } 209 210 @Test asyncBatchAnnotateFilesTest()211 public void asyncBatchAnnotateFilesTest() throws Exception { 212 AsyncBatchAnnotateFilesResponse expectedResponse = 213 AsyncBatchAnnotateFilesResponse.newBuilder() 214 .addAllResponses(new ArrayList<AsyncAnnotateFileResponse>()) 215 .build(); 216 Operation resultOperation = 217 Operation.newBuilder() 218 .setName("asyncBatchAnnotateFilesTest") 219 .setDone(true) 220 .setResponse(Any.pack(expectedResponse)) 221 .build(); 222 mockImageAnnotator.addResponse(resultOperation); 223 224 List<AsyncAnnotateFileRequest> requests = new ArrayList<>(); 225 226 AsyncBatchAnnotateFilesResponse actualResponse = 227 client.asyncBatchAnnotateFilesAsync(requests).get(); 228 Assert.assertEquals(expectedResponse, actualResponse); 229 230 List<AbstractMessage> actualRequests = mockImageAnnotator.getRequests(); 231 Assert.assertEquals(1, actualRequests.size()); 232 AsyncBatchAnnotateFilesRequest actualRequest = 233 ((AsyncBatchAnnotateFilesRequest) actualRequests.get(0)); 234 235 Assert.assertEquals(requests, actualRequest.getRequestsList()); 236 Assert.assertTrue( 237 channelProvider.isHeaderSent( 238 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 239 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 240 } 241 242 @Test asyncBatchAnnotateFilesExceptionTest()243 public void asyncBatchAnnotateFilesExceptionTest() throws Exception { 244 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 245 mockImageAnnotator.addException(exception); 246 247 try { 248 List<AsyncAnnotateFileRequest> requests = new ArrayList<>(); 249 client.asyncBatchAnnotateFilesAsync(requests).get(); 250 Assert.fail("No exception raised"); 251 } catch (ExecutionException e) { 252 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 253 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 254 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 255 } 256 } 257 } 258