|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.rust.codegen.server.smithy.protocols |
| 7 | + |
| 8 | +import io.kotest.assertions.throwables.shouldThrow |
| 9 | +import io.kotest.matchers.shouldBe |
| 10 | +import io.kotest.matchers.string.shouldContain |
| 11 | +import org.junit.jupiter.api.Test |
| 12 | +import software.amazon.smithy.aws.traits.protocols.AwsJson1_1Trait |
| 13 | +import software.amazon.smithy.aws.traits.protocols.RestJson1Trait |
| 14 | +import software.amazon.smithy.aws.traits.protocols.RestXmlTrait |
| 15 | +import software.amazon.smithy.codegen.core.CodegenException |
| 16 | +import software.amazon.smithy.rust.codegen.core.smithy.protocols.AwsJsonVersion |
| 17 | +import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel |
| 18 | + |
| 19 | + |
| 20 | +class ServerProtocolLoaderTest { |
| 21 | + private val testModel = |
| 22 | + """ |
| 23 | + ${"$"}version: "2" |
| 24 | +
|
| 25 | + namespace test |
| 26 | +
|
| 27 | + use aws.api#service |
| 28 | + use aws.protocols#awsJson1_0 |
| 29 | +
|
| 30 | + @awsJson1_0 |
| 31 | + @service( |
| 32 | + sdkId: "Test", |
| 33 | + arnNamespace: "test" |
| 34 | + ) |
| 35 | + service TestService { |
| 36 | + version: "2024-04-01" |
| 37 | + } |
| 38 | + """.asSmithyModel(smithyVersion = "2.0") |
| 39 | + |
| 40 | + private val testModelNoProtocol = |
| 41 | + """ |
| 42 | + ${"$"}version: "2" |
| 43 | +
|
| 44 | + namespace test |
| 45 | +
|
| 46 | + use aws.api#service |
| 47 | +
|
| 48 | + @service( |
| 49 | + sdkId: "Test", |
| 50 | + arnNamespace: "test" |
| 51 | + ) |
| 52 | + service TestService { |
| 53 | + version: "2024-04-01" |
| 54 | + } |
| 55 | + """.asSmithyModel(smithyVersion = "2.0") |
| 56 | + |
| 57 | + @Test |
| 58 | + fun `ensures protocols are matched`() { |
| 59 | + val loader = ServerProtocolLoader(ServerProtocolLoader.DefaultProtocols) |
| 60 | + |
| 61 | + val (shape, _) = loader.protocolFor(testModel, testModel.serviceShapes.first()) |
| 62 | + |
| 63 | + shape.name shouldBe "awsJson1_0" |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `ensures unmatched service protocol fails`() { |
| 68 | + val loader = ServerProtocolLoader( |
| 69 | + mapOf( |
| 70 | + RestJson1Trait.ID to |
| 71 | + ServerRestJsonFactory( |
| 72 | + additionalServerHttpBoundProtocolCustomizations = |
| 73 | + listOf( |
| 74 | + StreamPayloadSerializerCustomization(), |
| 75 | + ), |
| 76 | + ), |
| 77 | + RestXmlTrait.ID to |
| 78 | + ServerRestXmlFactory( |
| 79 | + additionalServerHttpBoundProtocolCustomizations = |
| 80 | + listOf( |
| 81 | + StreamPayloadSerializerCustomization(), |
| 82 | + ), |
| 83 | + ), |
| 84 | + AwsJson1_1Trait.ID to |
| 85 | + ServerAwsJsonFactory( |
| 86 | + AwsJsonVersion.Json11, |
| 87 | + additionalServerHttpBoundProtocolCustomizations = listOf(StreamPayloadSerializerCustomization()), |
| 88 | + ), |
| 89 | + ) |
| 90 | + ) |
| 91 | + val exception = shouldThrow<CodegenException> { |
| 92 | + loader.protocolFor(testModel, testModel.serviceShapes.first()) |
| 93 | + } |
| 94 | + exception.message shouldContain("Unable to find a matching protocol") |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun `ensures service without protocol fails`() { |
| 99 | + val loader = ServerProtocolLoader(ServerProtocolLoader.DefaultProtocols) |
| 100 | + val exception = shouldThrow<CodegenException> { |
| 101 | + loader.protocolFor(testModelNoProtocol, testModelNoProtocol.serviceShapes.first()) |
| 102 | + } |
| 103 | + exception.message shouldContain("Service must have a protocol trait") |
| 104 | + } |
| 105 | +} |
0 commit comments