• 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.retail.v2;
18 
19 import com.google.api.core.BetaApi;
20 import com.google.cloud.retail.v2.ProductServiceGrpc.ProductServiceImplBase;
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 MockProductServiceImpl extends ProductServiceImplBase {
34   private List<AbstractMessage> requests;
35   private Queue<Object> responses;
36 
MockProductServiceImpl()37   public MockProductServiceImpl() {
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
createProduct( CreateProductRequest request, StreamObserver<Product> responseObserver)64   public void createProduct(
65       CreateProductRequest request, StreamObserver<Product> responseObserver) {
66     Object response = responses.poll();
67     if (response instanceof Product) {
68       requests.add(request);
69       responseObserver.onNext(((Product) 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 CreateProduct, expected %s or %s",
78                   response == null ? "null" : response.getClass().getName(),
79                   Product.class.getName(),
80                   Exception.class.getName())));
81     }
82   }
83 
84   @Override
getProduct(GetProductRequest request, StreamObserver<Product> responseObserver)85   public void getProduct(GetProductRequest request, StreamObserver<Product> responseObserver) {
86     Object response = responses.poll();
87     if (response instanceof Product) {
88       requests.add(request);
89       responseObserver.onNext(((Product) 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 GetProduct, expected %s or %s",
98                   response == null ? "null" : response.getClass().getName(),
99                   Product.class.getName(),
100                   Exception.class.getName())));
101     }
102   }
103 
104   @Override
listProducts( ListProductsRequest request, StreamObserver<ListProductsResponse> responseObserver)105   public void listProducts(
106       ListProductsRequest request, StreamObserver<ListProductsResponse> responseObserver) {
107     Object response = responses.poll();
108     if (response instanceof ListProductsResponse) {
109       requests.add(request);
110       responseObserver.onNext(((ListProductsResponse) 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 ListProducts, expected %s or %s",
119                   response == null ? "null" : response.getClass().getName(),
120                   ListProductsResponse.class.getName(),
121                   Exception.class.getName())));
122     }
123   }
124 
125   @Override
updateProduct( UpdateProductRequest request, StreamObserver<Product> responseObserver)126   public void updateProduct(
127       UpdateProductRequest request, StreamObserver<Product> responseObserver) {
128     Object response = responses.poll();
129     if (response instanceof Product) {
130       requests.add(request);
131       responseObserver.onNext(((Product) 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 UpdateProduct, expected %s or %s",
140                   response == null ? "null" : response.getClass().getName(),
141                   Product.class.getName(),
142                   Exception.class.getName())));
143     }
144   }
145 
146   @Override
deleteProduct(DeleteProductRequest request, StreamObserver<Empty> responseObserver)147   public void deleteProduct(DeleteProductRequest request, StreamObserver<Empty> responseObserver) {
148     Object response = responses.poll();
149     if (response instanceof Empty) {
150       requests.add(request);
151       responseObserver.onNext(((Empty) response));
152       responseObserver.onCompleted();
153     } else if (response instanceof Exception) {
154       responseObserver.onError(((Exception) response));
155     } else {
156       responseObserver.onError(
157           new IllegalArgumentException(
158               String.format(
159                   "Unrecognized response type %s for method DeleteProduct, expected %s or %s",
160                   response == null ? "null" : response.getClass().getName(),
161                   Empty.class.getName(),
162                   Exception.class.getName())));
163     }
164   }
165 
166   @Override
importProducts( ImportProductsRequest request, StreamObserver<Operation> responseObserver)167   public void importProducts(
168       ImportProductsRequest request, StreamObserver<Operation> responseObserver) {
169     Object response = responses.poll();
170     if (response instanceof Operation) {
171       requests.add(request);
172       responseObserver.onNext(((Operation) response));
173       responseObserver.onCompleted();
174     } else if (response instanceof Exception) {
175       responseObserver.onError(((Exception) response));
176     } else {
177       responseObserver.onError(
178           new IllegalArgumentException(
179               String.format(
180                   "Unrecognized response type %s for method ImportProducts, expected %s or %s",
181                   response == null ? "null" : response.getClass().getName(),
182                   Operation.class.getName(),
183                   Exception.class.getName())));
184     }
185   }
186 
187   @Override
setInventory( SetInventoryRequest request, StreamObserver<Operation> responseObserver)188   public void setInventory(
189       SetInventoryRequest request, StreamObserver<Operation> responseObserver) {
190     Object response = responses.poll();
191     if (response instanceof Operation) {
192       requests.add(request);
193       responseObserver.onNext(((Operation) response));
194       responseObserver.onCompleted();
195     } else if (response instanceof Exception) {
196       responseObserver.onError(((Exception) response));
197     } else {
198       responseObserver.onError(
199           new IllegalArgumentException(
200               String.format(
201                   "Unrecognized response type %s for method SetInventory, expected %s or %s",
202                   response == null ? "null" : response.getClass().getName(),
203                   Operation.class.getName(),
204                   Exception.class.getName())));
205     }
206   }
207 
208   @Override
addFulfillmentPlaces( AddFulfillmentPlacesRequest request, StreamObserver<Operation> responseObserver)209   public void addFulfillmentPlaces(
210       AddFulfillmentPlacesRequest request, StreamObserver<Operation> responseObserver) {
211     Object response = responses.poll();
212     if (response instanceof Operation) {
213       requests.add(request);
214       responseObserver.onNext(((Operation) response));
215       responseObserver.onCompleted();
216     } else if (response instanceof Exception) {
217       responseObserver.onError(((Exception) response));
218     } else {
219       responseObserver.onError(
220           new IllegalArgumentException(
221               String.format(
222                   "Unrecognized response type %s for method AddFulfillmentPlaces, expected %s or %s",
223                   response == null ? "null" : response.getClass().getName(),
224                   Operation.class.getName(),
225                   Exception.class.getName())));
226     }
227   }
228 
229   @Override
removeFulfillmentPlaces( RemoveFulfillmentPlacesRequest request, StreamObserver<Operation> responseObserver)230   public void removeFulfillmentPlaces(
231       RemoveFulfillmentPlacesRequest request, StreamObserver<Operation> responseObserver) {
232     Object response = responses.poll();
233     if (response instanceof Operation) {
234       requests.add(request);
235       responseObserver.onNext(((Operation) response));
236       responseObserver.onCompleted();
237     } else if (response instanceof Exception) {
238       responseObserver.onError(((Exception) response));
239     } else {
240       responseObserver.onError(
241           new IllegalArgumentException(
242               String.format(
243                   "Unrecognized response type %s for method RemoveFulfillmentPlaces, expected %s or %s",
244                   response == null ? "null" : response.getClass().getName(),
245                   Operation.class.getName(),
246                   Exception.class.getName())));
247     }
248   }
249 
250   @Override
addLocalInventories( AddLocalInventoriesRequest request, StreamObserver<Operation> responseObserver)251   public void addLocalInventories(
252       AddLocalInventoriesRequest request, StreamObserver<Operation> responseObserver) {
253     Object response = responses.poll();
254     if (response instanceof Operation) {
255       requests.add(request);
256       responseObserver.onNext(((Operation) response));
257       responseObserver.onCompleted();
258     } else if (response instanceof Exception) {
259       responseObserver.onError(((Exception) response));
260     } else {
261       responseObserver.onError(
262           new IllegalArgumentException(
263               String.format(
264                   "Unrecognized response type %s for method AddLocalInventories, expected %s or %s",
265                   response == null ? "null" : response.getClass().getName(),
266                   Operation.class.getName(),
267                   Exception.class.getName())));
268     }
269   }
270 
271   @Override
removeLocalInventories( RemoveLocalInventoriesRequest request, StreamObserver<Operation> responseObserver)272   public void removeLocalInventories(
273       RemoveLocalInventoriesRequest request, StreamObserver<Operation> responseObserver) {
274     Object response = responses.poll();
275     if (response instanceof Operation) {
276       requests.add(request);
277       responseObserver.onNext(((Operation) response));
278       responseObserver.onCompleted();
279     } else if (response instanceof Exception) {
280       responseObserver.onError(((Exception) response));
281     } else {
282       responseObserver.onError(
283           new IllegalArgumentException(
284               String.format(
285                   "Unrecognized response type %s for method RemoveLocalInventories, expected %s or %s",
286                   response == null ? "null" : response.getClass().getName(),
287                   Operation.class.getName(),
288                   Exception.class.getName())));
289     }
290   }
291 }
292