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

feat: make generated test values comply with url path template #903

Merged
merged 2 commits into from
Jan 21, 2022
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 @@ -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