Skip to content

Instantly share code, notes, and snippets.

@ivangsa
Created September 25, 2024 16:18
Show Gist options
  • Select an option

  • Save ivangsa/f115feff649567802c62c4e3def9bf0e to your computer and use it in GitHub Desktop.

Select an option

Save ivangsa/f115feff649567802c62c4e3def9bf0e to your computer and use it in GitHub Desktop.
From OpenAPI to KarateDSL Tests with ZenWave SDK
@openapi-file=src/main/resources/public/apis/openapi.yml
Feature: CreateUpdateDeleteCustomerKarateTest
Background:
* url baseUrl
# * def auth = { username: '', password: '' }
* def authHeader = call read('classpath:karate-auth.js') auth
* configure headers = authHeader || {}
@business-flow
@operationId=[createCustomer, updateCustomer, deleteCustomer, getCustomer]
Scenario: CreateUpdateDeleteCustomerKarateTest
# createCustomer
Given path '/customers'
And request
"""
{
"id" : 54,
"version" : 93,
"name" : "name-9mq",
"email" : "eldon.block@yahoo.com",
"addresses" : [ {
"street" : "street-6906yzrgz",
"city" : "Kitstad"
} ],
"paymentMethods" : [ {
"id" : 93,
"version" : 83,
"type" : "MASTERCARD",
"cardNumber" : "cardNumber-lt4efsym7we7zqb"
} ]
}
"""
When method post
Then status 201
* def createCustomerResponse = response
* def customerId = response.id
# updateCustomer
* def pathParams = { id: 68 }
Given path '/customers/', pathParams.id
And request
"""
{
"id" : 33,
"version" : 25,
"name" : "name-4zih3tx9mzwnbz62afdss",
"email" : "betsy.roob@hotmail.com",
"addresses" : [ {
"street" : "street-5m",
"city" : "Padbergview"
} ],
"paymentMethods" : [ {
"id" : 81,
"version" : 75,
"type" : "VISA",
"cardNumber" : "cardNumber-ei0b72ietozm5eu"
} ]
}
"""
When method put
Then status 200
* def updateCustomerResponse = response
* def customerId = response.id
# deleteCustomer
* def pathParams = { id: 41 }
Given path '/customers/', pathParams.id
When method delete
Then status 204
* def deleteCustomerResponse = response
# getCustomer
* def pathParams = { id: 69 }
Given path '/customers/', pathParams.id
When method get
Then status 200
* def getCustomerResponse = response
* def customerId = response.id
@openapi-file=src/main/resources/public/apis/openapi.yml
Feature: CustomerApi
Background:
* url baseUrl
# * def auth = { username: '', password: '' }
* def authHeader = call read('classpath:karate-auth.js') auth
* configure headers = authHeader || {}
@operationId=createCustomer
Scenario: createCustomer
Given path '/customers'
And request
"""
{
"id" : 27,
"version" : 39,
"name" : "name-igg5xpg15axb20ssv6h",
"email" : "dewey.dibbert@gmail.com",
"addresses" : [ {
"street" : "street-o3z",
"city" : "North Stepanie"
} ],
"paymentMethods" : [ {
"id" : 35,
"version" : 81,
"type" : "VISA",
"cardNumber" : "cardNumber-f2twmzjgr6f6ufp"
} ]
}
"""
When method post
Then status 201
* def createCustomerResponse = response
* def customerId = response.id
@operationId=getCustomer
Scenario: getCustomer
* def pathParams = { id: 50 }
Given path '/customers/', pathParams.id
When method get
Then status 200
* def getCustomerResponse = response
* def customerId = response.id
@operationId=updateCustomer
Scenario: updateCustomer
* def pathParams = { id: 85 }
Given path '/customers/', pathParams.id
And request
"""
{
"id" : 81,
"version" : 97,
"name" : "name-505qcgrn06met1b",
"email" : "alycia.goodwin@hotmail.com",
"addresses" : [ {
"street" : "street-thtje5itfiytnmo9qrq",
"city" : "Mauriciostad"
} ],
"paymentMethods" : [ {
"id" : 26,
"version" : 11,
"type" : "MASTERCARD",
"cardNumber" : "cardNumber-4v31g76wuz"
} ]
}
"""
When method put
Then status 200
* def updateCustomerResponse = response
* def customerId = response.id
@operationId=deleteCustomer
Scenario: deleteCustomer
* def pathParams = { id: 47 }
Given path '/customers/', pathParams.id
When method delete
Then status 204
* def deleteCustomerResponse = response
@operationId=searchCustomers
Scenario: searchCustomers
Given path '/customers/search'
And def queryParams = page: 41, limit: 25, sort: [null-by2aunzpbul86hoemqjsf]
And request
"""
{
"name" : "name-lbxnuiozv7o0",
"email" : "leo.abbott@gmail.com",
"city" : "West Leonardberg",
"state" : "state-36s"
}
"""
When method post
Then status 201
* def searchCustomersResponse = response
* def customerPaginatedId = response.id
openapi: 3.0.1
info:
title: "Customer JPA Example"
version: 0.0.1
description: "Customer JPA Example"
contact:
email: email@domain.com
servers:
- description: localhost
url: http://localhost:8080/api
- description: custom
url: "{protocol}://{server}/{path}"
variables:
protocol:
enum: ['http', 'https']
default: 'http'
server:
default: 'localhost:8080'
path:
default: 'api'
tags:
- name: "Default"
- name: "Customer"
paths:
/customers:
post:
operationId: createCustomer
description: "createCustomer"
tags: [Customer]
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/Customer"
responses:
"201":
description: "OK"
content:
application/json:
schema:
$ref: "#/components/schemas/Customer"
/customers/{id}:
get:
operationId: getCustomer
description: "getCustomer"
tags: [Customer]
parameters:
- name: "id"
in: path
required: true
schema:
type: integer
format: int64
responses:
"200":
description: "OK"
content:
application/json:
schema:
$ref: "#/components/schemas/Customer"
put:
operationId: updateCustomer
description: "updateCustomer"
tags: [Customer]
parameters:
- name: "id"
in: path
required: true
schema:
type: integer
format: int64
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/Customer"
responses:
"200":
description: "OK"
content:
application/json:
schema:
$ref: "#/components/schemas/Customer"
delete:
operationId: deleteCustomer
description: "deleteCustomer"
tags: [Customer]
parameters:
- name: "id"
in: path
required: true
schema:
type: integer
format: int64
responses:
"204":
description: "OK"
/customers/search:
post:
operationId: searchCustomers
description: "searchCustomers"
tags: [Customer]
parameters:
- $ref: "#/components/parameters/page"
- $ref: "#/components/parameters/limit"
- $ref: "#/components/parameters/sort"
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/CustomerSearchCriteria"
responses:
"201":
description: "OK"
content:
application/json:
schema:
$ref: "#/components/schemas/CustomerPaginated"
components:
schemas:
Customer:
type: "object"
x-business-entity: "Customer"
required:
- "name"
- "email"
properties:
id:
type: "integer"
format: "int64"
version:
type: "integer"
name:
type: "string"
maxLength: 254
description: "Customer name"
email:
type: "string"
maxLength: 254
pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}"
description: ""
addresses:
type: "array"
items:
$ref: "#/components/schemas/Address"
minLength: 1
maxLength: 5
paymentMethods:
type: "array"
items:
$ref: "#/components/schemas/PaymentMethod"
CustomerPaginated:
allOf:
- $ref: "#/components/schemas/Page"
- x-business-entity-paginated: "Customer"
- properties:
content:
type: "array"
items:
$ref: "#/components/schemas/Customer"
CustomerSearchCriteria:
type: "object"
x-business-entity: "CustomerSearchCriteria"
properties:
name:
type: "string"
email:
type: "string"
city:
type: "string"
state:
type: "string"
PaymentMethodType:
type: "string"
x-business-entity: "PaymentMethodType"
enum:
- "VISA"
- "MASTERCARD"
Address:
type: "object"
x-business-entity: "Address"
required:
- "street"
- "city"
properties:
street:
type: "string"
maxLength: 254
city:
type: "string"
maxLength: 254
PaymentMethod:
type: "object"
x-business-entity: "PaymentMethod"
required:
- "type"
- "cardNumber"
properties:
id:
type: "integer"
format: "int64"
version:
type: "integer"
type:
$ref: "#/components/schemas/PaymentMethodType"
cardNumber:
type: "string"
Page:
type: object
required:
- "content"
- "totalElements"
- "totalPages"
- "size"
- "number"
properties:
number:
type: integer
minimum: 0
numberOfElements:
type: integer
minimum: 0
size:
type: integer
minimum: 0
maximum: 200
multipleOf: 25
totalElements:
type: integer
totalPages:
type: integer
parameters:
page:
name: page
in: query
description: The number of results page
schema:
type: integer
format: int32
default: 0
limit:
name: limit
in: query
description: The number of results in a single page
schema:
type: integer
format: int32
default: 20
sort:
name: sort
in: query
description: The number of results page
schema:
type: array
items:
type: string
securitySchemes:
basicAuth: # <-- arbitrary name for the security scheme
type: http
scheme: basic
bearerAuth: # <-- arbitrary name for the security scheme
type: http
scheme: bearer
bearerFormat: JWT # optional, arbitrary value for documentation purposes
security:
- basicAuth: [] # <-- use the same name here
- bearerAuth: [] # <-- use the same name here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment