Skip to content

Commit

Permalink
Closes #1882 - Enable multi value pattern matching for DateTime (#1925)
Browse files Browse the repository at this point in the history
* Fixes #1882 - Enable multi value pattern matching for DateTime
  • Loading branch information
klaasdellschaft committed Jul 19, 2022
1 parent dcd881c commit bfbbd8c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Thomas Akehurst
* Copyright (C) 2021-2022 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -70,7 +70,7 @@ public boolean isExactMatch() {

@Override
public double getDistance() {
double distance = -1;
double distance = 1;
if (isZoned && zonedActual != null) {
distance = calculateDistance(zonedExpected, zonedActual);
} else if (isLocal && localActual != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Thomas Akehurst
* Copyright (C) 2021-2022 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -240,7 +240,7 @@ private static ZonedDateTime parseZonedOrNull(String dateTimeString, DateTimePar

private static ZonedDateTime parseZonedOrNull(
String dateTimeString, List<DateTimeParser> parsers) {
if (parsers.isEmpty()) {
if (parsers.isEmpty() || dateTimeString == null) {
return null;
}

Expand All @@ -256,6 +256,10 @@ private static LocalDateTime parseLocalOrNull(String dateTimeString) {
}

private static LocalDateTime parseLocalOrNull(String dateTimeString, DateTimeParser parser) {
if (dateTimeString == null) {
return null;
}

try {
return parser != null
? parser.parseLocalDateTime(dateTimeString)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 Thomas Akehurst
* Copyright (C) 2011-2022 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,10 +27,7 @@
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_OK;
import static java.util.Collections.singletonList;
import static org.apache.hc.core5.http.ContentType.APPLICATION_JSON;
import static org.apache.hc.core5.http.ContentType.APPLICATION_OCTET_STREAM;
import static org.apache.hc.core5.http.ContentType.APPLICATION_XML;
import static org.apache.hc.core5.http.ContentType.TEXT_PLAIN;
import static org.apache.hc.core5.http.ContentType.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -817,7 +814,7 @@ public void returnsContentTypeHeaderEncodingInCorrectCase() {
}

@Test
public void matchesOnLiteralZonedDate() {
public void matchesInRequestBodyOnLiteralZonedDate() {
stubFor(
post("/date")
.withRequestBody(matchingJsonPath("$.date", before("2021-10-11T00:00:00Z")))
Expand All @@ -836,6 +833,24 @@ public void matchesOnLiteralZonedDate() {
is(404));
}

@Test
public void matchesQueryParameterOnLiteralZonedDate() {
stubFor(
get(urlPathEqualTo("/match-query-parameter"))
.withQueryParam("date", before("2021-10-11T00:00:00Z"))
.willReturn(ok()));

assertThat(
testClient.get("/match-query-parameter?date=2021-06-22T23%3A59%3A59Z").statusCode(),
is(200));

assertThat(
testClient.get("/match-query-parameter?date=2121-06-22T23%3A59%3A59Z").statusCode(),
is(404));

assertThat(testClient.get("/match-query-parameter").statusCode(), is(404));
}

@Test
public void matchesOnNowOffsetDate() {
stubFor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Thomas Akehurst
* Copyright (C) 2021-2022 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,12 +17,15 @@

import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.common.*;
import com.github.tomakehurst.wiremock.common.DateTimeOffset;
import com.github.tomakehurst.wiremock.common.DateTimeTruncation;
import com.github.tomakehurst.wiremock.common.DateTimeUnit;
import com.github.tomakehurst.wiremock.common.Json;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -86,11 +89,18 @@ public void matchesNowWithExpectedAndActualTruncated() {
assertFalse(matcher.match(bad.toString()).isExactMatch());
}

@Test
public void doesNotMatchWhenActualValueIsNull() {
StringValuePattern matcher = WireMock.after("2021-06-14T15:15:15Z");
assertFalse(matcher.match(null).isExactMatch());
}

@Test
public void returnsAReasonableDistanceWhenNoMatchForLocalExpectedZonedActual() {
StringValuePattern matcher = WireMock.after("2021-01-01T00:00:00Z");
assertThat(matcher.match("1971-01-01T00:00:00Z").getDistance(), is(0.5));
assertThat(matcher.match("1921-01-01T00:00:00Z").getDistance(), is(1.0));
assertThat(matcher.match(null).getDistance(), is(1.0));
assertThat(matcher.match("2020-01-01T00:00:00Z").getDistance(), is(0.01));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Thomas Akehurst
* Copyright (C) 2021-2022 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -73,6 +73,12 @@ public void doesNotMatchWhenActualValueUnparseable() {
assertFalse(matcher.match("2021-06-01T15:15:blahsdfj123").isExactMatch());
}

@Test
public void doesNotMatchWhenActualValueIsNull() {
StringValuePattern matcher = WireMock.before("2021-06-14T15:15:15");
assertFalse(matcher.match(null).isExactMatch());
}

@Test
public void doesNotMatchWhenExpectedValueUnparseable() {
StringValuePattern matcher = WireMock.before("2021-06-wrongstuff:15:15");
Expand All @@ -84,6 +90,7 @@ public void returnsAReasonableDistanceWhenNoMatchForZonedExpectedZonedActual() {
StringValuePattern matcher = WireMock.before("2021-01-01T00:00:00Z");
assertThat(matcher.match("2071-01-01T00:00:00Z").getDistance(), is(0.5));
assertThat(matcher.match("2121-01-01T00:00:00Z").getDistance(), is(1.0));
assertThat(matcher.match(null).getDistance(), is(1.0));
assertThat(matcher.match("2022-01-01T00:00:00Z").getDistance(), is(0.01));
}

Expand All @@ -92,6 +99,7 @@ public void returnsAReasonableDistanceWhenNoMatchForLocalExpectedZonedActual() {
StringValuePattern matcher = WireMock.before("2021-01-01T00:00:00");
assertThat(matcher.match("2071-01-01T00:00:00Z").getDistance(), is(0.5));
assertThat(matcher.match("2121-01-01T00:00:00Z").getDistance(), is(1.0));
assertThat(matcher.match(null).getDistance(), is(1.0));
assertThat(matcher.match("2022-01-01T00:00:00Z").getDistance(), is(0.01));
}

Expand All @@ -100,6 +108,7 @@ public void returnsAReasonableDistanceWhenNoMatchForLocalExpectedLocalActual() {
StringValuePattern matcher = WireMock.before("2021-01-01T00:00:00");
assertThat(matcher.match("2071-01-01T00:00:00").getDistance(), is(0.5));
assertThat(matcher.match("2121-01-01T00:00:00").getDistance(), is(1.0));
assertThat(matcher.match(null).getDistance(), is(1.0));
assertThat(matcher.match("2022-01-01T00:00:00").getDistance(), is(0.01));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Thomas Akehurst
* Copyright (C) 2021-2022 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -105,11 +105,18 @@ public void matchesActualInEpochTimeFormat() {
assertFalse(matcher.match(bad).isExactMatch());
}

@Test
public void doesNotMatchWhenActualValueIsNull() {
StringValuePattern matcher = WireMock.equalToDateTime("2021-06-14T12:13:14Z");
assertFalse(matcher.match(null).isExactMatch());
}

@Test
public void returnsAReasonableDistanceWhenNoMatchForLocalExpectedZonedActual() {
StringValuePattern matcher = WireMock.equalToDateTime("2021-01-01T00:00:00Z");
assertThat(matcher.match("2071-01-01T00:00:00Z").getDistance(), is(0.5));
assertThat(matcher.match("2121-01-01T00:00:00Z").getDistance(), is(1.0));
assertThat(matcher.match(null).getDistance(), is(1.0));
assertThat(matcher.match("2022-01-01T00:00:00Z").getDistance(), is(0.01));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.github.tomakehurst.wiremock.matching;

import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.http.HttpHeader.absent;
import static com.github.tomakehurst.wiremock.http.HttpHeader.httpHeader;
import static com.github.tomakehurst.wiremock.http.QueryParameter.queryParam;
Expand All @@ -42,6 +42,12 @@ public void returnsNonMatchForPresentHeaderWhenRequiredAbsent() {
MultiValuePattern.absent().match(httpHeader("the-key", "the value")).isExactMatch());
}

@Test
public void returnsNonMatchForAbsentHeaderWhenRequiredBeforeNow() {
assertFalse(
MultiValuePattern.of(beforeNow()).match(HttpHeader.absent("any-key")).isExactMatch());
}

@Test
public void returnsExactMatchForPresentHeaderWhenRequiredPresent() {
assertTrue(
Expand Down

0 comments on commit bfbbd8c

Please sign in to comment.