• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.v1;
18 
19 import com.google.api.core.BetaApi;
20 import com.google.cloud.vision.v1.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
batchAnnotateFiles( BatchAnnotateFilesRequest request, StreamObserver<BatchAnnotateFilesResponse> responseObserver)85   public void batchAnnotateFiles(
86       BatchAnnotateFilesRequest request,
87       StreamObserver<BatchAnnotateFilesResponse> responseObserver) {
88     Object response = responses.poll();
89     if (response instanceof BatchAnnotateFilesResponse) {
90       requests.add(request);
91       responseObserver.onNext(((BatchAnnotateFilesResponse) response));
92       responseObserver.onCompleted();
93     } else if (response instanceof Exception) {
94       responseObserver.onError(((Exception) response));
95     } else {
96       responseObserver.onError(
97           new IllegalArgumentException(
98               String.format(
99                   "Unrecognized response type %s for method BatchAnnotateFiles, expected %s or %s",
100                   response == null ? "null" : response.getClass().getName(),
101                   BatchAnnotateFilesResponse.class.getName(),
102                   Exception.class.getName())));
103     }
104   }
105 
106   @Override
asyncBatchAnnotateImages( AsyncBatchAnnotateImagesRequest request, StreamObserver<Operation> responseObserver)107   public void asyncBatchAnnotateImages(
108       AsyncBatchAnnotateImagesRequest request, StreamObserver<Operation> responseObserver) {
109     Object response = responses.poll();
110     if (response instanceof Operation) {
111       requests.add(request);
112       responseObserver.onNext(((Operation) response));
113       responseObserver.onCompleted();
114     } else if (response instanceof Exception) {
115       responseObserver.onError(((Exception) response));
116     } else {
117       responseObserver.onError(
118           new IllegalArgumentException(
119               String.format(
120                   "Unrecognized response type %s for method AsyncBatchAnnotateImages, expected %s or %s",
121                   response == null ? "null" : response.getClass().getName(),
122                   Operation.class.getName(),
123                   Exception.class.getName())));
124     }
125   }
126 
127   @Override
asyncBatchAnnotateFiles( AsyncBatchAnnotateFilesRequest request, StreamObserver<Operation> responseObserver)128   public void asyncBatchAnnotateFiles(
129       AsyncBatchAnnotateFilesRequest request, StreamObserver<Operation> responseObserver) {
130     Object response = responses.poll();
131     if (response instanceof Operation) {
132       requests.add(request);
133       responseObserver.onNext(((Operation) response));
134       responseObserver.onCompleted();
135     } else if (response instanceof Exception) {
136       responseObserver.onError(((Exception) response));
137     } else {
138       responseObserver.onError(
139           new IllegalArgumentException(
140               String.format(
141                   "Unrecognized response type %s for method AsyncBatchAnnotateFiles, expected %s or %s",
142                   response == null ? "null" : response.getClass().getName(),
143                   Operation.class.getName(),
144                   Exception.class.getName())));
145     }
146   }
147 }
148