Skip to content

Commit

Permalink
[ggj][engx][tests] feat(engx): Add context to MockServiceImpl error m…
Browse files Browse the repository at this point in the history
…essages (#655)

* chore(engx): Add class object to TypeNode

* feat(engx): Add context to MockServiceImpl error messages
  • Loading branch information
miraleung committed Feb 16, 2021
1 parent d686c96 commit 9ede330
Show file tree
Hide file tree
Showing 9 changed files with 500 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Generated;

Expand Down Expand Up @@ -484,13 +485,55 @@ private static Statement createHandleObjectStatement(
}

TypeNode exceptionType = TypeNode.withReference(ConcreteReference.withClazz(Exception.class));
Expr actualResponseTypeString =
MethodInvocationExpr.builder()
.setExprReferenceExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(localResponseVarExpr)
.setMethodName("getClass")
.build())
.setMethodName("getName")
.setReturnType(TypeNode.STRING)
.build();
Function<TypeNode, Expr> typeToStrFn =
t ->
MethodInvocationExpr.builder()
.setExprReferenceExpr(
VariableExpr.builder()
.setStaticReferenceType(t)
.setVariable(
Variable.builder()
.setType(TypeNode.CLASS_OBJECT)
.setName("class")
.build())
.build())
.setMethodName("getName")
.setReturnType(TypeNode.STRING)
.build();
Expr expectedResponseTypeOneString = typeToStrFn.apply(protoMethod.outputType());
Expr expectedResponseTypeTwoString = typeToStrFn.apply(exceptionType);

Expr newExceptionExpr =
NewObjectExpr.builder()
.setType(
TypeNode.withReference(ConcreteReference.withClazz(IllegalArgumentException.class)))
.setArguments(
Arrays.asList(
ValueExpr.withValue(StringObjectValue.withValue("Unrecognized response type"))))
// Generates something like:
// String.format("Unrecognized response type %s, expected %s or %s",
// Operation.class.getName(), Exception.class.getName());
MethodInvocationExpr.builder()
.setStaticReferenceType(TypeNode.STRING)
.setMethodName("format")
.setArguments(
ValueExpr.withValue(
StringObjectValue.withValue(
"Unrecognized response type %s for method "
+ protoMethod.name()
+ ", expected %s or %s")),
actualResponseTypeString,
expectedResponseTypeOneString,
expectedResponseTypeTwoString)
.build())
.build();

return IfStatement.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ public class MockEchoImpl extends EchoImplBase {
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method Echo, expected %s or %s",
response.getClass().getName(),
EchoResponse.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -67,7 +73,13 @@ public class MockEchoImpl extends EchoImplBase {
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method Expand, expected %s or %s",
response.getClass().getName(),
EchoResponse.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -84,7 +96,13 @@ public class MockEchoImpl extends EchoImplBase {
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method Collect, expected %s or %s",
response.getClass().getName(),
EchoResponse.class.getName(),
Exception.class.getName())));
}
}

Expand Down Expand Up @@ -114,7 +132,13 @@ public class MockEchoImpl extends EchoImplBase {
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method Chat, expected %s or %s",
response.getClass().getName(),
EchoResponse.class.getName(),
Exception.class.getName())));
}
}

Expand Down Expand Up @@ -145,7 +169,13 @@ public class MockEchoImpl extends EchoImplBase {
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method ChatAgain, expected %s or %s",
response.getClass().getName(),
EchoResponse.class.getName(),
Exception.class.getName())));
}
}

Expand Down Expand Up @@ -173,7 +203,13 @@ public class MockEchoImpl extends EchoImplBase {
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method PagedExpand, expected %s or %s",
response.getClass().getName(),
PagedExpandResponse.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -188,7 +224,13 @@ public class MockEchoImpl extends EchoImplBase {
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method SimplePagedExpand, expected %s or %s",
response.getClass().getName(),
PagedExpandResponse.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -202,7 +244,13 @@ public class MockEchoImpl extends EchoImplBase {
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method Wait, expected %s or %s",
response.getClass().getName(),
Operation.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -216,7 +264,13 @@ public class MockEchoImpl extends EchoImplBase {
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method Block, expected %s or %s",
response.getClass().getName(),
BlockResponse.class.getName(),
Exception.class.getName())));
}
}
}
66 changes: 57 additions & 9 deletions test/integration/goldens/asset/MockAssetServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ public void exportAssets(
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method ExportAssets, expected %s or %s",
response.getClass().getName(),
Operation.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -87,7 +93,13 @@ public void batchGetAssetsHistory(
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method BatchGetAssetsHistory, expected %s or %s",
response.getClass().getName(),
BatchGetAssetsHistoryResponse.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -101,7 +113,11 @@ public void createFeed(CreateFeedRequest request, StreamObserver<Feed> responseO
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method CreateFeed, expected %s or %s",
response.getClass().getName(), Feed.class.getName(), Exception.class.getName())));
}
}

Expand All @@ -115,7 +131,11 @@ public void getFeed(GetFeedRequest request, StreamObserver<Feed> responseObserve
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method GetFeed, expected %s or %s",
response.getClass().getName(), Feed.class.getName(), Exception.class.getName())));
}
}

Expand All @@ -130,7 +150,13 @@ public void listFeeds(
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method ListFeeds, expected %s or %s",
response.getClass().getName(),
ListFeedsResponse.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -144,7 +170,11 @@ public void updateFeed(UpdateFeedRequest request, StreamObserver<Feed> responseO
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method UpdateFeed, expected %s or %s",
response.getClass().getName(), Feed.class.getName(), Exception.class.getName())));
}
}

Expand All @@ -158,7 +188,13 @@ public void deleteFeed(DeleteFeedRequest request, StreamObserver<Empty> response
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method DeleteFeed, expected %s or %s",
response.getClass().getName(),
Empty.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -174,7 +210,13 @@ public void searchAllResources(
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method SearchAllResources, expected %s or %s",
response.getClass().getName(),
SearchAllResourcesResponse.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -190,7 +232,13 @@ public void searchAllIamPolicies(
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method SearchAllIamPolicies, expected %s or %s",
response.getClass().getName(),
SearchAllIamPoliciesResponse.class.getName(),
Exception.class.getName())));
}
}
}
32 changes: 28 additions & 4 deletions test/integration/goldens/credentials/MockIAMCredentialsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ public void generateAccessToken(
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method GenerateAccessToken, expected %s or %s",
response.getClass().getName(),
GenerateAccessTokenResponse.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -85,7 +91,13 @@ public void generateIdToken(
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method GenerateIdToken, expected %s or %s",
response.getClass().getName(),
GenerateIdTokenResponse.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -99,7 +111,13 @@ public void signBlob(SignBlobRequest request, StreamObserver<SignBlobResponse> r
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method SignBlob, expected %s or %s",
response.getClass().getName(),
SignBlobResponse.class.getName(),
Exception.class.getName())));
}
}

Expand All @@ -113,7 +131,13 @@ public void signJwt(SignJwtRequest request, StreamObserver<SignJwtResponse> resp
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method SignJwt, expected %s or %s",
response.getClass().getName(),
SignJwtResponse.class.getName(),
Exception.class.getName())));
}
}
}
Loading

0 comments on commit 9ede330

Please sign in to comment.