|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | +package aws.sdk.kotlin.codegen.protocols.core |
| 6 | + |
| 7 | +import software.amazon.smithy.kotlin.codegen.core.KotlinWriter |
| 8 | +import software.amazon.smithy.kotlin.codegen.model.expectShape |
| 9 | +import software.amazon.smithy.kotlin.codegen.rendering.protocol.HttpBindingResolver |
| 10 | +import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator |
| 11 | +import software.amazon.smithy.kotlin.codegen.test.defaultSettings |
| 12 | +import software.amazon.smithy.kotlin.codegen.test.newTestContext |
| 13 | +import software.amazon.smithy.kotlin.codegen.test.shouldContainOnlyOnceWithDiff |
| 14 | +import software.amazon.smithy.kotlin.codegen.test.toSmithyModel |
| 15 | +import software.amazon.smithy.model.Model |
| 16 | +import software.amazon.smithy.model.shapes.OperationShape |
| 17 | +import software.amazon.smithy.model.shapes.ServiceShape |
| 18 | +import software.amazon.smithy.model.shapes.Shape |
| 19 | +import software.amazon.smithy.model.shapes.ShapeId |
| 20 | +import software.amazon.smithy.model.traits.TimestampFormatTrait |
| 21 | +import kotlin.test.Test |
| 22 | + |
| 23 | +class AwsHttpBindingProtocolGeneratorTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + fun `it throws base service exception on error parse failure`() { |
| 27 | + val model = """ |
| 28 | + namespace com.test |
| 29 | + use aws.protocols#restJson1 |
| 30 | +
|
| 31 | + @restJson1 |
| 32 | + service Example { |
| 33 | + version: "1.0.0", |
| 34 | + operations: [GetFoo] |
| 35 | + } |
| 36 | +
|
| 37 | + operation GetFoo { |
| 38 | + errors: [FooError] |
| 39 | + } |
| 40 | + |
| 41 | + @error("server") |
| 42 | + structure FooError { |
| 43 | + payload: String |
| 44 | + } |
| 45 | + """.toSmithyModel() |
| 46 | + |
| 47 | + // This is the value that produces the name of the service base exception type |
| 48 | + val serviceSdkName = "SdkName" |
| 49 | + |
| 50 | + val testCtx = model.newTestContext( |
| 51 | + serviceName = "Example", |
| 52 | + settings = model.defaultSettings(sdkId = serviceSdkName) |
| 53 | + ) |
| 54 | + val writer = KotlinWriter("com.test") |
| 55 | + val unit = TestableAwsHttpBindingProtocolGenerator() |
| 56 | + val op = model.expectShape<OperationShape>("com.test#GetFoo") |
| 57 | + |
| 58 | + unit.renderThrowOperationError(testCtx.generationCtx, op, writer) |
| 59 | + |
| 60 | + val actual = writer.toString() |
| 61 | + val expected = """ |
| 62 | + throw ${serviceSdkName}Exception("Failed to parse response as 'restJson1' error", ex).also { |
| 63 | + """.trimIndent() |
| 64 | + |
| 65 | + actual.shouldContainOnlyOnceWithDiff(expected) |
| 66 | + } |
| 67 | + |
| 68 | + // A concrete implementation of AwsHttpBindingProtocolGenerator to exercise renderThrowOperationError() |
| 69 | + class TestableAwsHttpBindingProtocolGenerator : AwsHttpBindingProtocolGenerator() { |
| 70 | + override fun renderDeserializeErrorDetails( |
| 71 | + ctx: ProtocolGenerator.GenerationContext, |
| 72 | + op: OperationShape, |
| 73 | + writer: KotlinWriter |
| 74 | + ) { |
| 75 | + // NOP |
| 76 | + } |
| 77 | + |
| 78 | + override val defaultTimestampFormat: TimestampFormatTrait.Format |
| 79 | + get() = throw RuntimeException("Unneeded for test") |
| 80 | + |
| 81 | + override fun getProtocolHttpBindingResolver(model: Model, serviceShape: ServiceShape): HttpBindingResolver { |
| 82 | + throw RuntimeException("Unneeded for test") |
| 83 | + } |
| 84 | + |
| 85 | + override fun renderSerializeOperationBody( |
| 86 | + ctx: ProtocolGenerator.GenerationContext, |
| 87 | + op: OperationShape, |
| 88 | + writer: KotlinWriter |
| 89 | + ) { |
| 90 | + throw RuntimeException("Unneeded for test") |
| 91 | + } |
| 92 | + |
| 93 | + override fun renderDeserializeOperationBody( |
| 94 | + ctx: ProtocolGenerator.GenerationContext, |
| 95 | + op: OperationShape, |
| 96 | + writer: KotlinWriter |
| 97 | + ) { |
| 98 | + throw RuntimeException("Unneeded for test") |
| 99 | + } |
| 100 | + |
| 101 | + override fun renderSerializeDocumentBody( |
| 102 | + ctx: ProtocolGenerator.GenerationContext, |
| 103 | + shape: Shape, |
| 104 | + writer: KotlinWriter |
| 105 | + ) { |
| 106 | + throw RuntimeException("Unneeded for test") |
| 107 | + } |
| 108 | + |
| 109 | + override fun renderDeserializeDocumentBody( |
| 110 | + ctx: ProtocolGenerator.GenerationContext, |
| 111 | + shape: Shape, |
| 112 | + writer: KotlinWriter |
| 113 | + ) { |
| 114 | + throw RuntimeException("Unneeded for test") |
| 115 | + } |
| 116 | + |
| 117 | + override fun renderDeserializeException( |
| 118 | + ctx: ProtocolGenerator.GenerationContext, |
| 119 | + shape: Shape, |
| 120 | + writer: KotlinWriter |
| 121 | + ) { |
| 122 | + throw RuntimeException("Unneeded for test") |
| 123 | + } |
| 124 | + |
| 125 | + override val protocol: ShapeId |
| 126 | + get() = throw RuntimeException("Unneeded for test") |
| 127 | + } |
| 128 | +} |
0 commit comments