diff --git a/adapter/awslambda/src/test/java/io/gatehill/imposter/awslambda/OpenApiSimpleTest.kt b/adapter/awslambda/src/test/java/io/gatehill/imposter/awslambda/OpenApiSimpleTest.kt new file mode 100644 index 000000000..38c3b8c61 --- /dev/null +++ b/adapter/awslambda/src/test/java/io/gatehill/imposter/awslambda/OpenApiSimpleTest.kt @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2023-2023. + * + * This file is part of Imposter. + * + * "Commons Clause" License Condition v1.0 + * + * The Software is provided to you by the Licensor under the License, as + * defined below, subject to the following condition. + * + * Without limiting other conditions in the License, the grant of rights + * under the License will not include, and the License does not grant to + * you, the right to Sell the Software. + * + * For purposes of the foregoing, "Sell" means practicing any or all of + * the rights granted to you under the License to provide to third parties, + * for a fee or other consideration (including without limitation fees for + * hosting or consulting/support services related to the Software), a + * product or service whose value derives, entirely or substantially, from + * the functionality of the Software. Any license notice or attribution + * required by the License must also include this Commons Clause License + * Condition notice. + * + * Software: Imposter + * + * License: GNU Lesser General Public License version 3 + * + * Licensor: Peter Cornish + * + * Imposter is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Imposter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Imposter. If not, see . + */ + +package io.gatehill.imposter.awslambda + +import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent +import com.amazonaws.services.lambda.runtime.tests.annotations.Event +import org.hamcrest.CoreMatchers.containsString +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.params.ParameterizedTest + +/** + * Tests OpenAPI plugin. + */ +class OpenApiSimpleTest : AbstractHandlerTest() { + private var handler: HandlerV2? = null + + override val configDir = "/openapi-simple/config" + + @BeforeEach + fun setUp() { + configure() + handler = HandlerV2() + } + + @ParameterizedTest + @Event(value = "openapi-simple/requests/success.json", type = APIGatewayV2HTTPEvent::class) + fun `single request should be dispatched`(event: APIGatewayV2HTTPEvent) { + val responseEvent = handler!!.handleRequest(event, context!!) + + assertNotNull(responseEvent, "Response event should be returned") + assertEquals(200, responseEvent.statusCode) + assertThat(responseEvent.body, containsString("Cat")) + } + + @ParameterizedTest + @Event(value = "openapi-simple/requests/spec_ui.json", type = APIGatewayV2HTTPEvent::class) + fun `spec UI is returned`(event: APIGatewayV2HTTPEvent) { + val responseEvent = handler!!.handleRequest(event, context!!) + + assertNotNull(responseEvent, "Response event should be returned") + assertEquals(200, responseEvent.statusCode) + assertThat(responseEvent.body, containsString("Imposter API Sandbox")) + } +} diff --git a/adapter/awslambda/src/test/resources/openapi-simple/config/imposter-config.yaml b/adapter/awslambda/src/test/resources/openapi-simple/config/imposter-config.yaml new file mode 100644 index 000000000..940e60d65 --- /dev/null +++ b/adapter/awslambda/src/test/resources/openapi-simple/config/imposter-config.yaml @@ -0,0 +1,3 @@ +--- +plugin: openapi +specFile: petstore.yaml diff --git a/adapter/awslambda/src/test/resources/openapi-simple/config/petstore.yaml b/adapter/awslambda/src/test/resources/openapi-simple/config/petstore.yaml new file mode 100644 index 000000000..a48c2b773 --- /dev/null +++ b/adapter/awslambda/src/test/resources/openapi-simple/config/petstore.yaml @@ -0,0 +1,33 @@ +openapi: "3.0.1" + +info: + title: Sample Petstore service + version: "1.0.0" + +paths: + /pets: + get: + responses: + '200': + description: Returns all pets from the system + content: + application/json: + schema: + type: array + items: + type: object + required: + - id + - name + properties: + id: + type: integer + name: + type: string + examples: + itemsExample: + value: + [ + { "id": 101, "name": "Cat" }, + { "id": 102, "name": "Dog" } + ] diff --git a/adapter/awslambda/src/test/resources/openapi-simple/requests/spec_ui.json b/adapter/awslambda/src/test/resources/openapi-simple/requests/spec_ui.json new file mode 100644 index 000000000..f57e832eb --- /dev/null +++ b/adapter/awslambda/src/test/resources/openapi-simple/requests/spec_ui.json @@ -0,0 +1,12 @@ +{ + "headers": {}, + "queryStringParameters": {}, + "pathParameters": {}, + "isBase64Encoded": false, + "requestContext": { + "http": { + "path": "/_spec/", + "method": "GET" + } + } +} diff --git a/adapter/awslambda/src/test/resources/openapi-simple/requests/success.json b/adapter/awslambda/src/test/resources/openapi-simple/requests/success.json new file mode 100644 index 000000000..d064aab96 --- /dev/null +++ b/adapter/awslambda/src/test/resources/openapi-simple/requests/success.json @@ -0,0 +1,14 @@ +{ + "headers": { + "Accept": "application/json" + }, + "queryStringParameters": {}, + "pathParameters": {}, + "isBase64Encoded": false, + "requestContext": { + "http": { + "path": "/pets", + "method": "GET" + } + } +}