Skip to content

Commit

Permalink
test(lambda): improves coverage for openapi spec and examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Oct 2, 2023
1 parent 81a6ec5 commit f07664f
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
plugin: openapi
specFile: petstore.yaml
Original file line number Diff line number Diff line change
@@ -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" }
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"headers": {},
"queryStringParameters": {},
"pathParameters": {},
"isBase64Encoded": false,
"requestContext": {
"http": {
"path": "/_spec/",
"method": "GET"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"headers": {
"Accept": "application/json"
},
"queryStringParameters": {},
"pathParameters": {},
"isBase64Encoded": false,
"requestContext": {
"http": {
"path": "/pets",
"method": "GET"
}
}
}

0 comments on commit f07664f

Please sign in to comment.