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.cx.v3; 18 19 import com.google.api.core.BetaApi; 20 import com.google.cloud.dialogflow.cx.v3.AgentsGrpc.AgentsImplBase; 21 import com.google.longrunning.Operation; 22 import com.google.protobuf.AbstractMessage; 23 import com.google.protobuf.Empty; 24 import io.grpc.stub.StreamObserver; 25 import java.util.ArrayList; 26 import java.util.LinkedList; 27 import java.util.List; 28 import java.util.Queue; 29 import javax.annotation.Generated; 30 31 @BetaApi 32 @Generated("by gapic-generator-java") 33 public class MockAgentsImpl extends AgentsImplBase { 34 private List<AbstractMessage> requests; 35 private Queue<Object> responses; 36 MockAgentsImpl()37 public MockAgentsImpl() { 38 requests = new ArrayList<>(); 39 responses = new LinkedList<>(); 40 } 41 getRequests()42 public List<AbstractMessage> getRequests() { 43 return requests; 44 } 45 addResponse(AbstractMessage response)46 public void addResponse(AbstractMessage response) { 47 responses.add(response); 48 } 49 setResponses(List<AbstractMessage> responses)50 public void setResponses(List<AbstractMessage> responses) { 51 this.responses = new LinkedList<Object>(responses); 52 } 53 addException(Exception exception)54 public void addException(Exception exception) { 55 responses.add(exception); 56 } 57 reset()58 public void reset() { 59 requests = new ArrayList<>(); 60 responses = new LinkedList<>(); 61 } 62 63 @Override listAgents( ListAgentsRequest request, StreamObserver<ListAgentsResponse> responseObserver)64 public void listAgents( 65 ListAgentsRequest request, StreamObserver<ListAgentsResponse> responseObserver) { 66 Object response = responses.poll(); 67 if (response instanceof ListAgentsResponse) { 68 requests.add(request); 69 responseObserver.onNext(((ListAgentsResponse) response)); 70 responseObserver.onCompleted(); 71 } else if (response instanceof Exception) { 72 responseObserver.onError(((Exception) response)); 73 } else { 74 responseObserver.onError( 75 new IllegalArgumentException( 76 String.format( 77 "Unrecognized response type %s for method ListAgents, expected %s or %s", 78 response == null ? "null" : response.getClass().getName(), 79 ListAgentsResponse.class.getName(), 80 Exception.class.getName()))); 81 } 82 } 83 84 @Override getAgent(GetAgentRequest request, StreamObserver<Agent> responseObserver)85 public void getAgent(GetAgentRequest request, StreamObserver<Agent> responseObserver) { 86 Object response = responses.poll(); 87 if (response instanceof Agent) { 88 requests.add(request); 89 responseObserver.onNext(((Agent) 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 GetAgent, expected %s or %s", 98 response == null ? "null" : response.getClass().getName(), 99 Agent.class.getName(), 100 Exception.class.getName()))); 101 } 102 } 103 104 @Override createAgent(CreateAgentRequest request, StreamObserver<Agent> responseObserver)105 public void createAgent(CreateAgentRequest request, StreamObserver<Agent> responseObserver) { 106 Object response = responses.poll(); 107 if (response instanceof Agent) { 108 requests.add(request); 109 responseObserver.onNext(((Agent) 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 CreateAgent, expected %s or %s", 118 response == null ? "null" : response.getClass().getName(), 119 Agent.class.getName(), 120 Exception.class.getName()))); 121 } 122 } 123 124 @Override updateAgent(UpdateAgentRequest request, StreamObserver<Agent> responseObserver)125 public void updateAgent(UpdateAgentRequest request, StreamObserver<Agent> responseObserver) { 126 Object response = responses.poll(); 127 if (response instanceof Agent) { 128 requests.add(request); 129 responseObserver.onNext(((Agent) response)); 130 responseObserver.onCompleted(); 131 } else if (response instanceof Exception) { 132 responseObserver.onError(((Exception) response)); 133 } else { 134 responseObserver.onError( 135 new IllegalArgumentException( 136 String.format( 137 "Unrecognized response type %s for method UpdateAgent, expected %s or %s", 138 response == null ? "null" : response.getClass().getName(), 139 Agent.class.getName(), 140 Exception.class.getName()))); 141 } 142 } 143 144 @Override deleteAgent(DeleteAgentRequest request, StreamObserver<Empty> responseObserver)145 public void deleteAgent(DeleteAgentRequest request, StreamObserver<Empty> responseObserver) { 146 Object response = responses.poll(); 147 if (response instanceof Empty) { 148 requests.add(request); 149 responseObserver.onNext(((Empty) response)); 150 responseObserver.onCompleted(); 151 } else if (response instanceof Exception) { 152 responseObserver.onError(((Exception) response)); 153 } else { 154 responseObserver.onError( 155 new IllegalArgumentException( 156 String.format( 157 "Unrecognized response type %s for method DeleteAgent, expected %s or %s", 158 response == null ? "null" : response.getClass().getName(), 159 Empty.class.getName(), 160 Exception.class.getName()))); 161 } 162 } 163 164 @Override exportAgent(ExportAgentRequest request, StreamObserver<Operation> responseObserver)165 public void exportAgent(ExportAgentRequest request, StreamObserver<Operation> responseObserver) { 166 Object response = responses.poll(); 167 if (response instanceof Operation) { 168 requests.add(request); 169 responseObserver.onNext(((Operation) response)); 170 responseObserver.onCompleted(); 171 } else if (response instanceof Exception) { 172 responseObserver.onError(((Exception) response)); 173 } else { 174 responseObserver.onError( 175 new IllegalArgumentException( 176 String.format( 177 "Unrecognized response type %s for method ExportAgent, expected %s or %s", 178 response == null ? "null" : response.getClass().getName(), 179 Operation.class.getName(), 180 Exception.class.getName()))); 181 } 182 } 183 184 @Override restoreAgent( RestoreAgentRequest request, StreamObserver<Operation> responseObserver)185 public void restoreAgent( 186 RestoreAgentRequest request, StreamObserver<Operation> responseObserver) { 187 Object response = responses.poll(); 188 if (response instanceof Operation) { 189 requests.add(request); 190 responseObserver.onNext(((Operation) response)); 191 responseObserver.onCompleted(); 192 } else if (response instanceof Exception) { 193 responseObserver.onError(((Exception) response)); 194 } else { 195 responseObserver.onError( 196 new IllegalArgumentException( 197 String.format( 198 "Unrecognized response type %s for method RestoreAgent, expected %s or %s", 199 response == null ? "null" : response.getClass().getName(), 200 Operation.class.getName(), 201 Exception.class.getName()))); 202 } 203 } 204 205 @Override validateAgent( ValidateAgentRequest request, StreamObserver<AgentValidationResult> responseObserver)206 public void validateAgent( 207 ValidateAgentRequest request, StreamObserver<AgentValidationResult> responseObserver) { 208 Object response = responses.poll(); 209 if (response instanceof AgentValidationResult) { 210 requests.add(request); 211 responseObserver.onNext(((AgentValidationResult) response)); 212 responseObserver.onCompleted(); 213 } else if (response instanceof Exception) { 214 responseObserver.onError(((Exception) response)); 215 } else { 216 responseObserver.onError( 217 new IllegalArgumentException( 218 String.format( 219 "Unrecognized response type %s for method ValidateAgent, expected %s or %s", 220 response == null ? "null" : response.getClass().getName(), 221 AgentValidationResult.class.getName(), 222 Exception.class.getName()))); 223 } 224 } 225 226 @Override getAgentValidationResult( GetAgentValidationResultRequest request, StreamObserver<AgentValidationResult> responseObserver)227 public void getAgentValidationResult( 228 GetAgentValidationResultRequest request, 229 StreamObserver<AgentValidationResult> responseObserver) { 230 Object response = responses.poll(); 231 if (response instanceof AgentValidationResult) { 232 requests.add(request); 233 responseObserver.onNext(((AgentValidationResult) response)); 234 responseObserver.onCompleted(); 235 } else if (response instanceof Exception) { 236 responseObserver.onError(((Exception) response)); 237 } else { 238 responseObserver.onError( 239 new IllegalArgumentException( 240 String.format( 241 "Unrecognized response type %s for method GetAgentValidationResult, expected %s or %s", 242 response == null ? "null" : response.getClass().getName(), 243 AgentValidationResult.class.getName(), 244 Exception.class.getName()))); 245 } 246 } 247 } 248