Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions src/main/java/io/ejangs/docsa/global/init/TestUserInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,76 @@
import io.ejangs.docsa.domain.user.entity.User;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Profile({"local", "stg"})
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class TestUserInitializer {

private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final Environment environment;

private static final String testUserEmail = "test@test.com";
private static final String testUserName = "test";
private static final String testUserPassword = "Testtest1";
private static final String perfUserName = "perf";
private static final String perfUserPrefix = "perfdel";
private static final String perfUserDomain = "test.com";

@Value("${perf.seed.user-count:0}")
private int perfSeedUserCount;

@PostConstruct
@Transactional
public void intiTestUser() {
public void initTestUser() {
String encodedPassword = createBaseTestUserIfAbsent();
seedPerfUsersOnStg(encodedPassword);
}

private String createBaseTestUserIfAbsent() {
if (userRepository.findByEmail(testUserEmail).isEmpty()) {
String encodedPassword = passwordEncoder.encode(testUserPassword);
User user = User.builder()
.email(testUserEmail)
.name(testUserName)
.password(passwordEncoder.encode(testUserPassword))
.password(encodedPassword)
.build();
userRepository.save(user);
return encodedPassword;
}

return userRepository.findByEmail(testUserEmail)
.map(User::getPassword)
.orElseGet(() -> passwordEncoder.encode(testUserPassword));
}

private void seedPerfUsersOnStg(String encodedPassword) {
if (!environment.acceptsProfiles(Profiles.of("stg"))) {
return;
}
if (perfSeedUserCount <= 0) {
return;
}

for (int i = 1; i <= perfSeedUserCount; i++) {
String email = String.format("%s_u%03d@%s", perfUserPrefix, i, perfUserDomain);
String name = String.format("%s_u%03d", perfUserName, i);
if (userRepository.existsByEmail(email)) {
continue;
}
userRepository.save(User.builder()
.email(email)
.name(name)
.password(encodedPassword)
.build());
}
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application-stg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ app:
- "https://stg.docsa.o-r.kr"
- "https://staging.docsa-4hh.pages.dev"

perf:
seed:
user-count: ${PERF_SEED_USER_COUNT:0}

springdoc:
api-docs:
path: /v3/api-docs
Expand Down
Loading