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.talent.v4; 18 19 import com.google.api.core.BetaApi; 20 import com.google.cloud.talent.v4.CompletionGrpc.CompletionImplBase; 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 MockCompletionImpl extends CompletionImplBase { 32 private List<AbstractMessage> requests; 33 private Queue<Object> responses; 34 MockCompletionImpl()35 public MockCompletionImpl() { 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 completeQuery( CompleteQueryRequest request, StreamObserver<CompleteQueryResponse> responseObserver)62 public void completeQuery( 63 CompleteQueryRequest request, StreamObserver<CompleteQueryResponse> responseObserver) { 64 Object response = responses.poll(); 65 if (response instanceof CompleteQueryResponse) { 66 requests.add(request); 67 responseObserver.onNext(((CompleteQueryResponse) 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 CompleteQuery, expected %s or %s", 76 response == null ? "null" : response.getClass().getName(), 77 CompleteQueryResponse.class.getName(), 78 Exception.class.getName()))); 79 } 80 } 81 } 82