diff --git a/pom.xml b/pom.xml index 001436ed..e93e8c9c 100644 --- a/pom.xml +++ b/pom.xml @@ -352,10 +352,11 @@ test - org.wiremock + com.github.tomakehurst wiremock - 3.2.0 + 2.27.2 test + diff --git a/src/test/java/org/matomo/java/tracking/MatomoTrackerIT.java b/src/test/java/org/matomo/java/tracking/MatomoTrackerIT.java index fb51086c..7ce0894c 100644 --- a/src/test/java/org/matomo/java/tracking/MatomoTrackerIT.java +++ b/src/test/java/org/matomo/java/tracking/MatomoTrackerIT.java @@ -1,6 +1,27 @@ package org.matomo.java.tracking; -import com.github.tomakehurst.wiremock.junit5.WireMockTest; +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; +import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.status; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; +import static java.util.Collections.singleton; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; +import java.net.URI; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.Arrays; +import java.util.Locale.LanguageRange; +import java.util.concurrent.CompletableFuture; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.matomo.java.tracking.MatomoRequest.MatomoRequestBuilder; @@ -16,32 +37,11 @@ import org.matomo.java.tracking.parameters.UniqueId; import org.matomo.java.tracking.parameters.VisitorId; -import java.net.URI; -import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.util.Arrays; -import java.util.Locale.LanguageRange; -import java.util.concurrent.CompletableFuture; - -import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; -import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson; -import static com.github.tomakehurst.wiremock.client.WireMock.get; -import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; -import static com.github.tomakehurst.wiremock.client.WireMock.post; -import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; -import static com.github.tomakehurst.wiremock.client.WireMock.resetAllRequests; -import static com.github.tomakehurst.wiremock.client.WireMock.status; -import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; -import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; -import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; -import static com.github.tomakehurst.wiremock.client.WireMock.verify; -import static java.util.Collections.singleton; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -@WireMockTest(httpPort = 8099) class MatomoTrackerIT { + private static final WireMockServer wireMockServer = new WireMockServer( + WireMockConfiguration.options().dynamicPort()); + private static final int SITE_ID = 42; private final TrackerConfigurationBuilder trackerConfigurationBuilder = TrackerConfiguration.builder(); @@ -52,11 +52,16 @@ class MatomoTrackerIT { private CompletableFuture future; + @BeforeAll + static void beforeAll() { + wireMockServer.start(); + } + @BeforeEach void givenStub() { - resetAllRequests(); - stubFor(post(urlPathEqualTo("/matomo.php")).willReturn(status(204))); - stubFor(get(urlPathEqualTo("/matomo.php")).willReturn(status(204))); + wireMockServer.resetRequests(); + wireMockServer.stubFor(post(urlPathEqualTo("/matomo.php")).willReturn(status(204))); + wireMockServer.stubFor(get(urlPathEqualTo("/matomo.php")).willReturn(status(204))); } @Test @@ -98,13 +103,14 @@ void usesDefaultSiteId() { } private void givenTrackerConfigurationWithDefaultSiteId() { - trackerConfigurationBuilder.apiEndpoint(URI.create("http://localhost:8099/matomo.php")).defaultSiteId(SITE_ID); + trackerConfigurationBuilder.apiEndpoint(URI.create(String.format( + "http://localhost:%s/matomo.php", wireMockServer.port()))).defaultSiteId(SITE_ID); } private void thenGetsRequest(String expectedQuery) { assertThat(future).isNotCompletedExceptionally(); - verify( - getRequestedFor(urlEqualTo(String.format("/matomo.php?%s", expectedQuery))).withHeader("Accept", equalTo("*/*")) + wireMockServer.verify( + getRequestedFor(urlEqualTo(String.format("/matomo.php?%s", expectedQuery))) .withHeader("User-Agent", equalTo("MatomoJavaClient"))); } @@ -214,8 +220,10 @@ private void whenSendsBulkRequestAsync() { private void thenPostsRequestWithoutAuthToken(String expectedQuery, String contentLength) { assertThat(future).isNotCompletedExceptionally(); - verify(postRequestedFor(urlEqualTo("/matomo.php")).withHeader("Content-Length", equalTo(contentLength)) - .withHeader("Accept", equalTo("*/*")).withHeader("Content-Type", equalTo("application/json")) + wireMockServer.verify(postRequestedFor(urlEqualTo("/matomo.php")) + .withHeader("Content-Length", equalTo(contentLength)) + .withHeader("Accept", equalTo("*/*")) + .withHeader("Content-Type", equalTo("application/json")) .withHeader("User-Agent", equalTo("MatomoJavaClient")) .withRequestBody( equalToJson("{\"requests\":[\"?" + expectedQuery + "\"]}"))); @@ -244,8 +252,7 @@ void getContainsHeaders() { whenSendsRequestAsync(); assertThat(future).isNotCompletedExceptionally(); - verify(getRequestedFor(urlPathEqualTo("/matomo.php")) - .withHeader("Accept", equalTo("*/*")) + wireMockServer.verify(getRequestedFor(urlPathEqualTo("/matomo.php")) .withHeader("User-Agent", equalTo("MatomoJavaClient"))); } @@ -258,7 +265,7 @@ void postContainsHeaders() { whenSendsBulkRequestAsync(); assertThat(future).isNotCompletedExceptionally(); - verify(postRequestedFor(urlPathEqualTo("/matomo.php")) + wireMockServer.verify(postRequestedFor(urlPathEqualTo("/matomo.php")) .withHeader("Accept", equalTo("*/*")) .withHeader("Content-Length", equalTo("90")) .withHeader("Content-Type", equalTo("application/json")) @@ -275,7 +282,7 @@ void allowsToOverrideUserAgent() { whenSendsRequestAsync(); assertThat(future).isNotCompletedExceptionally(); - verify(getRequestedFor(urlPathEqualTo("/matomo.php")) + wireMockServer.verify(getRequestedFor(urlPathEqualTo("/matomo.php")) .withHeader("User-Agent", equalTo("Mozilla/5.0"))); } @@ -302,7 +309,7 @@ void tracksMinimalRequest() { whenSendsBulkRequestAsync(); assertThat(future).isNotCompletedExceptionally(); - verify(postRequestedFor(urlEqualTo("/matomo.php")).withHeader("Content-Length", equalTo("711")) + wireMockServer.verify(postRequestedFor(urlEqualTo("/matomo.php")).withHeader("Content-Length", equalTo("711")) .withHeader("Accept", equalTo("*/*")).withHeader("Content-Type", equalTo("application/json")) .withHeader("User-Agent", equalTo("MatomoJavaClient")) .withRequestBody( @@ -314,14 +321,14 @@ void tracksMinimalRequest() { @Test void doesNothingIfNotEnabled() { - resetAllRequests(); + wireMockServer.resetRequests(); givenTrackerConfigurationWithDefaultSiteId(); trackerConfigurationBuilder.enabled(false); whenSendsRequestAsync(); assertThat(future).isNotCompletedExceptionally(); - verify(0, postRequestedFor(urlPathEqualTo("/matomo.php"))); + wireMockServer.verify(0, postRequestedFor(urlPathEqualTo("/matomo.php"))); } @@ -353,8 +360,9 @@ void exampleWorks() { @Test void reportsErrors() { - stubFor(get(urlPathEqualTo("/failing")).willReturn(status(500))); - trackerConfigurationBuilder.apiEndpoint(URI.create("http://localhost:8099/failing")).defaultSiteId(SITE_ID); + wireMockServer.stubFor(get(urlPathEqualTo("/failing")).willReturn(status(500))); + trackerConfigurationBuilder.apiEndpoint(URI.create(String.format("http://localhost:%d/failing", + wireMockServer.port()))).defaultSiteId(SITE_ID); assertThatThrownBy(this::whenSendsRequestAsync).hasRootCauseInstanceOf(MatomoException.class) .hasRootCauseMessage("Tracking endpoint responded with code 500"); @@ -372,7 +380,7 @@ void includesDefaultTokenAuth() { whenSendsRequestAsync(); assertThat(future).isNotCompletedExceptionally(); - verify( + wireMockServer.verify( getRequestedFor( urlEqualTo( "/matomo.php?idsite=42token_auth=fdf6e8461ea9de33176b222519627f78&rec=1&apiv=1&_id=00bbccddeeff1122&send_image=0&rand=someRandom")) @@ -393,7 +401,7 @@ void includesMultipleQueriesInBulkRequest() throws Exception { future1.get(); assertThat(future1).isNotCompletedExceptionally(); - verify(postRequestedFor(urlEqualTo("/matomo.php")).withHeader("Content-Length", equalTo("297")) + wireMockServer.verify(postRequestedFor(urlEqualTo("/matomo.php")).withHeader("Content-Length", equalTo("297")) .withHeader("Accept", equalTo("*/*")).withHeader("Content-Type", equalTo("application/json")) .withHeader("User-Agent", equalTo("MatomoJavaClient")).withRequestBody(equalToJson( "{\"requests\" : [ \"?idsite=42&rec=1&action_name=First&apiv=1&_id=00bbccddeeff1122&send_image=0&rand=someRandom\", \"?idsite=42&rec=1&action_name=Second&apiv=1&_id=00bbccddeeff1122&send_image=0&rand=someRandom\", \"?idsite=42&rec=1&action_name=Third&apiv=1&_id=00bbccddeeff1122&send_image=0&rand=someRandom\" ]}"))); diff --git a/src/test/java/org/matomo/java/tracking/PiwikTrackerIT.java b/src/test/java/org/matomo/java/tracking/PiwikTrackerIT.java index 55cd4834..8ced188d 100644 --- a/src/test/java/org/matomo/java/tracking/PiwikTrackerIT.java +++ b/src/test/java/org/matomo/java/tracking/PiwikTrackerIT.java @@ -1,48 +1,55 @@ package org.matomo.java.tracking; -import com.github.tomakehurst.wiremock.junit5.WireMockTest; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.matomo.java.tracking.parameters.RandomValue; -import org.matomo.java.tracking.parameters.VisitorId; -import org.piwik.java.tracking.PiwikRequest; -import org.piwik.java.tracking.PiwikTracker; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.CompletableFuture; - import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson; import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; -import static com.github.tomakehurst.wiremock.client.WireMock.resetAllRequests; import static com.github.tomakehurst.wiremock.client.WireMock.status; -import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; -import static com.github.tomakehurst.wiremock.client.WireMock.verify; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -@WireMockTest(httpPort = 8099) +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.matomo.java.tracking.parameters.RandomValue; +import org.matomo.java.tracking.parameters.VisitorId; +import org.piwik.java.tracking.PiwikRequest; +import org.piwik.java.tracking.PiwikTracker; + class PiwikTrackerIT { + private static final WireMockServer wireMockServer = new WireMockServer( + WireMockConfiguration.options().dynamicPort()); + + private static final int SITE_ID = 42; - private final PiwikTracker piwikTracker = new PiwikTracker("http://localhost:8099/matomo.php"); + private PiwikTracker piwikTracker; private PiwikRequest request; + @BeforeAll + static void beforeAll() { + wireMockServer.start(); + } + @BeforeEach void setUp() throws MalformedURLException { - resetAllRequests(); - stubFor(post(urlPathEqualTo("/matomo.php")).willReturn(status(204))); - stubFor(get(urlPathEqualTo("/matomo.php")).willReturn(status(204))); + piwikTracker = new PiwikTracker(String.format("http://localhost:%d/matomo.php", wireMockServer.port())); + wireMockServer.resetRequests(); + wireMockServer.stubFor(post(urlPathEqualTo("/matomo.php")).willReturn(status(204))); + wireMockServer.stubFor(get(urlPathEqualTo("/matomo.php")).willReturn(status(204))); request = new PiwikRequest(SITE_ID, new URL("https://test.local/test/path?id=123")); request.setRandomValue(RandomValue.fromString("rand")); request.setVisitorId(VisitorId.fromHash(999999999999999999L)); @@ -57,9 +64,8 @@ void testSendRequest() { piwikTracker.sendRequest(request); - verify(getRequestedFor(urlEqualTo( + wireMockServer.verify(getRequestedFor(urlEqualTo( "/matomo.php?rec=1&idsite=42&url=https%3A%2F%2Ftest.local%2Ftest%2Fpath%3Fid%3D123&apiv=1&_id=0de0b6b3a763ffff&send_image=0&rand=rand¶meterName=parameterValue")) - .withHeader("Accept", equalTo("*/*")) .withHeader("User-Agent", equalTo("MatomoJavaClient"))) ; } @@ -75,9 +81,8 @@ void testSendRequestAsync() throws Exception { future.get(); assertThat(future).isNotCompletedExceptionally(); - verify(getRequestedFor(urlEqualTo( + wireMockServer.verify(getRequestedFor(urlEqualTo( "/matomo.php?rec=1&idsite=42&url=https%3A%2F%2Ftest.local%2Ftest%2Fpath%3Fid%3D123&apiv=1&_id=0de0b6b3a763ffff&send_image=0&rand=rand¶meterName=parameterValue")) - .withHeader("Accept", equalTo("*/*")) .withHeader("User-Agent", equalTo("MatomoJavaClient"))); } @@ -92,7 +97,7 @@ void testSendBulkRequest_Iterable() { piwikTracker.sendBulkRequest(requests); - verify(postRequestedFor(urlEqualTo("/matomo.php")) + wireMockServer.verify(postRequestedFor(urlEqualTo("/matomo.php")) .withHeader("Content-Length", equalTo("167")) .withHeader("Accept", equalTo("*/*")) .withHeader("Content-Type", equalTo("application/json")) @@ -122,7 +127,7 @@ void testSendBulkRequest_Iterable_StringFF() { piwikTracker.sendBulkRequest(requests, null); - verify(postRequestedFor(urlEqualTo("/matomo.php")) + wireMockServer.verify(postRequestedFor(urlEqualTo("/matomo.php")) .withHeader("Content-Length", equalTo("167")) .withHeader("Accept", equalTo("*/*")) .withHeader("Content-Type", equalTo("application/json")) @@ -139,7 +144,7 @@ void testSendBulkRequest_Iterable_StringFT() { piwikTracker.sendBulkRequest(requests, "12345678901234567890123456789012"); - verify(postRequestedFor(urlEqualTo("/matomo.php")) + wireMockServer.verify(postRequestedFor(urlEqualTo("/matomo.php")) .withHeader("Content-Length", equalTo("215")) .withHeader("Accept", equalTo("*/*")) .withHeader("Content-Type", equalTo("application/json")) @@ -162,7 +167,7 @@ void testSendBulkRequestAsync_Iterable() throws Exception { assertThat(future).isNotCompletedExceptionally(); - verify(postRequestedFor(urlEqualTo("/matomo.php")) + wireMockServer.verify(postRequestedFor(urlEqualTo("/matomo.php")) .withHeader("Content-Length", equalTo("167")) .withHeader("Accept", equalTo("*/*")) .withHeader("Content-Type", equalTo("application/json")) @@ -199,7 +204,7 @@ void testSendBulkRequestAsync_Iterable_String() throws Exception { assertThat(future).isNotCompletedExceptionally(); - verify(postRequestedFor(urlEqualTo("/matomo.php")) + wireMockServer.verify(postRequestedFor(urlEqualTo("/matomo.php")) .withHeader("Content-Length", equalTo("215")) .withHeader("Accept", equalTo("*/*")) .withHeader("Content-Type", equalTo("application/json"))