• 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.aiplatform.v1;
18 
19 import com.google.api.HttpBody;
20 import com.google.api.core.BetaApi;
21 import com.google.cloud.aiplatform.v1.PredictionServiceGrpc.PredictionServiceImplBase;
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 MockPredictionServiceImpl extends PredictionServiceImplBase {
33   private List<AbstractMessage> requests;
34   private Queue<Object> responses;
35 
MockPredictionServiceImpl()36   public MockPredictionServiceImpl() {
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
predict(PredictRequest request, StreamObserver<PredictResponse> responseObserver)63   public void predict(PredictRequest request, StreamObserver<PredictResponse> responseObserver) {
64     Object response = responses.poll();
65     if (response instanceof PredictResponse) {
66       requests.add(request);
67       responseObserver.onNext(((PredictResponse) response));
68       responseObserver.onCompleted();
69     } else if (response instanceof Exception) {
70       responseObserver.onError(((Exception) response));
71     } else {
72       responseObserver.onError(
73           new IllegalArgumentException(
74               String.format(
75                   "Unrecognized response type %s for method Predict, expected %s or %s",
76                   response == null ? "null" : response.getClass().getName(),
77                   PredictResponse.class.getName(),
78                   Exception.class.getName())));
79     }
80   }
81 
82   @Override
rawPredict(RawPredictRequest request, StreamObserver<HttpBody> responseObserver)83   public void rawPredict(RawPredictRequest request, StreamObserver<HttpBody> responseObserver) {
84     Object response = responses.poll();
85     if (response instanceof HttpBody) {
86       requests.add(request);
87       responseObserver.onNext(((HttpBody) response));
88       responseObserver.onCompleted();
89     } else if (response instanceof Exception) {
90       responseObserver.onError(((Exception) response));
91     } else {
92       responseObserver.onError(
93           new IllegalArgumentException(
94               String.format(
95                   "Unrecognized response type %s for method RawPredict, expected %s or %s",
96                   response == null ? "null" : response.getClass().getName(),
97                   HttpBody.class.getName(),
98                   Exception.class.getName())));
99     }
100   }
101 
102   @Override
explain(ExplainRequest request, StreamObserver<ExplainResponse> responseObserver)103   public void explain(ExplainRequest request, StreamObserver<ExplainResponse> responseObserver) {
104     Object response = responses.poll();
105     if (response instanceof ExplainResponse) {
106       requests.add(request);
107       responseObserver.onNext(((ExplainResponse) response));
108       responseObserver.onCompleted();
109     } else if (response instanceof Exception) {
110       responseObserver.onError(((Exception) response));
111     } else {
112       responseObserver.onError(
113           new IllegalArgumentException(
114               String.format(
115                   "Unrecognized response type %s for method Explain, expected %s or %s",
116                   response == null ? "null" : response.getClass().getName(),
117                   ExplainResponse.class.getName(),
118                   Exception.class.getName())));
119     }
120   }
121 }
122