refactor: register method Optional 반환 -> Exception 처리
Optional 반환과 Exception 반환이 혼용되어 사용되고 있어, Exception 으로 통일
This commit is contained in:
@@ -33,14 +33,8 @@ public class UserController {
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<Object> register(@RequestBody @Valid SignUpRequest request) {
|
||||
Optional<User> user = userService.register(request.toSignUp(passwordEncoder));
|
||||
|
||||
if (user.isEmpty()) {
|
||||
log.error("이미 존재하는 이메일 입니다. :: {}", request.getEmail());
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||
}
|
||||
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(SignUpResponse.of(user.get()));
|
||||
User user = userService.register(request.toSignUp(passwordEncoder));
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(SignUpResponse.of(user));
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
|
||||
@@ -25,15 +25,14 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Optional<User> register(@Valid SignUpDTO signUpDto) {
|
||||
public User register(@Valid SignUpDTO signUpDto) {
|
||||
Optional<User> user = userRepository.findByEmail(signUpDto.getEmail());
|
||||
if (user.isPresent()) {
|
||||
log.error("이미 존재하는 이메일이기 때문에 신규 회원가입을 진행할 수 없습니다. :: {}", signUpDto);
|
||||
return Optional.empty();
|
||||
throw new IllegalArgumentException("이미 존재하는 이메일이기 때문에 신규 회원가입을 진행할 수 없습니다.");
|
||||
}
|
||||
|
||||
User newUser = userRepository.save(signUpDto.toUser());
|
||||
return Optional.of(newUser);
|
||||
return userRepository.save(signUpDto.toUser());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,7 +9,7 @@ import javax.validation.Valid;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
Optional<User> register(@Valid SignUpDTO signUpDto);
|
||||
User register(@Valid SignUpDTO signUpDto);
|
||||
|
||||
Optional<User> delete(@Valid DeleteUserDTO deleteUser);
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.ticketing.server.user.service;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -49,10 +50,9 @@ class UserServiceImplTest {
|
||||
when(userRepository.findByEmail("ticketing@gmail.com")).thenReturn(Optional.of(user));
|
||||
|
||||
// when
|
||||
Optional<User> user = userService.register(signUp);
|
||||
|
||||
// then
|
||||
assertThat(user).isEmpty();
|
||||
assertThatThrownBy(() -> userService.register(signUp))
|
||||
.isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -63,10 +63,10 @@ class UserServiceImplTest {
|
||||
when(userRepository.save(any())).thenReturn(user);
|
||||
|
||||
// when
|
||||
Optional<User> user = userService.register(signUp);
|
||||
User user = userService.register(signUp);
|
||||
|
||||
// then
|
||||
assertThat(user).isPresent();
|
||||
assertThat(user).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -98,20 +98,20 @@ class UserServiceImplTest {
|
||||
@Test
|
||||
@DisplayName("패스워드 변경 시 이메일이 존재하지 않을 경우")
|
||||
void modifyPasswordFail() {
|
||||
// given
|
||||
// given
|
||||
when(userRepository.findByEmailAndIsDeletedFalse("ticketing@gmail.com")).thenReturn(Optional.empty());
|
||||
|
||||
// when
|
||||
// when
|
||||
Optional<User> user = userService.modifyPassword(changePassword);
|
||||
|
||||
// then
|
||||
// then
|
||||
assertThat(user).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("패스워드 변경 성공했을 경우")
|
||||
void modifyPasswordSuccess() {
|
||||
// given
|
||||
// given
|
||||
when(userRepository.findByEmailAndIsDeletedFalse("ticketing@gmail.com")).thenReturn(Optional.of(user));
|
||||
|
||||
// when
|
||||
|
||||
Reference in New Issue
Block a user