• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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
getAgent(GetAgentRequest request, StreamObserver<Agent> responseObserver)64   public void getAgent(GetAgentRequest request, StreamObserver<Agent> responseObserver) {
65     Object response = responses.poll();
66     if (response instanceof Agent) {
67       requests.add(request);
68       responseObserver.onNext(((Agent) 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 GetAgent, expected %s or %s",
77                   response == null ? "null" : response.getClass().getName(),
78                   Agent.class.getName(),
79                   Exception.class.getName())));
80     }
81   }
82 
83   @Override
setAgent(SetAgentRequest request, StreamObserver<Agent> responseObserver)84   public void setAgent(SetAgentRequest request, StreamObserver<Agent> responseObserver) {
85     Object response = responses.poll();
86     if (response instanceof Agent) {
87       requests.add(request);
88       responseObserver.onNext(((Agent) 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 SetAgent, expected %s or %s",
97                   response == null ? "null" : response.getClass().getName(),
98                   Agent.class.getName(),
99                   Exception.class.getName())));
100     }
101   }
102 
103   @Override
deleteAgent(DeleteAgentRequest request, StreamObserver<Empty> responseObserver)104   public void deleteAgent(DeleteAgentRequest request, StreamObserver<Empty> responseObserver) {
105     Object response = responses.poll();
106     if (response instanceof Empty) {
107       requests.add(request);
108       responseObserver.onNext(((Empty) response));
109       responseObserver.onCompleted();
110     } else if (response instanceof Exception) {
111       responseObserver.onError(((Exception) response));
112     } else {
113       responseObserver.onError(
114           new IllegalArgumentException(
115               String.format(
116                   "Unrecognized response type %s for method DeleteAgent, expected %s or %s",
117                   response == null ? "null" : response.getClass().getName(),
118                   Empty.class.getName(),
119                   Exception.class.getName())));
120     }
121   }
122 
123   @Override
searchAgents( SearchAgentsRequest request, StreamObserver<SearchAgentsResponse> responseObserver)124   public void searchAgents(
125       SearchAgentsRequest request, StreamObserver<SearchAgentsResponse> responseObserver) {
126     Object response = responses.poll();
127     if (response instanceof SearchAgentsResponse) {
128       requests.add(request);
129       responseObserver.onNext(((SearchAgentsResponse) 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 SearchAgents, expected %s or %s",
138                   response == null ? "null" : response.getClass().getName(),
139                   SearchAgentsResponse.class.getName(),
140                   Exception.class.getName())));
141     }
142   }
143 
144   @Override
trainAgent(TrainAgentRequest request, StreamObserver<Operation> responseObserver)145   public void trainAgent(TrainAgentRequest request, StreamObserver<Operation> responseObserver) {
146     Object response = responses.poll();
147     if (response instanceof Operation) {
148       requests.add(request);
149       responseObserver.onNext(((Operation) 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 TrainAgent, expected %s or %s",
158                   response == null ? "null" : response.getClass().getName(),
159                   Operation.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
importAgent(ImportAgentRequest request, StreamObserver<Operation> responseObserver)185   public void importAgent(ImportAgentRequest request, StreamObserver<Operation> responseObserver) {
186     Object response = responses.poll();
187     if (response instanceof Operation) {
188       requests.add(request);
189       responseObserver.onNext(((Operation) response));
190       responseObserver.onCompleted();
191     } else if (response instanceof Exception) {
192       responseObserver.onError(((Exception) response));
193     } else {
194       responseObserver.onError(
195           new IllegalArgumentException(
196               String.format(
197                   "Unrecognized response type %s for method ImportAgent, expected %s or %s",
198                   response == null ? "null" : response.getClass().getName(),
199                   Operation.class.getName(),
200                   Exception.class.getName())));
201     }
202   }
203 
204   @Override
restoreAgent( RestoreAgentRequest request, StreamObserver<Operation> responseObserver)205   public void restoreAgent(
206       RestoreAgentRequest request, StreamObserver<Operation> responseObserver) {
207     Object response = responses.poll();
208     if (response instanceof Operation) {
209       requests.add(request);
210       responseObserver.onNext(((Operation) response));
211       responseObserver.onCompleted();
212     } else if (response instanceof Exception) {
213       responseObserver.onError(((Exception) response));
214     } else {
215       responseObserver.onError(
216           new IllegalArgumentException(
217               String.format(
218                   "Unrecognized response type %s for method RestoreAgent, expected %s or %s",
219                   response == null ? "null" : response.getClass().getName(),
220                   Operation.class.getName(),
221                   Exception.class.getName())));
222     }
223   }
224 
225   @Override
getValidationResult( GetValidationResultRequest request, StreamObserver<ValidationResult> responseObserver)226   public void getValidationResult(
227       GetValidationResultRequest request, StreamObserver<ValidationResult> responseObserver) {
228     Object response = responses.poll();
229     if (response instanceof ValidationResult) {
230       requests.add(request);
231       responseObserver.onNext(((ValidationResult) response));
232       responseObserver.onCompleted();
233     } else if (response instanceof Exception) {
234       responseObserver.onError(((Exception) response));
235     } else {
236       responseObserver.onError(
237           new IllegalArgumentException(
238               String.format(
239                   "Unrecognized response type %s for method GetValidationResult, expected %s or %s",
240                   response == null ? "null" : response.getClass().getName(),
241                   ValidationResult.class.getName(),
242                   Exception.class.getName())));
243     }
244   }
245 }
246