Implements RegisterAccountProcessor

This commit is contained in:
Rebwon
2021-08-26 15:56:41 +09:00
committed by MaengSol
parent 4207f8b9d6
commit e3abe28387
6 changed files with 151 additions and 1 deletions

View File

@@ -0,0 +1,17 @@
package com.yam.app.account.application;
import lombok.Getter;
@Getter
public final class RegisterAccountCommand {
private final String email;
private final String nickname;
private final String password;
public RegisterAccountCommand(String email, String nickname, String password) {
this.email = email;
this.nickname = nickname;
this.password = password;
}
}

View File

@@ -0,0 +1,19 @@
package com.yam.app.account.domain;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public final class Account {
private Long id;
private String email;
private String nickname;
private String password;
public Account(String email, String nickname, String password) {
this.email = email;
this.nickname = nickname;
this.password = password;
}
}

View File

@@ -0,0 +1,10 @@
package com.yam.app.account.domain;
public interface AccountRepository {
boolean existsByEmail(String email);
boolean existsByNickname(String nickname);
Account save(Account entity);
}

View File

@@ -0,0 +1,21 @@
package com.yam.app.account.domain;
public final class RegisterAccountProcessor {
private final AccountRepository accountRepository;
public RegisterAccountProcessor(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
public Account process(String email, String nickname, String password) {
if (accountRepository.existsByEmail(email)) {
throw new IllegalStateException();
}
if (accountRepository.existsByNickname(nickname)) {
throw new IllegalStateException();
}
return accountRepository.save(new Account(email, nickname, password));
}
}

View File

@@ -1,4 +1,4 @@
package com.yam.app.account.infrastructure;
package com.yam.app.configuration;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;

View File

@@ -0,0 +1,83 @@
package com.yam.app.account.domain;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import com.yam.app.account.application.RegisterAccountCommand;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
@DisplayName("회원가입 도메인 서비스")
class RegisterAccountProcessorTest {
@TestFactory
@DisplayName("회원가입 시나리오")
Collection<DynamicTest> register_account_scenarios() {
var repository = new AccountRepositoryStub();
var processor = new RegisterAccountProcessor(repository);
return Arrays.asList(
DynamicTest.dynamicTest("회원가입에 성공한다.", () -> {
// Arrange
var command = new RegisterAccountCommand("rebwon@gmail.com", "rebwon", "password!");
// Act
Account account = processor.process(command.getEmail(), command.getNickname(),
command.getPassword());
// Assert
assertThat(account.getId()).isEqualTo(1L);
}),
DynamicTest.dynamicTest("이메일 검증에 실패하여 예외를 리턴한다.", () -> {
// Arrange
var command = new RegisterAccountCommand("rebwon@gmail.com", "rebwon", "password!");
// Act & Assert
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> processor.process(command.getEmail(), command.getNickname(),
command.getPassword()));
}),
DynamicTest.dynamicTest("닉네임 검증에 실패하여 예외를 리턴한다.", () -> {
// Arrange
var command = new RegisterAccountCommand("kitty@gmail.com", "rebwon", "password!");
// Act & Assert
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> processor.process(command.getEmail(), command.getNickname(),
command.getPassword()));
})
);
}
private static class AccountRepositoryStub implements AccountRepository {
private final Map<Long, Account> data = new ConcurrentHashMap<>();
private final AtomicLong idGenerator = new AtomicLong();
@Override
public boolean existsByEmail(String email) {
return data.values().stream()
.anyMatch(account -> account.getEmail().equals(email));
}
@Override
public boolean existsByNickname(String nickname) {
return data.values().stream()
.anyMatch(account -> account.getNickname().equals(nickname));
}
@Override
public Account save(Account entity) {
if (entity.getId() == null) {
entity.setId(idGenerator.incrementAndGet());
data.put(entity.getId(), entity);
}
return data.putIfAbsent(entity.getId(), entity);
}
}
}