From 3690abf2f196b1ec114d011b6ba89723f7cc166a Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 16 Feb 2021 20:58:31 +0200 Subject: [PATCH] Polish RESTEasy Reactive SSE tests Related to: https://github.com/quarkusio/quarkus/pull/15119#issuecomment-780035543 --- .../deployment/pom.xml | 5 ++++ .../deployment/test/sse/SseParserTest.java | 28 +++++++++--------- .../deployment/test/sse/SseTestCase.java | 29 +++++++++---------- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/pom.xml b/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/pom.xml index 4724ce111824e..d44e5222aec18 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/pom.xml +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/pom.xml @@ -35,6 +35,11 @@ rest-assured test + + org.assertj + assertj-core + test + io.quarkus quarkus-hibernate-validator-deployment diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/src/test/java/io/quarkus/resteasy/reactive/jsonb/deployment/test/sse/SseParserTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/src/test/java/io/quarkus/resteasy/reactive/jsonb/deployment/test/sse/SseParserTest.java index 012b33ef28d9b..6dd57db0cbbaf 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/src/test/java/io/quarkus/resteasy/reactive/jsonb/deployment/test/sse/SseParserTest.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/src/test/java/io/quarkus/resteasy/reactive/jsonb/deployment/test/sse/SseParserTest.java @@ -59,13 +59,13 @@ public void testParser() { testParser("data:DATA\nretry:ASD\n\n", "DATA", null, null, null, SseEvent.RECONNECT_NOT_SET); // two events - testParser(Arrays.asList("data:foo\n\ndata:bar\n\n"), + testParser(Collections.singletonList("data:foo\n\ndata:bar\n\n"), Arrays.asList(new InboundSseEventImpl(null, null) .setData("foo"), new InboundSseEventImpl(null, null) .setData("bar"))); // two events with data - testParser(Arrays.asList("data:DATA\nid:ID\n:COMMENT\nretry:23\nevent:NAME\n\n" + testParser(Collections.singletonList("data:DATA\nid:ID\n:COMMENT\nretry:23\nevent:NAME\n\n" + "data:DATA2\nid:ID2\n:COMMENT2\nretry:232\nevent:NAME2\n\n"), Arrays.asList(new InboundSseEventImpl(null, null) .setData("DATA") @@ -80,7 +80,7 @@ public void testParser() { .setReconnectDelay(232) .setName("NAME2"))); // two events with data, only ID is persistent - testParser(Arrays.asList("data:DATA\nid:ID\n:COMMENT\nretry:23\nevent:NAME\n\n" + testParser(Collections.singletonList("data:DATA\nid:ID\n:COMMENT\nretry:23\nevent:NAME\n\n" + "data:DATA2\n\n"), Arrays.asList(new InboundSseEventImpl(null, null) .setData("DATA") @@ -106,19 +106,20 @@ public void testParser() { .setData("bar"))); // one event in two buffers testParser(Arrays.asList("data:f", "oo\n\n"), - Arrays.asList(new InboundSseEventImpl(null, null) + Collections.singletonList(new InboundSseEventImpl(null, null) .setData("foo"))); // one event in two buffers within a utf-8 char testParserWithBytes( Arrays.asList(new byte[] { 'd', 'a', 't', 'a', ':', (byte) 0b11000010 }, new byte[] { (byte) 0b10100010, '\n', '\n' }), - Arrays.asList(new InboundSseEventImpl(null, null) + Collections.singletonList(new InboundSseEventImpl(null, null) .setData("ยข"))); // BOM testParserWithBytes( - Arrays.asList(new byte[] { (byte) 0xFE, (byte) 0xFF, 'd', 'a', 't', 'a', ':', 'f', 'o', 'o', '\n', '\n' }), - Arrays.asList(new InboundSseEventImpl(null, null) + Collections.singletonList( + new byte[] { (byte) 0xFE, (byte) 0xFF, 'd', 'a', 't', 'a', ':', 'f', 'o', 'o', '\n', '\n' }), + Collections.singletonList(new InboundSseEventImpl(null, null) .setData("foo"))); // invalid BOM location @@ -128,25 +129,26 @@ public void testParser() { Arrays.asList(new byte[] { 'd', 'a', 't', 'a', ':', 'f', 'o', 'o', '\n', '\n' }, new byte[] { (byte) 0xFE, (byte) 0xFF, 'd', 'a', 't', 'a', ':', 'f', 'o', 'o', '\n', '\n' }), - Arrays.asList(new InboundSseEventImpl(null, null) + Collections.singletonList(new InboundSseEventImpl(null, null) .setData("foo")))); // invalid UTF-8 Assertions.assertThrows(IllegalStateException.class, () -> testParserWithBytes( - Arrays.asList(new byte[] { 'd', 'a', 't', 'a', ':', (byte) 0b1111_1000, 'f', 'o', 'o', '\n', '\n' }), - Arrays.asList())); + Collections.singletonList( + new byte[] { 'd', 'a', 't', 'a', ':', (byte) 0b1111_1000, 'f', 'o', 'o', '\n', '\n' }), + Collections.emptyList())); } private void testParser(String event, String data, String comment, String lastId, String name, long reconnectDelay) { if (data != null) { - testParser(Arrays.asList(event), Arrays.asList(new InboundSseEventImpl(null, null) + testParser(Collections.singletonList(event), Collections.singletonList(new InboundSseEventImpl(null, null) .setData(data) .setComment(comment) .setId(lastId) .setName(name) .setReconnectDelay(reconnectDelay))); } else { - testParser(Arrays.asList(event), Collections.emptyList()); + testParser(Collections.singletonList(event), Collections.emptyList()); } } @@ -159,7 +161,7 @@ private void testParserWithBytes(List events, List expe SseEventSourceImpl eventSource = new SseEventSourceImpl(null, 500, TimeUnit.MILLISECONDS); SseParser parser = eventSource.getSseParser(); CountDownLatch latch = new CountDownLatch(expectedEvents.size()); - List receivedEvents = new ArrayList<>(expectedEvents.size()); + List receivedEvents = Collections.synchronizedList(new ArrayList<>(expectedEvents.size())); eventSource.register(evt -> { latch.countDown(); receivedEvents.add(evt); diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/src/test/java/io/quarkus/resteasy/reactive/jsonb/deployment/test/sse/SseTestCase.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/src/test/java/io/quarkus/resteasy/reactive/jsonb/deployment/test/sse/SseTestCase.java index a937ba1bc99be..cc4abb6b1ed4a 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/src/test/java/io/quarkus/resteasy/reactive/jsonb/deployment/test/sse/SseTestCase.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/src/test/java/io/quarkus/resteasy/reactive/jsonb/deployment/test/sse/SseTestCase.java @@ -1,9 +1,11 @@ package io.quarkus.resteasy.reactive.jsonb.deployment.test.sse; +import static org.assertj.core.api.Assertions.assertThat; + import java.net.URI; import java.time.Duration; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @@ -18,7 +20,6 @@ import org.jboss.resteasy.reactive.client.impl.MultiInvoker; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -53,7 +54,7 @@ private void testSse(String path) throws Exception { try (SseEventSource eventSource = SseEventSource.target(target).reconnectingEvery(Integer.MAX_VALUE, TimeUnit.SECONDS) .build()) { CompletableFuture> res = new CompletableFuture<>(); - List collect = new ArrayList<>(); + List collect = Collections.synchronizedList(new ArrayList<>()); eventSource.register(new Consumer() { @Override public void accept(InboundSseEvent inboundSseEvent) { @@ -68,17 +69,17 @@ public void accept(Throwable throwable) { res.complete(collect); }); eventSource.open(); - Assertions.assertEquals(Arrays.asList("hello", "stef"), res.get(5, TimeUnit.SECONDS)); + assertThat(res.get(5, TimeUnit.SECONDS)).containsExactly("hello", "stef"); } } @Test - public void testMultiFromSse() throws Exception { + public void testMultiFromSse() { testMulti("sse"); } @Test - public void testMultiFromMulti() throws Exception { + public void testMultiFromMulti() { testMulti("sse/multi"); } @@ -86,21 +87,19 @@ private void testMulti(String path) { Client client = ClientBuilder.newBuilder().build(); WebTarget target = client.target(uri.toString() + path); Multi multi = target.request().rx(MultiInvoker.class).get(String.class); - List list = multi.collectItems().asList().await().atMost(Duration.ofSeconds(30)); - Assertions.assertEquals(2, list.size()); - Assertions.assertEquals("hello", list.get(0)); - Assertions.assertEquals("stef", list.get(1)); + List list = multi.collect().asList().await().atMost(Duration.ofSeconds(30)); + assertThat(list).containsExactly("hello", "stef"); } @Test - public void testJsonMultiFromSse() throws Exception { + public void testJsonMultiFromSse() { testJsonMulti("sse/json"); testJsonMulti("sse/json2"); testJsonMulti("sse/blocking/json"); } @Test - public void testJsonMultiFromMulti() throws Exception { + public void testJsonMultiFromMulti() { testJsonMulti("sse/json/multi"); } @@ -108,9 +107,7 @@ private void testJsonMulti(String path) { Client client = ClientBuilder.newBuilder().build(); WebTarget target = client.target(uri.toString() + path); Multi multi = target.request().rx(MultiInvoker.class).get(Message.class); - List list = multi.collectItems().asList().await().atMost(Duration.ofSeconds(30)); - Assertions.assertEquals(2, list.size()); - Assertions.assertEquals("hello", list.get(0).name); - Assertions.assertEquals("stef", list.get(1).name); + List list = multi.collect().asList().await().atMost(Duration.ofSeconds(30)); + assertThat(list).extracting("name").containsExactly("hello", "stef"); } }