Unverified Commit 45323f74 authored by david-perez's avatar david-perez Committed by GitHub
Browse files

Minimize `AdditionalErrorsDecoratorTest.kt` (#1339)

parent ad569b17
Loading
Loading
Loading
Loading
+22 −43
Original line number Diff line number Diff line
@@ -9,69 +9,48 @@ import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverTestSymbolProvider
import software.amazon.smithy.rust.codegen.smithy.transformers.OperationNormalizer
import software.amazon.smithy.rust.codegen.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.util.lookup

class AdditionalErrorsDecoratorTest {
    private val baseModel = """
        namespace smithy.test
        use aws.protocols#restJson1

        @restJson1
        @title("Test")
        service Test {
            version: "2022-01-01",
            operations: [
                Infallible,
                Fallible
            ]
        }
        namespace test

        operation Infallible {
            input: InfallibleInput,
            output: InfallibleOutput
            input: InputOutput,
            output: InputOutput
        }
        structure InfallibleInput {}
        structure InfallibleOutput {}

        operation Fallible {
            input: FallibleInput,
            output: FallibleOutput,
            errors: [FallibleError]
            input: InputOutput,
            output: InputOutput,
            errors: [AnError]
        }
        structure FallibleInput {}
        structure FallibleOutput {}
        
        structure InputOutput { }
        
        @error("client")
        structure FallibleError {}
        structure AnError { }
    """.asSmithyModel()
    private val model = OperationNormalizer.transform(baseModel)
    private val symbolProvider = serverTestSymbolProvider(model)
    private val service = ServiceShape.builder().id("smithy.test#Test").build()

    @Test
    fun `add InternalServerError to infallible operations only`() {
        val service = ServiceShape.builder().id("smithy.test#Test").build()
        val infallibleId = ShapeId.from("smithy.test#Infallible")
        val fallibleId = ShapeId.from("smithy.test#Fallible")
        model.expectShape(infallibleId, OperationShape::class.java).errors.isEmpty() shouldBe true
        model.expectShape(fallibleId, OperationShape::class.java).errors.size shouldBe 1
        val model = AddInternalServerErrorToInfallibleOpsDecorator().transformModel(service, model)
        model.expectShape(infallibleId, OperationShape::class.java).errors.isEmpty() shouldBe false
        model.expectShape(infallibleId, OperationShape::class.java).errors.size shouldBe 1
        model.expectShape(fallibleId, OperationShape::class.java).errors.size shouldBe 1
        model.lookup<OperationShape>("test#Infallible").errors.isEmpty() shouldBe true
        model.lookup<OperationShape>("test#Fallible").errors.size shouldBe 1
        val transformedModel = AddInternalServerErrorToInfallibleOpsDecorator().transformModel(service, model)
        transformedModel.lookup<OperationShape>("test#Infallible").errors.size shouldBe 1
        transformedModel.lookup<OperationShape>("test#Fallible").errors.size shouldBe 1
    }

    @Test
    fun `add InternalServerError to all model operations`() {
        val service = ServiceShape.builder().id("smithy.test#Test").build()
        val infallibleId = ShapeId.from("smithy.test#Infallible")
        val fallibleId = ShapeId.from("smithy.test#Fallible")
        model.expectShape(infallibleId, OperationShape::class.java).errors.isEmpty() shouldBe true
        model.expectShape(fallibleId, OperationShape::class.java).errors.size shouldBe 1
        val model = AddInternalServerErrorToAllOpsDecorator().transformModel(service, model)
        model.expectShape(infallibleId, OperationShape::class.java).errors.isEmpty() shouldBe false
        model.expectShape(infallibleId, OperationShape::class.java).errors.size shouldBe 1
        model.expectShape(fallibleId, OperationShape::class.java).errors.size shouldBe 2
        model.lookup<OperationShape>("test#Infallible").errors.isEmpty() shouldBe true
        model.lookup<OperationShape>("test#Fallible").errors.size shouldBe 1
        val transformedModel = AddInternalServerErrorToAllOpsDecorator().transformModel(service, model)
        transformedModel.lookup<OperationShape>("test#Infallible").errors.size shouldBe 1
        transformedModel.lookup<OperationShape>("test#Fallible").errors.size shouldBe 2
    }
}