Skip to content

Commit

Permalink
[ggj] fix(engx): Handle nulls with poll() in MockServiceImpl (#693)
Browse files Browse the repository at this point in the history
* fix(mixins): handle unit tests for mixin pagination methods

* fix(engx): Handle nulls with poll() in MockServiceImpl
  • Loading branch information
miraleung authored Mar 13, 2021
1 parent 69924c5 commit 3f531bd
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NewObjectExpr;
import com.google.api.generator.engine.ast.RelationalOperationExpr;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.ast.StringObjectValue;
import com.google.api.generator.engine.ast.TernaryExpr;
import com.google.api.generator.engine.ast.ThisObjectValue;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.ValueExpr;
Expand Down Expand Up @@ -304,7 +306,7 @@ private static MethodDefinition createGenericProtoMethodOverride(Method protoMet
.setVariableExpr(localResponseVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(
MethodInvocationExpr.builder()
.setMethodName("remove")
.setMethodName("poll")
.setExprReferenceExpr(responsesVarExpr)
.setReturnType(objectType)
.build())
Expand Down Expand Up @@ -485,15 +487,23 @@ private static Statement createHandleObjectStatement(
}

TypeNode exceptionType = TypeNode.withReference(ConcreteReference.withClazz(Exception.class));
// Constructs `response == null ? "" : response.getClass().getName()`.
Expr actualResponseTypeString =
MethodInvocationExpr.builder()
.setExprReferenceExpr(
TernaryExpr.builder()
.setConditionExpr(
RelationalOperationExpr.equalToWithExprs(
localResponseVarExpr, ValueExpr.createNullExpr()))
.setThenExpr(ValueExpr.withValue(StringObjectValue.withValue("null")))
.setElseExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(localResponseVarExpr)
.setMethodName("getClass")
.setExprReferenceExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(localResponseVarExpr)
.setMethodName("getClass")
.build())
.setMethodName("getName")
.setReturnType(TypeNode.STRING)
.build())
.setMethodName("getName")
.setReturnType(TypeNode.STRING)
.build();
Function<TypeNode, Expr> typeToStrFn =
t ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class MockEchoImpl extends EchoImplBase {

@Override
public void echo(EchoRequest request, StreamObserver<EchoResponse> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof EchoResponse) {
requests.add(request);
responseObserver.onNext(((EchoResponse) response));
Expand All @@ -57,15 +57,15 @@ public class MockEchoImpl extends EchoImplBase {
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method Echo, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
EchoResponse.class.getName(),
Exception.class.getName())));
}
}

@Override
public void expand(ExpandRequest request, StreamObserver<EchoResponse> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof EchoResponse) {
requests.add(request);
responseObserver.onNext(((EchoResponse) response));
Expand All @@ -77,7 +77,7 @@ public class MockEchoImpl extends EchoImplBase {
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method Expand, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
EchoResponse.class.getName(),
Exception.class.getName())));
}
Expand All @@ -100,7 +100,7 @@ public class MockEchoImpl extends EchoImplBase {
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method Collect, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
EchoResponse.class.getName(),
Exception.class.getName())));
}
Expand Down Expand Up @@ -136,7 +136,7 @@ public class MockEchoImpl extends EchoImplBase {
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method Chat, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
EchoResponse.class.getName(),
Exception.class.getName())));
}
Expand Down Expand Up @@ -173,7 +173,7 @@ public class MockEchoImpl extends EchoImplBase {
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method ChatAgain, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
EchoResponse.class.getName(),
Exception.class.getName())));
}
Expand All @@ -195,7 +195,7 @@ public class MockEchoImpl extends EchoImplBase {
@Override
public void pagedExpand(
PagedExpandRequest request, StreamObserver<PagedExpandResponse> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof PagedExpandResponse) {
requests.add(request);
responseObserver.onNext(((PagedExpandResponse) response));
Expand All @@ -207,7 +207,7 @@ public class MockEchoImpl extends EchoImplBase {
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method PagedExpand, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
PagedExpandResponse.class.getName(),
Exception.class.getName())));
}
Expand All @@ -216,7 +216,7 @@ public class MockEchoImpl extends EchoImplBase {
@Override
public void simplePagedExpand(
PagedExpandRequest request, StreamObserver<PagedExpandResponse> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof PagedExpandResponse) {
requests.add(request);
responseObserver.onNext(((PagedExpandResponse) response));
Expand All @@ -228,15 +228,15 @@ public class MockEchoImpl extends EchoImplBase {
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method SimplePagedExpand, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
PagedExpandResponse.class.getName(),
Exception.class.getName())));
}
}

@Override
public void wait(WaitRequest request, StreamObserver<Operation> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof Operation) {
requests.add(request);
responseObserver.onNext(((Operation) response));
Expand All @@ -248,15 +248,15 @@ public class MockEchoImpl extends EchoImplBase {
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method Wait, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
Operation.class.getName(),
Exception.class.getName())));
}
}

@Override
public void block(BlockRequest request, StreamObserver<BlockResponse> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof BlockResponse) {
requests.add(request);
responseObserver.onNext(((BlockResponse) response));
Expand All @@ -268,7 +268,7 @@ public class MockEchoImpl extends EchoImplBase {
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method Block, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
BlockResponse.class.getName(),
Exception.class.getName())));
}
Expand Down
50 changes: 28 additions & 22 deletions test/integration/goldens/asset/MockAssetServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void reset() {
@Override
public void exportAssets(
ExportAssetsRequest request, StreamObserver<Operation> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof Operation) {
requests.add(request);
responseObserver.onNext(((Operation) response));
Expand All @@ -75,7 +75,7 @@ public void exportAssets(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method ExportAssets, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
Operation.class.getName(),
Exception.class.getName())));
}
Expand All @@ -85,7 +85,7 @@ public void exportAssets(
public void batchGetAssetsHistory(
BatchGetAssetsHistoryRequest request,
StreamObserver<BatchGetAssetsHistoryResponse> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof BatchGetAssetsHistoryResponse) {
requests.add(request);
responseObserver.onNext(((BatchGetAssetsHistoryResponse) response));
Expand All @@ -97,15 +97,15 @@ public void batchGetAssetsHistory(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method BatchGetAssetsHistory, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
BatchGetAssetsHistoryResponse.class.getName(),
Exception.class.getName())));
}
}

@Override
public void createFeed(CreateFeedRequest request, StreamObserver<Feed> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof Feed) {
requests.add(request);
responseObserver.onNext(((Feed) response));
Expand All @@ -117,13 +117,15 @@ public void createFeed(CreateFeedRequest request, StreamObserver<Feed> responseO
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method CreateFeed, expected %s or %s",
response.getClass().getName(), Feed.class.getName(), Exception.class.getName())));
response == null ? "null" : response.getClass().getName(),
Feed.class.getName(),
Exception.class.getName())));
}
}

@Override
public void getFeed(GetFeedRequest request, StreamObserver<Feed> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof Feed) {
requests.add(request);
responseObserver.onNext(((Feed) response));
Expand All @@ -135,14 +137,16 @@ public void getFeed(GetFeedRequest request, StreamObserver<Feed> responseObserve
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method GetFeed, expected %s or %s",
response.getClass().getName(), Feed.class.getName(), Exception.class.getName())));
response == null ? "null" : response.getClass().getName(),
Feed.class.getName(),
Exception.class.getName())));
}
}

@Override
public void listFeeds(
ListFeedsRequest request, StreamObserver<ListFeedsResponse> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof ListFeedsResponse) {
requests.add(request);
responseObserver.onNext(((ListFeedsResponse) response));
Expand All @@ -154,15 +158,15 @@ public void listFeeds(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method ListFeeds, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
ListFeedsResponse.class.getName(),
Exception.class.getName())));
}
}

@Override
public void updateFeed(UpdateFeedRequest request, StreamObserver<Feed> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof Feed) {
requests.add(request);
responseObserver.onNext(((Feed) response));
Expand All @@ -174,13 +178,15 @@ public void updateFeed(UpdateFeedRequest request, StreamObserver<Feed> responseO
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method UpdateFeed, expected %s or %s",
response.getClass().getName(), Feed.class.getName(), Exception.class.getName())));
response == null ? "null" : response.getClass().getName(),
Feed.class.getName(),
Exception.class.getName())));
}
}

@Override
public void deleteFeed(DeleteFeedRequest request, StreamObserver<Empty> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof Empty) {
requests.add(request);
responseObserver.onNext(((Empty) response));
Expand All @@ -192,7 +198,7 @@ public void deleteFeed(DeleteFeedRequest request, StreamObserver<Empty> response
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method DeleteFeed, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
Empty.class.getName(),
Exception.class.getName())));
}
Expand All @@ -202,7 +208,7 @@ public void deleteFeed(DeleteFeedRequest request, StreamObserver<Empty> response
public void searchAllResources(
SearchAllResourcesRequest request,
StreamObserver<SearchAllResourcesResponse> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof SearchAllResourcesResponse) {
requests.add(request);
responseObserver.onNext(((SearchAllResourcesResponse) response));
Expand All @@ -214,7 +220,7 @@ public void searchAllResources(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method SearchAllResources, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
SearchAllResourcesResponse.class.getName(),
Exception.class.getName())));
}
Expand All @@ -224,7 +230,7 @@ public void searchAllResources(
public void searchAllIamPolicies(
SearchAllIamPoliciesRequest request,
StreamObserver<SearchAllIamPoliciesResponse> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof SearchAllIamPoliciesResponse) {
requests.add(request);
responseObserver.onNext(((SearchAllIamPoliciesResponse) response));
Expand All @@ -236,7 +242,7 @@ public void searchAllIamPolicies(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method SearchAllIamPolicies, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
SearchAllIamPoliciesResponse.class.getName(),
Exception.class.getName())));
}
Expand All @@ -245,7 +251,7 @@ public void searchAllIamPolicies(
@Override
public void analyzeIamPolicy(
AnalyzeIamPolicyRequest request, StreamObserver<AnalyzeIamPolicyResponse> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof AnalyzeIamPolicyResponse) {
requests.add(request);
responseObserver.onNext(((AnalyzeIamPolicyResponse) response));
Expand All @@ -257,7 +263,7 @@ public void analyzeIamPolicy(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method AnalyzeIamPolicy, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
AnalyzeIamPolicyResponse.class.getName(),
Exception.class.getName())));
}
Expand All @@ -266,7 +272,7 @@ public void analyzeIamPolicy(
@Override
public void analyzeIamPolicyLongrunning(
AnalyzeIamPolicyLongrunningRequest request, StreamObserver<Operation> responseObserver) {
Object response = responses.remove();
Object response = responses.poll();
if (response instanceof Operation) {
requests.add(request);
responseObserver.onNext(((Operation) response));
Expand All @@ -278,7 +284,7 @@ public void analyzeIamPolicyLongrunning(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method AnalyzeIamPolicyLongrunning, expected %s or %s",
response.getClass().getName(),
response == null ? "null" : response.getClass().getName(),
Operation.class.getName(),
Exception.class.getName())));
}
Expand Down
Loading

0 comments on commit 3f531bd

Please sign in to comment.