Skip to content

Commit e791377

Browse files
committed
refactor: seed test data with sql script and remove @DrtiestContext
1 parent 8fb6cfc commit e791377

File tree

13 files changed

+61
-127
lines changed

13 files changed

+61
-127
lines changed

backend/spring-boot/src/main/java/org/bugzkit/api/shared/config/DataInit.java

Lines changed: 15 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.springframework.security.crypto.password.PasswordEncoder;
1818
import org.springframework.stereotype.Component;
1919

20-
@Profile({"dev", "prod", "test"})
20+
@Profile({"dev", "prod"})
2121
@Component
2222
public class DataInit implements ApplicationRunner {
2323
private final UserRepository userRepository;
@@ -45,41 +45,39 @@ public DataInit(
4545

4646
@Override
4747
public void run(ApplicationArguments args) {
48-
initRoles();
49-
saveUsers();
48+
getRoles();
49+
seedUsers();
5050
}
5151

52-
private void initRoles() {
52+
private void getRoles() {
5353
userRole = roleRepository.findByName(RoleName.USER).orElseThrow();
5454
adminRole = roleRepository.findByName(RoleName.ADMIN).orElseThrow();
5555
}
5656

57-
private void saveUsers() {
57+
private void seedUsers() {
5858
if (!userRepository.existsByUsername("admin"))
5959
userRepository.save(
6060
User.builder()
6161
.username("admin")
62-
.email("admin@localhost")
62+
.email("office@bugzkit.com")
6363
.password(bCryptPasswordEncoder.encode(password))
6464
.active(true)
6565
.lock(false)
6666
.roles(Set.of(userRole, adminRole))
6767
.build());
68-
if (!userRepository.existsByUsername("user"))
69-
userRepository.save(
70-
User.builder()
71-
.username("user")
72-
.email("user@localhost")
73-
.password(bCryptPasswordEncoder.encode(password))
74-
.active(true)
75-
.lock(false)
76-
.roles(Collections.singleton(userRole))
77-
.build());
7868
if (environment.getActiveProfiles()[0].equals("dev")) devUsers();
79-
else if (environment.getActiveProfiles()[0].equals("test")) testUsers();
8069
}
8170

8271
private void devUsers() {
72+
userRepository.save(
73+
User.builder()
74+
.username("user")
75+
.email("user@localhost")
76+
.password(bCryptPasswordEncoder.encode(password))
77+
.active(true)
78+
.lock(false)
79+
.roles(Collections.singleton(userRole))
80+
.build());
8381
List<User> users =
8482
faker
8583
.collection(
@@ -96,89 +94,4 @@ private void devUsers() {
9694
.generate();
9795
userRepository.saveAll(users);
9896
}
99-
100-
private void testUsers() {
101-
userRepository.saveAll(
102-
List.of(
103-
User.builder()
104-
.username("deactivated1")
105-
.email("deactivate1d@localhost")
106-
.password(bCryptPasswordEncoder.encode(password))
107-
.active(false)
108-
.lock(false)
109-
.roles(Collections.singleton(userRole))
110-
.build(),
111-
User.builder()
112-
.username("deactivated2")
113-
.email("deactivated2@localhost")
114-
.password(bCryptPasswordEncoder.encode(password))
115-
.active(false)
116-
.lock(false)
117-
.roles(Collections.singleton(userRole))
118-
.build(),
119-
User.builder()
120-
.username("deactivated3")
121-
.email("deactivated3@localhost")
122-
.password(bCryptPasswordEncoder.encode(password))
123-
.active(false)
124-
.lock(false)
125-
.roles(Collections.singleton(userRole))
126-
.build(),
127-
User.builder()
128-
.username("locked")
129-
.email("locked@localhost")
130-
.password(bCryptPasswordEncoder.encode(password))
131-
.active(true)
132-
.lock(true)
133-
.roles(Collections.singleton(userRole))
134-
.build(),
135-
User.builder()
136-
.username("update1")
137-
.email("update1@localhost")
138-
.password(bCryptPasswordEncoder.encode(password))
139-
.active(true)
140-
.lock(false)
141-
.roles(Collections.singleton(userRole))
142-
.build(),
143-
User.builder()
144-
.username("update2")
145-
.email("update2@localhost")
146-
.password(bCryptPasswordEncoder.encode(password))
147-
.active(true)
148-
.lock(false)
149-
.roles(Collections.singleton(userRole))
150-
.build(),
151-
User.builder()
152-
.username("update3")
153-
.email("update3@localhost")
154-
.password(bCryptPasswordEncoder.encode(password))
155-
.active(true)
156-
.lock(false)
157-
.roles(Collections.singleton(userRole))
158-
.build(),
159-
User.builder()
160-
.username("update4")
161-
.email("update4@localhost")
162-
.password(bCryptPasswordEncoder.encode(password))
163-
.active(true)
164-
.lock(false)
165-
.roles(Collections.singleton(userRole))
166-
.build(),
167-
User.builder()
168-
.username("delete1")
169-
.email("delete1@localhost")
170-
.password(bCryptPasswordEncoder.encode(password))
171-
.active(true)
172-
.lock(false)
173-
.roles(Collections.singleton(userRole))
174-
.build(),
175-
User.builder()
176-
.username("delete2")
177-
.email("delete2@localhost")
178-
.password(bCryptPasswordEncoder.encode(password))
179-
.active(true)
180-
.lock(false)
181-
.roles(Collections.singleton(userRole))
182-
.build()));
183-
}
18497
}

backend/spring-boot/src/test/java/org/bugzkit/api/admin/integration/UserControllerIT.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@
2828
import org.springframework.boot.test.context.SpringBootTest;
2929
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
3030
import org.springframework.http.MediaType;
31-
import org.springframework.test.annotation.DirtiesContext;
3231
import org.springframework.test.context.ActiveProfiles;
3332
import org.springframework.test.web.servlet.MockMvc;
3433
import tools.jackson.databind.ObjectMapper;
3534

36-
@DirtiesContext
3735
@AutoConfigureMockMvc
3836
@ActiveProfiles("test")
3937
@SpringBootTest

backend/spring-boot/src/test/java/org/bugzkit/api/auth/integration/AuthControllerIT.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@
4444
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
4545
import org.springframework.http.HttpHeaders;
4646
import org.springframework.http.MediaType;
47-
import org.springframework.test.annotation.DirtiesContext;
4847
import org.springframework.test.context.ActiveProfiles;
4948
import org.springframework.test.context.bean.override.mockito.MockitoBean;
5049
import org.springframework.test.web.servlet.MockMvc;
5150
import tools.jackson.databind.ObjectMapper;
5251

53-
@DirtiesContext
5452
@AutoConfigureMockMvc
5553
@ActiveProfiles("test")
5654
@SpringBootTest

backend/spring-boot/src/test/java/org/bugzkit/api/auth/integration/OAuth2UserServiceIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import org.junit.jupiter.api.Test;
1212
import org.springframework.beans.factory.annotation.Autowired;
1313
import org.springframework.boot.test.context.SpringBootTest;
14-
import org.springframework.test.annotation.DirtiesContext;
1514
import org.springframework.test.context.ActiveProfiles;
15+
import org.springframework.transaction.annotation.Transactional;
1616

17-
@DirtiesContext
17+
@Transactional
1818
@ActiveProfiles("test")
1919
@SpringBootTest
2020
class OAuth2UserServiceIT extends DatabaseContainers {

backend/spring-boot/src/test/java/org/bugzkit/api/shared/config/DatabaseContainers.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
package org.bugzkit.api.shared.config;
22

3-
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
43
import org.springframework.test.context.DynamicPropertyRegistry;
54
import org.springframework.test.context.DynamicPropertySource;
5+
import org.springframework.test.context.jdbc.Sql;
6+
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
67
import org.testcontainers.containers.GenericContainer;
7-
import org.testcontainers.junit.jupiter.Container;
8-
import org.testcontainers.junit.jupiter.Testcontainers;
98
import org.testcontainers.postgresql.PostgreSQLContainer;
109
import org.testcontainers.utility.DockerImageName;
1110

12-
@Testcontainers
11+
@Sql(
12+
scripts = {"classpath:sql/truncate.sql", "classpath:sql/test-data.sql"},
13+
executionPhase = ExecutionPhase.BEFORE_TEST_CLASS)
1314
public abstract class DatabaseContainers {
14-
@Container @ServiceConnection
1515
static PostgreSQLContainer postgres =
1616
new PostgreSQLContainer(DockerImageName.parse("postgres").withTag("latest"));
1717

18-
@Container
1918
static GenericContainer<?> redis =
2019
new GenericContainer<>(DockerImageName.parse("redis").withTag("latest"))
2120
.withExposedPorts(6379)
2221
.withCommand("redis-server", "--requirepass", "root");
2322

23+
static {
24+
postgres.start();
25+
redis.start();
26+
}
27+
2428
@DynamicPropertySource
25-
static void redisProperties(DynamicPropertyRegistry registry) {
29+
static void containerProperties(DynamicPropertyRegistry registry) {
30+
registry.add("spring.datasource.url", postgres::getJdbcUrl);
31+
registry.add("spring.datasource.username", postgres::getUsername);
32+
registry.add("spring.datasource.password", postgres::getPassword);
2633
registry.add("spring.data.redis.host", redis::getHost);
2734
registry.add("spring.data.redis.port", () -> redis.getMappedPort(6379));
2835
}

backend/spring-boot/src/test/java/org/bugzkit/api/shared/integration/AccessingResourcesIT.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
import org.springframework.boot.test.context.SpringBootTest;
2323
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
2424
import org.springframework.http.MediaType;
25-
import org.springframework.test.annotation.DirtiesContext;
2625
import org.springframework.test.context.ActiveProfiles;
2726
import org.springframework.test.web.servlet.MockMvc;
2827
import tools.jackson.databind.ObjectMapper;
2928

30-
@DirtiesContext
3129
@AutoConfigureMockMvc
3230
@ActiveProfiles("test")
3331
@SpringBootTest

backend/spring-boot/src/test/java/org/bugzkit/api/shared/integration/FlywayMigrationIT.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import org.bugzkit.api.shared.config.DatabaseContainers;
44
import org.junit.jupiter.api.Test;
55
import org.springframework.boot.test.context.SpringBootTest;
6-
import org.springframework.test.annotation.DirtiesContext;
76
import org.springframework.test.context.ActiveProfiles;
87

9-
@DirtiesContext
108
@ActiveProfiles("test")
119
@SpringBootTest
1210
class FlywayMigrationIT extends DatabaseContainers {

backend/spring-boot/src/test/java/org/bugzkit/api/shared/integration/RateLimitIT.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
import org.springframework.boot.test.context.SpringBootTest;
1919
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
2020
import org.springframework.http.MediaType;
21-
import org.springframework.test.annotation.DirtiesContext;
2221
import org.springframework.test.context.ActiveProfiles;
2322
import org.springframework.test.context.TestPropertySource;
2423
import org.springframework.test.context.bean.override.mockito.MockitoBean;
2524
import org.springframework.test.web.servlet.MockMvc;
2625
import tools.jackson.databind.ObjectMapper;
2726

28-
@DirtiesContext
2927
@AutoConfigureMockMvc
3028
@ActiveProfiles("test")
3129
@SpringBootTest

backend/spring-boot/src/test/java/org/bugzkit/api/user/integration/ProfileControllerIT.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323
import org.springframework.boot.test.context.SpringBootTest;
2424
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
2525
import org.springframework.http.MediaType;
26-
import org.springframework.test.annotation.DirtiesContext;
2726
import org.springframework.test.context.ActiveProfiles;
2827
import org.springframework.test.context.bean.override.mockito.MockitoBean;
2928
import org.springframework.test.web.servlet.MockMvc;
3029
import tools.jackson.databind.ObjectMapper;
3130

32-
@DirtiesContext
3331
@AutoConfigureMockMvc
3432
@ActiveProfiles("test")
3533
@SpringBootTest

backend/spring-boot/src/test/java/org/bugzkit/api/user/integration/RoleControllerIT.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
import org.springframework.boot.test.context.SpringBootTest;
1414
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
1515
import org.springframework.http.MediaType;
16-
import org.springframework.test.annotation.DirtiesContext;
1716
import org.springframework.test.context.ActiveProfiles;
1817
import org.springframework.test.web.servlet.MockMvc;
1918
import tools.jackson.databind.ObjectMapper;
2019

21-
@DirtiesContext
2220
@AutoConfigureMockMvc
2321
@ActiveProfiles("test")
2422
@SpringBootTest

0 commit comments

Comments
 (0)