Skip to content

Commit

Permalink
fix: just use the DateTimeFormatter to parse any datetime value
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jun 12, 2020
1 parent 174acc0 commit 05dddad
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package au.com.dius.pact.consumer.junit5

import au.com.dius.pact.consumer.MockServer
import au.com.dius.pact.consumer.dsl.PactDslJsonBody
import au.com.dius.pact.consumer.dsl.PactDslWithProvider
import au.com.dius.pact.core.model.RequestResponsePact
import au.com.dius.pact.core.model.annotations.Pact
import org.apache.http.HttpResponse
import org.apache.http.client.fluent.Request
import org.apache.http.entity.StringEntity
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

@ExtendWith(PactConsumerTestExt)
@PactTestFor(providerName = 'ProviderWithDateTime')
class DateTimeWithTimezoneTest {
@Pact(consumer = 'Consumer')
RequestResponsePact pactWithTimezone(PactDslWithProvider builder) {
builder
.uponReceiving('a request with some datetime info')
.method('POST')
.path('/values')
.body(new PactDslJsonBody().datetime('datetime', "YYYY-MM-dd'T'HH:mm:ss.SSSxxx"))
.willRespondWith()
.status(200)
.body(new PactDslJsonBody().datetime('datetime', "YYYY-MM-dd'T'HH:mm:ss.SSSxxx"))
.toPact()
}

@Test
void testFiles(MockServer mockServer) {
HttpResponse httpResponse = Request.Post("${mockServer.url}/values")
.body(new StringEntity('{"datetime": "' +
DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSSxxx").format(ZonedDateTime.now())
+ '"}', 'application/json', 'UTF-8'))
.execute().returnResponse()
assert httpResponse.statusLine.statusCode == 200
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import org.w3c.dom.Text
import java.math.BigDecimal
import java.math.BigInteger
import java.text.ParseException
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException

Expand Down Expand Up @@ -323,7 +321,7 @@ fun <M : Mismatch> matchDateTime(
emptyList()
} else {
try {
ZonedDateTime.parse(safeToString(actual), DateTimeFormatter.ofPattern(pattern).withZone(ZoneId.systemDefault()))
DateTimeFormatter.ofPattern(pattern).parse(safeToString(actual))
emptyList<M>()
} catch (e: DateTimeParseException) {
listOf(mismatchFactory.create(expected, actual,
Expand Down

0 comments on commit 05dddad

Please sign in to comment.