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.v1p2beta1; 18 19 import com.google.api.core.BetaApi; 20 import com.google.cloud.vision.v1p2beta1.ImageAnnotatorGrpc.ImageAnnotatorImplBase; 21 import com.google.longrunning.Operation; 22 import com.google.protobuf.AbstractMessage; 23 import io.grpc.stub.StreamObserver; 24 import java.util.ArrayList; 25 import java.util.LinkedList; 26 import java.util.List; 27 import java.util.Queue; 28 import javax.annotation.Generated; 29 30 @BetaApi 31 @Generated("by gapic-generator-java") 32 public class MockImageAnnotatorImpl extends ImageAnnotatorImplBase { 33 private List<AbstractMessage> requests; 34 private Queue<Object> responses; 35 MockImageAnnotatorImpl()36 public MockImageAnnotatorImpl() { 37 requests = new ArrayList<>(); 38 responses = new LinkedList<>(); 39 } 40 getRequests()41 public List<AbstractMessage> getRequests() { 42 return requests; 43 } 44 addResponse(AbstractMessage response)45 public void addResponse(AbstractMessage response) { 46 responses.add(response); 47 } 48 setResponses(List<AbstractMessage> responses)49 public void setResponses(List<AbstractMessage> responses) { 50 this.responses = new LinkedList<Object>(responses); 51 } 52 addException(Exception exception)53 public void addException(Exception exception) { 54 responses.add(exception); 55 } 56 reset()57 public void reset() { 58 requests = new ArrayList<>(); 59 responses = new LinkedList<>(); 60 } 61 62 @Override batchAnnotateImages( BatchAnnotateImagesRequest request, StreamObserver<BatchAnnotateImagesResponse> responseObserver)63 public void batchAnnotateImages( 64 BatchAnnotateImagesRequest request, 65 StreamObserver<BatchAnnotateImagesResponse> responseObserver) { 66 Object response = responses.poll(); 67 if (response instanceof BatchAnnotateImagesResponse) { 68 requests.add(request); 69 responseObserver.onNext(((BatchAnnotateImagesResponse) response)); 70 responseObserver.onCompleted(); 71 } else if (response instanceof Exception) { 72 responseObserver.onError(((Exception) response)); 73 } else { 74 responseObserver.onError( 75 new IllegalArgumentException( 76 String.format( 77 "Unrecognized response type %s for method BatchAnnotateImages, expected %s or %s", 78 response == null ? "null" : response.getClass().getName(), 79 BatchAnnotateImagesResponse.class.getName(), 80 Exception.class.getName()))); 81 } 82 } 83 84 @Override asyncBatchAnnotateFiles( AsyncBatchAnnotateFilesRequest request, StreamObserver<Operation> responseObserver)85 public void asyncBatchAnnotateFiles( 86 AsyncBatchAnnotateFilesRequest request, StreamObserver<Operation> responseObserver) { 87 Object response = responses.poll(); 88 if (response instanceof Operation) { 89 requests.add(request); 90 responseObserver.onNext(((Operation) response)); 91 responseObserver.onCompleted(); 92 } else if (response instanceof Exception) { 93 responseObserver.onError(((Exception) response)); 94 } else { 95 responseObserver.onError( 96 new IllegalArgumentException( 97 String.format( 98 "Unrecognized response type %s for method AsyncBatchAnnotateFiles, expected %s or %s", 99 response == null ? "null" : response.getClass().getName(), 100 Operation.class.getName(), 101 Exception.class.getName()))); 102 } 103 } 104 } 105