• 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.dataproc.v1;
18 
19 import com.google.api.core.BetaApi;
20 import com.google.cloud.dataproc.v1.ClusterControllerGrpc.ClusterControllerImplBase;
21 import com.google.longrunning.Operation;
22 import com.google.protobuf.AbstractMessage;
23 import io.grpc.stub.StreamObserver;
24 import java.util.ArrayList;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Queue;
28 import javax.annotation.Generated;
29 
30 @BetaApi
31 @Generated("by gapic-generator-java")
32 public class MockClusterControllerImpl extends ClusterControllerImplBase {
33   private List<AbstractMessage> requests;
34   private Queue<Object> responses;
35 
MockClusterControllerImpl()36   public MockClusterControllerImpl() {
37     requests = new ArrayList<>();
38     responses = new LinkedList<>();
39   }
40 
getRequests()41   public List<AbstractMessage> getRequests() {
42     return requests;
43   }
44 
addResponse(AbstractMessage response)45   public void addResponse(AbstractMessage response) {
46     responses.add(response);
47   }
48 
setResponses(List<AbstractMessage> responses)49   public void setResponses(List<AbstractMessage> responses) {
50     this.responses = new LinkedList<Object>(responses);
51   }
52 
addException(Exception exception)53   public void addException(Exception exception) {
54     responses.add(exception);
55   }
56 
reset()57   public void reset() {
58     requests = new ArrayList<>();
59     responses = new LinkedList<>();
60   }
61 
62   @Override
createCluster( CreateClusterRequest request, StreamObserver<Operation> responseObserver)63   public void createCluster(
64       CreateClusterRequest request, StreamObserver<Operation> responseObserver) {
65     Object response = responses.poll();
66     if (response instanceof Operation) {
67       requests.add(request);
68       responseObserver.onNext(((Operation) 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 CreateCluster, expected %s or %s",
77                   response == null ? "null" : response.getClass().getName(),
78                   Operation.class.getName(),
79                   Exception.class.getName())));
80     }
81   }
82 
83   @Override
updateCluster( UpdateClusterRequest request, StreamObserver<Operation> responseObserver)84   public void updateCluster(
85       UpdateClusterRequest request, StreamObserver<Operation> responseObserver) {
86     Object response = responses.poll();
87     if (response instanceof Operation) {
88       requests.add(request);
89       responseObserver.onNext(((Operation) 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 UpdateCluster, expected %s or %s",
98                   response == null ? "null" : response.getClass().getName(),
99                   Operation.class.getName(),
100                   Exception.class.getName())));
101     }
102   }
103 
104   @Override
stopCluster(StopClusterRequest request, StreamObserver<Operation> responseObserver)105   public void stopCluster(StopClusterRequest request, StreamObserver<Operation> responseObserver) {
106     Object response = responses.poll();
107     if (response instanceof Operation) {
108       requests.add(request);
109       responseObserver.onNext(((Operation) 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 StopCluster, expected %s or %s",
118                   response == null ? "null" : response.getClass().getName(),
119                   Operation.class.getName(),
120                   Exception.class.getName())));
121     }
122   }
123 
124   @Override
startCluster( StartClusterRequest request, StreamObserver<Operation> responseObserver)125   public void startCluster(
126       StartClusterRequest request, StreamObserver<Operation> responseObserver) {
127     Object response = responses.poll();
128     if (response instanceof Operation) {
129       requests.add(request);
130       responseObserver.onNext(((Operation) response));
131       responseObserver.onCompleted();
132     } else if (response instanceof Exception) {
133       responseObserver.onError(((Exception) response));
134     } else {
135       responseObserver.onError(
136           new IllegalArgumentException(
137               String.format(
138                   "Unrecognized response type %s for method StartCluster, expected %s or %s",
139                   response == null ? "null" : response.getClass().getName(),
140                   Operation.class.getName(),
141                   Exception.class.getName())));
142     }
143   }
144 
145   @Override
deleteCluster( DeleteClusterRequest request, StreamObserver<Operation> responseObserver)146   public void deleteCluster(
147       DeleteClusterRequest request, StreamObserver<Operation> responseObserver) {
148     Object response = responses.poll();
149     if (response instanceof Operation) {
150       requests.add(request);
151       responseObserver.onNext(((Operation) 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 DeleteCluster, expected %s or %s",
160                   response == null ? "null" : response.getClass().getName(),
161                   Operation.class.getName(),
162                   Exception.class.getName())));
163     }
164   }
165 
166   @Override
getCluster(GetClusterRequest request, StreamObserver<Cluster> responseObserver)167   public void getCluster(GetClusterRequest request, StreamObserver<Cluster> responseObserver) {
168     Object response = responses.poll();
169     if (response instanceof Cluster) {
170       requests.add(request);
171       responseObserver.onNext(((Cluster) response));
172       responseObserver.onCompleted();
173     } else if (response instanceof Exception) {
174       responseObserver.onError(((Exception) response));
175     } else {
176       responseObserver.onError(
177           new IllegalArgumentException(
178               String.format(
179                   "Unrecognized response type %s for method GetCluster, expected %s or %s",
180                   response == null ? "null" : response.getClass().getName(),
181                   Cluster.class.getName(),
182                   Exception.class.getName())));
183     }
184   }
185 
186   @Override
listClusters( ListClustersRequest request, StreamObserver<ListClustersResponse> responseObserver)187   public void listClusters(
188       ListClustersRequest request, StreamObserver<ListClustersResponse> responseObserver) {
189     Object response = responses.poll();
190     if (response instanceof ListClustersResponse) {
191       requests.add(request);
192       responseObserver.onNext(((ListClustersResponse) response));
193       responseObserver.onCompleted();
194     } else if (response instanceof Exception) {
195       responseObserver.onError(((Exception) response));
196     } else {
197       responseObserver.onError(
198           new IllegalArgumentException(
199               String.format(
200                   "Unrecognized response type %s for method ListClusters, expected %s or %s",
201                   response == null ? "null" : response.getClass().getName(),
202                   ListClustersResponse.class.getName(),
203                   Exception.class.getName())));
204     }
205   }
206 
207   @Override
diagnoseCluster( DiagnoseClusterRequest request, StreamObserver<Operation> responseObserver)208   public void diagnoseCluster(
209       DiagnoseClusterRequest request, StreamObserver<Operation> responseObserver) {
210     Object response = responses.poll();
211     if (response instanceof Operation) {
212       requests.add(request);
213       responseObserver.onNext(((Operation) response));
214       responseObserver.onCompleted();
215     } else if (response instanceof Exception) {
216       responseObserver.onError(((Exception) response));
217     } else {
218       responseObserver.onError(
219           new IllegalArgumentException(
220               String.format(
221                   "Unrecognized response type %s for method DiagnoseCluster, expected %s or %s",
222                   response == null ? "null" : response.getClass().getName(),
223                   Operation.class.getName(),
224                   Exception.class.getName())));
225     }
226   }
227 }
228