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.texttospeech.v1; 18 19 import com.google.api.core.BetaApi; 20 import com.google.cloud.texttospeech.v1.TextToSpeechGrpc.TextToSpeechImplBase; 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 MockTextToSpeechImpl extends TextToSpeechImplBase { 32 private List<AbstractMessage> requests; 33 private Queue<Object> responses; 34 MockTextToSpeechImpl()35 public MockTextToSpeechImpl() { 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 listVoices( ListVoicesRequest request, StreamObserver<ListVoicesResponse> responseObserver)62 public void listVoices( 63 ListVoicesRequest request, StreamObserver<ListVoicesResponse> responseObserver) { 64 Object response = responses.poll(); 65 if (response instanceof ListVoicesResponse) { 66 requests.add(request); 67 responseObserver.onNext(((ListVoicesResponse) 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 ListVoices, expected %s or %s", 76 response == null ? "null" : response.getClass().getName(), 77 ListVoicesResponse.class.getName(), 78 Exception.class.getName()))); 79 } 80 } 81 82 @Override synthesizeSpeech( SynthesizeSpeechRequest request, StreamObserver<SynthesizeSpeechResponse> responseObserver)83 public void synthesizeSpeech( 84 SynthesizeSpeechRequest request, StreamObserver<SynthesizeSpeechResponse> responseObserver) { 85 Object response = responses.poll(); 86 if (response instanceof SynthesizeSpeechResponse) { 87 requests.add(request); 88 responseObserver.onNext(((SynthesizeSpeechResponse) response)); 89 responseObserver.onCompleted(); 90 } else if (response instanceof Exception) { 91 responseObserver.onError(((Exception) response)); 92 } else { 93 responseObserver.onError( 94 new IllegalArgumentException( 95 String.format( 96 "Unrecognized response type %s for method SynthesizeSpeech, expected %s or %s", 97 response == null ? "null" : response.getClass().getName(), 98 SynthesizeSpeechResponse.class.getName(), 99 Exception.class.getName()))); 100 } 101 } 102 } 103