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