Skip to content

Adjust ssdk protocol tests to use promises #297

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

Merged
merged 1 commit into from
Mar 31, 2021
Merged
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 @@ -281,21 +281,17 @@ private void generateServerRequestTest(OperationShape operation, HttpRequestTest
writer.openBlock("it($S, async () => {", "});\n", testName, () -> {
Symbol serviceSymbol = serverSymbolProvider.toSymbol(service);
Symbol handlerSymbol = serviceSymbol.expectProperty("handler", Symbol.class);
Symbol inputType = operationSymbol.expectProperty("inputType", Symbol.class);
Symbol outputType = operationSymbol.expectProperty("outputType", Symbol.class);

// Create a mock function to set in place of the server operation function so we can capture
// input and other information.
writer.write("let testFunction = jest.fn();");
writer.write("testFunction.mockReturnValue({});");
writer.write("testFunction.mockReturnValue(Promise.resolve({}));");

// We use a partial here so that we don't have to define the entire service, but still get the advantages
// the type checker, including excess property checking. Later on we'll use `as` to cast this to the
// full service so that we can actually use it.
writer.openBlock("const testService: Partial<$T> = {", "};", serviceSymbol, () -> {
writer.addImport("Operation", "__Operation", "@aws-smithy/server-common");
writer.write("$L: testFunction as __Operation<$T, $T>,",
operationSymbol.getName(), inputType, outputType);
writer.write("$L: testFunction as $T,", operationSymbol.getName(), operationSymbol);
});

String getHandlerName = "get" + handlerSymbol.getName();
Expand All @@ -322,7 +318,6 @@ private void generateServerRequestTest(OperationShape operation, HttpRequestTest

// Capture the input. We need to cast this to any so we can index into it.
writer.write("let r: any = testFunction.mock.calls[0][0];").write("");

writeRequestParamAssertions(operation, testCase);
});
}
Expand Down Expand Up @@ -492,16 +487,16 @@ public void generateServerResponseTest(OperationShape operation, HttpResponseTes
writer.openBlock("it($S, async () => {", "});\n", testName, () -> {
Symbol outputType = operationSymbol.expectProperty("outputType", Symbol.class);
writer.openBlock("class TestService implements Partial<$T> {", "}", serviceSymbol, () -> {
writer.openBlock("$L(input: any, request: HttpRequest): $T {", "}",
writer.openBlock("$L(input: any, request: HttpRequest): Promise<$T> {", "}",
operationSymbol.getName(), outputType, () -> {
Optional<ShapeId> outputOptional = operation.getOutput();
if (outputOptional.isPresent()) {
StructureShape outputShape = model.expectShape(outputOptional.get(), StructureShape.class);
writer.writeInline("let response = ");
testCase.getParams().accept(new CommandInputNodeVisitor(outputShape, true));
writer.write("return { ...response, '$$metadata': {} };");
writer.write("return Promise.resolve({ ...response, '$$metadata': {} });");
} else {
writer.write("return { '$$metadata': {} };");
writer.write("return Promise.resolve({ '$$metadata': {} });");
}
});
});
Expand Down Expand Up @@ -547,7 +542,7 @@ private void generateServerErrorResponseTest(
// but using the partial in the meantime will give us proper type checking on the
// operation we want.
writer.openBlock("class TestService implements Partial<$T> {", "}", serviceSymbol, () -> {
writer.openBlock("$L(input: any, request: HttpRequest): $T {", "}",
writer.openBlock("$L(input: any, request: HttpRequest): Promise<$T> {", "}",
operationSymbol.getName(), outputType, () -> {
// Write out an object according to what's defined in the test case.
writer.writeInline("const response = ");
Expand Down Expand Up @@ -608,7 +603,9 @@ private void writeServerResponseTest(OperationShape operation, HttpResponseTestC
writer.openBlock("const serFn: (op: $1T) => __OperationSerializer<$2T, $1T, __SmithyException> = (op) =>"
+ " { return new TestSerializer(); };", serviceOperationsSymbol, serviceSymbol);

writer.write("const handler = new $T(service, testMux, serFn);", handlerSymbol);
writer.addImport("serializeFrameworkException", null,
"./protocols/" + ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
writer.write("const handler = new $T(service, testMux, serFn, serializeFrameworkException);", handlerSymbol);
writer.write("let r = await handler.handle(request)").write("");
writeHttpResponseAssertions(testCase);
}
Expand Down