Skip to content

Commit

Permalink
feat(compatibility-suite): Add V3 HTTP generator scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jul 21, 2023
1 parent acc1aec commit cdf6d2c
Showing 1 changed file with 96 additions and 9 deletions.
105 changes: 96 additions & 9 deletions compatibility-suite/src/test/groovy/steps/v3/Generators.groovy
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package steps.v3

import au.com.dius.pact.core.model.IRequest
import au.com.dius.pact.core.model.IResponse
import au.com.dius.pact.core.model.Request
import au.com.dius.pact.core.model.JsonUtils
import au.com.dius.pact.core.model.Response
import au.com.dius.pact.core.model.generators.GeneratorTestMode
import au.com.dius.pact.core.support.json.JsonParser
import au.com.dius.pact.core.support.json.JsonValue
Expand All @@ -21,10 +23,12 @@ class Generators {
GeneratorTestMode testMode = GeneratorTestMode.Provider
JsonValue originalJson
JsonValue generatedJson
Response response
IResponse generatedResponse

@Given('a request configured with the following generators:')
void a_request_configured_with_the_following_generators(DataTable dataTable) {
request = new Request()
request = new Request('GET', '/path/one')
def entry = dataTable.entries().first()
if (entry['body']) {
configureBody(entry['body'], request)
Expand All @@ -43,6 +47,27 @@ class Generators {
}
}

@Given('a response configured with the following generators:')
void a_response_configured_with_the_following_generators(DataTable dataTable) {
response = new Response()
def entry = dataTable.entries().first()
if (entry['body']) {
configureBody(entry['body'], response)
}
if (entry['generators']) {
JsonValue json
if (entry['generators'].startsWith('JSON:')) {
json = JsonParser.INSTANCE.parseString(entry['generators'][5..-1])
} else {
File contents = new File("pact-compatibility-suite/fixtures/${entry['generators']}")
contents.withInputStream {
json = JsonParser.INSTANCE.parseStream(it)
}
}
response.generators = au.com.dius.pact.core.model.generators.Generators.fromJson(json)
}
}

@Given('the generator test mode is set as {string}')
void the_generator_test_mode_is_set_as(String mode) {
testMode = mode == 'Consumer' ? GeneratorTestMode.Consumer : GeneratorTestMode.Provider
Expand All @@ -51,20 +76,30 @@ class Generators {
@When('the request is prepared for use')
void the_request_prepared_for_use() {
generatedRequest = request.generatedRequest(context, testMode)
originalJson = JsonParser.INSTANCE.parseString(request.body.valueAsString())
generatedJson = JsonParser.INSTANCE.parseString(generatedRequest.body.valueAsString())
originalJson = request.body.present ? JsonParser.INSTANCE.parseString(request.body.valueAsString()) : null
generatedJson = generatedRequest.body.present ?
JsonParser.INSTANCE.parseString(generatedRequest.body.valueAsString()) : null
}

@When('the response is prepared for use')
void the_response_is_prepared_for_use() {
generatedResponse = response.generatedResponse(context, testMode)
originalJson = response.body.present ? JsonParser.INSTANCE.parseString(response.body.valueAsString()) : null
generatedJson = generatedResponse.body.present ?
JsonParser.INSTANCE.parseString(generatedResponse.body.valueAsString()) : null
}

@When('the request is prepared for use with a {string} context:')
void the_request_is_prepared_for_use_with_a_context(String type, DataTable dataTable) {
context[type] = JsonParser.parseString(dataTable.values().first()).asObject().entries
generatedRequest = request.generatedRequest(context, testMode)
originalJson = JsonParser.INSTANCE.parseString(request.body.valueAsString())
generatedJson = JsonParser.INSTANCE.parseString(generatedRequest.body.valueAsString())
originalJson = request.body.present ? JsonParser.INSTANCE.parseString(request.body.valueAsString()) : null
generatedJson = generatedRequest.body.present ?
JsonParser.INSTANCE.parseString(generatedRequest.body.valueAsString()) : null
}

@Then('the value for {string} will have been replaced with a {string}')
void the_value_for_will_have_been_replaced_with_a_value(String path, String type) {
@Then('the body value for {string} will have been replaced with a {string}')
void the_body_value_for_will_have_been_replaced_with_a_value(String path, String type) {
def originalElement = JsonUtils.INSTANCE.fetchPath(originalJson, path)
def element = JsonUtils.INSTANCE.fetchPath(generatedJson, path)
assert originalElement != element
Expand Down Expand Up @@ -111,12 +146,64 @@ class Generators {
}
}

@Then('the value for {string} will have been replaced with {string}')
void the_value_for_will_have_been_replaced_with_value(String path, String value) {
@Then('the body value for {string} will have been replaced with {string}')
void the_body_value_for_will_have_been_replaced_with_value(String path, String value) {
def originalElement = JsonUtils.INSTANCE.fetchPath(originalJson, path)
def element = JsonUtils.INSTANCE.fetchPath(generatedJson, path)
assert originalElement != element
assert element.type() == 'String'
assert element.toString() == value
}

@Then('the request {string} will be set as {string}')
void the_request_will_be_set_as(String part, String value) {
switch (part) {
case 'path' -> {
assert generatedRequest.path == value
}
default -> throw new AssertionError("Invalid HTTP part: $part")
}
}

@Then('the request {string} will match {string}')
void the_request_will_match(String part, String regex) {
switch (part) {
case 'path' -> {
assert generatedRequest.path ==~ regex
}
case ~/^header.*/ -> {
def header = (part =~ /\[(.*)]/)[0][1]
assert generatedRequest.headers[header].every { it ==~ regex }
}
case ~/^queryParameter.*/ -> {
def name = (part =~ /\[(.*)]/)[0][1]
assert generatedRequest.query[name].every { it ==~ regex }
}
default -> throw new AssertionError("Invalid HTTP part: $part")
}
}

@Then('the response {string} will not be {string}')
void the_response_will_not_be(String part, String value) {
switch (part) {
case 'status' -> {
assert generatedResponse.status != value.toInteger()
}
default -> throw new AssertionError("Invalid HTTP part: $part")
}
}

@Then('the response {string} will match {string}')
void the_response_will_match(String part, String regex) {
switch (part) {
case 'status' -> {
assert generatedResponse.status ==~ regex
}
case ~/^header.*/ -> {
def header = (part =~ /\[(.*)]/)[0][1]
assert generatedResponse.headers[header].every { it ==~ regex }
}
default -> throw new AssertionError("Invalid HTTP part: $part")
}
}
}

0 comments on commit cdf6d2c

Please sign in to comment.