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.dialogflow.v2; 18 19 import com.google.api.core.BetaApi; 20 import com.google.cloud.dialogflow.v2.DocumentsGrpc.DocumentsImplBase; 21 import com.google.longrunning.Operation; 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 MockDocumentsImpl extends DocumentsImplBase { 33 private List<AbstractMessage> requests; 34 private Queue<Object> responses; 35 MockDocumentsImpl()36 public MockDocumentsImpl() { 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 listDocuments( ListDocumentsRequest request, StreamObserver<ListDocumentsResponse> responseObserver)63 public void listDocuments( 64 ListDocumentsRequest request, StreamObserver<ListDocumentsResponse> responseObserver) { 65 Object response = responses.poll(); 66 if (response instanceof ListDocumentsResponse) { 67 requests.add(request); 68 responseObserver.onNext(((ListDocumentsResponse) response)); 69 responseObserver.onCompleted(); 70 } else if (response instanceof Exception) { 71 responseObserver.onError(((Exception) response)); 72 } else { 73 responseObserver.onError( 74 new IllegalArgumentException( 75 String.format( 76 "Unrecognized response type %s for method ListDocuments, expected %s or %s", 77 response == null ? "null" : response.getClass().getName(), 78 ListDocumentsResponse.class.getName(), 79 Exception.class.getName()))); 80 } 81 } 82 83 @Override getDocument(GetDocumentRequest request, StreamObserver<Document> responseObserver)84 public void getDocument(GetDocumentRequest request, StreamObserver<Document> responseObserver) { 85 Object response = responses.poll(); 86 if (response instanceof Document) { 87 requests.add(request); 88 responseObserver.onNext(((Document) 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 GetDocument, expected %s or %s", 97 response == null ? "null" : response.getClass().getName(), 98 Document.class.getName(), 99 Exception.class.getName()))); 100 } 101 } 102 103 @Override createDocument( CreateDocumentRequest request, StreamObserver<Operation> responseObserver)104 public void createDocument( 105 CreateDocumentRequest request, StreamObserver<Operation> responseObserver) { 106 Object response = responses.poll(); 107 if (response instanceof Operation) { 108 requests.add(request); 109 responseObserver.onNext(((Operation) response)); 110 responseObserver.onCompleted(); 111 } else if (response instanceof Exception) { 112 responseObserver.onError(((Exception) response)); 113 } else { 114 responseObserver.onError( 115 new IllegalArgumentException( 116 String.format( 117 "Unrecognized response type %s for method CreateDocument, expected %s or %s", 118 response == null ? "null" : response.getClass().getName(), 119 Operation.class.getName(), 120 Exception.class.getName()))); 121 } 122 } 123 124 @Override importDocuments( ImportDocumentsRequest request, StreamObserver<Operation> responseObserver)125 public void importDocuments( 126 ImportDocumentsRequest request, StreamObserver<Operation> responseObserver) { 127 Object response = responses.poll(); 128 if (response instanceof Operation) { 129 requests.add(request); 130 responseObserver.onNext(((Operation) response)); 131 responseObserver.onCompleted(); 132 } else if (response instanceof Exception) { 133 responseObserver.onError(((Exception) response)); 134 } else { 135 responseObserver.onError( 136 new IllegalArgumentException( 137 String.format( 138 "Unrecognized response type %s for method ImportDocuments, expected %s or %s", 139 response == null ? "null" : response.getClass().getName(), 140 Operation.class.getName(), 141 Exception.class.getName()))); 142 } 143 } 144 145 @Override deleteDocument( DeleteDocumentRequest request, StreamObserver<Operation> responseObserver)146 public void deleteDocument( 147 DeleteDocumentRequest request, StreamObserver<Operation> responseObserver) { 148 Object response = responses.poll(); 149 if (response instanceof Operation) { 150 requests.add(request); 151 responseObserver.onNext(((Operation) response)); 152 responseObserver.onCompleted(); 153 } else if (response instanceof Exception) { 154 responseObserver.onError(((Exception) response)); 155 } else { 156 responseObserver.onError( 157 new IllegalArgumentException( 158 String.format( 159 "Unrecognized response type %s for method DeleteDocument, expected %s or %s", 160 response == null ? "null" : response.getClass().getName(), 161 Operation.class.getName(), 162 Exception.class.getName()))); 163 } 164 } 165 166 @Override updateDocument( UpdateDocumentRequest request, StreamObserver<Operation> responseObserver)167 public void updateDocument( 168 UpdateDocumentRequest request, StreamObserver<Operation> responseObserver) { 169 Object response = responses.poll(); 170 if (response instanceof Operation) { 171 requests.add(request); 172 responseObserver.onNext(((Operation) response)); 173 responseObserver.onCompleted(); 174 } else if (response instanceof Exception) { 175 responseObserver.onError(((Exception) response)); 176 } else { 177 responseObserver.onError( 178 new IllegalArgumentException( 179 String.format( 180 "Unrecognized response type %s for method UpdateDocument, expected %s or %s", 181 response == null ? "null" : response.getClass().getName(), 182 Operation.class.getName(), 183 Exception.class.getName()))); 184 } 185 } 186 187 @Override reloadDocument( ReloadDocumentRequest request, StreamObserver<Operation> responseObserver)188 public void reloadDocument( 189 ReloadDocumentRequest request, StreamObserver<Operation> responseObserver) { 190 Object response = responses.poll(); 191 if (response instanceof Operation) { 192 requests.add(request); 193 responseObserver.onNext(((Operation) response)); 194 responseObserver.onCompleted(); 195 } else if (response instanceof Exception) { 196 responseObserver.onError(((Exception) response)); 197 } else { 198 responseObserver.onError( 199 new IllegalArgumentException( 200 String.format( 201 "Unrecognized response type %s for method ReloadDocument, expected %s or %s", 202 response == null ? "null" : response.getClass().getName(), 203 Operation.class.getName(), 204 Exception.class.getName()))); 205 } 206 } 207 208 @Override exportDocument( ExportDocumentRequest request, StreamObserver<Operation> responseObserver)209 public void exportDocument( 210 ExportDocumentRequest request, StreamObserver<Operation> responseObserver) { 211 Object response = responses.poll(); 212 if (response instanceof Operation) { 213 requests.add(request); 214 responseObserver.onNext(((Operation) response)); 215 responseObserver.onCompleted(); 216 } else if (response instanceof Exception) { 217 responseObserver.onError(((Exception) response)); 218 } else { 219 responseObserver.onError( 220 new IllegalArgumentException( 221 String.format( 222 "Unrecognized response type %s for method ExportDocument, expected %s or %s", 223 response == null ? "null" : response.getClass().getName(), 224 Operation.class.getName(), 225 Exception.class.getName()))); 226 } 227 } 228 } 229