Skip to content

Commit

Permalink
Merge pull request #1179 from phac-nml/junit5-itest-refactor
Browse files Browse the repository at this point in the history
Junit5 itest refactor
  • Loading branch information
joshsadam authored Jan 28, 2022
2 parents e4a9686 + dc4974a commit b834d2b
Show file tree
Hide file tree
Showing 23 changed files with 220 additions and 154 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 8 additions & 6 deletions doc/developer/interface/testing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ Running specific UI tests through [IntelliJ] IDEA
*Recommended [IntelliJ] Default/Template JUnit VM Options Configuration:*

```
-ea
-Dspring.profiles.active=it
-Dspring.datasource.url=jdbc:mysql://localhost:3306/irida_integration_test
-Dirida.it.rootdirectory=/tmp/irida/
-Dirida.it.nosandbox=true
-Dirida.it.headless=false
-ea
-Dspring.profiles.active=it
-Dspring.datasource.url=jdbc:mysql://localhost:3306/irida_integration_test
-Dirida.it.rootdirectory=/tmp/irida/
-Dirida.db.profile=it
-Djunit.platform.execution.listeners.deactivate=ca.corefacility.bioinformatics.irida.junit5.listeners.Unit*
-Dirida.it.nosandbox=true
-Dirida.it.headless=true
-Dwebdriver.chrome.driver=/PATH/TO/chromedriver
```

Expand Down
11 changes: 5 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,8 @@
<argLine>${failsafeArgLine}</argLine>
<systemPropertyVariables>
<junit.platform.execution.listeners.deactivate>${exclude.test.listeners}</junit.platform.execution.listeners.deactivate>
<server.port>${server.port}</server.port>
<spring.profiles.active>test</spring.profiles.active>
<irida.db.profile>it</irida.db.profile>
<server.base.url>http://localhost:${server.port}</server.base.url>
</systemPropertyVariables>
<rerunFailingTestsCount>5</rerunFailingTestsCount>
</configuration>
Expand Down Expand Up @@ -877,7 +875,7 @@
<apiDocsUrl>http://localhost:${server.port}/v3/api-docs</apiDocsUrl>
<outputFileName>open-api.json</outputFileName>
<outputDir>doc/swagger-ui</outputDir>
<skip>false</skip>
<skip>${tomcat.skip}</skip>
</configuration>
</plugin>
</plugins>
Expand Down Expand Up @@ -956,28 +954,28 @@
<id>ui_testing</id>
<properties>
<include.tags><![CDATA[IntegrationTest & UI]]></include.tags>
<tomcat.skip>true</tomcat.skip>
<exclude.test.listeners>
ca.corefacility.bioinformatics.irida.junit5.listeners.UnitTestListener,
ca.corefacility.bioinformatics.irida.junit5.listeners.IntegrationTestListener,
</exclude.test.listeners>
</properties>
</profile>
<profile>
<id>service_testing</id>
<properties>
<include.tags><![CDATA[IntegrationTest & Service]]></include.tags>
<tomcat.skip>true</tomcat.skip>
<exclude.test.listeners>
ca.corefacility.bioinformatics.irida.junit5.listeners.UnitTestListener,
ca.corefacility.bioinformatics.irida.junit5.listeners.IntegrationTestListener,
ca.corefacility.bioinformatics.irida.junit5.listeners.IntegrationUITestListener,
</exclude.test.listeners>
<tomcat.skip>true</tomcat.skip>
</properties>
</profile>
<profile>
<id>rest_testing</id>
<properties>
<include.tags><![CDATA[IntegrationTest & Rest]]></include.tags>
<tomcat.skip>true</tomcat.skip>
<exclude.test.listeners>
ca.corefacility.bioinformatics.irida.junit5.listeners.UnitTestListener,
ca.corefacility.bioinformatics.irida.junit5.listeners.IntegrationUITestListener,
Expand Down Expand Up @@ -1010,6 +1008,7 @@
<id>all_testing</id>
<properties>
<include.tags>IntegrationTest</include.tags>
<tomcat.skip>true</tomcat.skip>
<exclude.test.listeners>
ca.corefacility.bioinformatics.irida.junit5.listeners.UnitTestListener
</exclude.test.listeners>
Expand Down
4 changes: 2 additions & 2 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ test_service() {
}

test_rest() {
mvn clean verify -Prest_testing -B -Dspring-boot.run.arguments="--spring.datasource.url=$JDBC_URL --sequence.file.base.directory=$SEQUENCE_FILE_DIR --reference.file.base.directory=$REFERENCE_FILE_DIR --output.file.base.directory=$OUTPUT_FILE_DIR --spring.datasource.dbcp2.max-wait=$DB_MAX_WAIT_MILLIS --file.processing.decompress=true" -Dspring.datasource.url=$JDBC_URL -Dfile.processing.decompress=true -Dirida.it.rootdirectory=$TMP_DIRECTORY -Dsequence.file.base.directory=$SEQUENCE_FILE_DIR -Dreference.file.base.directory=$REFERENCE_FILE_DIR -Doutput.file.base.directory=$OUTPUT_FILE_DIR -Dspring.datasource.dbcp2.max-wait=$DB_MAX_WAIT_MILLIS $@
mvn clean verify -B -Prest_testing -Dspring.datasource.url=$JDBC_URL -Dfile.processing.decompress=true -Dirida.it.rootdirectory=$TMP_DIRECTORY -Dsequence.file.base.directory=$SEQUENCE_FILE_DIR -Dreference.file.base.directory=$REFERENCE_FILE_DIR -Doutput.file.base.directory=$OUTPUT_FILE_DIR -Dspring.datasource.dbcp2.max-wait=$DB_MAX_WAIT_MILLIS $@
exit_code=$?
return $exit_code
}
Expand Down Expand Up @@ -146,7 +146,7 @@ test_ui() {
docker start $SELENIUM_DOCKER_NAME || docker run -d -p 4444:4444 --name $SELENIUM_DOCKER_NAME -v $PWD:$PWD -v $TMP_DIRECTORY/irida:$TMP_DIRECTORY/irida -v /dev/shm:/dev/shm selenium/standalone-chrome:$SELENIUM_DOCKER_TAG
SELENIUM_OPTS="-Dwebdriver.selenium_url=$SELENIUM_URL -Dserver.port=33333 -Dserver.base.url=http://$HOSTNAME:33333 -Djava.io.tmpdir=$TMP_DIRECTORY/irida"
fi
mvn clean verify -B -Pui_testing $SELENIUM_OPTS -Dirida.it.nosandbox=true -Dirida.it.headless=$HEADLESS -Dspring-boot.run.arguments="--spring.datasource.url=$JDBC_URL --java.io.tmpdir=$TMP_DIRECTORY/irida --sequence.file.base.directory=$SEQUENCE_FILE_DIR --reference.file.base.directory=$REFERENCE_FILE_DIR --output.file.base.directory=$OUTPUT_FILE_DIR --spring.datasource.dbcp2.max-wait=$DB_MAX_WAIT_MILLIS --file.processing.decompress=true" -Dspring.datasource.url=$JDBC_URL -Dfile.processing.decompress=true -Dirida.it.rootdirectory=$TMP_DIRECTORY -Dsequence.file.base.directory=$SEQUENCE_FILE_DIR -Dreference.file.base.directory=$REFERENCE_FILE_DIR -Doutput.file.base.directory=$OUTPUT_FILE_DIR -Dspring.datasource.dbcp2.max-wait=$DB_MAX_WAIT_MILLIS $@
mvn clean verify -B -Pui_testing $SELENIUM_OPTS -Dirida.it.nosandbox=true -Dirida.it.headless=$HEADLESS -Dspring.datasource.url=$JDBC_URL -Dfile.processing.decompress=true -Dirida.it.rootdirectory=$TMP_DIRECTORY -Dsequence.file.base.directory=$SEQUENCE_FILE_DIR -Dreference.file.base.directory=$REFERENCE_FILE_DIR -Doutput.file.base.directory=$OUTPUT_FILE_DIR -Dspring.datasource.dbcp2.max-wait=$DB_MAX_WAIT_MILLIS $@
exit_code=$?
if [[ "$DO_KILL_DOCKER" = true && "$SELENIUM_DOCKER" = true ]]; then docker rm -f -v $SELENIUM_DOCKER_NAME; fi
return $exit_code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
ca.corefacility.bioinformatics.irida.junit5.listeners.UnitTestListener
ca.corefacility.bioinformatics.irida.junit5.listeners.IntegrationTestListener
ca.corefacility.bioinformatics.irida.junit5.listeners.IntegrationUITestListener
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ca.corefacility.bioinformatics.irida.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;

import ca.corefacility.bioinformatics.irida.ria.integration.pages.AbstractPage;
import ca.corefacility.bioinformatics.irida.ria.integration.utilities.RemoteApiUtilities;
import ca.corefacility.bioinformatics.irida.ria.web.oauth.OltuAuthorizationController;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;

/**
* Configuration to be loaded in IntegrationTest via {@Link Import} annotation.
* This waits for the servlet container initialized event and uses
* {@Link LocalHostUriTemplateHandler} to retrieve the current URL to the servlet
* container and set it up within the specific testing utilities.
*
* Also provides a {@Link LocalHostUriTemplateHandler} {@Link Bean} which can be used
* in tests to access the servlet containers root uri.
*
*/
@TestConfiguration
@Profile("it")
public class IridaIntegrationTestUriConfig {

private LocalHostUriTemplateHandler uriTemplateHandler;

@Autowired
private OltuAuthorizationController oltuAuthorizationController;

@EventListener
public void onServletContainerInitialized(WebServerInitializedEvent event) {
uriTemplateHandler = new LocalHostUriTemplateHandler(event.getApplicationContext().getEnvironment());
String baseUrl = uriTemplateHandler.getRootUri() + "/";
AbstractPage.setBaseUrl(baseUrl);
RemoteApiUtilities.setBaseUrl(baseUrl);
oltuAuthorizationController.setServerBase(baseUrl);

// Setup RestAssured
RestAssured.requestSpecification = new RequestSpecBuilder()
.setContentType(ContentType.JSON)
.setAccept(ContentType.JSON)
.setBaseUri(uriTemplateHandler.getRootUri())
.build();
}

@Lazy
@Bean
public LocalHostUriTemplateHandler uriTemplateHandler() {
return uriTemplateHandler;
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

import ca.corefacility.bioinformatics.irida.config.IridaIntegrationTestUriConfig;
import ca.corefacility.bioinformatics.irida.junit5.listeners.IntegrationUITestListener;
import ca.corefacility.bioinformatics.irida.ria.integration.pages.AbstractPage;
import ca.corefacility.bioinformatics.irida.ria.integration.pages.LoginPage;
Expand All @@ -41,7 +44,8 @@
*/
@Tag("IntegrationTest") @Tag("UI")
@ActiveProfiles("it")
@SpringBootTest
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Import(IridaIntegrationTestUriConfig.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class })
@DatabaseTearDown("classpath:/ca/corefacility/bioinformatics/irida/test/integration/TableReset.xml")
@DbUnitConfiguration(dataSetLoader = NullReplacementDatasetLoader.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
public class AbstractPage {
private static final Logger logger = LoggerFactory.getLogger(AbstractPage.class);
protected static final String BASE_URL = System.getProperty("server.base.url", "http://localhost:" + System.getProperty("server.port", "8080")) + "/";
protected static String BASE_URL;
protected static final Long TIME_OUT_IN_SECONDS = 10L;

protected final int DEFAULT_WAIT = 500;
Expand All @@ -47,6 +47,10 @@ public String getErrors() {
return errors.getText();
}

public static void setBaseUrl(String baseUrl) {
BASE_URL = baseUrl;
}

protected static void get(WebDriver driver, String relativeUrl) {
String url = BASE_URL + relativeUrl;
driver.get(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RemoteApiUtilities {
protected static final String BASE_URL =
System.getProperty("server.base.url", "http://localhost:" + System.getProperty("server.port", "8080")) + "/";
protected static String BASE_URL;

public static void setBaseUrl(String baseUrl) {
BASE_URL = baseUrl;
}

public static void addRemoteApi(WebDriver driver, String clientId, String clientSecret) {
CreateRemoteAPIPage page = new CreateRemoteAPIPage(driver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.context.annotation.Import;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestExecutionListeners;
Expand All @@ -30,14 +32,16 @@
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.DatabaseTearDown;

import ca.corefacility.bioinformatics.irida.config.IridaIntegrationTestUriConfig;
import ca.corefacility.bioinformatics.irida.model.enums.AnalysisState;
import ca.corefacility.bioinformatics.irida.web.controller.api.RESTAnalysisSubmissionController;
import ca.corefacility.bioinformatics.irida.web.spring.view.NewickFileView;

@Tag("IntegrationTest") @Tag("Rest")
@SpringBootTest
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class })
@ActiveProfiles("it")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Import(IridaIntegrationTestUriConfig.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class })
@DatabaseSetup("/ca/corefacility/bioinformatics/irida/web/controller/test/integration/analysis/RESTAnalysisSubmissionControllerIT.xml")
@DatabaseTearDown("classpath:/ca/corefacility/bioinformatics/irida/test/integration/TableReset.xml")
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,44 @@

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

import ca.corefacility.bioinformatics.irida.web.controller.test.integration.util.ITestSystemProperties;
import ca.corefacility.bioinformatics.irida.config.IridaIntegrationTestUriConfig;

import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.DatabaseTearDown;
import com.google.common.net.HttpHeaders;
import com.google.common.net.MediaType;

import io.restassured.response.Response;

/**
* Integration tests for projects.
*
*/
@Tag("IntegrationTest") @Tag("Rest")
@SpringBootTest
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class })
@ActiveProfiles("it")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Import(IridaIntegrationTestUriConfig.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class })
@DatabaseSetup("/ca/corefacility/bioinformatics/irida/web/controller/test/integration/project/ProjectIntegrationTest.xml")
@DatabaseTearDown("classpath:/ca/corefacility/bioinformatics/irida/test/integration/TableReset.xml")
public class ProjectIT {

private static final String PROJECTS = "/api/projects";

@Autowired
private LocalHostUriTemplateHandler uriTemplateHandler;

/**
* If I try to issue a create request for an object with an invalid field
* name, the server should respond with 400.
Expand Down Expand Up @@ -75,7 +84,7 @@ public void testCreateProject() {
.post(PROJECTS);
String location = r.getHeader(HttpHeaders.LOCATION);
assertNotNull(location, "Project location must not be null");
assertTrue(location.startsWith(ITestSystemProperties.BASE_URL + "/api/projects/"));
assertTrue(location.startsWith(uriTemplateHandler.getRootUri() + "/api/projects/"));
String responseBody = asUser().get(location).asString();
assertTrue(r.asString().equals(responseBody), "Result of POST must equal result of GET");
String projectUsersLocation = from(responseBody).get("resource.links.find{it.rel=='project/users'}.href");
Expand Down Expand Up @@ -117,9 +126,9 @@ public void testGetProjects() {

@Test
public void testDeleteProject() {
String projectUri = ITestSystemProperties.BASE_URL + "/api/projects/5";
String projectUri = "/api/projects/5";
asUser().expect().body("resource.links.rel", hasItems("collection")).and()
.body("resource.links.href", hasItems(ITestSystemProperties.BASE_URL + "/api/projects")).when().delete(projectUri);
.body("resource.links.href", hasItems(uriTemplateHandler.getRootUri() + "/api/projects")).when().delete(projectUri);
}

/**
Expand All @@ -130,7 +139,7 @@ public void testDeleteProject() {
*/
@Test
public void verifyExistenceOfProjectWithHEAD() {
String projectUri = ITestSystemProperties.BASE_URL + "/api/projects/5";
String projectUri = "/api/projects/5";
asUser().expect().statusCode(HttpStatus.OK.value()).when().head(projectUri);
asUser().given().header("Accept", MediaType.JSON_UTF_8.toString()).expect().statusCode(HttpStatus.OK.value())
.when().head(projectUri);
Expand Down
Loading

0 comments on commit b834d2b

Please sign in to comment.