From c488c14d25d4eab61e074b825dfba956c5e2557c Mon Sep 17 00:00:00 2001 From: Chris Eager Date: Mon, 20 Dec 2021 17:41:48 -0800 Subject: [PATCH] Migrate gcm-sender-async tests to JUnit 5 --- gcm-sender-async/pom.xml | 1 - .../gcm/server/MessageTest.java | 13 ++-- .../whispersystems/gcm/server/SenderTest.java | 65 ++++++++++--------- .../gcm/server/SimultaneousSenderTest.java | 45 +++++++------ 4 files changed, 61 insertions(+), 63 deletions(-) diff --git a/gcm-sender-async/pom.xml b/gcm-sender-async/pom.xml index 779eabe36..dcf32b649 100644 --- a/gcm-sender-async/pom.xml +++ b/gcm-sender-async/pom.xml @@ -49,4 +49,3 @@ - diff --git a/gcm-sender-async/src/test/java/org/whispersystems/gcm/server/MessageTest.java b/gcm-sender-async/src/test/java/org/whispersystems/gcm/server/MessageTest.java index 740e0a9ee..e62e000a9 100644 --- a/gcm-sender-async/src/test/java/org/whispersystems/gcm/server/MessageTest.java +++ b/gcm-sender-async/src/test/java/org/whispersystems/gcm/server/MessageTest.java @@ -4,17 +4,16 @@ */ package org.whispersystems.gcm.server; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.whispersystems.gcm.server.util.JsonHelpers.jsonFixture; import java.io.IOException; - -import static org.junit.Assert.assertEquals; -import static org.whispersystems.gcm.server.util.JsonHelpers.jsonFixture; +import org.junit.jupiter.api.Test; public class MessageTest { @Test - public void testMinimal() throws IOException { + void testMinimal() throws IOException { Message message = Message.newBuilder() .withDestination("1") .build(); @@ -23,7 +22,7 @@ public class MessageTest { } @Test - public void testComplete() throws IOException { + void testComplete() throws IOException { Message message = Message.newBuilder() .withDestination("1") .withCollapseKey("collapse") @@ -36,7 +35,7 @@ public class MessageTest { } @Test - public void testWithData() throws IOException { + void testWithData() throws IOException { Message message = Message.newBuilder() .withDestination("2") .withDataPart("key1", "value1") diff --git a/gcm-sender-async/src/test/java/org/whispersystems/gcm/server/SenderTest.java b/gcm-sender-async/src/test/java/org/whispersystems/gcm/server/SenderTest.java index 179361ccc..0504c8b0e 100644 --- a/gcm-sender-async/src/test/java/org/whispersystems/gcm/server/SenderTest.java +++ b/gcm-sender-async/src/test/java/org/whispersystems/gcm/server/SenderTest.java @@ -12,12 +12,11 @@ import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.ok; import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; -import static com.github.tomakehurst.wiremock.client.WireMock.verify; -import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.whispersystems.gcm.server.util.FixtureHelpers.fixture; import static org.whispersystems.gcm.server.util.JsonHelpers.jsonFixture; @@ -26,19 +25,21 @@ import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.tomakehurst.wiremock.client.CountMatchingStrategy; -import com.github.tomakehurst.wiremock.junit.WireMockRule; +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; import java.io.IOException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; -public class SenderTest { +class SenderTest { - @Rule - public WireMockRule wireMock = new WireMockRule(options().dynamicPort().dynamicHttpsPort()); + @RegisterExtension + private final WireMockExtension wireMock = WireMockExtension.newInstance() + .options(wireMockConfig().dynamicPort().dynamicHttpsPort()) + .build(); private static final ObjectMapper mapper = new ObjectMapper(); @@ -49,14 +50,14 @@ public class SenderTest { } @Test - public void testSuccess() throws InterruptedException, ExecutionException, TimeoutException, IOException { + void testSuccess() throws InterruptedException, ExecutionException, TimeoutException, IOException { wireMock.stubFor(any(anyUrl()) .willReturn(aResponse() .withStatus(200) .withBody(fixture("fixtures/response-success.json")))); - Sender sender = new Sender("foobarbaz", mapper, 10, "http://localhost:" + wireMock.port() + "/gcm/send"); + Sender sender = new Sender("foobarbaz", mapper, 10, "http://localhost:" + wireMock.getPort() + "/gcm/send"); CompletableFuture future = sender.send(Message.newBuilder().withDestination("1").build()); Result result = future.get(10, TimeUnit.SECONDS); @@ -68,19 +69,19 @@ public class SenderTest { assertNull(result.getError()); assertNull(result.getCanonicalRegistrationId()); - verify(1, postRequestedFor(urlEqualTo("/gcm/send")) + wireMock.verify(1, postRequestedFor(urlEqualTo("/gcm/send")) .withHeader("Authorization", equalTo("key=foobarbaz")) .withHeader("Content-Type", equalTo("application/json")) .withRequestBody(equalTo(jsonFixture("fixtures/message-minimal.json")))); } @Test - public void testBadApiKey() throws InterruptedException, TimeoutException { + void testBadApiKey() throws InterruptedException, TimeoutException { wireMock.stubFor(any(anyUrl()) .willReturn(aResponse() .withStatus(401))); - Sender sender = new Sender("foobar", mapper, 10, "http://localhost:" + wireMock.port() + "/gcm/send"); + Sender sender = new Sender("foobar", mapper, 10, "http://localhost:" + wireMock.getPort() + "/gcm/send"); CompletableFuture future = sender.send(Message.newBuilder().withDestination("1").build()); try { @@ -90,16 +91,16 @@ public class SenderTest { assertTrue(ee.getCause() instanceof AuthenticationFailedException); } - verify(1, anyRequestedFor(anyUrl())); + wireMock.verify(1, anyRequestedFor(anyUrl())); } @Test - public void testBadRequest() throws TimeoutException, InterruptedException { + void testBadRequest() throws TimeoutException, InterruptedException { wireMock.stubFor(any(anyUrl()) .willReturn(aResponse() .withStatus(400))); - Sender sender = new Sender("foobarbaz", mapper, 10, "http://localhost:" + wireMock.port() + "/gcm/send"); + Sender sender = new Sender("foobarbaz", mapper, 10, "http://localhost:" + wireMock.getPort() + "/gcm/send"); CompletableFuture future = sender.send(Message.newBuilder().withDestination("1").build()); try { @@ -109,16 +110,16 @@ public class SenderTest { assertTrue(e.getCause() instanceof InvalidRequestException); } - verify(1, anyRequestedFor(anyUrl())); + wireMock.verify(1, anyRequestedFor(anyUrl())); } @Test - public void testServerError() throws TimeoutException, InterruptedException { + void testServerError() throws TimeoutException, InterruptedException { wireMock.stubFor(any(anyUrl()) .willReturn(aResponse() .withStatus(503))); - Sender sender = new Sender("foobarbaz", mapper, 3, "http://localhost:" + wireMock.port() + "/gcm/send"); + Sender sender = new Sender("foobarbaz", mapper, 3, "http://localhost:" + wireMock.getPort() + "/gcm/send"); CompletableFuture future = sender.send(Message.newBuilder().withDestination("1").build()); try { @@ -128,15 +129,15 @@ public class SenderTest { assertTrue(ee.getCause() instanceof ServerFailedException); } - verify(3, anyRequestedFor(anyUrl())); + wireMock.verify(3, anyRequestedFor(anyUrl())); } @Test - public void testServerErrorRecovery() throws InterruptedException, ExecutionException, TimeoutException { + void testServerErrorRecovery() throws InterruptedException, ExecutionException, TimeoutException { wireMock.stubFor(any(anyUrl()).willReturn(aResponse().withStatus(503))); - Sender sender = new Sender("foobarbaz", mapper, 4, "http://localhost:" + wireMock.port() + "/gcm/send"); + Sender sender = new Sender("foobarbaz", mapper, 4, "http://localhost:" + wireMock.getPort() + "/gcm/send"); CompletableFuture future = sender.send(Message.newBuilder().withDestination("1").build()); // up to three failures can happen, with 100ms exponential backoff @@ -150,7 +151,7 @@ public class SenderTest { Result result = future.get(10, TimeUnit.SECONDS); - verify(new CountMatchingStrategy(CountMatchingStrategy.GREATER_THAN, 1), anyRequestedFor(anyUrl())); + wireMock.verify(new CountMatchingStrategy(CountMatchingStrategy.GREATER_THAN, 1), anyRequestedFor(anyUrl())); assertTrue(result.isSuccess()); assertFalse(result.isThrottled()); assertFalse(result.isUnregistered()); @@ -160,14 +161,14 @@ public class SenderTest { } @Test - public void testNetworkError() throws TimeoutException, InterruptedException { + void testNetworkError() throws TimeoutException, InterruptedException { wireMock.stubFor(any(anyUrl()) .willReturn(ok())); - Sender sender = new Sender("foobarbaz", mapper ,2, "http://localhost:" + wireMock.port() + "/gcm/send"); + Sender sender = new Sender("foobarbaz", mapper ,2, "http://localhost:" + wireMock.getPort() + "/gcm/send"); - wireMock.stop(); + wireMock.getRuntimeInfo().getWireMock().shutdown(); CompletableFuture future = sender.send(Message.newBuilder().withDestination("1").build()); @@ -179,12 +180,12 @@ public class SenderTest { } @Test - public void testNotRegistered() throws InterruptedException, ExecutionException, TimeoutException { + void testNotRegistered() throws InterruptedException, ExecutionException, TimeoutException { wireMock.stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200) .withBody(fixture("fixtures/response-not-registered.json")))); - Sender sender = new Sender("foobarbaz", mapper,2, "http://localhost:" + wireMock.port() + "/gcm/send"); + Sender sender = new Sender("foobarbaz", mapper,2, "http://localhost:" + wireMock.getPort() + "/gcm/send"); CompletableFuture future = sender.send(Message.newBuilder() .withDestination("2") .withDataPart("message", "new message!") diff --git a/gcm-sender-async/src/test/java/org/whispersystems/gcm/server/SimultaneousSenderTest.java b/gcm-sender-async/src/test/java/org/whispersystems/gcm/server/SimultaneousSenderTest.java index 496768156..a5fcf958e 100644 --- a/gcm-sender-async/src/test/java/org/whispersystems/gcm/server/SimultaneousSenderTest.java +++ b/gcm-sender-async/src/test/java/org/whispersystems/gcm/server/SimultaneousSenderTest.java @@ -6,32 +6,33 @@ package org.whispersystems.gcm.server; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.post; -import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; -import static junit.framework.TestCase.assertTrue; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.whispersystems.gcm.server.util.FixtureHelpers.fixture; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.tomakehurst.wiremock.core.WireMockConfiguration; -import com.github.tomakehurst.wiremock.junit.WireMockRule; +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; import java.util.LinkedList; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; -public class SimultaneousSenderTest { +class SimultaneousSenderTest { - @Rule - public WireMockRule wireMock = new WireMockRule(WireMockConfiguration.options().dynamicPort().dynamicHttpsPort()); + @RegisterExtension + private final WireMockExtension wireMock = WireMockExtension.newInstance() + .options(wireMockConfig().dynamicPort().dynamicHttpsPort()) + .build(); private static final ObjectMapper mapper = new ObjectMapper(); @@ -42,13 +43,13 @@ public class SimultaneousSenderTest { } @Test - public void testSimultaneousSuccess() throws TimeoutException, InterruptedException, ExecutionException, JsonProcessingException { - stubFor(post(urlPathEqualTo("/gcm/send")) + void testSimultaneousSuccess() throws TimeoutException, InterruptedException, ExecutionException { + wireMock.stubFor(post(urlPathEqualTo("/gcm/send")) .willReturn(aResponse() .withStatus(200) .withBody(fixture("fixtures/response-success.json")))); - Sender sender = new Sender("foobarbaz", mapper, 2, "http://localhost:" + wireMock.port() + "/gcm/send"); + Sender sender = new Sender("foobarbaz", mapper, 2, "http://localhost:" + wireMock.getPort() + "/gcm/send"); List> results = new LinkedList<>(); for (int i=0;i<1000;i++) { @@ -65,13 +66,13 @@ public class SimultaneousSenderTest { } @Test - @Ignore - public void testSimultaneousFailure() throws TimeoutException, InterruptedException { - stubFor(post(urlPathEqualTo("/gcm/send")) + @Disabled + void testSimultaneousFailure() { + wireMock.stubFor(post(urlPathEqualTo("/gcm/send")) .willReturn(aResponse() .withStatus(503))); - Sender sender = new Sender("foobarbaz", mapper, 2, "http://localhost:" + wireMock.port() + "/gcm/send"); + Sender sender = new Sender("foobarbaz", mapper, 2, "http://localhost:" + wireMock.getPort() + "/gcm/send"); List> futures = new LinkedList<>(); for (int i=0;i<1000;i++) { @@ -79,11 +80,9 @@ public class SimultaneousSenderTest { } for (CompletableFuture future : futures) { - try { - Result result = future.get(60, TimeUnit.SECONDS); - } catch (ExecutionException e) { - assertTrue(e.getCause().toString(), e.getCause() instanceof ServerFailedException); - } + final ExecutionException e = assertThrows(ExecutionException.class, () -> future.get(60, TimeUnit.SECONDS)); + + assertTrue(e.getCause() instanceof ServerFailedException, e.getCause().toString()); } } }