Skip to content

Commit

Permalink
Add JUnit5 Extension, just for fun
Browse files Browse the repository at this point in the history
  • Loading branch information
kifj committed Feb 25, 2024
1 parent 126347c commit 49806fa
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 53 deletions.
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
</systemPropertyVariables>
<groups>Unittests</groups>
<excludedGroups>Arquillian,Testcontainers</excludedGroups>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -742,8 +746,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<groups>Testcontainers</groups>
<excludedGroups>Arquillian,Unittests</excludedGroups>
</configuration>
</plugin>
</plugins>
Expand Down Expand Up @@ -782,7 +786,8 @@
<jboss.home>${jboss.home}</jboss.home>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
</systemPropertyVariables>
<excludedgroups>Testcontainers</excludedgroups>
<groups>Arquillian</groups>
<excludedGroups>Testcontainers,Unittests</excludedGroups>
</configuration>
</plugin>
</plugins>
Expand Down Expand Up @@ -824,7 +829,8 @@
<jboss.password>12345</jboss.password>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
</systemPropertyVariables>
<excludedgroups>Testcontainers</excludedgroups>
<groups>Arquillian</groups>
<excludedgroups>Testcontainers,Unittests</excludedgroups>
</configuration>
</plugin>
</plugins>
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/x1/stomp/control/BasicAuthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ private void checkDelegate(ClientRequestContext requestContext) {
}

private Class<?> getDeclaringClass(ClientRequestContext requestContext) {
if (requestContext instanceof ClientRequestContextImpl == false) {
throw new IllegalStateException(
"Failed to get ClientInvocation from request context. Is RestEasy client used underneath?");
if (requestContext instanceof ClientRequestContextImpl clientRequestContext) {
return clientRequestContext.getInvocation().getClientInvoker().getDeclaring();
}
var invocation = ((ClientRequestContextImpl) requestContext).getInvocation();
return invocation.getClientInvoker().getDeclaring();
throw new IllegalStateException(
"Failed to get ClientInvocation from request context. Is RestEasy client used underneath?");
}
}
4 changes: 1 addition & 3 deletions src/main/java/x1/stomp/model/JaxbSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof JaxbLink)) {
if (!(o instanceof JaxbLink jaxbLink)) {
return false;
}

JaxbLink jaxbLink = (JaxbLink) o;

if (uri != null ? !uri.equals(jaxbLink.uri) : jaxbLink.uri != null) {
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/x1/service/test/ResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -34,6 +35,7 @@

@ExtendWith(ArquillianExtension.class)
@DisplayName("Resolver Test")
@Tag("Arquillian")
public class ResolverTest {
private static final String STAGE = "local";
private String hostname;
Expand All @@ -44,7 +46,8 @@ public class ResolverTest {
@Deployment
public static Archive<?> createTestArchive() {
var libraries = Maven.resolver().loadPomFromFile("pom.xml")
.resolve("x1.wildfly:service-registry", "org.assertj:assertj-core").withTransitivity().asFile();
.resolve("x1.wildfly:service-registry", "org.assertj:assertj-core", "org.hamcrest:hamcrest-core")
.withTransitivity().asFile();

return ShrinkWrap.create(WebArchive.class, VersionData.APP_NAME_MAJOR_MINOR + ".war").addPackages(true, "x1.stomp")
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/x1/stomp/test/AbstractIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.extension.ExtendWith;

import x1.stomp.boundary.JacksonConfig;
import x1.stomp.version.VersionData;

@ExtendWith(ArquillianExtension.class)
@Tag("Arquillian")
public abstract class AbstractIT {

protected Client client;
Expand All @@ -30,7 +32,7 @@ public abstract class AbstractIT {
@Deployment
public static Archive<?> createTestArchive() {
var libraries = Maven.resolver().loadPomFromFile("pom.xml")
.resolve("org.assertj:assertj-core").withTransitivity().asFile();
.resolve("org.assertj:assertj-core", "org.hamcrest:hamcrest-core").withTransitivity().asFile();

return ShrinkWrap.create(WebArchive.class, VersionData.APP_NAME_MAJOR_MINOR + ".war").addPackages(true, "x1.stomp")
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
Expand All @@ -49,11 +51,11 @@ public void tearDown() {
client.close();
}

protected Integer getPortOffset() {
public Integer getPortOffset() {
return Integer.valueOf(System.getProperty("jboss.socket.binding.port-offset", "0"));
}

protected String getHost() {
public String getHost() {
return System.getProperty("jboss.bind.address", "127.0.0.1");
}

Expand Down
37 changes: 19 additions & 18 deletions src/test/java/x1/stomp/test/ContainerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
import static jakarta.ws.rs.core.Response.Status.*;
import static x1.stomp.test.ResponseAssert.assertThat;

import java.net.URI;

import static x1.stomp.test.ErrorResponseAssert.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.TestInstance;
import org.slf4j.Logger;
Expand All @@ -26,41 +30,37 @@
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.UriBuilder;
import x1.stomp.boundary.ErrorResponse;
import x1.stomp.boundary.JacksonConfig;
import x1.stomp.model.Share;

@Testcontainers
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Tag("Testcontainers")
@DisplayName("Testcontainer")
public class ContainerTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ContainerTest.class);
private static final String PATH_SHARES = "shares";
private static final String PATH_PARAM_KEY = "{key}";
private static final String PARAM_KEY = "key";
private static final String TEST_SHARE = "AAPL";

private static Network network = Network.builder().build();
private static Network network = Network.newNetwork();

@Container
private static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15-alpine")
.withNetwork(network)
.withNetworkAliases("postgres")
.withDatabaseName("stocks")
.withInitScript("init.sql");
.withNetwork(network).withNetworkAliases("postgres").withDatabaseName("stocks").withInitScript("init.sql");

@SuppressWarnings("rawtypes")
@Container
private static GenericContainer<?> wildfly = new GenericContainer(DockerImageName.parse("registry.x1/j7beck/x1-wildfly-stomp-test:1.8"))
.dependsOn(postgres).withNetwork(network)
.withEnv("DB_SERVER", "postgres")
.withEnv("DB_PORT", "5432")
.withEnv("DB_USER", postgres.getUsername())
.withEnv("DB_PASSWORD", postgres.getPassword())
.withExposedPorts(8080)
.waitingFor(Wait.forHttp("/").forStatusCode(200));

private String baseUrl;
private static GenericContainer<?> wildfly = new GenericContainer<>(
DockerImageName.parse("registry.x1/j7beck/x1-wildfly-stomp-test:1.8")).dependsOn(postgres).withNetwork(network)
.withEnv("DB_SERVER", "postgres").withEnv("DB_PORT", "5432").withEnv("DB_USER", postgres.getUsername())
.withEnv("DB_PASSWORD", postgres.getPassword()).withExposedPorts(8080)
.waitingFor(Wait.forHttp("/").forStatusCode(Status.OK.getStatusCode()));

private URI baseUrl;
private Client client;

@BeforeAll
Expand All @@ -70,8 +70,9 @@ static void enableLogging() {

@BeforeEach
public void setup() {
client = ClientBuilder.newClient().register(JacksonConfig.class);
baseUrl = "http://" + wildfly.getHost() + ":" + wildfly.getFirstMappedPort() + "/rest";
client = ClientBuilder.newClient().register(JacksonConfig.class);
baseUrl = UriBuilder.fromUri("http://" + wildfly.getHost() + ":" + wildfly.getFirstMappedPort()).path("rest")
.build();
}

@AfterEach
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/x1/stomp/test/EntitiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
Expand All @@ -23,6 +24,7 @@
@Testcontainers
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Tag("Testcontainers")
@DisplayName("Entities")
public class EntitiesTest {
@Container
private static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15-alpine");
Expand Down Expand Up @@ -58,13 +60,16 @@ void teardownEntityManagerFactory() {
@Test
void insertShare() {
var tx = em.getTransaction();

tx.begin();
var s1 = new Share("TEST1");
em.persist(s1);
tx.commit();

tx.begin();
var count = em.createNamedQuery(Share.COUNT_ALL, Long.class).getSingleResult();
assertEquals(1, count);

var s2 = em.find(Share.class, s1.getId());
assertNotNull(s2);
assertEquals(s1.getKey(), s2.getKey());
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/x1/stomp/test/JsonHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;

Expand All @@ -29,6 +30,7 @@
import static x1.stomp.model.Action.UNSUBSCRIBE;

@DisplayName("test JSON mapping")
@Tag("Unittests")
public class JsonHelperTest {
private final JsonHelper jsonHelper = new JsonHelper();

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/x1/stomp/test/SchemaExportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import x1.stomp.model.Share;

@Tag("Unittests")
public class SchemaExportTest {
@Test
public void testSchemaExport() {
Expand Down
31 changes: 14 additions & 17 deletions src/test/java/x1/stomp/test/ShareSubscriptionWebSocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import jakarta.ejb.EJB;
import jakarta.inject.Inject;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;

import x1.stomp.control.QuoteUpdater;
Expand All @@ -22,11 +22,10 @@
import static org.assertj.core.api.Assertions.assertThat;

@DisplayName("ShareSubscription WebSocket Test")
public class ShareSubscriptionWebSocketTest extends AbstractIT {
@ExtendWith(WebsocketExtension.class)
public class ShareSubscriptionWebSocketTest extends AbstractIT implements WebSocketTest {
private static final String TEST_SHARE = "MSFT";

private String baseUrl;

@Inject
private Logger log;

Expand All @@ -38,23 +37,22 @@ public class ShareSubscriptionWebSocketTest extends AbstractIT {

@Inject
private WebSocketClient webSocketClient;

@BeforeEach
public void setup() {
super.setup();
var host = getHost();
var port = 8080 + getPortOffset();
baseUrl = "ws://" + host + ":" + port + "/" + VersionData.APP_NAME_MAJOR_MINOR + "/ws/stocks";
log.debug("baseUrl={}", baseUrl);

@Override
public WebSocketClient getWebSocketClient() {
return webSocketClient;
}

@Override
public String getPath() {
return "/" + VersionData.APP_NAME_MAJOR_MINOR + "/ws/stocks";
}

@Test
public void testWebSocket() throws Exception {
webSocketClient.openConnection(baseUrl);
Thread.sleep(500);
var command = new Command(SUBSCRIBE, TEST_SHARE);
var message = jsonHelper.toJSON(command);
log.debug("Sending {} to {}", command, baseUrl);
log.debug("Sending {} to {}", command);
webSocketClient.sendMessage(message);
Thread.sleep(2500);

Expand All @@ -75,7 +73,7 @@ public void testWebSocket() throws Exception {

command.setAction(UNSUBSCRIBE);
message = jsonHelper.toJSON(command);
log.debug("Sending {} to {}", command, baseUrl);
log.debug("Sending {} to {}", command);
webSocketClient.sendMessage(message);

Thread.sleep(2500);
Expand All @@ -84,7 +82,6 @@ public void testWebSocket() throws Exception {
var event = jsonHelper.fromJSON(response, SubscriptionEvent.class);
assertThat(event.getKey()).isEqualTo(TEST_SHARE);
assertThat(event.getAction()).isEqualTo(UNSUBSCRIBE);
webSocketClient.closeConnection();
}

}
11 changes: 11 additions & 0 deletions src/test/java/x1/stomp/test/WebSocketTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package x1.stomp.test;

public interface WebSocketTest {
Integer getPortOffset();

String getHost();

WebSocketClient getWebSocketClient();

String getPath();
}
Loading

0 comments on commit 49806fa

Please sign in to comment.