Skip to content

Commit

Permalink
test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pvannierop committed Jan 9, 2024
1 parent 78c62ac commit c7ca3fa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,17 +69,15 @@ 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)
.withRealmImportFile("security/keycloak-configuration-generated.json")
.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)
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -149,9 +146,13 @@ public static class PortInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues values = TestPropertyValues.of(
"server.port=" + CBIO_PORT
);
values.applyTo(applicationContext);
applicationContext.addApplicationListener(
(ApplicationListener<WebServerInitializedEvent>) event -> {
Testcontainers.exposeHostPorts(CBIO_PORT, KEYCLOAK_PORT, SESSION_SERVICE_PORT);
Testcontainers.exposeHostPorts(CBIO_PORT, KEYCLOAK_PORT, MONGO_PORT);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)
Expand All @@ -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:<port>
// in the browser container.
public static class PortInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener(
(ApplicationListener<WebServerInitializedEvent>) event -> {
Testcontainers.exposeHostPorts(CBIO_PORT, KC_PORT, SESSION_PORT);
});
}
}

@Test
public void a_loginSuccess() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)
Expand All @@ -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:<port>
// in the browser container.
public static class PortInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener(
(ApplicationListener<WebServerInitializedEvent>) event -> {
Testcontainers.exposeHostPorts(CBIO_PORT, KC_PORT, SESSION_PORT);
});
}
}

@Test
public void a_loginSuccess() {
Util.testLogin(CBIO_URL_FROM_BROWSER, chromedriverContainer);
Expand Down

0 comments on commit c7ca3fa

Please sign in to comment.