Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RESTEASY-3414] Migrate the resteasy-reactor module to JUnit 5 #3881

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions resteasy-reactor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package org.jboss.resteasy.reactor;

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 java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import reactor.core.publisher.Mono;

Expand All @@ -21,27 +17,27 @@ public void testFromCompletionStage() {
final CompletableFuture<Integer> cs = new CompletableFuture<>();
cs.complete(1);
final Mono<?> mono = provider.fromCompletionStage(cs);
assertEquals(1, mono.block());
Assertions.assertEquals(1, mono.block());
}

@Test
public void testFromCompletionStageNotDeferred() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final Mono<?> mono = provider.fromCompletionStage(someAsyncMethod(latch));

assertTrue(latch.await(1, TimeUnit.SECONDS));
assertEquals("Hello!", mono.block());
assertEquals(0, latch.getCount());
Assertions.assertTrue(latch.await(1, TimeUnit.SECONDS));
Assertions.assertEquals("Hello!", mono.block());
Assertions.assertEquals(0, latch.getCount());
}

@Test
public void testFromCompletionStageDeferred() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final Mono<?> mono = provider.fromCompletionStage(() -> someAsyncMethod(latch));

assertFalse(latch.await(1, TimeUnit.SECONDS));
assertEquals("Hello!", mono.block());
assertEquals(0, latch.getCount());
Assertions.assertFalse(latch.await(1, TimeUnit.SECONDS));
Assertions.assertEquals("Hello!", mono.block());
Assertions.assertEquals(0, latch.getCount());
}

private CompletableFuture<String> someAsyncMethod(final CountDownLatch latch) {
Expand All @@ -52,7 +48,7 @@ private CompletableFuture<String> someAsyncMethod(final CountDownLatch latch) {
@Test
public void testToCompletionStageCase() throws Exception {
final Object actual = provider.toCompletionStage(Mono.just(1)).toCompletableFuture().get();
assertEquals(1, actual);
Assertions.assertEquals(1, actual);
}

@Test
Expand All @@ -62,6 +58,6 @@ public void testToCompletionStageNullCase() throws Exception {
cs.complete(null);
final Mono<?> mono = Mono.fromCompletionStage(cs);
final Object actual = provider.toCompletionStage(mono).toCompletableFuture().get();
assertNull(actual);
Assertions.assertNull(actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.jboss.resteasy.test.TestPortProvider.generateURL;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.time.Duration;
import java.util.ArrayList;
Expand Down Expand Up @@ -42,11 +37,12 @@
import org.jboss.resteasy.client.jaxrs.internal.ClientInvocation;
import org.jboss.resteasy.plugins.server.netty.NettyJaxrsServer;
import org.jboss.resteasy.test.TestPortProvider;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.DefaultEventExecutor;
Expand All @@ -66,7 +62,7 @@ public class ReactorTest {
private static AtomicReference<Object> value = new AtomicReference<Object>();
private static final Logger LOG = Logger.getLogger(NettyJaxrsServer.class);

@BeforeClass
@BeforeAll
public static void beforeClass() throws Exception {
server = new NettyJaxrsServer();
server.setPort(TestPortProvider.getPort());
Expand All @@ -78,15 +74,15 @@ public static void beforeClass() throws Exception {
server.start();
}

@AfterClass
@AfterAll
public static void afterClass() throws Exception {
server.stop();
server = null;
}

private ResteasyClient client;

@Before
@BeforeEach
public void before() {
final ReactorNettyClientHttpEngine reactorEngine = new ReactorNettyClientHttpEngine(
HttpClient.create(),
Expand All @@ -102,25 +98,25 @@ public void before() {
latch = new CountDownLatch(1);
}

@After
@AfterEach
public void after() {
client.close();
}

@Test
public void testMono() throws Exception {
assertEquals(0, ReactorResource.monoEndpointCounter.get());
Assertions.assertEquals(0, ReactorResource.monoEndpointCounter.get());
Mono<Response> mono = client.target(generateURL("/mono")).request().rx(MonoRxInvoker.class).get();
Thread.sleep(1_000);
// Make HTTP call does not happen until a subscription happens.
assertEquals(0, ReactorResource.monoEndpointCounter.get());
Assertions.assertEquals(0, ReactorResource.monoEndpointCounter.get());
mono.subscribe((Response r) -> {
value.set(r.readEntity(String.class));
latch.countDown();
});
latch.await();
assertEquals(1, ReactorResource.monoEndpointCounter.get());
assertEquals("got it", value.get());
Assertions.assertEquals(1, ReactorResource.monoEndpointCounter.get());
Assertions.assertEquals("got it", value.get());
}

@Test
Expand All @@ -134,7 +130,7 @@ public void testFlux() throws Exception {
(Throwable t) -> LOG.error(t.getMessage(), t),
() -> latch.countDown());
latch.await();
assertArrayEquals(new String[] { "one", "two" }, data.toArray());
Assertions.assertArrayEquals(new String[] { "one", "two" }, data.toArray());
}

@Test
Expand Down Expand Up @@ -261,7 +257,7 @@ public void close() {
.timeout(Duration.ofMillis(500))
.subscribe(
ignore -> {
fail("Should have got timeout exception");
Assertions.fail("Should have got timeout exception");
},
t -> {
if (!(t instanceof TimeoutException)) {
Expand All @@ -271,10 +267,11 @@ public void close() {
},
latch::countDown);

assertNull("Inner timeout should not have occurred!", innerTimeoutException.get());
assertTrue("Test timed out", latch.await(innerTimeout.multipliedBy(2).toMillis(), TimeUnit.MILLISECONDS));
assertTrue("Server disconnect didn't happen.", serverConnDisconnectingEvent.await(
serverResponseDelay.dividedBy(2).toMillis(), TimeUnit.MILLISECONDS));
Assertions.assertNull(innerTimeoutException.get(), "Inner timeout should not have occurred!");
Assertions.assertTrue(latch.await(innerTimeout.multipliedBy(2).toMillis(), TimeUnit.MILLISECONDS),
"Test timed out");
Assertions.assertTrue(serverConnDisconnectingEvent.await(
serverResponseDelay.dividedBy(2).toMillis(), TimeUnit.MILLISECONDS), "Server disconnect didn't happen.");
} finally {
server.disposeNow();
}
Expand All @@ -283,10 +280,10 @@ public void close() {
@Test
public void testInjection() {
Integer data = client.target(generateURL("/injection")).request().get(Integer.class);
assertEquals((Integer) 24, data);
Assertions.assertEquals((Integer) 24, data);

data = client.target(generateURL("/injection-async")).request().get(Integer.class);
assertEquals((Integer) 42, data);
Assertions.assertEquals((Integer) 42, data);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jboss.resteasy.reactor.proxyframework;

import static org.jboss.resteasy.reactor.proxyframework.CustomResource.THE_CUSTOM_RESOURCE;
import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;
Expand All @@ -15,11 +14,12 @@
import org.jboss.resteasy.client.jaxrs.engines.ReactorNettyClientHttpEngine;
import org.jboss.resteasy.plugins.server.netty.NettyJaxrsServer;
import org.jboss.resteasy.test.TestPortProvider;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.DefaultEventExecutor;
Expand All @@ -41,7 +41,7 @@ public interface RemoteCustomResource {

private static NettyJaxrsServer server;

@BeforeClass
@BeforeAll
public static void beforeClass() {
port = TestPortProvider.getPort();
server = new NettyJaxrsServer();
Expand All @@ -54,15 +54,15 @@ public static void beforeClass() {
server.start();
}

@AfterClass
@AfterAll
public static void afterClass() {
server.stop();
server = null;
}

private ResteasyClient client;

@Before
@BeforeEach
public void before() {
final ReactorNettyClientHttpEngine reactorEngine = new ReactorNettyClientHttpEngine(
HttpClient.create(),
Expand All @@ -76,15 +76,15 @@ public void before() {
.build();
}

@After
@AfterEach
public void after() {
client.close();
}

@Test
public void givenRemoteServiceInterfaceAndWorkingRemoteServiceWhenProxyThenGenerateProxyWithMono() {
final var proxy = client.target("http://localhost:" + port).proxy(RemoteCustomResource.class);
assertEquals(THE_CUSTOM_RESOURCE, proxy.getCustomResource().block());
Assertions.assertEquals(THE_CUSTOM_RESOURCE, proxy.getCustomResource().block());

}
}