Skip to content

Commit 3f531bd

Browse files
authored
[ggj] fix(engx): Handle nulls with poll() in MockServiceImpl (#693)
* fix(mixins): handle unit tests for mixin pagination methods * fix(engx): Handle nulls with poll() in MockServiceImpl
1 parent 69924c5 commit 3f531bd

13 files changed

Lines changed: 247 additions & 223 deletions

File tree

src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
import com.google.api.generator.engine.ast.MethodDefinition;
2929
import com.google.api.generator.engine.ast.MethodInvocationExpr;
3030
import com.google.api.generator.engine.ast.NewObjectExpr;
31+
import com.google.api.generator.engine.ast.RelationalOperationExpr;
3132
import com.google.api.generator.engine.ast.ScopeNode;
3233
import com.google.api.generator.engine.ast.Statement;
3334
import com.google.api.generator.engine.ast.StringObjectValue;
35+
import com.google.api.generator.engine.ast.TernaryExpr;
3436
import com.google.api.generator.engine.ast.ThisObjectValue;
3537
import com.google.api.generator.engine.ast.TypeNode;
3638
import com.google.api.generator.engine.ast.ValueExpr;
@@ -304,7 +306,7 @@ private static MethodDefinition createGenericProtoMethodOverride(Method protoMet
304306
.setVariableExpr(localResponseVarExpr.toBuilder().setIsDecl(true).build())
305307
.setValueExpr(
306308
MethodInvocationExpr.builder()
307-
.setMethodName("remove")
309+
.setMethodName("poll")
308310
.setExprReferenceExpr(responsesVarExpr)
309311
.setReturnType(objectType)
310312
.build())
@@ -485,15 +487,23 @@ private static Statement createHandleObjectStatement(
485487
}
486488

487489
TypeNode exceptionType = TypeNode.withReference(ConcreteReference.withClazz(Exception.class));
490+
// Constructs `response == null ? "" : response.getClass().getName()`.
488491
Expr actualResponseTypeString =
489-
MethodInvocationExpr.builder()
490-
.setExprReferenceExpr(
492+
TernaryExpr.builder()
493+
.setConditionExpr(
494+
RelationalOperationExpr.equalToWithExprs(
495+
localResponseVarExpr, ValueExpr.createNullExpr()))
496+
.setThenExpr(ValueExpr.withValue(StringObjectValue.withValue("null")))
497+
.setElseExpr(
491498
MethodInvocationExpr.builder()
492-
.setExprReferenceExpr(localResponseVarExpr)
493-
.setMethodName("getClass")
499+
.setExprReferenceExpr(
500+
MethodInvocationExpr.builder()
501+
.setExprReferenceExpr(localResponseVarExpr)
502+
.setMethodName("getClass")
503+
.build())
504+
.setMethodName("getName")
505+
.setReturnType(TypeNode.STRING)
494506
.build())
495-
.setMethodName("getName")
496-
.setReturnType(TypeNode.STRING)
497507
.build();
498508
Function<TypeNode, Expr> typeToStrFn =
499509
t ->

src/test/java/com/google/api/generator/gapic/composer/goldens/MockEchoImpl.golden

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class MockEchoImpl extends EchoImplBase {
4545

4646
@Override
4747
public void echo(EchoRequest request, StreamObserver<EchoResponse> responseObserver) {
48-
Object response = responses.remove();
48+
Object response = responses.poll();
4949
if (response instanceof EchoResponse) {
5050
requests.add(request);
5151
responseObserver.onNext(((EchoResponse) response));
@@ -57,15 +57,15 @@ public class MockEchoImpl extends EchoImplBase {
5757
new IllegalArgumentException(
5858
String.format(
5959
"Unrecognized response type %s for method Echo, expected %s or %s",
60-
response.getClass().getName(),
60+
response == null ? "null" : response.getClass().getName(),
6161
EchoResponse.class.getName(),
6262
Exception.class.getName())));
6363
}
6464
}
6565

6666
@Override
6767
public void expand(ExpandRequest request, StreamObserver<EchoResponse> responseObserver) {
68-
Object response = responses.remove();
68+
Object response = responses.poll();
6969
if (response instanceof EchoResponse) {
7070
requests.add(request);
7171
responseObserver.onNext(((EchoResponse) response));
@@ -77,7 +77,7 @@ public class MockEchoImpl extends EchoImplBase {
7777
new IllegalArgumentException(
7878
String.format(
7979
"Unrecognized response type %s for method Expand, expected %s or %s",
80-
response.getClass().getName(),
80+
response == null ? "null" : response.getClass().getName(),
8181
EchoResponse.class.getName(),
8282
Exception.class.getName())));
8383
}
@@ -100,7 +100,7 @@ public class MockEchoImpl extends EchoImplBase {
100100
new IllegalArgumentException(
101101
String.format(
102102
"Unrecognized response type %s for method Collect, expected %s or %s",
103-
response.getClass().getName(),
103+
response == null ? "null" : response.getClass().getName(),
104104
EchoResponse.class.getName(),
105105
Exception.class.getName())));
106106
}
@@ -136,7 +136,7 @@ public class MockEchoImpl extends EchoImplBase {
136136
new IllegalArgumentException(
137137
String.format(
138138
"Unrecognized response type %s for method Chat, expected %s or %s",
139-
response.getClass().getName(),
139+
response == null ? "null" : response.getClass().getName(),
140140
EchoResponse.class.getName(),
141141
Exception.class.getName())));
142142
}
@@ -173,7 +173,7 @@ public class MockEchoImpl extends EchoImplBase {
173173
new IllegalArgumentException(
174174
String.format(
175175
"Unrecognized response type %s for method ChatAgain, expected %s or %s",
176-
response.getClass().getName(),
176+
response == null ? "null" : response.getClass().getName(),
177177
EchoResponse.class.getName(),
178178
Exception.class.getName())));
179179
}
@@ -195,7 +195,7 @@ public class MockEchoImpl extends EchoImplBase {
195195
@Override
196196
public void pagedExpand(
197197
PagedExpandRequest request, StreamObserver<PagedExpandResponse> responseObserver) {
198-
Object response = responses.remove();
198+
Object response = responses.poll();
199199
if (response instanceof PagedExpandResponse) {
200200
requests.add(request);
201201
responseObserver.onNext(((PagedExpandResponse) response));
@@ -207,7 +207,7 @@ public class MockEchoImpl extends EchoImplBase {
207207
new IllegalArgumentException(
208208
String.format(
209209
"Unrecognized response type %s for method PagedExpand, expected %s or %s",
210-
response.getClass().getName(),
210+
response == null ? "null" : response.getClass().getName(),
211211
PagedExpandResponse.class.getName(),
212212
Exception.class.getName())));
213213
}
@@ -216,7 +216,7 @@ public class MockEchoImpl extends EchoImplBase {
216216
@Override
217217
public void simplePagedExpand(
218218
PagedExpandRequest request, StreamObserver<PagedExpandResponse> responseObserver) {
219-
Object response = responses.remove();
219+
Object response = responses.poll();
220220
if (response instanceof PagedExpandResponse) {
221221
requests.add(request);
222222
responseObserver.onNext(((PagedExpandResponse) response));
@@ -228,15 +228,15 @@ public class MockEchoImpl extends EchoImplBase {
228228
new IllegalArgumentException(
229229
String.format(
230230
"Unrecognized response type %s for method SimplePagedExpand, expected %s or %s",
231-
response.getClass().getName(),
231+
response == null ? "null" : response.getClass().getName(),
232232
PagedExpandResponse.class.getName(),
233233
Exception.class.getName())));
234234
}
235235
}
236236

237237
@Override
238238
public void wait(WaitRequest request, StreamObserver<Operation> responseObserver) {
239-
Object response = responses.remove();
239+
Object response = responses.poll();
240240
if (response instanceof Operation) {
241241
requests.add(request);
242242
responseObserver.onNext(((Operation) response));
@@ -248,15 +248,15 @@ public class MockEchoImpl extends EchoImplBase {
248248
new IllegalArgumentException(
249249
String.format(
250250
"Unrecognized response type %s for method Wait, expected %s or %s",
251-
response.getClass().getName(),
251+
response == null ? "null" : response.getClass().getName(),
252252
Operation.class.getName(),
253253
Exception.class.getName())));
254254
}
255255
}
256256

257257
@Override
258258
public void block(BlockRequest request, StreamObserver<BlockResponse> responseObserver) {
259-
Object response = responses.remove();
259+
Object response = responses.poll();
260260
if (response instanceof BlockResponse) {
261261
requests.add(request);
262262
responseObserver.onNext(((BlockResponse) response));
@@ -268,7 +268,7 @@ public class MockEchoImpl extends EchoImplBase {
268268
new IllegalArgumentException(
269269
String.format(
270270
"Unrecognized response type %s for method Block, expected %s or %s",
271-
response.getClass().getName(),
271+
response == null ? "null" : response.getClass().getName(),
272272
BlockResponse.class.getName(),
273273
Exception.class.getName())));
274274
}

test/integration/goldens/asset/MockAssetServiceImpl.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void reset() {
6363
@Override
6464
public void exportAssets(
6565
ExportAssetsRequest request, StreamObserver<Operation> responseObserver) {
66-
Object response = responses.remove();
66+
Object response = responses.poll();
6767
if (response instanceof Operation) {
6868
requests.add(request);
6969
responseObserver.onNext(((Operation) response));
@@ -75,7 +75,7 @@ public void exportAssets(
7575
new IllegalArgumentException(
7676
String.format(
7777
"Unrecognized response type %s for method ExportAssets, expected %s or %s",
78-
response.getClass().getName(),
78+
response == null ? "null" : response.getClass().getName(),
7979
Operation.class.getName(),
8080
Exception.class.getName())));
8181
}
@@ -85,7 +85,7 @@ public void exportAssets(
8585
public void batchGetAssetsHistory(
8686
BatchGetAssetsHistoryRequest request,
8787
StreamObserver<BatchGetAssetsHistoryResponse> responseObserver) {
88-
Object response = responses.remove();
88+
Object response = responses.poll();
8989
if (response instanceof BatchGetAssetsHistoryResponse) {
9090
requests.add(request);
9191
responseObserver.onNext(((BatchGetAssetsHistoryResponse) response));
@@ -97,15 +97,15 @@ public void batchGetAssetsHistory(
9797
new IllegalArgumentException(
9898
String.format(
9999
"Unrecognized response type %s for method BatchGetAssetsHistory, expected %s or %s",
100-
response.getClass().getName(),
100+
response == null ? "null" : response.getClass().getName(),
101101
BatchGetAssetsHistoryResponse.class.getName(),
102102
Exception.class.getName())));
103103
}
104104
}
105105

106106
@Override
107107
public void createFeed(CreateFeedRequest request, StreamObserver<Feed> responseObserver) {
108-
Object response = responses.remove();
108+
Object response = responses.poll();
109109
if (response instanceof Feed) {
110110
requests.add(request);
111111
responseObserver.onNext(((Feed) response));
@@ -117,13 +117,15 @@ public void createFeed(CreateFeedRequest request, StreamObserver<Feed> responseO
117117
new IllegalArgumentException(
118118
String.format(
119119
"Unrecognized response type %s for method CreateFeed, expected %s or %s",
120-
response.getClass().getName(), Feed.class.getName(), Exception.class.getName())));
120+
response == null ? "null" : response.getClass().getName(),
121+
Feed.class.getName(),
122+
Exception.class.getName())));
121123
}
122124
}
123125

124126
@Override
125127
public void getFeed(GetFeedRequest request, StreamObserver<Feed> responseObserver) {
126-
Object response = responses.remove();
128+
Object response = responses.poll();
127129
if (response instanceof Feed) {
128130
requests.add(request);
129131
responseObserver.onNext(((Feed) response));
@@ -135,14 +137,16 @@ public void getFeed(GetFeedRequest request, StreamObserver<Feed> responseObserve
135137
new IllegalArgumentException(
136138
String.format(
137139
"Unrecognized response type %s for method GetFeed, expected %s or %s",
138-
response.getClass().getName(), Feed.class.getName(), Exception.class.getName())));
140+
response == null ? "null" : response.getClass().getName(),
141+
Feed.class.getName(),
142+
Exception.class.getName())));
139143
}
140144
}
141145

142146
@Override
143147
public void listFeeds(
144148
ListFeedsRequest request, StreamObserver<ListFeedsResponse> responseObserver) {
145-
Object response = responses.remove();
149+
Object response = responses.poll();
146150
if (response instanceof ListFeedsResponse) {
147151
requests.add(request);
148152
responseObserver.onNext(((ListFeedsResponse) response));
@@ -154,15 +158,15 @@ public void listFeeds(
154158
new IllegalArgumentException(
155159
String.format(
156160
"Unrecognized response type %s for method ListFeeds, expected %s or %s",
157-
response.getClass().getName(),
161+
response == null ? "null" : response.getClass().getName(),
158162
ListFeedsResponse.class.getName(),
159163
Exception.class.getName())));
160164
}
161165
}
162166

163167
@Override
164168
public void updateFeed(UpdateFeedRequest request, StreamObserver<Feed> responseObserver) {
165-
Object response = responses.remove();
169+
Object response = responses.poll();
166170
if (response instanceof Feed) {
167171
requests.add(request);
168172
responseObserver.onNext(((Feed) response));
@@ -174,13 +178,15 @@ public void updateFeed(UpdateFeedRequest request, StreamObserver<Feed> responseO
174178
new IllegalArgumentException(
175179
String.format(
176180
"Unrecognized response type %s for method UpdateFeed, expected %s or %s",
177-
response.getClass().getName(), Feed.class.getName(), Exception.class.getName())));
181+
response == null ? "null" : response.getClass().getName(),
182+
Feed.class.getName(),
183+
Exception.class.getName())));
178184
}
179185
}
180186

181187
@Override
182188
public void deleteFeed(DeleteFeedRequest request, StreamObserver<Empty> responseObserver) {
183-
Object response = responses.remove();
189+
Object response = responses.poll();
184190
if (response instanceof Empty) {
185191
requests.add(request);
186192
responseObserver.onNext(((Empty) response));
@@ -192,7 +198,7 @@ public void deleteFeed(DeleteFeedRequest request, StreamObserver<Empty> response
192198
new IllegalArgumentException(
193199
String.format(
194200
"Unrecognized response type %s for method DeleteFeed, expected %s or %s",
195-
response.getClass().getName(),
201+
response == null ? "null" : response.getClass().getName(),
196202
Empty.class.getName(),
197203
Exception.class.getName())));
198204
}
@@ -202,7 +208,7 @@ public void deleteFeed(DeleteFeedRequest request, StreamObserver<Empty> response
202208
public void searchAllResources(
203209
SearchAllResourcesRequest request,
204210
StreamObserver<SearchAllResourcesResponse> responseObserver) {
205-
Object response = responses.remove();
211+
Object response = responses.poll();
206212
if (response instanceof SearchAllResourcesResponse) {
207213
requests.add(request);
208214
responseObserver.onNext(((SearchAllResourcesResponse) response));
@@ -214,7 +220,7 @@ public void searchAllResources(
214220
new IllegalArgumentException(
215221
String.format(
216222
"Unrecognized response type %s for method SearchAllResources, expected %s or %s",
217-
response.getClass().getName(),
223+
response == null ? "null" : response.getClass().getName(),
218224
SearchAllResourcesResponse.class.getName(),
219225
Exception.class.getName())));
220226
}
@@ -224,7 +230,7 @@ public void searchAllResources(
224230
public void searchAllIamPolicies(
225231
SearchAllIamPoliciesRequest request,
226232
StreamObserver<SearchAllIamPoliciesResponse> responseObserver) {
227-
Object response = responses.remove();
233+
Object response = responses.poll();
228234
if (response instanceof SearchAllIamPoliciesResponse) {
229235
requests.add(request);
230236
responseObserver.onNext(((SearchAllIamPoliciesResponse) response));
@@ -236,7 +242,7 @@ public void searchAllIamPolicies(
236242
new IllegalArgumentException(
237243
String.format(
238244
"Unrecognized response type %s for method SearchAllIamPolicies, expected %s or %s",
239-
response.getClass().getName(),
245+
response == null ? "null" : response.getClass().getName(),
240246
SearchAllIamPoliciesResponse.class.getName(),
241247
Exception.class.getName())));
242248
}
@@ -245,7 +251,7 @@ public void searchAllIamPolicies(
245251
@Override
246252
public void analyzeIamPolicy(
247253
AnalyzeIamPolicyRequest request, StreamObserver<AnalyzeIamPolicyResponse> responseObserver) {
248-
Object response = responses.remove();
254+
Object response = responses.poll();
249255
if (response instanceof AnalyzeIamPolicyResponse) {
250256
requests.add(request);
251257
responseObserver.onNext(((AnalyzeIamPolicyResponse) response));
@@ -257,7 +263,7 @@ public void analyzeIamPolicy(
257263
new IllegalArgumentException(
258264
String.format(
259265
"Unrecognized response type %s for method AnalyzeIamPolicy, expected %s or %s",
260-
response.getClass().getName(),
266+
response == null ? "null" : response.getClass().getName(),
261267
AnalyzeIamPolicyResponse.class.getName(),
262268
Exception.class.getName())));
263269
}
@@ -266,7 +272,7 @@ public void analyzeIamPolicy(
266272
@Override
267273
public void analyzeIamPolicyLongrunning(
268274
AnalyzeIamPolicyLongrunningRequest request, StreamObserver<Operation> responseObserver) {
269-
Object response = responses.remove();
275+
Object response = responses.poll();
270276
if (response instanceof Operation) {
271277
requests.add(request);
272278
responseObserver.onNext(((Operation) response));
@@ -278,7 +284,7 @@ public void analyzeIamPolicyLongrunning(
278284
new IllegalArgumentException(
279285
String.format(
280286
"Unrecognized response type %s for method AnalyzeIamPolicyLongrunning, expected %s or %s",
281-
response.getClass().getName(),
287+
response == null ? "null" : response.getClass().getName(),
282288
Operation.class.getName(),
283289
Exception.class.getName())));
284290
}

0 commit comments

Comments
 (0)