44import io .ejangs .docsa .domain .user .entity .User ;
55import jakarta .annotation .PostConstruct ;
66import lombok .RequiredArgsConstructor ;
7+ import org .springframework .core .env .Environment ;
8+ import org .springframework .core .env .Profiles ;
9+ import org .springframework .beans .factory .annotation .Value ;
710import org .springframework .context .annotation .Profile ;
811import org .springframework .security .crypto .password .PasswordEncoder ;
912import org .springframework .stereotype .Component ;
@@ -17,20 +20,60 @@ public class TestUserInitializer {
1720
1821 private final UserRepository userRepository ;
1922 private final PasswordEncoder passwordEncoder ;
23+ private final Environment environment ;
2024
2125 private static final String testUserEmail = "test@test.com" ;
2226 private static final String testUserName = "test" ;
2327 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 ;
2434
2535 @ PostConstruct
26- public void intiTestUser () {
36+ public void initTestUser () {
37+ String encodedPassword = createBaseTestUserIfAbsent ();
38+ seedPerfUsersOnStg (encodedPassword );
39+ }
40+
41+ private String createBaseTestUserIfAbsent () {
2742 if (userRepository .findByEmail (testUserEmail ).isEmpty ()) {
43+ String encodedPassword = passwordEncoder .encode (testUserPassword );
2844 User user = User .builder ()
2945 .email (testUserEmail )
3046 .name (testUserName )
31- .password (passwordEncoder . encode ( testUserPassword ) )
47+ .password (encodedPassword )
3248 .build ();
3349 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 ());
3477 }
3578 }
3679}
0 commit comments