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.ConversationsGrpc.ConversationsImplBase; 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 MockConversationsImpl extends ConversationsImplBase { 32 private List<AbstractMessage> requests; 33 private Queue<Object> responses; 34 MockConversationsImpl()35 public MockConversationsImpl() { 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 createConversation( CreateConversationRequest request, StreamObserver<Conversation> responseObserver)62 public void createConversation( 63 CreateConversationRequest request, StreamObserver<Conversation> responseObserver) { 64 Object response = responses.poll(); 65 if (response instanceof Conversation) { 66 requests.add(request); 67 responseObserver.onNext(((Conversation) 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 CreateConversation, expected %s or %s", 76 response == null ? "null" : response.getClass().getName(), 77 Conversation.class.getName(), 78 Exception.class.getName()))); 79 } 80 } 81 82 @Override listConversations( ListConversationsRequest request, StreamObserver<ListConversationsResponse> responseObserver)83 public void listConversations( 84 ListConversationsRequest request, 85 StreamObserver<ListConversationsResponse> responseObserver) { 86 Object response = responses.poll(); 87 if (response instanceof ListConversationsResponse) { 88 requests.add(request); 89 responseObserver.onNext(((ListConversationsResponse) response)); 90 responseObserver.onCompleted(); 91 } else if (response instanceof Exception) { 92 responseObserver.onError(((Exception) response)); 93 } else { 94 responseObserver.onError( 95 new IllegalArgumentException( 96 String.format( 97 "Unrecognized response type %s for method ListConversations, expected %s or %s", 98 response == null ? "null" : response.getClass().getName(), 99 ListConversationsResponse.class.getName(), 100 Exception.class.getName()))); 101 } 102 } 103 104 @Override getConversation( GetConversationRequest request, StreamObserver<Conversation> responseObserver)105 public void getConversation( 106 GetConversationRequest request, StreamObserver<Conversation> responseObserver) { 107 Object response = responses.poll(); 108 if (response instanceof Conversation) { 109 requests.add(request); 110 responseObserver.onNext(((Conversation) 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 GetConversation, expected %s or %s", 119 response == null ? "null" : response.getClass().getName(), 120 Conversation.class.getName(), 121 Exception.class.getName()))); 122 } 123 } 124 125 @Override completeConversation( CompleteConversationRequest request, StreamObserver<Conversation> responseObserver)126 public void completeConversation( 127 CompleteConversationRequest request, StreamObserver<Conversation> responseObserver) { 128 Object response = responses.poll(); 129 if (response instanceof Conversation) { 130 requests.add(request); 131 responseObserver.onNext(((Conversation) 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 CompleteConversation, expected %s or %s", 140 response == null ? "null" : response.getClass().getName(), 141 Conversation.class.getName(), 142 Exception.class.getName()))); 143 } 144 } 145 146 @Override listMessages( ListMessagesRequest request, StreamObserver<ListMessagesResponse> responseObserver)147 public void listMessages( 148 ListMessagesRequest request, StreamObserver<ListMessagesResponse> responseObserver) { 149 Object response = responses.poll(); 150 if (response instanceof ListMessagesResponse) { 151 requests.add(request); 152 responseObserver.onNext(((ListMessagesResponse) 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 ListMessages, expected %s or %s", 161 response == null ? "null" : response.getClass().getName(), 162 ListMessagesResponse.class.getName(), 163 Exception.class.getName()))); 164 } 165 } 166 167 @Override suggestConversationSummary( SuggestConversationSummaryRequest request, StreamObserver<SuggestConversationSummaryResponse> responseObserver)168 public void suggestConversationSummary( 169 SuggestConversationSummaryRequest request, 170 StreamObserver<SuggestConversationSummaryResponse> responseObserver) { 171 Object response = responses.poll(); 172 if (response instanceof SuggestConversationSummaryResponse) { 173 requests.add(request); 174 responseObserver.onNext(((SuggestConversationSummaryResponse) response)); 175 responseObserver.onCompleted(); 176 } else if (response instanceof Exception) { 177 responseObserver.onError(((Exception) response)); 178 } else { 179 responseObserver.onError( 180 new IllegalArgumentException( 181 String.format( 182 "Unrecognized response type %s for method SuggestConversationSummary, expected %s or %s", 183 response == null ? "null" : response.getClass().getName(), 184 SuggestConversationSummaryResponse.class.getName(), 185 Exception.class.getName()))); 186 } 187 } 188 189 @Override generateStatelessSummary( GenerateStatelessSummaryRequest request, StreamObserver<GenerateStatelessSummaryResponse> responseObserver)190 public void generateStatelessSummary( 191 GenerateStatelessSummaryRequest request, 192 StreamObserver<GenerateStatelessSummaryResponse> responseObserver) { 193 Object response = responses.poll(); 194 if (response instanceof GenerateStatelessSummaryResponse) { 195 requests.add(request); 196 responseObserver.onNext(((GenerateStatelessSummaryResponse) response)); 197 responseObserver.onCompleted(); 198 } else if (response instanceof Exception) { 199 responseObserver.onError(((Exception) response)); 200 } else { 201 responseObserver.onError( 202 new IllegalArgumentException( 203 String.format( 204 "Unrecognized response type %s for method GenerateStatelessSummary, expected %s or %s", 205 response == null ? "null" : response.getClass().getName(), 206 GenerateStatelessSummaryResponse.class.getName(), 207 Exception.class.getName()))); 208 } 209 } 210 } 211