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.retail.v2; 18 19 import com.google.api.core.BetaApi; 20 import com.google.cloud.retail.v2.PredictionServiceGrpc.PredictionServiceImplBase; 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 MockPredictionServiceImpl extends PredictionServiceImplBase { 32 private List<AbstractMessage> requests; 33 private Queue<Object> responses; 34 MockPredictionServiceImpl()35 public MockPredictionServiceImpl() { 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 predict(PredictRequest request, StreamObserver<PredictResponse> responseObserver)62 public void predict(PredictRequest request, StreamObserver<PredictResponse> responseObserver) { 63 Object response = responses.poll(); 64 if (response instanceof PredictResponse) { 65 requests.add(request); 66 responseObserver.onNext(((PredictResponse) response)); 67 responseObserver.onCompleted(); 68 } else if (response instanceof Exception) { 69 responseObserver.onError(((Exception) response)); 70 } else { 71 responseObserver.onError( 72 new IllegalArgumentException( 73 String.format( 74 "Unrecognized response type %s for method Predict, expected %s or %s", 75 response == null ? "null" : response.getClass().getName(), 76 PredictResponse.class.getName(), 77 Exception.class.getName()))); 78 } 79 } 80 } 81