fix : userUpdate Request DTO Fix. fix 6 test-script error

This commit is contained in:
kms
2022-11-05 16:11:46 +09:00
parent 946eb66ebe
commit 0aa4406c0a
5 changed files with 8 additions and 31 deletions

View File

@@ -20,8 +20,6 @@ public class UserUpdate {
private Long id; private Long id;
@Email @Email
private String email; private String email;
private String username;
private String password;
private String bio; private String bio;
private String image; private String image;
} }

View File

@@ -80,7 +80,6 @@ public class User implements UserDetails {
} }
public void update(UserUpdate userUpdate) { public void update(UserUpdate userUpdate) {
this.password = userUpdate.getPassword();
this.bio = userUpdate.getBio(); this.bio = userUpdate.getBio();
this.image = userUpdate.getImage(); this.image = userUpdate.getImage();
} }

View File

@@ -64,13 +64,6 @@ public class UserServiceImpl implements UserService {
public UserResponse updateUser(UserUpdate userUpdate, UserAuth userAuth){ public UserResponse updateUser(UserUpdate userUpdate, UserAuth userAuth){
User user = userRepository.findById(userAuth.getId()).orElseThrow(() -> new CustomException(Error.USER_NOT_FOUND)); User user = userRepository.findById(userAuth.getId()).orElseThrow(() -> new CustomException(Error.USER_NOT_FOUND));
if(userUpdate.getUsername() != null){
userRepository.findByUsername(userUpdate.getUsername())
.filter(found -> !found.getId().equals(userRepository.findById(user.getId())))
.ifPresent(found -> new CustomException(Error.DUPLICATE_USER));
user.changeUsername(userUpdate.getUsername());
}
if(userUpdate.getEmail() != null){ if(userUpdate.getEmail() != null){
userRepository.findAllByEmail(userUpdate.getEmail()) userRepository.findAllByEmail(userUpdate.getEmail())
.stream().filter(found -> !found.getId().equals(userRepository.findById(user.getId()))) .stream().filter(found -> !found.getId().equals(userRepository.findById(user.getId())))

View File

@@ -78,13 +78,12 @@ class UserControllerTest {
.content(objectMapper.writeValueAsString(userUpdate)) .content(objectMapper.writeValueAsString(userUpdate))
.with(csrf()) .with(csrf())
).andExpect(status().isOk()) ).andExpect(status().isOk())
.andExpect(jsonPath("$.user.email", Matchers.equalTo(userUpdate.getEmail()))) .andExpect(jsonPath("$.user.email", Matchers.equalTo(userUpdate.getEmail())));
.andExpect(jsonPath("$.user.username", Matchers.equalTo(userUpdate.getUsername())));
} }
private static Stream<Arguments> updateUser(){ private static Stream<Arguments> updateUser(){
return Stream.of( return Stream.of(
Arguments.of(UserUpdate.builder().username("update name").email("update@gmail.com").password("update password").build() Arguments.of(UserUpdate.builder().email("update@gmail.com").build()
)); ));
} }

View File

@@ -157,18 +157,16 @@ class UserServiceImplTest {
@DisplayName("유저 업데이트 성공 테스트") @DisplayName("유저 업데이트 성공 테스트")
@Test @Test
void updateUserSuccess(){ void updateUserSuccess(){
UserUpdate userUpdate = UserUpdate.builder().username("fixuser").email("kms@gmail.com").build(); UserUpdate userUpdate = UserUpdate.builder().email("kms@gmail.com").build();
UserAuth userAuth = UserAuth.builder().id(1L).build(); UserAuth userAuth = UserAuth.builder().id(1L).build();
User responseUser = User.builder().username(userUpdate.getUsername()).email(userUpdate.getEmail()).build(); User responseUser = User.builder().email(userUpdate.getEmail()).build();
when(userRepository.findById(eq(userAuth.getId()))).thenReturn(Optional.of(responseUser)); when(userRepository.findById(eq(userAuth.getId()))).thenReturn(Optional.of(responseUser));
when(userRepository.save(any(User.class))).thenReturn(responseUser); when(userRepository.save(any(User.class))).thenReturn(responseUser);
UserResponse userResponse = userService.updateUser(userUpdate, userAuth); UserResponse userResponse = userService.updateUser(userUpdate, userAuth);
assertEquals(userUpdate.getUsername(), userResponse.getUsername());
assertEquals(userUpdate.getEmail(), userResponse.getEmail()); assertEquals(userUpdate.getEmail(), userResponse.getEmail());
assertNotNull(userResponse.getEmail()); assertNotNull(userResponse.getEmail());
assertNotNull(userResponse.getUsername());
} }
@@ -179,17 +177,7 @@ class UserServiceImplTest {
when(userRepository.findById(AdditionalMatchers.not(eq(repoUser.getId())))).thenThrow(new CustomException(Error.USER_NOT_FOUND)); when(userRepository.findById(AdditionalMatchers.not(eq(repoUser.getId())))).thenThrow(new CustomException(Error.USER_NOT_FOUND));
// case 2 // case 2
if(userUpdate.getUsername() != null){ if(userUpdate.getEmail() != null){
lenient().when(userRepository.findByUsername(eq(repoUser.getUsername()))).thenThrow(new CustomException(Error.DUPLICATE_USER));
try{
userService.updateUser(userUpdate,userAuth);
}catch(CustomException e){
assertThat(e.getError().equals(Error.DUPLICATE_USER));
assertThat(e.getError().getMessage().equals(Error.DUPLICATE_USER.getMessage()));
}
}
// case 3
else if(userUpdate.getEmail() != null){
lenient().when(userRepository.findByEmail(eq(repoUser.getEmail()))).thenThrow(new CustomException(Error.DUPLICATE_USER)); lenient().when(userRepository.findByEmail(eq(repoUser.getEmail()))).thenThrow(new CustomException(Error.DUPLICATE_USER));
try{ try{
userService.updateUser(userUpdate,userAuth); userService.updateUser(userUpdate,userAuth);
@@ -241,11 +229,11 @@ class UserServiceImplTest {
} }
private static Stream<Arguments> invalidUpdateUsers(){ private static Stream<Arguments> invalidUpdateUsers(){
return Stream.of( return Stream.of(
Arguments.of(UserUpdate.builder().username("update").email("update@gmail.com").id(1L).password("password").build(), Arguments.of(UserUpdate.builder().email("update@gmail.com").id(1L).build(),
UserAuth.builder().id(3L).build()), UserAuth.builder().id(3L).build()),
Arguments.of(UserUpdate.builder().username("update").email("update@gmail.com").id(1L).password("password").build(), Arguments.of(UserUpdate.builder().email("update@gmail.com").id(1L).build(),
UserAuth.builder().id(3L).build()), UserAuth.builder().id(3L).build()),
Arguments.of(UserUpdate.builder().email("update@gmail.com").id(1L).password("password").build(), Arguments.of(UserUpdate.builder().email("update@gmail.com").id(1L).build(),
UserAuth.builder().id(3L).build()) UserAuth.builder().id(3L).build())
); );
} }