Skip to content

Commit

Permalink
refactor: #73 service 코드 로직 수정 및 불필요한 코드 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
tinon1004 committed Mar 24, 2024
1 parent 484f529 commit d09adb5
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 140 deletions.
4 changes: 0 additions & 4 deletions src/main/java/com/ajou/hertz/common/file/dto/FileDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,4 @@ public static FileDto create(
) {
return new FileDto(originalName, storedName, url);
}

public String getStoredFileUrl() {
return url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.springframework.web.multipart.MultipartFile;

import com.ajou.hertz.common.auth.UserPrincipal;
import com.ajou.hertz.common.file.dto.FileDto;
import com.ajou.hertz.common.validator.PhoneNumber;
import com.ajou.hertz.domain.user.dto.UserDto;
import com.ajou.hertz.domain.user.dto.request.SignUpRequest;
Expand Down Expand Up @@ -137,9 +136,7 @@ public UserResponse updateProfileImageV1(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@RequestPart("profileImage") MultipartFile profileImage
) {
FileDto uploadedFile = userProfileImageCommandService.uploadProfileImage(userPrincipal.getUserId(),
profileImage);
UserDto userUpdated = userCommandService.updateProfileImage(userPrincipal.getUserId(), uploadedFile);
UserDto userUpdated = userCommandService.updateUserProfileImage(userPrincipal.getUserId(), profileImage);
return UserResponse.from(userUpdated);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.ajou.hertz.common.file.dto.FileDto;
import com.ajou.hertz.common.kakao.dto.response.KakaoUserInfoResponse;
import com.ajou.hertz.common.properties.HertzProperties;
import com.ajou.hertz.domain.user.constant.Gender;
Expand All @@ -32,6 +32,7 @@ public class UserCommandService {
private final PasswordEncoder passwordEncoder;
private final HertzProperties hertzProperties;
private final UserProfileImageRepository userProfileImageRepository;
private final UserProfileImageCommandService userProfileImageCommandService;

/**
* 새로운 회원을 등록한다.
Expand Down Expand Up @@ -134,18 +135,15 @@ private String generateRandom16CharString() {
}

/**
* 전달된 이미지로 프로필 이미지 url을 변경한다.
* 유저의 프로필 이미지를 업데이트합니다.
*
* @param userId 유저의 ID
* @param uploadedFile 변경할 이미지 파일
* @param profileImage 변경할 프로필 이미지
*
* @return 변경된 유저 정보
*/
public UserDto updateProfileImage(Long userId, FileDto uploadedFile) {
User user = userQueryService.getById(userId);
String newProfileImageUrl = uploadedFile.getStoredFileUrl();
user.changeProfileImageUrl(newProfileImageUrl);
return UserDto.from(user);
public UserDto updateUserProfileImage(Long userId, MultipartFile profileImage) {
return userProfileImageCommandService.updateProfileImage(userId, profileImage);
}

/**
Expand All @@ -154,7 +152,7 @@ public UserDto updateProfileImage(Long userId, FileDto uploadedFile) {
* @param userId 유저의 ID
* @param contactLink 변경할 연락 수단
*
*@return 변경된 유저 정보
* @return 변경된 유저 정보
*/
public UserDto updateContactLink(Long userId, String contactLink) {
User user = userQueryService.getById(userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.ajou.hertz.common.file.dto.FileDto;
import com.ajou.hertz.common.file.service.FileService;
import com.ajou.hertz.domain.user.dto.UserDto;
import com.ajou.hertz.domain.user.entity.User;
import com.ajou.hertz.domain.user.entity.UserProfileImage;
import com.ajou.hertz.domain.user.repository.UserProfileImageRepository;
Expand All @@ -29,26 +30,28 @@ public class UserProfileImageCommandService {
* @param userId 유저 id
* @param newProfileImage 새로운 프로필 이미지
*
* @return 업로드된 파일 정보가 담긴 dto
* @return 업데이트된 유저 정보
*/
public FileDto uploadProfileImage(Long userId, MultipartFile newProfileImage) {
User user = userQueryService.getById(userId);
public UserDto updateProfileImage(Long userId, MultipartFile newProfileImage) {

User user = userQueryService.getById(userId);
Optional<UserProfileImage> optionalOldProfileImage = userProfileImageRepository.findById(userId);

String uploadPath = "user-profile-images/";
FileDto uploadedFile = fileService.uploadFile(newProfileImage, uploadPath);
String newProfileImageUrl = uploadedFile.getUrl();
if (optionalOldProfileImage.isPresent()) {
UserProfileImage oldProfileImage = optionalOldProfileImage.get();
userProfileImageRepository.delete(oldProfileImage);
userProfileImageRepository.flush();
fileService.deleteFile(oldProfileImage.getStoredName());
}

String uploadPath = "user-profile-images/";
FileDto uploadedFile = fileService.uploadFile(newProfileImage, uploadPath);

UserProfileImage newUserProfileImage = UserProfileImage.create(
user, uploadedFile.getOriginalName(), uploadedFile.getStoredName(), uploadedFile.getStoredFileUrl());
UserProfileImage newUserProfileImage = UserProfileImage.create(user, uploadedFile.getOriginalName(),
uploadedFile.getStoredName(), newProfileImageUrl);
userProfileImageRepository.save(newUserProfileImage);

return uploadedFile;
user.changeProfileImageUrl(newProfileImageUrl);
return UserDto.from(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.ajou.hertz.common.auth.JwtTokenProvider;
import com.ajou.hertz.common.auth.UserPrincipal;
import com.ajou.hertz.common.config.SecurityConfig;
import com.ajou.hertz.common.file.dto.FileDto;
import com.ajou.hertz.domain.user.constant.Gender;
import com.ajou.hertz.domain.user.constant.RoleType;
import com.ajou.hertz.domain.user.controller.UserController;
Expand Down Expand Up @@ -219,7 +218,7 @@ public void securitySetUp() throws Exception {
}

@Test
void 주어진_id와_새로운_프로필_이미지로_프로필_이미지를_변경한다() throws Exception {
void 주어진_id와_변경할_프로필_이미지로_프로필_이미지를_변경한다() throws Exception {
// given
long userId = 1L;
MockMultipartFile profileImage = new MockMultipartFile(
Expand All @@ -229,12 +228,9 @@ public void securitySetUp() throws Exception {
"test".getBytes()
);
UserDetails userDetails = createTestUser(userId);
FileDto uploadedFile = createFileDto();
UserDto expectedResult = createUserDto(userId);

given(userProfileImageCommandService.uploadProfileImage(userId, profileImage)).willReturn(
uploadedFile);
given(userCommandService.updateProfileImage(userId, uploadedFile)).willReturn(expectedResult);
given(userCommandService.updateUserProfileImage(userId, profileImage)).willReturn(expectedResult);

// when & then
mvc.perform(
Expand All @@ -249,8 +245,7 @@ public void securitySetUp() throws Exception {
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.profileImageUrl").value(expectedResult.getProfileImageUrl()));
then(userProfileImageCommandService).should().uploadProfileImage(userId, profileImage);
then(userCommandService).should().updateProfileImage(userId, uploadedFile);
then(userCommandService).should().updateUserProfileImage(userId, profileImage);
verifyEveryMocksShouldHaveNoMoreInteractions();
}

Expand Down Expand Up @@ -324,12 +319,4 @@ private UserDto createUserDto() throws Exception {
private UserDetails createTestUser(Long userId) throws Exception {
return new UserPrincipal(createUserDto(userId));
}

private FileDto createFileDto() throws Exception {
return ReflectionUtils.createFileDto(
"test.jpg",
"test-stored.jpg",
"https://example.com/user-profile-images/storedFileName.jpg");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.event.annotation.BeforeTestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.ajou.hertz.common.file.dto.FileDto;
import com.ajou.hertz.common.file.service.FileService;
Expand All @@ -35,6 +36,7 @@
import com.ajou.hertz.domain.user.exception.UserPhoneDuplicationException;
import com.ajou.hertz.domain.user.repository.UserRepository;
import com.ajou.hertz.domain.user.service.UserCommandService;
import com.ajou.hertz.domain.user.service.UserProfileImageCommandService;
import com.ajou.hertz.domain.user.service.UserQueryService;
import com.ajou.hertz.util.ReflectionUtils;

Expand All @@ -60,6 +62,9 @@ class UserCommandServiceTest {
@Mock
private FileService fileService;

@Mock
private UserProfileImageCommandService userProfileImageCommandService;

@BeforeTestMethod
public void setUp() {
given(hertzProperties.userDefaultProfileImageUrl()).willReturn("https://user-default-profile-image");
Expand Down Expand Up @@ -177,84 +182,23 @@ static Stream<Arguments> testDataForCreateNewUserWithKakao() throws Exception {
assertThat(t).isInstanceOf(UserKakaoUidDuplicationException.class);
}

// @Test
// void 주어진_id와_새_프로필_이미지로_프로필_이미지를_변경하고_이전_프로필_이미지는_삭제한다() throws Exception {
// Long userId = 1L;
// MockMultipartFile newProfileImage = new MockMultipartFile(
// "profileImage",
// "test.jpg",
// "image/jpeg",
// "test".getBytes()
// );
// User user = createUser(userId, "password", null, Gender.MALE);
// given(userQueryService.getById(userId)).willReturn(user);
// given(fileService.uploadFile(any(), anyString())).willReturn(
// FileDto.create("new_profile_image.jpg", "new_profile_image.jpg",
// "https://example.com/user-profile-images"));
//
// // When
// UserDto updatedUser = sut.updateProfileImageUrl(userId, newProfileImage);
//
// // Then
// then(userQueryService).should().getById(userId);
// then(fileService).should().uploadFile(eq(newProfileImage), anyString());
// then(fileService).should().deleteFile(anyString());
// verifyEveryMocksShouldHaveNoMoreInteractions();
// assertThat(updatedUser).hasFieldOrPropertyWithValue("profileImageUrl", user.getProfileImageUrl());
// }
//
// @Test
// void 주어진_유저_ID와_새로운_프로필_이미지로_기존의_프로필_이미지를_변경한다_존재하지_않는_유저라면_예외가_발생한다() throws Exception {
// // given
// Long userId = 1L;
// MockMultipartFile newProfileImage = new MockMultipartFile(
// "profileImage",
// "test.jpg",
// "image/jpeg",
// "test".getBytes()
// );
// given(userQueryService.getById(userId)).willThrow(UserNotFoundByIdException.class);
//
// // when
// Throwable t = catchThrowable(() -> sut.updateProfileImageUrl(userId, newProfileImage));
//
// // then
// then(userQueryService).should().getById(userId);
// verifyEveryMocksShouldHaveNoMoreInteractions();
// assertThat(t).isInstanceOf(UserNotFoundByIdException.class);
// }

@Test
void 주어진_유저_ID와_저장된_이미지로_프로필_이미지_URL을_변경한다() throws Exception {
// given
void 프로필_이미지_변경_시_uploadProfileImage가_잘_호출되는지_확인한다() throws Exception {
// Given
Long userId = 1L;
User user = createUser(userId, "$2a$abc123", "12345");
FileDto uploadedFile = createFileDto();
given(userQueryService.getById(userId)).willReturn(user);
MultipartFile profileImage = org.mockito.Mockito.mock(MultipartFile.class);
User testUser = createUser(userId, "password", "kakaoUid");
UserDto expectedUserDto = UserDto.from(testUser);
given(userProfileImageCommandService.updateProfileImage(userId, profileImage)).willReturn(expectedUserDto);

// when
UserDto updatedUserDto = sut.updateProfileImage(userId, uploadedFile);
// When
UserDto result = sut.updateUserProfileImage(userId, profileImage);

// then
then(userQueryService).should().getById(userId);
verifyEveryMocksShouldHaveNoMoreInteractions();
assertThat(updatedUserDto.getProfileImageUrl()).isEqualTo(uploadedFile.getUrl());
}
// Then
then(userProfileImageCommandService).should().updateProfileImage(userId, profileImage);
assertThat(result).isEqualTo(expectedUserDto);

@Test
void 주어진_유저_ID와_저장된_이미지로_프로필_이미지를_변경한다_존재하지_않는_유저라면_예외가_발생한다() throws Exception {
// given
Long userId = 1L;
FileDto uploadedFile = createFileDto();
given(userQueryService.getById(userId)).willThrow(UserNotFoundByIdException.class);

// when
Throwable t = catchThrowable(() -> sut.updateProfileImage(userId, uploadedFile));

// then
then(userQueryService).should().getById(userId);
verifyEveryMocksShouldHaveNoMoreInteractions();
assertThat(t).isInstanceOf(UserNotFoundByIdException.class);
}

@Test
Expand Down Expand Up @@ -295,6 +239,7 @@ private void verifyEveryMocksShouldHaveNoMoreInteractions() {
then(userRepository).shouldHaveNoMoreInteractions();
then(passwordEncoder).shouldHaveNoMoreInteractions();
then(fileService).shouldHaveNoMoreInteractions();
then(userProfileImageCommandService).shouldHaveNoMoreInteractions();
}

private static User createUser(Long id, String password, String kakaoUid, Gender gender) throws Exception {
Expand Down
Loading

0 comments on commit d09adb5

Please sign in to comment.