Skip to content

Commit

Permalink
Fixes wiremock#1882 - Enable multi value pattern matching for DateTime
Browse files Browse the repository at this point in the history
- Use "1.0" as match distance where realistically no distance can be calculated
- Apply Spotless checks
  • Loading branch information
klaasdellschaft committed Jul 14, 2022
1 parent c90c99c commit 27310d2
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 73 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 All @@ -15,19 +15,20 @@
*/
package com.github.tomakehurst.wiremock.matching;

import static com.github.tomakehurst.wiremock.common.DateTimeParser.ZONED_PARSERS;
import static java.util.Collections.singletonList;

import com.github.tomakehurst.wiremock.common.DateTimeOffset;
import com.github.tomakehurst.wiremock.common.DateTimeParser;
import com.github.tomakehurst.wiremock.common.DateTimeTruncation;
import com.github.tomakehurst.wiremock.common.DateTimeUnit;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.util.List;

import static com.github.tomakehurst.wiremock.common.DateTimeParser.ZONED_PARSERS;
import static java.util.Collections.singletonList;

public abstract class AbstractDateTimePattern extends StringValuePattern {

private final ZonedDateTime zonedDateTime;
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 All @@ -15,40 +15,11 @@
*/
package com.github.tomakehurst.wiremock;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.client.WireMock.any;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.common.DateTimeTruncation.FIRST_MINUTE_OF_HOUR;
import static com.github.tomakehurst.wiremock.common.DateTimeUnit.HOURS;
import static com.github.tomakehurst.wiremock.http.RequestMethod.GET;
import static com.github.tomakehurst.wiremock.http.RequestMethod.POST;
import static com.github.tomakehurst.wiremock.testsupport.MultipartBody.part;
import static com.github.tomakehurst.wiremock.testsupport.TestHttpHeader.withHeader;
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.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.github.tomakehurst.wiremock.admin.model.ListStubMappingsResult;
import com.github.tomakehurst.wiremock.http.Fault;
import com.github.tomakehurst.wiremock.matching.StringValuePattern;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import com.github.tomakehurst.wiremock.testsupport.WireMockResponse;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.MalformedChunkCodingException;
import org.apache.hc.core5.http.NoHttpResponseException;
Expand All @@ -60,6 +31,33 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.github.tomakehurst.wiremock.client.WireMock.any;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.common.DateTimeTruncation.FIRST_MINUTE_OF_HOUR;
import static com.github.tomakehurst.wiremock.common.DateTimeUnit.HOURS;
import static com.github.tomakehurst.wiremock.http.RequestMethod.GET;
import static com.github.tomakehurst.wiremock.http.RequestMethod.POST;
import static com.github.tomakehurst.wiremock.testsupport.MultipartBody.part;
import static com.github.tomakehurst.wiremock.testsupport.TestHttpHeader.withHeader;
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.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class StubbingAcceptanceTest extends AcceptanceTestBase {

@BeforeAll
Expand Down Expand Up @@ -851,9 +849,7 @@ public void matchesQueryParameterOnLiteralZonedDate() {
testClient.get("/match-query-parameter?date=2121-06-22T23%3A59%3A59Z").statusCode(),
is(404));

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

@Test
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 @@ -15,18 +15,22 @@
*/
package com.github.tomakehurst.wiremock.matching;

import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
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 org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;

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

public class AfterDateTimePatternTest {

Expand Down Expand Up @@ -97,7 +101,7 @@ 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(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 All @@ -15,23 +15,24 @@
*/
package com.github.tomakehurst.wiremock.matching;

import static com.github.tomakehurst.wiremock.common.DateTimeTruncation.*;
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
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.DateTimeUnit;
import com.github.tomakehurst.wiremock.common.Json;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import org.junit.jupiter.api.Test;

import static com.github.tomakehurst.wiremock.common.DateTimeTruncation.*;
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class BeforeDateTimePatternTest {

Expand Down Expand Up @@ -90,7 +91,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(null).getDistance(), is(1.0));
assertThat(matcher.match("2022-01-01T00:00:00Z").getDistance(), is(0.01));
}

Expand All @@ -99,7 +100,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(null).getDistance(), is(1.0));
assertThat(matcher.match("2022-01-01T00:00:00Z").getDistance(), is(0.01));
}

Expand All @@ -108,7 +109,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(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 All @@ -15,23 +15,24 @@
*/
package com.github.tomakehurst.wiremock.matching;

import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.HOURS;
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static org.hamcrest.MatcherAssert.assertThat;
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.DateTimeOffset;
import com.github.tomakehurst.wiremock.common.DateTimeTruncation;
import com.github.tomakehurst.wiremock.common.Json;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.junit.jupiter.api.Test;

import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.HOURS;
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class EqualToDateTimePatternTest {

Expand Down Expand Up @@ -116,7 +117,7 @@ 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(null).getDistance(), is(1.0));
assertThat(matcher.match("2022-01-01T00:00:00Z").getDistance(), is(0.01));
}

Expand Down

0 comments on commit 27310d2

Please sign in to comment.