Skip to content

Commit

Permalink
Downgrade WireMock to support JDK 8 (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
dheid committed Nov 2, 2023
1 parent a6e5e58 commit a744122
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 77 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,11 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>3.2.0</version>
<version>2.27.2</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
96 changes: 52 additions & 44 deletions src/test/java/org/matomo/java/tracking/MatomoTrackerIT.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand All @@ -52,11 +52,16 @@ class MatomoTrackerIT {

private CompletableFuture<Void> 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
Expand Down Expand Up @@ -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")));
}

Expand Down Expand Up @@ -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 + "\"]}")));
Expand Down Expand Up @@ -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")));

}
Expand All @@ -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"))
Expand All @@ -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")));

}
Expand All @@ -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(
Expand All @@ -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")));

}

Expand Down Expand Up @@ -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");
Expand All @@ -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"))
Expand All @@ -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\" ]}")));
Expand Down
67 changes: 36 additions & 31 deletions src/test/java/org/matomo/java/tracking/PiwikTrackerIT.java
Original file line number Diff line number Diff line change
@@ -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));
Expand All @@ -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&parameterName=parameterValue"))
.withHeader("Accept", equalTo("*/*"))
.withHeader("User-Agent", equalTo("MatomoJavaClient")))
;
}
Expand All @@ -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&parameterName=parameterValue"))
.withHeader("Accept", equalTo("*/*"))
.withHeader("User-Agent", equalTo("MatomoJavaClient")));
}

Expand All @@ -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"))
Expand Down Expand Up @@ -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"))
Expand All @@ -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"))
Expand All @@ -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"))
Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit a744122

Please sign in to comment.