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.language.v1; 18 19 import com.google.api.core.BetaApi; 20 import com.google.cloud.language.v1.LanguageServiceGrpc.LanguageServiceImplBase; 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 MockLanguageServiceImpl extends LanguageServiceImplBase { 32 private List<AbstractMessage> requests; 33 private Queue<Object> responses; 34 MockLanguageServiceImpl()35 public MockLanguageServiceImpl() { 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 analyzeSentiment( AnalyzeSentimentRequest request, StreamObserver<AnalyzeSentimentResponse> responseObserver)62 public void analyzeSentiment( 63 AnalyzeSentimentRequest request, StreamObserver<AnalyzeSentimentResponse> responseObserver) { 64 Object response = responses.poll(); 65 if (response instanceof AnalyzeSentimentResponse) { 66 requests.add(request); 67 responseObserver.onNext(((AnalyzeSentimentResponse) 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 AnalyzeSentiment, expected %s or %s", 76 response == null ? "null" : response.getClass().getName(), 77 AnalyzeSentimentResponse.class.getName(), 78 Exception.class.getName()))); 79 } 80 } 81 82 @Override analyzeEntities( AnalyzeEntitiesRequest request, StreamObserver<AnalyzeEntitiesResponse> responseObserver)83 public void analyzeEntities( 84 AnalyzeEntitiesRequest request, StreamObserver<AnalyzeEntitiesResponse> responseObserver) { 85 Object response = responses.poll(); 86 if (response instanceof AnalyzeEntitiesResponse) { 87 requests.add(request); 88 responseObserver.onNext(((AnalyzeEntitiesResponse) 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 AnalyzeEntities, expected %s or %s", 97 response == null ? "null" : response.getClass().getName(), 98 AnalyzeEntitiesResponse.class.getName(), 99 Exception.class.getName()))); 100 } 101 } 102 103 @Override analyzeEntitySentiment( AnalyzeEntitySentimentRequest request, StreamObserver<AnalyzeEntitySentimentResponse> responseObserver)104 public void analyzeEntitySentiment( 105 AnalyzeEntitySentimentRequest request, 106 StreamObserver<AnalyzeEntitySentimentResponse> responseObserver) { 107 Object response = responses.poll(); 108 if (response instanceof AnalyzeEntitySentimentResponse) { 109 requests.add(request); 110 responseObserver.onNext(((AnalyzeEntitySentimentResponse) response)); 111 responseObserver.onCompleted(); 112 } else if (response instanceof Exception) { 113 responseObserver.onError(((Exception) response)); 114 } else { 115 responseObserver.onError( 116 new IllegalArgumentException( 117 String.format( 118 "Unrecognized response type %s for method AnalyzeEntitySentiment, expected %s or %s", 119 response == null ? "null" : response.getClass().getName(), 120 AnalyzeEntitySentimentResponse.class.getName(), 121 Exception.class.getName()))); 122 } 123 } 124 125 @Override analyzeSyntax( AnalyzeSyntaxRequest request, StreamObserver<AnalyzeSyntaxResponse> responseObserver)126 public void analyzeSyntax( 127 AnalyzeSyntaxRequest request, StreamObserver<AnalyzeSyntaxResponse> responseObserver) { 128 Object response = responses.poll(); 129 if (response instanceof AnalyzeSyntaxResponse) { 130 requests.add(request); 131 responseObserver.onNext(((AnalyzeSyntaxResponse) response)); 132 responseObserver.onCompleted(); 133 } else if (response instanceof Exception) { 134 responseObserver.onError(((Exception) response)); 135 } else { 136 responseObserver.onError( 137 new IllegalArgumentException( 138 String.format( 139 "Unrecognized response type %s for method AnalyzeSyntax, expected %s or %s", 140 response == null ? "null" : response.getClass().getName(), 141 AnalyzeSyntaxResponse.class.getName(), 142 Exception.class.getName()))); 143 } 144 } 145 146 @Override classifyText( ClassifyTextRequest request, StreamObserver<ClassifyTextResponse> responseObserver)147 public void classifyText( 148 ClassifyTextRequest request, StreamObserver<ClassifyTextResponse> responseObserver) { 149 Object response = responses.poll(); 150 if (response instanceof ClassifyTextResponse) { 151 requests.add(request); 152 responseObserver.onNext(((ClassifyTextResponse) response)); 153 responseObserver.onCompleted(); 154 } else if (response instanceof Exception) { 155 responseObserver.onError(((Exception) response)); 156 } else { 157 responseObserver.onError( 158 new IllegalArgumentException( 159 String.format( 160 "Unrecognized response type %s for method ClassifyText, expected %s or %s", 161 response == null ? "null" : response.getClass().getName(), 162 ClassifyTextResponse.class.getName(), 163 Exception.class.getName()))); 164 } 165 } 166 167 @Override annotateText( AnnotateTextRequest request, StreamObserver<AnnotateTextResponse> responseObserver)168 public void annotateText( 169 AnnotateTextRequest request, StreamObserver<AnnotateTextResponse> responseObserver) { 170 Object response = responses.poll(); 171 if (response instanceof AnnotateTextResponse) { 172 requests.add(request); 173 responseObserver.onNext(((AnnotateTextResponse) response)); 174 responseObserver.onCompleted(); 175 } else if (response instanceof Exception) { 176 responseObserver.onError(((Exception) response)); 177 } else { 178 responseObserver.onError( 179 new IllegalArgumentException( 180 String.format( 181 "Unrecognized response type %s for method AnnotateText, expected %s or %s", 182 response == null ? "null" : response.getClass().getName(), 183 AnnotateTextResponse.class.getName(), 184 Exception.class.getName()))); 185 } 186 } 187 } 188