Skip to content

Commit

Permalink
⚡ : use singleton container pattern
Browse files Browse the repository at this point in the history
using a singleton container with emptyDatabase between tests instead
of reloading a new spring-context for each integration test.

now spring is reloaded only if necessary, not when some data must change
in the database.
  • Loading branch information
juwit committed Jul 3, 2020
1 parent e1f5986 commit f109f5e
Show file tree
Hide file tree
Showing 22 changed files with 235 additions and 367 deletions.
11 changes: 2 additions & 9 deletions src/test/java/io/gaia_app/GaiaIT.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
package io.gaia_app;

import io.gaia_app.test.MongoContainer;
import io.gaia_app.test.MongoContainer;
import io.gaia_app.test.SharedMongoContainerTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
@Testcontainers
class GaiaIT {

@Container
private static MongoContainer mongoContainer = new MongoContainer();
class GaiaIT extends SharedMongoContainerTest {

@Autowired
private Gaia gaia;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
package io.gaia_app.client.controller

import io.gaia_app.test.MongoContainer
import io.gaia_app.test.SharedMongoContainerTest
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.hasSize
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.security.test.context.support.WithMockUser
import org.springframework.test.annotation.DirtiesContext
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers

@SpringBootTest
@DirtiesContext
@Testcontainers
@AutoConfigureMockMvc
class AuthenticationRestControllerIT {
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class AuthenticationRestControllerIT: SharedMongoContainerTest() {

@Autowired
lateinit var mockMvc: MockMvc

companion object {
@Container
val mongoContainer = MongoContainer()
.withScript("src/test/resources/db/00_team.js")
.withScript("src/test/resources/db/10_user.js")
@BeforeAll
fun setUp() {
mongo.emptyDatabase()
mongo.runScript("src/test/resources/db/00_team.js")
mongo.runScript("src/test/resources/db/10_user.js")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.springframework.web.bind.annotation.RestController

@SpringBootTest(classes = [SecurityConfig::class, SecurityConfigIT.FakeRestController::class])
@AutoConfigureMockMvc
@DirtiesContext
@TestPropertySource(properties = ["gaia.admin-password=admin456"])
class SecurityConfigIT {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package io.gaia_app.config.security;

import io.gaia_app.test.MongoContainer;
import io.gaia_app.test.SharedMongoContainerTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
Expand All @@ -18,12 +15,7 @@

@SpringBootTest
@AutoConfigureMockMvc
@DirtiesContext
@Testcontainers
public class StateApiSecurityConfigIT {

@Container
private static MongoContainer mongoContainer = new MongoContainer();
public class StateApiSecurityConfigIT extends SharedMongoContainerTest {

@Autowired
private StateApiSecurityConfig.StateApiSecurityProperties props;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package io.gaia_app.config.security.actuator;

import io.gaia_app.test.MongoContainer;
import io.gaia_app.test.SharedMongoContainerTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand All @@ -18,16 +15,11 @@

@SpringBootTest
@AutoConfigureMockMvc
@DirtiesContext
@Testcontainers
class ActuatorSecurityConfigIT {
class ActuatorSecurityConfigIT extends SharedMongoContainerTest {

@Autowired
private MockMvc mockMvc;

@Container
private static MongoContainer mongo = new MongoContainer();

@Test
void actuatorHealth_shouldNotNeedAuthentication() throws Exception {
this.mockMvc.perform(get("/admin/health")).andDo(print()).andExpect(status().isOk())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
package io.gaia_app.config.security.ldap;

import io.gaia_app.test.MongoContainer;
import io.gaia_app.test.SharedMongoContainerTest;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

@DirtiesContext
@Testcontainers
class LdapSecurityConfigIT {

@Container
private static MongoContainer mongoContainer = new MongoContainer();

@Nested
@SpringBootTest(properties = "gaia.ldap.enabled=false")
class LdapSecurityConfigNotLoadedTest {
class LdapSecurityConfigNotLoadedTest extends SharedMongoContainerTest {
@Test
void ldapSecurityConfig_shouldNotBeInstantiated(
@Autowired(required = false) LdapSecurityConfig ldapSecurityConfig) {
Expand All @@ -35,7 +29,7 @@ void ldapSecurityConfig_shouldNotBeInstantiated(
"gaia.ldap.userDnPatterns=test_dn",
"gaia.ldap.url=ldap://test_url",
})
class LdapSecurityConfigLoadedTest {
class LdapSecurityConfigLoadedTest extends SharedMongoContainerTest {
@Test
void ldapSecurityConfig_shouldBeInstantiated(
@Autowired(required = false) LdapSecurityConfig ldapSecurityConfig) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
package io.gaia_app.config.security.oauth2

import io.gaia_app.test.MongoContainer
import io.gaia_app.test.SharedMongoContainerTest
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.annotation.DirtiesContext
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.TestPropertySource
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers

@DirtiesContext
@Testcontainers
class OAuth2ClientSecurityConfigIT {

companion object {
@Container
val mongoContainer = MongoContainer()
}

@Nested
@SpringBootTest
inner class OAuth2ClientSecurityConfigNotLoadedTest {
inner class OAuth2ClientSecurityConfigNotLoadedTest: SharedMongoContainerTest() {

@Test
fun `oauth2ClientSecurityConfig should not be instantiated`(
Expand All @@ -47,7 +37,7 @@ class OAuth2ClientSecurityConfigIT {
@Nested
@SpringBootTest
@ActiveProfiles("oauth2")
inner class OAuth2ClientSecurityConfigLoadedTest {
inner class OAuth2ClientSecurityConfigLoadedTest: SharedMongoContainerTest() {

@Test
fun `oauth2ClientSecurityConfig should be instantiated`(
Expand Down Expand Up @@ -75,7 +65,7 @@ class OAuth2ClientSecurityConfigIT {
"spring.security.oauth2.client.provider.dummy.token-uri=https://dummy.com/oauth/token",
"spring.security.oauth2.client.provider.dummy.user-info-uri=https://dummy.com/api/v4/user",
"spring.security.oauth2.client.provider.dummy.user-name-attribute=username"])
inner class OAuth2ClientSecurityTest {
inner class OAuth2ClientSecurityTest: SharedMongoContainerTest() {

@Autowired
lateinit var mockMvc: MockMvc
Expand Down
29 changes: 11 additions & 18 deletions src/test/java/io/gaia_app/e2e/SeleniumIT.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.gaia_app.e2e;

import io.gaia_app.test.MongoContainer;
import io.gaia_app.test.MongoContainer;
import io.gaia_app.test.SharedMongoContainerTest;
import io.percy.selenium.Percy;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.*;
Expand All @@ -15,9 +14,6 @@
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.annotation.DirtiesContext;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.File;
import java.io.IOException;
Expand All @@ -27,25 +23,13 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
@Testcontainers
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Tag("e2e")
public class SeleniumIT {
public class SeleniumIT extends SharedMongoContainerTest {

@LocalServerPort
private int serverPort;

@Container
private static MongoContainer mongoContainer = new MongoContainer()
.withScript("src/test/resources/db/00_team.js")
.withScript("src/test/resources/db/10_user.js")
.withScript("src/test/resources/db/20_module.js")
.withScript("src/test/resources/db/30_stack.js")
.withScript("src/test/resources/db/40_job.js")
.withScript("src/test/resources/db/50_step.js")
.withScript("src/test/resources/db/60_terraformState.js");

private static WebDriver driver;

private static Percy percy;
Expand All @@ -65,6 +49,15 @@ public static void openServerAndBrowser() throws IOException {
percy = new Percy(driver);

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

mongo.emptyDatabase();
mongo.runScript("src/test/resources/db/00_team.js");
mongo.runScript("src/test/resources/db/10_user.js");
mongo.runScript("src/test/resources/db/20_module.js");
mongo.runScript("src/test/resources/db/30_stack.js");
mongo.runScript("src/test/resources/db/40_job.js");
mongo.runScript("src/test/resources/db/50_step.js");
mongo.runScript("src/test/resources/db/60_terraformState.js");
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.gaia_app.modules.controller;

import io.gaia_app.test.MongoContainer
import io.gaia_app.test.SharedMongoContainerTest
import org.hamcrest.Matchers.*
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.core.io.ClassPathResource
import org.springframework.http.MediaType
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user
import org.springframework.test.annotation.DirtiesContext
import org.springframework.test.web.client.MockRestServiceServer.bindTo
import org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo
import org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess
Expand All @@ -19,25 +20,23 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.web.client.RestTemplate
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers

@SpringBootTest
@DirtiesContext
@Testcontainers
@AutoConfigureWebClient
@AutoConfigureMockMvc
class DockerRegistryRestControllerIT {
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DockerRegistryRestControllerIT: SharedMongoContainerTest() {

@Autowired
lateinit var mockMvc: MockMvc

@Autowired
lateinit var restTemplate: RestTemplate

companion object {
@Container
val mongoContainer = MongoContainer().withScript("src/test/resources/db/10_user.js")
@BeforeAll
internal fun setUp() {
mongo.emptyDatabase()
mongo.runScript("src/test/resources/db/10_user.js")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package io.gaia_app.modules.controller;

import io.gaia_app.test.MongoContainer;
import io.gaia_app.test.SharedMongoContainerTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.hamcrest.Matchers.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
Expand All @@ -24,24 +21,19 @@
* Simple integration test that validates the security configuration of the TeamsRestController, and its http routes
*/
@SpringBootTest
@DirtiesContext
@Testcontainers
@AutoConfigureMockMvc
@WithMockUser(value = "admin", roles = "ADMIN")
class ModuleRestControllerIT {

@Container
private static MongoContainer mongoContainer = new MongoContainer()
.withScript("src/test/resources/db/00_team.js")
.withScript("src/test/resources/db/10_user.js")
.withScript("src/test/resources/db/20_module.js");
class ModuleRestControllerIT extends SharedMongoContainerTest {

@Autowired
private MockMvc mockMvc;

@BeforeEach
void setup() {
mongoContainer.resetDatabase();
mongo.emptyDatabase();
mongo.runScript("src/test/resources/db/00_team.js");
mongo.runScript("src/test/resources/db/10_user.js");
mongo.runScript("src/test/resources/db/20_module.js");
}

@Test
Expand Down
Loading

0 comments on commit f109f5e

Please sign in to comment.