diff --git a/pom.xml b/pom.xml index 9a760772..52462e37 100644 --- a/pom.xml +++ b/pom.xml @@ -653,7 +653,11 @@ org.apache.maven.plugins maven-surefire-plugin - true + + org.jboss.logmanager.LogManager + + Unittests + Arquillian,Testcontainers @@ -742,8 +746,8 @@ org.apache.maven.plugins maven-surefire-plugin - false Testcontainers + Arquillian,Unittests @@ -782,7 +786,8 @@ ${jboss.home} org.jboss.logmanager.LogManager - Testcontainers + Arquillian + Testcontainers,Unittests @@ -824,7 +829,8 @@ 12345 org.jboss.logmanager.LogManager - Testcontainers + Arquillian + Testcontainers,Unittests diff --git a/src/main/java/x1/stomp/control/BasicAuthFilter.java b/src/main/java/x1/stomp/control/BasicAuthFilter.java index 1c1e03da..5a61ea2f 100644 --- a/src/main/java/x1/stomp/control/BasicAuthFilter.java +++ b/src/main/java/x1/stomp/control/BasicAuthFilter.java @@ -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?"); } } diff --git a/src/main/java/x1/stomp/model/JaxbSupport.java b/src/main/java/x1/stomp/model/JaxbSupport.java index 25516c83..2c9aeace 100644 --- a/src/main/java/x1/stomp/model/JaxbSupport.java +++ b/src/main/java/x1/stomp/model/JaxbSupport.java @@ -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; } diff --git a/src/test/java/x1/service/test/ResolverTest.java b/src/test/java/x1/service/test/ResolverTest.java index 14c8cd5c..0bea551c 100644 --- a/src/test/java/x1/service/test/ResolverTest.java +++ b/src/test/java/x1/service/test/ResolverTest.java @@ -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; @@ -34,6 +35,7 @@ @ExtendWith(ArquillianExtension.class) @DisplayName("Resolver Test") +@Tag("Arquillian") public class ResolverTest { private static final String STAGE = "local"; private String hostname; @@ -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") diff --git a/src/test/java/x1/stomp/test/AbstractIT.java b/src/test/java/x1/stomp/test/AbstractIT.java index cd46780e..78842a84 100644 --- a/src/test/java/x1/stomp/test/AbstractIT.java +++ b/src/test/java/x1/stomp/test/AbstractIT.java @@ -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; @@ -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") @@ -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"); } diff --git a/src/test/java/x1/stomp/test/ContainerTest.java b/src/test/java/x1/stomp/test/ContainerTest.java index 2c9d5eea..2b385cf4 100644 --- a/src/test/java/x1/stomp/test/ContainerTest.java +++ b/src/test/java/x1/stomp/test/ContainerTest.java @@ -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; @@ -26,6 +30,8 @@ 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; @@ -33,6 +39,7 @@ @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"; @@ -40,27 +47,20 @@ public class ContainerTest { 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 @@ -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 diff --git a/src/test/java/x1/stomp/test/EntitiesTest.java b/src/test/java/x1/stomp/test/EntitiesTest.java index 350f22c4..b0b086cc 100644 --- a/src/test/java/x1/stomp/test/EntitiesTest.java +++ b/src/test/java/x1/stomp/test/EntitiesTest.java @@ -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; @@ -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"); @@ -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()); diff --git a/src/test/java/x1/stomp/test/JsonHelperTest.java b/src/test/java/x1/stomp/test/JsonHelperTest.java index a573a859..83894b98 100644 --- a/src/test/java/x1/stomp/test/JsonHelperTest.java +++ b/src/test/java/x1/stomp/test/JsonHelperTest.java @@ -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; @@ -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(); diff --git a/src/test/java/x1/stomp/test/SchemaExportTest.java b/src/test/java/x1/stomp/test/SchemaExportTest.java index ae37e600..3ae72c7e 100644 --- a/src/test/java/x1/stomp/test/SchemaExportTest.java +++ b/src/test/java/x1/stomp/test/SchemaExportTest.java @@ -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() { diff --git a/src/test/java/x1/stomp/test/ShareSubscriptionWebSocketTest.java b/src/test/java/x1/stomp/test/ShareSubscriptionWebSocketTest.java index bd96fcc5..31c4d544 100644 --- a/src/test/java/x1/stomp/test/ShareSubscriptionWebSocketTest.java +++ b/src/test/java/x1/stomp/test/ShareSubscriptionWebSocketTest.java @@ -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; @@ -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; @@ -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); @@ -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); @@ -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(); } } diff --git a/src/test/java/x1/stomp/test/WebSocketTest.java b/src/test/java/x1/stomp/test/WebSocketTest.java new file mode 100644 index 00000000..341cdbdb --- /dev/null +++ b/src/test/java/x1/stomp/test/WebSocketTest.java @@ -0,0 +1,11 @@ +package x1.stomp.test; + +public interface WebSocketTest { + Integer getPortOffset(); + + String getHost(); + + WebSocketClient getWebSocketClient(); + + String getPath(); +} diff --git a/src/test/java/x1/stomp/test/WebsocketExtension.java b/src/test/java/x1/stomp/test/WebsocketExtension.java new file mode 100644 index 00000000..1a1b1a9c --- /dev/null +++ b/src/test/java/x1/stomp/test/WebsocketExtension.java @@ -0,0 +1,49 @@ +package x1.stomp.test; + +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.TestInstancePostProcessor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class WebsocketExtension implements TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback { + private static final Logger LOG = LoggerFactory.getLogger(WebsocketExtension.class); + private String baseUrl; + + @Override + public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception { + if (testInstance instanceof WebSocketTest webSocketTest) { + var host = webSocketTest.getHost(); + var port = 8080 + webSocketTest.getPortOffset(); + var path = webSocketTest.getPath(); + baseUrl = "ws://" + host + ":" + port + path; + } else { + LOG.warn("WebsocketExtension should be used with WebSocketTest: {}", testInstance); + } + } + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + var testInstance = context.getRequiredTestInstance(); + if (testInstance instanceof WebSocketTest webSocketTest) { + LOG.info("openConnection to baseUrl={}", baseUrl); + if (webSocketTest.getWebSocketClient() != null) { + webSocketTest.getWebSocketClient().openConnection(baseUrl); + Thread.sleep(500); + } + } + + } + + @Override + public void afterEach(ExtensionContext context) throws Exception { + var testInstance = context.getRequiredTestInstance(); + if (testInstance instanceof WebSocketTest webSocketTest) { + if (webSocketTest.getWebSocketClient() != null) { + webSocketTest.getWebSocketClient().closeConnection(); + } + } + } + +} diff --git a/src/test/resources/simplelogger.properties b/src/test/resources/simplelogger.properties index f618aa56..cec060a7 100644 --- a/src/test/resources/simplelogger.properties +++ b/src/test/resources/simplelogger.properties @@ -1,2 +1,3 @@ org.slf4j.simpleLogger.defaultLogLevel=info -org.slf4j.simpleLogger.log.x1=debug \ No newline at end of file +org.slf4j.simpleLogger.log.x1=debug +org.slf4j.simpleLogger.log.org.hibernate=warn \ No newline at end of file