• 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.HttpBody;
20 import com.google.api.core.BetaApi;
21 import com.google.cloud.retail.v2.UserEventServiceGrpc.UserEventServiceImplBase;
22 import com.google.longrunning.Operation;
23 import com.google.protobuf.AbstractMessage;
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 MockUserEventServiceImpl extends UserEventServiceImplBase {
34   private List<AbstractMessage> requests;
35   private Queue<Object> responses;
36 
MockUserEventServiceImpl()37   public MockUserEventServiceImpl() {
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
writeUserEvent( WriteUserEventRequest request, StreamObserver<UserEvent> responseObserver)64   public void writeUserEvent(
65       WriteUserEventRequest request, StreamObserver<UserEvent> responseObserver) {
66     Object response = responses.poll();
67     if (response instanceof UserEvent) {
68       requests.add(request);
69       responseObserver.onNext(((UserEvent) 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 WriteUserEvent, expected %s or %s",
78                   response == null ? "null" : response.getClass().getName(),
79                   UserEvent.class.getName(),
80                   Exception.class.getName())));
81     }
82   }
83 
84   @Override
collectUserEvent( CollectUserEventRequest request, StreamObserver<HttpBody> responseObserver)85   public void collectUserEvent(
86       CollectUserEventRequest request, StreamObserver<HttpBody> responseObserver) {
87     Object response = responses.poll();
88     if (response instanceof HttpBody) {
89       requests.add(request);
90       responseObserver.onNext(((HttpBody) response));
91       responseObserver.onCompleted();
92     } else if (response instanceof Exception) {
93       responseObserver.onError(((Exception) response));
94     } else {
95       responseObserver.onError(
96           new IllegalArgumentException(
97               String.format(
98                   "Unrecognized response type %s for method CollectUserEvent, expected %s or %s",
99                   response == null ? "null" : response.getClass().getName(),
100                   HttpBody.class.getName(),
101                   Exception.class.getName())));
102     }
103   }
104 
105   @Override
purgeUserEvents( PurgeUserEventsRequest request, StreamObserver<Operation> responseObserver)106   public void purgeUserEvents(
107       PurgeUserEventsRequest request, StreamObserver<Operation> responseObserver) {
108     Object response = responses.poll();
109     if (response instanceof Operation) {
110       requests.add(request);
111       responseObserver.onNext(((Operation) response));
112       responseObserver.onCompleted();
113     } else if (response instanceof Exception) {
114       responseObserver.onError(((Exception) response));
115     } else {
116       responseObserver.onError(
117           new IllegalArgumentException(
118               String.format(
119                   "Unrecognized response type %s for method PurgeUserEvents, expected %s or %s",
120                   response == null ? "null" : response.getClass().getName(),
121                   Operation.class.getName(),
122                   Exception.class.getName())));
123     }
124   }
125 
126   @Override
importUserEvents( ImportUserEventsRequest request, StreamObserver<Operation> responseObserver)127   public void importUserEvents(
128       ImportUserEventsRequest request, StreamObserver<Operation> responseObserver) {
129     Object response = responses.poll();
130     if (response instanceof Operation) {
131       requests.add(request);
132       responseObserver.onNext(((Operation) response));
133       responseObserver.onCompleted();
134     } else if (response instanceof Exception) {
135       responseObserver.onError(((Exception) response));
136     } else {
137       responseObserver.onError(
138           new IllegalArgumentException(
139               String.format(
140                   "Unrecognized response type %s for method ImportUserEvents, expected %s or %s",
141                   response == null ? "null" : response.getClass().getName(),
142                   Operation.class.getName(),
143                   Exception.class.getName())));
144     }
145   }
146 
147   @Override
rejoinUserEvents( RejoinUserEventsRequest request, StreamObserver<Operation> responseObserver)148   public void rejoinUserEvents(
149       RejoinUserEventsRequest request, StreamObserver<Operation> responseObserver) {
150     Object response = responses.poll();
151     if (response instanceof Operation) {
152       requests.add(request);
153       responseObserver.onNext(((Operation) response));
154       responseObserver.onCompleted();
155     } else if (response instanceof Exception) {
156       responseObserver.onError(((Exception) response));
157     } else {
158       responseObserver.onError(
159           new IllegalArgumentException(
160               String.format(
161                   "Unrecognized response type %s for method RejoinUserEvents, expected %s or %s",
162                   response == null ? "null" : response.getClass().getName(),
163                   Operation.class.getName(),
164                   Exception.class.getName())));
165     }
166   }
167 }
168