Skip to content

Commit

Permalink
cbor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Jun 24, 2024
1 parent 67940c6 commit e7d7414
Show file tree
Hide file tree
Showing 13 changed files with 479 additions and 37 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ subprojects {
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.10.2")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.10.2")
testImplementation("org.hamcrest:hamcrest:2.2")
testImplementation("org.mockito:mockito-junit-jupiter:5.12.0")
}

// Reusable license copySpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ private String registerBodyComparatorStub(String mediaType) {
additionalStubs.add("protocol-test-text-stub.ts");
return "compareEquivalentTextBodies(bodyString, r.body)";
case "application/cbor":
writer.addSubPathImport("cbor", null,
writer.addImportSubmodule("cbor", null,
TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR);
additionalStubs.add("protocol-test-cbor-stub.ts");
return "compareEquivalentCborBodies(bodyString, r.body)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,15 @@ public TypeScriptWriter addImport(String name, String as, PackageContainer from)
return this.addImport(name, as, from.getPackageName());
}

public TypeScriptWriter addSubPathImport(String name, String as, PackageContainer from, String subPath) {
/**
* Same as {@link #addImport(String, String, PackageContainer)} but appends a
* submodule path, for example "@smithy/core/cbor".
*/
public TypeScriptWriter addImportSubmodule(String name, String as, PackageContainer from, String submodule) {
if (from instanceof Dependency) {
addDependency((Dependency) from);
}
return this.addImport(name, as, from.getPackageName() + subPath);
return this.addImport(name, as, from.getPackageName() + submodule);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,18 @@
public class CborMemberDeserVisitor extends DocumentMemberDeserVisitor {
private final String dataSource;
private final ProtocolGenerator.GenerationContext context;
private final TimestampFormatTrait.Format defaultTimestampFormat;

/**
* Constructor.
*
* @param context The generation context.
* @param dataSource The in-code location of the data to provide an output of
* ({@code output.foo}, {@code entry}, etc.)
* @param defaultTimestampFormat The default timestamp format used in absence
* of a TimestampFormat trait.
*/
public CborMemberDeserVisitor(ProtocolGenerator.GenerationContext context,
String dataSource,
TimestampFormatTrait.Format defaultTimestampFormat) {
super(context, dataSource, defaultTimestampFormat);
String dataSource) {
super(context, dataSource, TimestampFormatTrait.Format.EPOCH_SECONDS);
this.context = context;
this.defaultTimestampFormat = defaultTimestampFormat;
context.getWriter().addImport("_json", null, TypeScriptDependency.AWS_SMITHY_CLIENT);
this.serdeElisionEnabled = !context.getSettings().generateServerSdk();
this.dataSource = dataSource;
Expand All @@ -60,7 +55,7 @@ public String timestampShape(TimestampShape shape) {
dataSource,
HttpBinding.Location.DOCUMENT,
shape,
defaultTimestampFormat,
TimestampFormatTrait.Format.EPOCH_SECONDS,
requiresNumericEpochSecondsInPayload(),
context.getSettings().generateClient()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ public class CborMemberSerVisitor extends DocumentMemberSerVisitor {
* @param context The generation context.
* @param dataSource The in-code location of the data to provide an input of
* ({@code input.foo}, {@code entry}, etc.)
* @param defaultTimestampFormat The default timestamp format used in absence
* of a TimestampFormat trait.
*/
public CborMemberSerVisitor(ProtocolGenerator.GenerationContext context,
String dataSource,
TimestampFormatTrait.Format defaultTimestampFormat) {
super(context, dataSource, defaultTimestampFormat);
String dataSource) {
super(context, dataSource, TimestampFormatTrait.Format.EPOCH_SECONDS);
this.context = context;
this.serdeElisionEnabled = true;
this.dataSource = dataSource;
Expand Down Expand Up @@ -71,7 +68,7 @@ public String doubleShape(DoubleShape shape) {
*/
@Override
public String timestampShape(TimestampShape shape) {
context.getWriter().addSubPathImport(
context.getWriter().addImportSubmodule(
"dateToTag",
"__dateToTag",
TypeScriptDependency.SMITHY_CORE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.SparseTrait;
import software.amazon.smithy.model.traits.TimestampFormatTrait;
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.integration.DocumentShapeDeserVisitor;
Expand Down Expand Up @@ -200,7 +199,7 @@ protected void deserializeUnion(ProtocolGenerator.GenerationContext context, Uni

private CborMemberDeserVisitor getMemberVisitor(String dataSource) {
return new CborMemberDeserVisitor(
getContext(), dataSource, TimestampFormatTrait.Format.EPOCH_SECONDS
getContext(), dataSource
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,6 @@ protected void serializeUnion(ProtocolGenerator.GenerationContext context, Union
}

private DocumentMemberSerVisitor getMemberVisitor(String dataSource) {
return new CborMemberSerVisitor(getContext(), dataSource, TIMESTAMP_FORMAT);
return new CborMemberSerVisitor(getContext(), dataSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.TimestampFormatTrait;
import software.amazon.smithy.protocol.traits.Rpcv2CborTrait;
import software.amazon.smithy.typescript.codegen.CodegenUtils;
import software.amazon.smithy.typescript.codegen.SmithyCoreSubmodules;
Expand Down Expand Up @@ -52,12 +51,15 @@ public SmithyRpcV2Cbor() {
public void generateSharedComponents(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

writer.addSubPathImport("parseCborBody", "parseBody",
TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR);
writer.addSubPathImport("parseCborErrorBody", "parseErrorBody",
TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR);
writer.addSubPathImport("loadSmithyRpcV2CborErrorCode", null,
TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR);
writer.addImportSubmodule("parseCborBody", "parseBody",
TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR
)
.addImportSubmodule("parseCborErrorBody", "parseErrorBody",
TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR
)
.addImportSubmodule("loadSmithyRpcV2CborErrorCode", null,
TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR
);

ServiceShape service = context.getService();
deserializingErrorShapes.forEach(error -> generateErrorDeserializer(context, error));
Expand Down Expand Up @@ -219,7 +221,7 @@ protected void generateOperationDeserializer(GenerationContext context, Operatio
): Promise<$T> => {""", "}",
methodName, responseType, serdeContextType, outputType,
() -> {
writer.addSubPathImport(
writer.addImportSubmodule(
"checkCborResponse", "cr",
TypeScriptDependency.SMITHY_CORE,
SmithyCoreSubmodules.CBOR
Expand Down Expand Up @@ -268,12 +270,12 @@ protected void serializeInputDocument(GenerationContext generationContext,
StructureShape inputStructure) {
TypeScriptWriter writer = generationContext.getWriter();

writer.addSubPathImport(
writer.addImportSubmodule(
"cbor", null,
TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR
);
writer.write("body = cbor.serialize($L);", inputStructure.accept(
new CborMemberSerVisitor(generationContext, "input", getDocumentTimestampFormat()))
new CborMemberSerVisitor(generationContext, "input"))
);
}

Expand All @@ -289,7 +291,7 @@ protected void writeErrorCodeParser(GenerationContext generationContext) {
writer.write("populateBodyWithQueryCompatibility(parsedOutput, output.headers);");
}

writer.addSubPathImport(
writer.addImportSubmodule(
"loadSmithyRpcV2CborErrorCode", null,
TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR
);
Expand All @@ -305,8 +307,7 @@ protected void deserializeOutputDocument(GenerationContext generationContext,
writer.write("contents = $L;", outputStructure.accept(
new CborMemberDeserVisitor(
generationContext,
"data",
getDocumentTimestampFormat()
"data"
)
));
}
Expand All @@ -323,10 +324,6 @@ protected void writeSharedRequestHeaders(GenerationContext context) {
});
}

protected TimestampFormatTrait.Format getDocumentTimestampFormat() {
return TimestampFormatTrait.Format.EPOCH_SECONDS;
}

@Override
protected boolean enableSerdeElision() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,16 @@ public void addsFormatterForSymbols() {
public void addsFormatterForSymbolReferences() {
// TODO
}

@Test
public void addImportSubmodule() {
TypeScriptWriter writer = new TypeScriptWriter("foo");
writer.addDependency(TypeScriptDependency.SMITHY_CORE);
writer.addImportSubmodule("symbol", "__symbol", () -> "@smithy/core", "/submodule");
String result = writer.toString();

assertThat(result.trim(), equalTo("""
%simport { symbol as __symbol } from "@smithy/core/submodule";
""".formatted(CODEGEN_INDICATOR).trim()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package software.amazon.smithy.typescript.codegen.protocols.cbor;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.BlobShape;
import software.amazon.smithy.model.shapes.TimestampShape;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class CborMemberDeserVisitorTest {
private CborMemberDeserVisitor subject;

@BeforeEach
void setup(
@Mock ProtocolGenerator.GenerationContext context,
@Mock Model model,
@Mock TypeScriptWriter typeScriptWriter,
@Mock TypeScriptSettings settings
) {
when(context.getModel()).thenReturn(model);
when(context.getWriter()).thenReturn(typeScriptWriter);
when(context.getSettings()).thenReturn(settings);

subject = new CborMemberDeserVisitor(
context,
"data"
);
}

@Test
void blobShape(@Mock BlobShape blobShape) {
// no decoder for blob in cbor.
assertEquals(
"data",
subject.blobShape(
blobShape
)
);
}

@Test
void timestampShape(@Mock TimestampShape timestampShape) {
// protocol always uses this timestamp format.
assertEquals(
"__expectNonNull(__parseEpochTimestamp(data))",
subject.timestampShape(timestampShape)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package software.amazon.smithy.typescript.codegen.protocols.cbor;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.BlobShape;
import software.amazon.smithy.model.shapes.DoubleShape;
import software.amazon.smithy.model.shapes.FloatShape;
import software.amazon.smithy.model.shapes.TimestampShape;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class CborMemberSerVisitorTest {

private CborMemberSerVisitor subject;

@BeforeEach
void setup(
@Mock ProtocolGenerator.GenerationContext context,
@Mock Model model,
@Mock TypeScriptWriter typeScriptWriter
) {
when(context.getModel()).thenReturn(model);
lenient().when(context.getWriter()).thenReturn(typeScriptWriter);

subject = new CborMemberSerVisitor(
context,
"data"
);
}

@Test
void blobShape(@Mock BlobShape blob) {
// no encoder for blob in cbor.
assertEquals(
"data",
subject.blobShape(blob)
);
}

@Test
void floatShape(@Mock FloatShape floatShape) {
// no serializer function for float in cbor.
assertEquals(
"data",
subject.floatShape(floatShape)
);
}

@Test
void doubleShape(@Mock DoubleShape doubleShape) {
// no serializer function for double in cbor.
assertEquals(
"data",
subject.doubleShape(doubleShape)
);
}

@Test
void timestampShape(@Mock TimestampShape timestampShape) {
assertEquals(
"__dateToTag(data)",
subject.timestampShape(timestampShape)
);
}
}
Loading

0 comments on commit e7d7414

Please sign in to comment.