Skip to content

Test using Testcontainers #35

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

Draft
wants to merge 5 commits into
base: eap-7
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<version.wildfly.maven.plugin>1.0.2.Final</version.wildfly.maven.plugin>
<!-- Define the version of the JBoss BOMs we want to import to specify
tested stacks. -->
<version.jboss.bom.eap>7.0.1.GA</version.jboss.bom.eap>
<version.jboss.bom.eap>7.1.5.GA</version.jboss.bom.eap>
<!-- other plugin versions -->
<version.surefire.plugin>2.19.1</version.surefire.plugin>
<version.war.plugin>2.1.1</version.war.plugin>
Expand Down Expand Up @@ -94,6 +94,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>1.19.8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -134,6 +141,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -157,6 +165,23 @@
<artifactId>resteasy-multipart-provider</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!-- Maven will append the version to the finalName (which is the name
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/openshift/service/DemoResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void killSwitch(@Context SecurityContext context) throws IOException {
@GET
@Path("healthcheck/")
@Produces({"application/json"})
public Response checkHealth(@Context SecurityContext context) throws IOException {
public Response checkHealth() throws IOException {

String response = new String("{\"response\":\"Health Status: " + health + "\", \"health\": " + health + "}");

Expand Down
48 changes: 24 additions & 24 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,34 @@
</servlet-mapping>

<!-- Use HTTP Basic for authentication -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>RealmUsersRoles</realm-name>
</login-config>
<!-- <login-config>-->
<!-- <auth-method>BASIC</auth-method>-->
<!-- <realm-name>RealmUsersRoles</realm-name>-->
<!-- </login-config>-->

<context-param>
<param-name>resteasy.role.based.security</param-name>
<param-value>true</param-value>
</context-param>
<!-- <context-param>-->
<!-- <param-name>resteasy.role.based.security</param-name>-->
<!-- <param-value>true</param-value>-->
<!-- </context-param>-->

<security-constraint>
<web-resource-collection>
<web-resource-name>Tasks</web-resource-name>
<url-pattern>/ws/tasks/*</url-pattern>
</web-resource-collection>
<web-resource-collection>
<web-resource-name>Users</web-resource-name>
<url-pattern>/ws/users/*</url-pattern>
</web-resource-collection>
<!-- <security-constraint>-->
<!-- <web-resource-collection>-->
<!-- <web-resource-name>Tasks</web-resource-name>-->
<!-- <url-pattern>/ws/tasks/*</url-pattern>-->
<!-- </web-resource-collection>-->
<!-- <web-resource-collection>-->
<!-- <web-resource-name>Users</web-resource-name>-->
<!-- <url-pattern>/ws/users/*</url-pattern>-->
<!-- </web-resource-collection>-->

<auth-constraint>
<role-name>guest</role-name>
</auth-constraint>
</security-constraint>
<!-- <auth-constraint>-->
<!-- <role-name>guest</role-name>-->
<!-- </auth-constraint>-->
<!-- </security-constraint>-->

<security-role>
<role-name>guest</role-name>
</security-role>
<!-- <security-role>-->
<!-- <role-name>guest</role-name>-->
<!-- </security-role>-->

<distributable/>
</web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public DefaultDeployment withPersistence() {
return this;
}

public DefaultDeployment withOriginalPersistence() {
webArchive = webArchive.addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource(new File(WEBAPP_SRC, "WEB-INF/tasks-rs-ds.xml"))
.addAsWebInfResource(new File(WEBAPP_SRC, "WEB-INF/web.xml"));
return this;
}

public DefaultDeployment withImportedData() {
webArchive = webArchive.addAsResource("import.sql");
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.jboss.as.quickstarts.tasksrs.service;

import com.openshift.service.DemoResource;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import org.jboss.as.quickstarts.tasksrs.DefaultDeployment;
import org.jboss.as.quickstarts.tasksrs.model.Resources;
import org.jboss.as.quickstarts.tasksrs.model.Task;
import org.jboss.as.quickstarts.tasksrs.model.TaskDao;
import org.jboss.as.quickstarts.tasksrs.model.TaskDaoImpl;
import org.jboss.as.quickstarts.tasksrs.model.User;
import org.jboss.as.quickstarts.tasksrs.model.UserDao;
import org.jboss.as.quickstarts.tasksrs.model.UserDaoImpl;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.ClassRule;
import org.junit.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.images.builder.Transferable;

import java.io.ByteArrayOutputStream;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;

public class UserResourceIntegrationTest {

public static byte[] deployment() throws IllegalArgumentException {
WebArchive webArchive = new DefaultDeployment().withOriginalPersistence().withImportedData().getArchive()
.addClasses(Resources.class, User.class, UserDao.class, Task.class, TaskDao.class, TaskDaoImpl.class, UserDaoImpl.class, TaskResource.class, UserResource.class, DemoResource.class);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
webArchive.as(ZipExporter.class).exportTo(outputStream);
return outputStream.toByteArray();
}

@ClassRule
public static final GenericContainer<?> wildfly
= new GenericContainer<>("jboss/wildfly:21.0.2.Final")
.withExposedPorts(8080, 9990)
.withCopyToContainer(
Transferable.of(deployment()),
"/opt/jboss/wildfly/standalone/deployments/test.war");

@Test
public void test() {
RequestSpecification requestSpecification = RestAssured.given()
.baseUri("http://" + wildfly.getHost() + ":" + wildfly.getMappedPort(8080) + "/test");
requestSpecification
.get("/ws/demo/healthcheck")
.andReturn()
.then()
.statusCode(200)
.body("health", equalTo(1))
.body("response", equalTo("Health Status: 1"));

requestSpecification
.accept(ContentType.JSON)
.get("/ws/users")
.andReturn()
.then()
.statusCode(200)
.body(".", hasSize(2));
}

}
17 changes: 17 additions & 0 deletions src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.testcontainers" level="INFO"/>
<!-- The following logger can be used for containers logs since 1.18.0 -->
<logger name="tc" level="INFO"/>
<logger name="com.github.dockerjava" level="WARN"/>
<logger name="com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire" level="OFF"/>
</configuration>