From c7ca3fa027de94c6038d3d8b93e8be74c4a2be0d Mon Sep 17 00:00:00 2001 From: pvannierop Date: Tue, 9 Jan 2024 17:36:31 +0100 Subject: [PATCH] test refactor --- .../security/AbstractContainerTest.java | 19 +++---- .../security/OAuth2AuthIntegrationTest.java | 53 ++----------------- .../security/SamlAuthIntegrationTest.java | 53 ++----------------- 3 files changed, 19 insertions(+), 106 deletions(-) diff --git a/src/test/java/org/cbioportal/test/integration/security/AbstractContainerTest.java b/src/test/java/org/cbioportal/test/integration/security/AbstractContainerTest.java index e85703a5a55..c148365687c 100644 --- a/src/test/java/org/cbioportal/test/integration/security/AbstractContainerTest.java +++ b/src/test/java/org/cbioportal/test/integration/security/AbstractContainerTest.java @@ -5,6 +5,7 @@ import org.cbioportal.test.integration.OAuth2KeycloakInitializer; import org.cbioportal.test.integration.SamlKeycloakInitializer; import org.openqa.selenium.chrome.ChromeOptions; +import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationListener; @@ -68,8 +69,7 @@ public class AbstractContainerTest { sessionServiceContainer.setPortBindings(ImmutableList.of(String.format("%s:5000", SESSION_SERVICE_PORT))); mongoContainer = new GenericContainer(DockerImageName.parse(MONGO_IMAGE_VERSION)) - .withEnv("MONGO_INITDB_DATABASE", "session_service") - .withReuse(true); + .withEnv("MONGO_INITDB_DATABASE", "session_service"); mongoContainer.setPortBindings(ImmutableList.of(String.format("%s:27017", MONGO_PORT, MONGO_PORT))); keycloakContainer = new KeycloakContainer(KEYCLOAK_IMAGE_VERSION) @@ -77,8 +77,7 @@ public class AbstractContainerTest { .withAdminUsername("admin") .withAdminPassword("admin") .withEnv("KC_HOSTNAME", "host.testcontainers.internal") - .withEnv("KC_HOSTNAME_ADMIN", "localhost") - .withReuse(true); + .withEnv("KC_HOSTNAME_ADMIN", "localhost"); keycloakContainer.setPortBindings(ImmutableList.of(String.format("%s:8080", KEYCLOAK_PORT))); mockServerContainer = new GenericContainer(MOCKSERVER_IMAGE_VERSION) @@ -88,8 +87,7 @@ public class AbstractContainerTest { mysqlContainer = (MySQLContainer) new MySQLContainer(MYSQL_IMAGE_VERSION) .withClasspathResourceMapping("cgds.sql", "/docker-entrypoint-initdb.d/a_schema.sql", BindMode.READ_ONLY) .withClasspathResourceMapping("seed_mini.sql", "/docker-entrypoint-initdb.d/b_seed.sql", BindMode.READ_ONLY) - .withStartupTimeout(Duration.ofMinutes(10)) - .withReuse(true); + .withStartupTimeout(Duration.ofMinutes(10)); mysqlContainer.setPortBindings(ImmutableList.of(String.format("%s:3306", MYSQL_PORT))); ChromeOptions options = new ChromeOptions(); @@ -104,8 +102,7 @@ public class AbstractContainerTest { // activate this to record movies of the tests (greate for debugging) .withRecordingMode(RECORD_ALL, new File("/home/pnp300")) .withAccessToHost(true) - .withCapabilities(options) - .withReuse(true); + .withCapabilities(options); sessionServiceContainer.start(); mongoContainer.start(); @@ -149,9 +146,13 @@ public static class PortInitializer implements ApplicationContextInitializer { @Override public void initialize(ConfigurableApplicationContext applicationContext) { + TestPropertyValues values = TestPropertyValues.of( + "server.port=" + CBIO_PORT + ); + values.applyTo(applicationContext); applicationContext.addApplicationListener( (ApplicationListener) event -> { - Testcontainers.exposeHostPorts(CBIO_PORT, KEYCLOAK_PORT, SESSION_SERVICE_PORT); + Testcontainers.exposeHostPorts(CBIO_PORT, KEYCLOAK_PORT, MONGO_PORT); }); } } diff --git a/src/test/java/org/cbioportal/test/integration/security/OAuth2AuthIntegrationTest.java b/src/test/java/org/cbioportal/test/integration/security/OAuth2AuthIntegrationTest.java index f46d6859f6f..5cfe2ca673c 100644 --- a/src/test/java/org/cbioportal/test/integration/security/OAuth2AuthIntegrationTest.java +++ b/src/test/java/org/cbioportal/test/integration/security/OAuth2AuthIntegrationTest.java @@ -1,25 +1,18 @@ package org.cbioportal.test.integration.security; import org.cbioportal.PortalApplication; -import org.cbioportal.test.integration.MysqlInitializer; -import org.cbioportal.test.integration.OAuth2KeycloakInitializer; import org.cbioportal.test.integration.security.util.Util; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.web.context.WebServerInitializedEvent; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ApplicationListener; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; -import org.testcontainers.Testcontainers; -import static org.cbioportal.test.integration.security.OAuth2AuthIntegrationTest.MyKeycloakInitializer; +import static org.cbioportal.test.integration.security.AbstractContainerTest.*; @RunWith(SpringRunner.class) @SpringBootTest( @@ -28,9 +21,6 @@ ) @TestPropertySource( properties = { - "app.name=cbioportal", - "server.port=8080", - "filter_groups_by_appname=false", "authenticate=oauth2", "dat.method=oauth2", // DB settings (also see MysqlInitializer) @@ -56,51 +46,16 @@ } ) @ContextConfiguration(initializers = { - OAuth2AuthIntegrationTest.MyMysqlInitializer.class, - MyKeycloakInitializer.class, - OAuth2AuthIntegrationTest.PortInitializer.class + MyMysqlInitializer.class, + MyOAuth2KeycloakInitializer.class, + PortInitializer.class }) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @DirtiesContext // needed to reuse port 8080 for multiple tests public class OAuth2AuthIntegrationTest extends AbstractContainerTest { - private final static int CBIO_PORT = 8080; - private final static int KC_PORT = 8084; - private final static int SESSION_PORT = 27017; public final static String CBIO_URL_FROM_BROWSER = String.format("http://host.testcontainers.internal:%d", CBIO_PORT); - - // Update application properties with connection info on Keycloak container - public static class MyKeycloakInitializer extends OAuth2KeycloakInitializer { - @Override - public void initialize( - ConfigurableApplicationContext configurableApplicationContext) { - super.initializeImpl(configurableApplicationContext, keycloakContainer); - } - } - - // Update application properties with connection info on Mysql container - public static class MyMysqlInitializer extends MysqlInitializer { - @Override - public void initialize( - ConfigurableApplicationContext configurableApplicationContext) { - super.initializeImpl(configurableApplicationContext, mysqlContainer); - } - } - - // Expose the ports for the cBioPortal Spring application and keycloak inside - // the Chrome container. Each address is available on http://host.testcontainers.internal: - // in the browser container. - public static class PortInitializer implements - ApplicationContextInitializer { - @Override - public void initialize(ConfigurableApplicationContext applicationContext) { - applicationContext.addApplicationListener( - (ApplicationListener) event -> { - Testcontainers.exposeHostPorts(CBIO_PORT, KC_PORT, SESSION_PORT); - }); - } - } @Test public void a_loginSuccess() { diff --git a/src/test/java/org/cbioportal/test/integration/security/SamlAuthIntegrationTest.java b/src/test/java/org/cbioportal/test/integration/security/SamlAuthIntegrationTest.java index f4a3265c1e7..f5ae67eb281 100644 --- a/src/test/java/org/cbioportal/test/integration/security/SamlAuthIntegrationTest.java +++ b/src/test/java/org/cbioportal/test/integration/security/SamlAuthIntegrationTest.java @@ -1,23 +1,18 @@ package org.cbioportal.test.integration.security; import org.cbioportal.PortalApplication; -import org.cbioportal.test.integration.MysqlInitializer; -import org.cbioportal.test.integration.SamlKeycloakInitializer; import org.cbioportal.test.integration.security.util.Util; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.web.context.WebServerInitializedEvent; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ApplicationListener; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; -import org.testcontainers.Testcontainers; + +import static org.cbioportal.test.integration.security.AbstractContainerTest.*; @RunWith(SpringRunner.class) @SpringBootTest( @@ -26,9 +21,6 @@ ) @TestPropertySource( properties = { - "app.name=cbioportal", - "server.port=8080", - "filter_groups_by_appname=false", "authenticate=saml", "dat.method=oauth2", // DB settings (also see MysqlInitializer) @@ -50,52 +42,17 @@ } ) @ContextConfiguration(initializers = { - SamlAuthIntegrationTest.MyMysqlInitializer.class, - SamlAuthIntegrationTest.MySamlKeycloakInitializer.class, - SamlAuthIntegrationTest.PortInitializer.class + MyMysqlInitializer.class, + MySamlKeycloakInitializer.class, + PortInitializer.class }) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @DirtiesContext // needed to reuse port 8080 for multiple tests public class SamlAuthIntegrationTest extends AbstractContainerTest { - private final static int CBIO_PORT = 8080; - private final static int KC_PORT = 8084; - private final static int SESSION_PORT = 27017; public final static String CBIO_URL_FROM_BROWSER = String.format("http://host.testcontainers.internal:%d", CBIO_PORT); - // Update application properties with connection info on Keycloak container - public static class MySamlKeycloakInitializer extends SamlKeycloakInitializer { - @Override - public void initialize( - ConfigurableApplicationContext configurableApplicationContext) { - super.initializeImpl(configurableApplicationContext, keycloakContainer); - } - } - - // Update application properties with connection info on Mysql container - public static class MyMysqlInitializer extends MysqlInitializer { - @Override - public void initialize( - ConfigurableApplicationContext configurableApplicationContext) { - super.initializeImpl(configurableApplicationContext, mysqlContainer); - } - } - - // Expose the ports for the cBioPortal Spring application and keycloak inside - // the Chrome container. Each address is available on http://host.testcontainers.internal: - // in the browser container. - public static class PortInitializer implements - ApplicationContextInitializer { - @Override - public void initialize(ConfigurableApplicationContext applicationContext) { - applicationContext.addApplicationListener( - (ApplicationListener) event -> { - Testcontainers.exposeHostPorts(CBIO_PORT, KC_PORT, SESSION_PORT); - }); - } - } - @Test public void a_loginSuccess() { Util.testLogin(CBIO_URL_FROM_BROWSER, chromedriverContainer);