Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ggj][engx][tests] feat(engx): Add context to MockServiceImpl error messages #655

Merged
merged 2 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public enum TypeKind {
public static final TypeNode SHORT_OBJECT =
withReference(ConcreteReference.withClazz(Short.class));

public static final TypeNode CLASS_OBJECT =
withReference(ConcreteReference.withClazz(Class.class));

public static final TypeNode BYTESTRING =
TypeNode.withReference(ConcreteReference.withClazz(ByteString.class));

Expand Down
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 @@ -316,10 +316,7 @@ public static Expr createLroSettingsBuilderExpr(
t ->
VariableExpr.builder()
.setVariable(
Variable.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Class.class)))
.setName("class")
.build())
Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
.setStaticReferenceType(t)
.build();
builderSettingsExpr =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1561,11 +1561,7 @@ private static List<Statement> createRpcLroExceptionTestCatchBody(

Expr testExpectedValueExpr =
VariableExpr.builder()
.setVariable(
Variable.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Class.class)))
.setName("class")
.build())
.setVariable(Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
.setStaticReferenceType(FIXED_TYPESTORE.get("InvalidArgumentException"))
.build();
Expr getCauseExpr =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -999,11 +999,7 @@ private static List<MethodDefinition> createDefaultHelperAndGetterMethods(
.setArguments(
VariableExpr.builder()
.setVariable(
Variable.builder()
.setType(
TypeNode.withReference(ConcreteReference.withClazz(Class.class)))
.setName("class")
.build())
Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
.setStaticReferenceType(
typeStore.get(ClassNames.getServiceStubSettingsClassName(service)))
.build())
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())));
}
}
}
Loading