Skip to content

Commit

Permalink
feat(compatibility-suite): Implemented Synchronous Messages feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Aug 17, 2023
1 parent 403f6cb commit 6aec655
Show file tree
Hide file tree
Showing 14 changed files with 772 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,66 @@ class SharedSteps {
}
contentType ?: 'text/plain'
}

@SuppressWarnings(['AbcMetric', 'SpaceAfterOpeningBrace'])
static void matchTypeOfElement(String type, JsonValue element) {
switch (type) {
case 'integer' -> {
assert element.type() == 'Integer'
assert element.toString() ==~ /\d+/
}
case 'decimal number' -> {
assert element.type() == 'Decimal'
assert element.toString() ==~ /\d+\.\d+/
}
case 'hexadecimal number' -> {
assert element.type() == 'String'
assert element.toString() ==~ /[a-fA-F0-9]+/
}
case 'random string' -> {
assert element.type() == 'String'
}
case 'string from the regex' -> {
assert element.type() == 'String'
assert element.toString() ==~ /\d{1,8}/
}
case 'date' -> {
assert element.type() == 'String'
assert element.toString() ==~ /\d{4}-\d{2}-\d{2}/
}
case 'time' -> {
assert element.type() == 'String'
assert element.toString() ==~ /\d{2}:\d{2}:\d{2}/
}
case 'date-time' -> {
assert element.type() == 'String'
assert element.toString() ==~ /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{1,9}/
}
case 'UUID' -> {
assert element.type() == 'String'
UUID.fromString(element.toString())
}
case 'simple UUID' -> {
assert element.type() == 'String'
assert element.toString() ==~ /[0-9a-zA-Z]{32}/
}
case 'lower-case-hyphenated UUID' -> {
assert element.type() == 'String'
UUID.fromString(element.toString())
}
case 'upper-case-hyphenated UUID' -> {
assert element.type() == 'String'
UUID.fromString(element.toString())
}
case 'URN UUID' -> {
assert element.type() == 'String'
assert element.toString().startsWith('urn:uuid:')
UUID.fromString(element.toString().substring('urn:uuid:'.length()))
}
case 'boolean' -> {
assert element.type() == 'Boolean'
}
default -> throw new AssertionError("Invalid type: $type")
}
}
}
45 changes: 1 addition & 44 deletions compatibility-suite/src/test/groovy/steps/v3/Generators.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.cucumber.java.en.When

import static steps.shared.SharedSteps.configureBody
import static steps.shared.SharedSteps.determineContentType
import static steps.shared.SharedSteps.matchTypeOfElement

@SuppressWarnings('SpaceAfterOpeningBrace')
class Generators {
Expand Down Expand Up @@ -111,50 +112,6 @@ class Generators {
matchTypeOfElement(type, element)
}

static void matchTypeOfElement(String type, JsonValue element) {
switch (type) {
case 'integer' -> {
assert element.type() == 'Integer'
assert element.toString() ==~ /\d+/
}
case 'decimal number' -> {
assert element.type() == 'Decimal'
assert element.toString() ==~ /\d+\.\d+/
}
case 'hexadecimal number' -> {
assert element.type() == 'String'
assert element.toString() ==~ /[a-fA-F0-9]+/
}
case 'random string' -> {
assert element.type() == 'String'
}
case 'string from the regex' -> {
assert element.type() == 'String'
assert element.toString() ==~ /\d{1,8}/
}
case 'date' -> {
assert element.type() == 'String'
assert element.toString() ==~ /\d{4}-\d{2}-\d{2}/
}
case 'time' -> {
assert element.type() == 'String'
assert element.toString() ==~ /\d{2}:\d{2}:\d{2}/
}
case 'date-time' -> {
assert element.type() == 'String'
assert element.toString() ==~ /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{1,9}/
}
case 'UUID' -> {
assert element.type() == 'String'
UUID.fromString(element.toString())
}
case 'boolean' -> {
assert element.type() == 'Boolean'
}
default -> throw new AssertionError("Invalid type: $type")
}
}

@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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import static au.com.dius.pact.consumer.ConsumerPactRunnerKt.runMessageConsumerT
import static au.com.dius.pact.core.support.Json.toJson
import static steps.shared.SharedSteps.configureBody
import static steps.shared.SharedSteps.determineContentType
import static steps.v3.Generators.matchTypeOfElement
import static steps.shared.SharedSteps.matchTypeOfElement

@SuppressWarnings(['ThrowRuntimeException'])
class MessageConsumer {
Expand Down
63 changes: 1 addition & 62 deletions compatibility-suite/src/test/groovy/steps/v4/Generators.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import io.cucumber.java.en.When

import static steps.shared.SharedSteps.configureBody
import static steps.shared.SharedSteps.determineContentType
import static steps.shared.SharedSteps.matchTypeOfElement

@SuppressWarnings('SpaceAfterOpeningBrace')
class Generators {
Expand Down Expand Up @@ -76,68 +77,6 @@ class Generators {
matchTypeOfElement(type, element)
}

@SuppressWarnings('AbcMetric')
static void matchTypeOfElement(String type, JsonValue element) {
switch (type) {
case 'integer' -> {
assert element.type() == 'Integer'
assert element.toString() ==~ /\d+/
}
case 'decimal number' -> {
assert element.type() == 'Decimal'
assert element.toString() ==~ /\d+\.\d+/
}
case 'hexadecimal number' -> {
assert element.type() == 'String'
assert element.toString() ==~ /[a-fA-F0-9]+/
}
case 'random string' -> {
assert element.type() == 'String'
}
case 'string from the regex' -> {
assert element.type() == 'String'
assert element.toString() ==~ /\d{1,8}/
}
case 'date' -> {
assert element.type() == 'String'
assert element.toString() ==~ /\d{4}-\d{2}-\d{2}/
}
case 'time' -> {
assert element.type() == 'String'
assert element.toString() ==~ /\d{2}:\d{2}:\d{2}/
}
case 'date-time' -> {
assert element.type() == 'String'
assert element.toString() ==~ /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{1,9}/
}
case 'UUID' -> {
assert element.type() == 'String'
UUID.fromString(element.toString())
}
case 'simple UUID' -> {
assert element.type() == 'String'
assert element.toString() ==~ /[0-9a-zA-Z]{32}/
}
case 'lower-case-hyphenated UUID' -> {
assert element.type() == 'String'
UUID.fromString(element.toString())
}
case 'upper-case-hyphenated UUID' -> {
assert element.type() == 'String'
UUID.fromString(element.toString())
}
case 'URN UUID' -> {
assert element.type() == 'String'
assert element.toString().startsWith('urn:uuid:')
UUID.fromString(element.toString().substring('urn:uuid:'.length()))
}
case 'boolean' -> {
assert element.type() == 'Boolean'
}
default -> throw new AssertionError("Invalid type: $type")
}
}

@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)
Expand Down
18 changes: 9 additions & 9 deletions compatibility-suite/src/test/groovy/steps/v4/SharedV4Data.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class SharedV4PactData {
String scenarioId
PactBuilder pactBuilder = new PactBuilder('V4 consumer', 'V4 provider', PactSpecVersion.V4)
List<Closure> builderCallbacks = []
V4Pact pact
String pactJsonStr
JsonValue.Object pactJson

@SuppressWarnings('UnnecessaryConstructor')
SharedV4PactData() { }
Expand All @@ -32,9 +35,6 @@ static Integer numType(String numType) {

class SharedV4Steps {
SharedV4PactData sharedV4PactData
V4Pact pact
String pactJsonStr
JsonValue.Object pactJson

SharedV4Steps(SharedV4PactData sharedV4PactData) {
this.sharedV4PactData = sharedV4PactData
Expand All @@ -45,27 +45,27 @@ class SharedV4Steps {
sharedV4PactData.builderCallbacks.forEach {
sharedV4PactData.pactBuilder.interactions.add(it.call())
}
pact = sharedV4PactData.pactBuilder.toPact()
pactJsonStr = Json.INSTANCE.prettyPrint(pact.toMap(PactSpecVersion.V3))
pactJson = JsonParser.parseString(pactJsonStr).asObject()
sharedV4PactData.pact = sharedV4PactData.pactBuilder.toPact()
sharedV4PactData.pactJsonStr = Json.INSTANCE.prettyPrint(sharedV4PactData.pact.toMap(PactSpecVersion.V4))
sharedV4PactData.pactJson = JsonParser.parseString(sharedV4PactData.pactJsonStr).asObject()
}

@Then('the {numType} interaction in the Pact file will have a type of {string}')
void the_interaction_in_the_pact_file_will_have_a_type_of(Integer index, String type) {
JsonValue.Array interactions = pactJson['interactions'].asArray()
JsonValue.Array interactions = sharedV4PactData.pactJson['interactions'].asArray()
assert interactions.get(index)['type'] == type
}

@Then('the {numType} interaction in the Pact file will have {string} = {string}')
void the_first_interaction_in_the_pact_file_will_have(Integer index, String name, String value) {
JsonValue.Array interactions = pactJson['interactions'].asArray()
JsonValue.Array interactions = sharedV4PactData.pactJson['interactions'].asArray()
def json = JsonParser.parseString(value)
assert interactions.get(index)[name] == json
}

@Then('there will be an interaction in the Pact file with a type of {string}')
void there_will_be_an_interaction_in_the_pact_file_with_a_type_of(String type) {
JsonValue.Array interactions = pactJson['interactions'].asArray()
JsonValue.Array interactions = sharedV4PactData.pactJson['interactions'].asArray()
assert interactions.values.find { it['type'] == type } != null
}
}
Loading

0 comments on commit 6aec655

Please sign in to comment.