• 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.cx.v3beta1;
18 
19 import com.google.api.core.BetaApi;
20 import com.google.cloud.dialogflow.cx.v3beta1.FlowsGrpc.FlowsImplBase;
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 MockFlowsImpl extends FlowsImplBase {
34   private List<AbstractMessage> requests;
35   private Queue<Object> responses;
36 
MockFlowsImpl()37   public MockFlowsImpl() {
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
createFlow(CreateFlowRequest request, StreamObserver<Flow> responseObserver)64   public void createFlow(CreateFlowRequest request, StreamObserver<Flow> responseObserver) {
65     Object response = responses.poll();
66     if (response instanceof Flow) {
67       requests.add(request);
68       responseObserver.onNext(((Flow) 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 CreateFlow, expected %s or %s",
77                   response == null ? "null" : response.getClass().getName(),
78                   Flow.class.getName(),
79                   Exception.class.getName())));
80     }
81   }
82 
83   @Override
deleteFlow(DeleteFlowRequest request, StreamObserver<Empty> responseObserver)84   public void deleteFlow(DeleteFlowRequest request, StreamObserver<Empty> responseObserver) {
85     Object response = responses.poll();
86     if (response instanceof Empty) {
87       requests.add(request);
88       responseObserver.onNext(((Empty) 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 DeleteFlow, expected %s or %s",
97                   response == null ? "null" : response.getClass().getName(),
98                   Empty.class.getName(),
99                   Exception.class.getName())));
100     }
101   }
102 
103   @Override
listFlows( ListFlowsRequest request, StreamObserver<ListFlowsResponse> responseObserver)104   public void listFlows(
105       ListFlowsRequest request, StreamObserver<ListFlowsResponse> responseObserver) {
106     Object response = responses.poll();
107     if (response instanceof ListFlowsResponse) {
108       requests.add(request);
109       responseObserver.onNext(((ListFlowsResponse) 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 ListFlows, expected %s or %s",
118                   response == null ? "null" : response.getClass().getName(),
119                   ListFlowsResponse.class.getName(),
120                   Exception.class.getName())));
121     }
122   }
123 
124   @Override
getFlow(GetFlowRequest request, StreamObserver<Flow> responseObserver)125   public void getFlow(GetFlowRequest request, StreamObserver<Flow> responseObserver) {
126     Object response = responses.poll();
127     if (response instanceof Flow) {
128       requests.add(request);
129       responseObserver.onNext(((Flow) 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 GetFlow, expected %s or %s",
138                   response == null ? "null" : response.getClass().getName(),
139                   Flow.class.getName(),
140                   Exception.class.getName())));
141     }
142   }
143 
144   @Override
updateFlow(UpdateFlowRequest request, StreamObserver<Flow> responseObserver)145   public void updateFlow(UpdateFlowRequest request, StreamObserver<Flow> responseObserver) {
146     Object response = responses.poll();
147     if (response instanceof Flow) {
148       requests.add(request);
149       responseObserver.onNext(((Flow) 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 UpdateFlow, expected %s or %s",
158                   response == null ? "null" : response.getClass().getName(),
159                   Flow.class.getName(),
160                   Exception.class.getName())));
161     }
162   }
163 
164   @Override
trainFlow(TrainFlowRequest request, StreamObserver<Operation> responseObserver)165   public void trainFlow(TrainFlowRequest 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 TrainFlow, expected %s or %s",
178                   response == null ? "null" : response.getClass().getName(),
179                   Operation.class.getName(),
180                   Exception.class.getName())));
181     }
182   }
183 
184   @Override
validateFlow( ValidateFlowRequest request, StreamObserver<FlowValidationResult> responseObserver)185   public void validateFlow(
186       ValidateFlowRequest request, StreamObserver<FlowValidationResult> responseObserver) {
187     Object response = responses.poll();
188     if (response instanceof FlowValidationResult) {
189       requests.add(request);
190       responseObserver.onNext(((FlowValidationResult) 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 ValidateFlow, expected %s or %s",
199                   response == null ? "null" : response.getClass().getName(),
200                   FlowValidationResult.class.getName(),
201                   Exception.class.getName())));
202     }
203   }
204 
205   @Override
getFlowValidationResult( GetFlowValidationResultRequest request, StreamObserver<FlowValidationResult> responseObserver)206   public void getFlowValidationResult(
207       GetFlowValidationResultRequest request,
208       StreamObserver<FlowValidationResult> responseObserver) {
209     Object response = responses.poll();
210     if (response instanceof FlowValidationResult) {
211       requests.add(request);
212       responseObserver.onNext(((FlowValidationResult) response));
213       responseObserver.onCompleted();
214     } else if (response instanceof Exception) {
215       responseObserver.onError(((Exception) response));
216     } else {
217       responseObserver.onError(
218           new IllegalArgumentException(
219               String.format(
220                   "Unrecognized response type %s for method GetFlowValidationResult, expected %s or %s",
221                   response == null ? "null" : response.getClass().getName(),
222                   FlowValidationResult.class.getName(),
223                   Exception.class.getName())));
224     }
225   }
226 
227   @Override
importFlow(ImportFlowRequest request, StreamObserver<Operation> responseObserver)228   public void importFlow(ImportFlowRequest request, StreamObserver<Operation> responseObserver) {
229     Object response = responses.poll();
230     if (response instanceof Operation) {
231       requests.add(request);
232       responseObserver.onNext(((Operation) response));
233       responseObserver.onCompleted();
234     } else if (response instanceof Exception) {
235       responseObserver.onError(((Exception) response));
236     } else {
237       responseObserver.onError(
238           new IllegalArgumentException(
239               String.format(
240                   "Unrecognized response type %s for method ImportFlow, expected %s or %s",
241                   response == null ? "null" : response.getClass().getName(),
242                   Operation.class.getName(),
243                   Exception.class.getName())));
244     }
245   }
246 
247   @Override
exportFlow(ExportFlowRequest request, StreamObserver<Operation> responseObserver)248   public void exportFlow(ExportFlowRequest request, StreamObserver<Operation> responseObserver) {
249     Object response = responses.poll();
250     if (response instanceof Operation) {
251       requests.add(request);
252       responseObserver.onNext(((Operation) response));
253       responseObserver.onCompleted();
254     } else if (response instanceof Exception) {
255       responseObserver.onError(((Exception) response));
256     } else {
257       responseObserver.onError(
258           new IllegalArgumentException(
259               String.format(
260                   "Unrecognized response type %s for method ExportFlow, expected %s or %s",
261                   response == null ? "null" : response.getClass().getName(),
262                   Operation.class.getName(),
263                   Exception.class.getName())));
264     }
265   }
266 }
267