Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

[BM-142] 회원 정보 수정 Service 메서드 구현 #37

Merged
merged 3 commits into from
Jul 31, 2022
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
8 changes: 8 additions & 0 deletions src/main/java/com/saiko/bidmarket/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public User(String username, String profileImage, String provider, String provid
this.group = group;
}

public void update(String username, String profileImage) {
Assert.notNull(username, "username must be provide");
Assert.notNull(profileImage, "profileImage must be provide");

Comment on lines +66 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용자의 profile image가 없는 경우는 없나요??
실제로 없는 경우에는 어떻게 처리하면 좋을까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

구글 OAuth 로그인시 구글 프로필 사진을 유저 정보에 저장합니다. 프론트에서 요청을 보낼때 기존 데이터를 보내주면 될것 같습니다.

this.username = username;
this.profileImage = profileImage;
}

public Long getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public User findByProviderAndProviderId(String provider, String providerId) {
}

@Override
@Transactional
public User join(OAuth2User oAuth2User, String authorizedClientRegistrationId) {
Assert.notNull(oAuth2User, "OAuth2User must be provided");
Assert.hasText(authorizedClientRegistrationId,
Expand Down Expand Up @@ -77,6 +76,9 @@ public User findById(long id) {

@Override
public void updateUser(long id, UserUpdateRequest request) {
Assert.notNull(request, "request must be provide");

final User user = findById(id);
user.update(request.getUsername(), request.getProfileImageUrl());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.test.util.ReflectionTestUtils;

import com.saiko.bidmarket.common.exception.NotFoundException;
import com.saiko.bidmarket.user.controller.dto.UserUpdateRequest;
import com.saiko.bidmarket.user.entity.Group;
import com.saiko.bidmarket.user.entity.User;
import com.saiko.bidmarket.user.repository.UserRepository;
Expand Down Expand Up @@ -228,7 +229,7 @@ void itReturnExistUser() {
"test",
"test",
new Group()
);
);

//when
when(userRepository.findById(existUserId)).thenReturn(Optional.of(existUser));
Expand All @@ -241,4 +242,79 @@ void itReturnExistUser() {
}
}

@Nested
@DisplayName("updateUser 메서드는")
class DescribeUpdateUser {

@Nested
@DisplayName("null 값인 userUpdateRequest가 인자로 들어오면")
class ContextReceiveNullUserUpdateRequest {

@Test
@DisplayName("IllegalArgumentException을 반환한다.")
void itThrowIllegalArgumentException() {
//given
final long userId = 1;
final UserUpdateRequest request = null;

//then
Assertions.assertThatThrownBy(() -> defaultUserService.updateUser(userId, request))
.isInstanceOf(IllegalArgumentException.class);
}
}

@Nested
@DisplayName("존재하지 않는 유저의 id값이 들어오면")
class ContextReceiveNotExistUserId {

@Test
@DisplayName("NotFoundException을 반환한다.")
void itThrowNotFoundException() {
//given
final long userId = 1;
final UserUpdateRequest request = new UserUpdateRequest("test", "test");

//when
when(userRepository.findById(userId)).thenReturn(Optional.empty());

//then
Assertions.assertThatThrownBy(() -> defaultUserService.updateUser(userId, request))
.isInstanceOf(NotFoundException.class);
}
}

@Nested
@DisplayName("존재하는 유저와 유효한 userUpdateRequest를 인자로 받으면")
class ContextReceiveValidUserAndUpdateRequest {

@Test
@DisplayName("해당 유저 정보를 update한다.")
void itUpdateUser() {
//given
final long userId = 1;
final UserUpdateRequest request = new UserUpdateRequest("update", "update");
final User targetUser = new User("before",
"before",
"provider",
"providerId",
new Group());

ReflectionTestUtils.setField(targetUser, "id", 1L);

//when
when(userRepository.findById(userId)).thenReturn(Optional.of(targetUser));

defaultUserService.updateUser(userId, request);

final String expected = "update";
final String actualUsername = targetUser.getUsername();
final String actualProfileImage = targetUser.getProfileImage();

//then
Assertions.assertThat(actualUsername).isEqualTo(expected);
Assertions.assertThat(actualProfileImage).isEqualTo(expected);
}
}
}

}