|
4 | 4 | import io.ejangs.docsa.domain.user.entity.User; |
5 | 5 | import jakarta.annotation.PostConstruct; |
6 | 6 | import lombok.RequiredArgsConstructor; |
| 7 | +import org.springframework.core.env.Environment; |
| 8 | +import org.springframework.core.env.Profiles; |
| 9 | +import org.springframework.beans.factory.annotation.Value; |
7 | 10 | import org.springframework.context.annotation.Profile; |
8 | 11 | import org.springframework.security.crypto.password.PasswordEncoder; |
9 | 12 | import org.springframework.stereotype.Component; |
10 | 13 | import org.springframework.transaction.annotation.Transactional; |
11 | 14 |
|
12 | 15 | @Component |
13 | 16 | @Profile({"local", "stg"}) |
| 17 | +@Transactional(rollbackFor = Exception.class) |
14 | 18 | @RequiredArgsConstructor |
15 | 19 | public class TestUserInitializer { |
16 | 20 |
|
17 | 21 | private final UserRepository userRepository; |
18 | 22 | private final PasswordEncoder passwordEncoder; |
| 23 | + private final Environment environment; |
19 | 24 |
|
20 | 25 | private static final String testUserEmail = "test@test.com"; |
21 | 26 | private static final String testUserName = "test"; |
22 | 27 | private static final String testUserPassword = "Testtest1"; |
| 28 | + private static final String perfUserName = "perf"; |
| 29 | + private static final String perfUserPrefix = "perfdel"; |
| 30 | + private static final String perfUserDomain = "test.com"; |
| 31 | + |
| 32 | + @Value("${perf.seed.user-count:0}") |
| 33 | + private int perfSeedUserCount; |
23 | 34 |
|
24 | 35 | @PostConstruct |
25 | | - @Transactional |
26 | | - public void intiTestUser() { |
| 36 | + public void initTestUser() { |
| 37 | + String encodedPassword = createBaseTestUserIfAbsent(); |
| 38 | + seedPerfUsersOnStg(encodedPassword); |
| 39 | + } |
| 40 | + |
| 41 | + private String createBaseTestUserIfAbsent() { |
27 | 42 | if (userRepository.findByEmail(testUserEmail).isEmpty()) { |
| 43 | + String encodedPassword = passwordEncoder.encode(testUserPassword); |
28 | 44 | User user = User.builder() |
29 | 45 | .email(testUserEmail) |
30 | 46 | .name(testUserName) |
31 | | - .password(passwordEncoder.encode(testUserPassword)) |
| 47 | + .password(encodedPassword) |
32 | 48 | .build(); |
33 | 49 | userRepository.save(user); |
| 50 | + return encodedPassword; |
| 51 | + } |
| 52 | + |
| 53 | + return userRepository.findByEmail(testUserEmail) |
| 54 | + .map(User::getPassword) |
| 55 | + .orElseGet(() -> passwordEncoder.encode(testUserPassword)); |
| 56 | + } |
| 57 | + |
| 58 | + private void seedPerfUsersOnStg(String encodedPassword) { |
| 59 | + if (!environment.acceptsProfiles(Profiles.of("stg"))) { |
| 60 | + return; |
| 61 | + } |
| 62 | + if (perfSeedUserCount <= 0) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + for (int i = 1; i <= perfSeedUserCount; i++) { |
| 67 | + String email = String.format("%s_u%03d@%s", perfUserPrefix, i, perfUserDomain); |
| 68 | + String name = String.format("%s_u%03d", perfUserName, i); |
| 69 | + if (userRepository.existsByEmail(email)) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + userRepository.save(User.builder() |
| 73 | + .email(email) |
| 74 | + .name(name) |
| 75 | + .password(encodedPassword) |
| 76 | + .build()); |
34 | 77 | } |
35 | 78 | } |
36 | 79 | } |
0 commit comments