Skip to content

Commit

Permalink
feat: make generated test values comply with url path template (#903)
Browse files Browse the repository at this point in the history
This includes nested messages creation when there are url paths with subfields mentioned (like `/v1/{field.name=projects/*/databases/*/collectionGroups/*/fields/*}`)

This is needed for rest transports tests because, unlike grpc, request fields must match path templates for rest logic pass the tests.

Main changes are in `DefaultValueComposer` and `HttpRuleParser` classes.

The generated pattern-matching samples are in the following format:
  given the pattern pattern:
  `/v1/{field.name=projects/*/databases/*/collectionGroups/*/fields/*}`
  the value will be:
  `field.name=projects/project-1234/databases/database-1234/collectionGroups/collectionGroup-1234/fields/field-1234`
  • Loading branch information
vam-google authored Jan 21, 2022
1 parent 90df081 commit bfb35cd
Show file tree
Hide file tree
Showing 17 changed files with 403 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ private MethodDefinition createRpcTestMethod(
AssignmentExpr.builder()
.setVariableExpr(responsesElementVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(
DefaultValueComposer.createDefaultValue(
DefaultValueComposer.createValue(
Field.builder()
.setType(repeatedResponseType)
.setName("responsesElement")
Expand All @@ -377,23 +377,23 @@ private MethodDefinition createRpcTestMethod(
method.name(), methodOutputMessage.name()));

expectedResponseValExpr =
DefaultValueComposer.createSimplePagedResponse(
DefaultValueComposer.createSimplePagedResponseValue(
method.outputType(),
firstRepeatedField.name(),
responsesElementVarExpr,
firstRepeatedField.isMap());
} else {
if (messageTypes.containsKey(methodOutputType.reference().fullName())) {
expectedResponseValExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
DefaultValueComposer.createSimpleMessageBuilderValue(
messageTypes.get(methodOutputType.reference().fullName()),
resourceNames,
messageTypes);
} else {
// Wrap this in a field so we don't have to split the helper into lots of different methods,
// or duplicate it for VariableExpr.
expectedResponseValExpr =
DefaultValueComposer.createDefaultValue(
DefaultValueComposer.createValue(
Field.builder()
.setType(methodOutputType)
.setIsMessage(true)
Expand Down Expand Up @@ -422,7 +422,7 @@ private MethodDefinition createRpcTestMethod(
AssignmentExpr.builder()
.setVariableExpr(resultOperationVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(
DefaultValueComposer.createSimpleOperationBuilderExpr(
DefaultValueComposer.createSimpleOperationBuilderValue(
String.format("%sTest", JavaStyle.toLowerCamelCase(method.name())),
expectedResponseVarExpr))
.build());
Expand Down Expand Up @@ -457,22 +457,32 @@ private MethodDefinition createRpcTestMethod(
argExprs.add(requestVarExpr);
requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(requestMessage);
Map<String, String> pathParamValuePatterns = Collections.emptyMap();
if (getTransportContext().useValuePatterns() && method.hasHttpBindings()) {
pathParamValuePatterns = method.httpBindings().getPathParametersValuePatterns();
}
Expr valExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
requestMessage, resourceNames, messageTypes);
DefaultValueComposer.createSimpleMessageBuilderValue(
requestMessage, resourceNames, messageTypes, pathParamValuePatterns);
methodExprs.add(
AssignmentExpr.builder()
.setVariableExpr(requestVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(valExpr)
.build());
} else {
Map<String, String> valuePatterns = Collections.emptyMap();
if (getTransportContext().useValuePatterns() && method.hasHttpBindings()) {
valuePatterns = method.httpBindings().getPathParametersValuePatterns();
}
for (MethodArgument methodArg : methodSignature) {
String methodArgName = JavaStyle.toLowerCamelCase(methodArg.name());
VariableExpr varExpr =
VariableExpr.withVariable(
Variable.builder().setType(methodArg.type()).setName(methodArgName).build());
argExprs.add(varExpr);
Expr valExpr = createDefaultValue(methodArg, resourceNames);
Expr valExpr =
DefaultValueComposer.createMethodArgValue(
methodArg, resourceNames, messageTypes, valuePatterns);
methodExprs.add(
AssignmentExpr.builder()
.setVariableExpr(varExpr.toBuilder().setIsDecl(true).build())
Expand Down Expand Up @@ -737,9 +747,6 @@ protected abstract List<Statement> createStreamingRpcExceptionTestStatements(
Map<String, ResourceName> resourceNames,
Map<String, Message> messageTypes);

protected abstract Expr createDefaultValue(
MethodArgument methodArg, Map<String, ResourceName> resourceNames);

protected List<Statement> createRpcExceptionTestStatements(
Method method,
List<MethodArgument> methodSignature,
Expand All @@ -748,6 +755,7 @@ protected List<Statement> createRpcExceptionTestStatements(
Map<String, Message> messageTypes) {
List<VariableExpr> argVarExprs = new ArrayList<>();
List<Expr> tryBodyExprs = new ArrayList<>();

if (methodSignature.isEmpty()) {
// Construct the actual request.
VariableExpr varExpr =
Expand All @@ -756,22 +764,32 @@ protected List<Statement> createRpcExceptionTestStatements(
argVarExprs.add(varExpr);
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(requestMessage);
Map<String, String> valuePatterns = Collections.emptyMap();
if (getTransportContext().useValuePatterns() && method.hasHttpBindings()) {
valuePatterns = method.httpBindings().getPathParametersValuePatterns();
}
Expr valExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
requestMessage, resourceNames, messageTypes);
DefaultValueComposer.createSimpleMessageBuilderValue(
requestMessage, resourceNames, messageTypes, valuePatterns);
tryBodyExprs.add(
AssignmentExpr.builder()
.setVariableExpr(varExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(valExpr)
.build());
} else {
Map<String, String> valuePatterns = Collections.emptyMap();
if (getTransportContext().useValuePatterns() && method.hasHttpBindings()) {
valuePatterns = method.httpBindings().getPathParametersValuePatterns();
}
for (MethodArgument methodArg : methodSignature) {
String methodArgName = JavaStyle.toLowerCamelCase(methodArg.name());
VariableExpr varExpr =
VariableExpr.withVariable(
Variable.builder().setType(methodArg.type()).setName(methodArgName).build());
argVarExprs.add(varExpr);
Expr valExpr = createDefaultValue(methodArg, resourceNames);
Expr valExpr =
DefaultValueComposer.createMethodArgValue(
methodArg, resourceNames, messageTypes, valuePatterns);
tryBodyExprs.add(
AssignmentExpr.builder()
.setVariableExpr(varExpr.toBuilder().setIsDecl(true).build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public abstract class TransportContext {

public abstract List<String> operationsClientNames();

public abstract boolean useValuePatterns();

protected static TypeNode classToType(Class<?> clazz) {
return TypeNode.withReference(ConcreteReference.withClazz(clazz));
}
Expand Down Expand Up @@ -140,6 +142,8 @@ public abstract Builder setTransportApiClientHeaderProviderBuilderNames(

public abstract Builder setOperationsClientNames(List<String> operationsClientNames);

public abstract Builder setUseValuePatterns(boolean useValuePatterns);

public abstract TransportContext build();
}
}
Loading

0 comments on commit bfb35cd

Please sign in to comment.