• 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.IntentsGrpc.IntentsImplBase;
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 MockIntentsImpl extends IntentsImplBase {
34   private List<AbstractMessage> requests;
35   private Queue<Object> responses;
36 
MockIntentsImpl()37   public MockIntentsImpl() {
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
listIntents( ListIntentsRequest request, StreamObserver<ListIntentsResponse> responseObserver)64   public void listIntents(
65       ListIntentsRequest request, StreamObserver<ListIntentsResponse> responseObserver) {
66     Object response = responses.poll();
67     if (response instanceof ListIntentsResponse) {
68       requests.add(request);
69       responseObserver.onNext(((ListIntentsResponse) 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 ListIntents, expected %s or %s",
78                   response == null ? "null" : response.getClass().getName(),
79                   ListIntentsResponse.class.getName(),
80                   Exception.class.getName())));
81     }
82   }
83 
84   @Override
getIntent(GetIntentRequest request, StreamObserver<Intent> responseObserver)85   public void getIntent(GetIntentRequest request, StreamObserver<Intent> responseObserver) {
86     Object response = responses.poll();
87     if (response instanceof Intent) {
88       requests.add(request);
89       responseObserver.onNext(((Intent) 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 GetIntent, expected %s or %s",
98                   response == null ? "null" : response.getClass().getName(),
99                   Intent.class.getName(),
100                   Exception.class.getName())));
101     }
102   }
103 
104   @Override
createIntent(CreateIntentRequest request, StreamObserver<Intent> responseObserver)105   public void createIntent(CreateIntentRequest request, StreamObserver<Intent> responseObserver) {
106     Object response = responses.poll();
107     if (response instanceof Intent) {
108       requests.add(request);
109       responseObserver.onNext(((Intent) 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 CreateIntent, expected %s or %s",
118                   response == null ? "null" : response.getClass().getName(),
119                   Intent.class.getName(),
120                   Exception.class.getName())));
121     }
122   }
123 
124   @Override
updateIntent(UpdateIntentRequest request, StreamObserver<Intent> responseObserver)125   public void updateIntent(UpdateIntentRequest request, StreamObserver<Intent> responseObserver) {
126     Object response = responses.poll();
127     if (response instanceof Intent) {
128       requests.add(request);
129       responseObserver.onNext(((Intent) 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 UpdateIntent, expected %s or %s",
138                   response == null ? "null" : response.getClass().getName(),
139                   Intent.class.getName(),
140                   Exception.class.getName())));
141     }
142   }
143 
144   @Override
deleteIntent(DeleteIntentRequest request, StreamObserver<Empty> responseObserver)145   public void deleteIntent(DeleteIntentRequest 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 DeleteIntent, expected %s or %s",
158                   response == null ? "null" : response.getClass().getName(),
159                   Empty.class.getName(),
160                   Exception.class.getName())));
161     }
162   }
163 
164   @Override
batchUpdateIntents( BatchUpdateIntentsRequest request, StreamObserver<Operation> responseObserver)165   public void batchUpdateIntents(
166       BatchUpdateIntentsRequest request, StreamObserver<Operation> responseObserver) {
167     Object response = responses.poll();
168     if (response instanceof Operation) {
169       requests.add(request);
170       responseObserver.onNext(((Operation) response));
171       responseObserver.onCompleted();
172     } else if (response instanceof Exception) {
173       responseObserver.onError(((Exception) response));
174     } else {
175       responseObserver.onError(
176           new IllegalArgumentException(
177               String.format(
178                   "Unrecognized response type %s for method BatchUpdateIntents, expected %s or %s",
179                   response == null ? "null" : response.getClass().getName(),
180                   Operation.class.getName(),
181                   Exception.class.getName())));
182     }
183   }
184 
185   @Override
batchDeleteIntents( BatchDeleteIntentsRequest request, StreamObserver<Operation> responseObserver)186   public void batchDeleteIntents(
187       BatchDeleteIntentsRequest request, StreamObserver<Operation> responseObserver) {
188     Object response = responses.poll();
189     if (response instanceof Operation) {
190       requests.add(request);
191       responseObserver.onNext(((Operation) response));
192       responseObserver.onCompleted();
193     } else if (response instanceof Exception) {
194       responseObserver.onError(((Exception) response));
195     } else {
196       responseObserver.onError(
197           new IllegalArgumentException(
198               String.format(
199                   "Unrecognized response type %s for method BatchDeleteIntents, expected %s or %s",
200                   response == null ? "null" : response.getClass().getName(),
201                   Operation.class.getName(),
202                   Exception.class.getName())));
203     }
204   }
205 }
206