Skip to content

Commit

Permalink
chore: add a test to verify matching a number with a regular expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Aug 1, 2021
1 parent e2970f3 commit 4cde881
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package au.com.dius.pact.provider.junit5

import au.com.dius.pact.provider.junitsupport.Provider
import au.com.dius.pact.provider.junitsupport.loader.PactFolder
import com.github.tomakehurst.wiremock.WireMockServer
import groovy.util.logging.Slf4j
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.TestTemplate
import org.junit.jupiter.api.extension.ExtendWith
import ru.lanwen.wiremock.ext.WiremockResolver
import ru.lanwen.wiremock.ext.WiremockUriResolver

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse
import static com.github.tomakehurst.wiremock.client.WireMock.get
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo

@Provider('NumberService')
@PactFolder('pacts')
@ExtendWith([
WiremockResolver,
WiremockUriResolver
])
@Slf4j
class MatchNumberWithRegexTest {

@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider)
void testTemplate(PactVerificationContext context) {
context.verifyInteraction()
}

@BeforeEach
void before(PactVerificationContext context, @WiremockResolver.Wiremock WireMockServer server,
@WiremockUriResolver.WiremockUri String uri) throws MalformedURLException {
context.setTarget(HttpTestTarget.fromUrl(new URL(uri)))

server.stubFor(
get(urlPathEqualTo('/data'))
.willReturn(aResponse()
.withStatus(200)
.withHeader('Content-Type', 'application/json')
.withBody('{\n' +
' "number": 47576476.9092\n' +
'}'))
)
}
}
40 changes: 40 additions & 0 deletions provider/junit5/src/test/resources/pacts/match-numbers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"provider": {
"name": "NumberService"
},
"consumer": {
"name": "consumer"
},
"interactions": [
{
"description": "Get data",
"request": {
"method": "GET",
"path": "/data"
},
"response": {
"status": 200,
"body": {
"number": 1234.5677
},
"matchingRules": {
"body": {
"$.number": {
"matchers": [
{
"match": "regex",
"regex": "\\d+\\.\\d{4}"
}
]
}
}
}
}
}
],
"metadata": {
"pact-specification": {
"version": "3.0.0"
}
}
}

0 comments on commit 4cde881

Please sign in to comment.