Skip to content

Commit

Permalink
fix: fix the getUser phonenumber through Feign rquest
Browse files Browse the repository at this point in the history
  • Loading branch information
JIUNG9 committed Jan 15, 2024
1 parent d77735c commit c5961b7
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,36 @@
import com.bit.lotte.flower.user.social.service.MapAuthIdToUserIdService;
import com.bit.lotte.flower.user.social.service.SocialUserLoginManager;
import com.bit.lotte.flower.user.social.service.SoftDeleteStrategyService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
public class SocialUserFeignController {

private final MapAuthIdToUserIdService<AuthId> mapAuthIdToUserIdService;
private final SoftDeleteStrategyService softDeleteStrategyService;
private final GetUserInfoService<UserId> getUserInfoService;
private final GetUserInfoService<AuthId> getUserInfoService;
private final SocialUserLoginManager socialUserLoginManager;


@Autowired
public SocialUserFeignController(
MapAuthIdToUserIdService<AuthId> mapAuthIdToUserIdService,
SoftDeleteStrategyService softDeleteStrategyService,
@Qualifier("GetUserInfoByOauthId") GetUserInfoService<AuthId> getUserInfoService,
SocialUserLoginManager socialUserLoginManager) {
this.mapAuthIdToUserIdService = mapAuthIdToUserIdService;
this.softDeleteStrategyService = softDeleteStrategyService;
this.getUserInfoService = getUserInfoService;
this.socialUserLoginManager = socialUserLoginManager;
}

@PostMapping("/client/social")
public CommonResponse<UserLoginDataResponse> userLogin(
@RequestBody UserLoginCommand userLoginCommand) {
Expand All @@ -37,7 +50,7 @@ public CommonResponse<UserLoginDataResponse> userLogin(
@GetMapping("/client/users/{userId}/phone-number")
CommonResponse<String> getUserPhoneNumber(@PathVariable Long userId) {
return CommonResponse.success(
getUserInfoService.getUserdata(new UserId(userId)).getPhoneNumber());
getUserInfoService.getUserdata(new AuthId(userId)).getPhoneNumber());
}

@PutMapping("/client/users/{userId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@

@Component
@RequiredArgsConstructor
public class FindSocialUserByLongIdService {
public class FindSocialUserByIdService {

private final SocialUserJpaRepository repository;

public SocialUser findUserElseThrowError(Long id) {
return repository.findById(id).orElseThrow(() -> {
public SocialUser findUserByUserIdElseThrowError(Long userId) {
return repository.findById(userId).orElseThrow(() -> {
throw new SocialUserDomainException("존재하지 않는 회원입니다.");
});

}

public SocialUser findUserByOauthIdElseThrowAnError(Long oauthId) {
return repository.findByOauthId(oauthId).orElseThrow(() -> {
throw new SocialUserDomainException("존재하지 않는 회원입니다.");
});


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
public interface SocialUserJpaRepository extends JpaRepository<SocialUser, Long> {

Optional<SocialUser> findByOauthIdAndIsDeletedFalse(Long oauthId);

List<SocialUser> findAllByOauthId(Long oauthId);
Optional<SocialUser> findByOauthId(Long oauthId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.bit.lotte.flower.user.social.service;

import com.bit.lotte.flower.user.common.valueobject.AuthId;
import com.bit.lotte.flower.user.social.dto.response.UserDataDto;
import com.bit.lotte.flower.user.social.entity.SocialUser;
import com.bit.lotte.flower.user.social.mapper.SocialUserMapper;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByIdService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service("GetUserInfoByOauthId")
public class GetUserInfoByOauthId implements GetUserInfoService<AuthId> {

private final FindSocialUserByIdService findUserByIdService;

@Override
public UserDataDto getUserdata(AuthId id) {
SocialUser socialUser = findUserByIdService.findUserByOauthIdElseThrowAnError(id.getValue());
return SocialUserMapper.socialUserToUserMyPageDataResponse(socialUser);
}
}

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.bit.lotte.flower.user.social.service;

import com.bit.lotte.flower.user.common.valueobject.UserId;
import com.bit.lotte.flower.user.common.valueobject.BaseId;
import com.bit.lotte.flower.user.social.dto.response.UserDataDto;
import org.springframework.stereotype.Service;

@Service
public interface GetUserInfoService<ID extends UserId> {
public interface GetUserInfoService<ID extends BaseId> {
UserDataDto getUserdata(ID id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
import com.bit.lotte.flower.user.social.dto.response.UserDataDto;
import com.bit.lotte.flower.user.social.entity.SocialUser;
import com.bit.lotte.flower.user.social.mapper.SocialUserMapper;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByLongIdService;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByIdService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class GetUserInfoServiceImpl implements
@Service("GetUserInfoServiceByUserId")
public class GetUserInfoServiceByUserId implements
GetUserInfoService<UserId> {

private final FindSocialUserByLongIdService findUserByIdService;
private final FindSocialUserByIdService findUserByIdService;

@Override
public UserDataDto getUserdata(UserId id) {
SocialUser socialUser = findUserByIdService.findUserElseThrowError(id.getValue());
SocialUser socialUser = findUserByIdService.findUserByUserIdElseThrowError(id.getValue());
return SocialUserMapper.socialUserToUserMyPageDataResponse(socialUser);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.bit.lotte.flower.user.social.service;

import com.bit.lotte.flower.user.social.entity.SocialUser;
import com.bit.lotte.flower.user.social.exception.SocialUserDomainException;
import com.bit.lotte.flower.user.social.mapper.SocialUserMapper;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByLongIdService;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByIdService;
import com.bit.lotte.flower.user.social.repository.SocialUserJpaRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -14,12 +13,12 @@
public class SocialUserUpdateServiceImpl implements
SocialUpdateUserService {

private final FindSocialUserByLongIdService findUserByIdService;
private final FindSocialUserByIdService findUserByIdService;
private final SocialUserJpaRepository repository;

@Override
public void updateUserInfo(Long userId, String nickname, String email, String phoneNumber) {
SocialUser socialUser = findUserByIdService.findUserElseThrowError(userId);
SocialUser socialUser = findUserByIdService.findUserByUserIdElseThrowError(userId);
repository.save(
SocialUserMapper.updateUserInfo(socialUser, nickname, email, phoneNumber)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.bit.lotte.flower.user.common.valueobject.UserId;
import com.bit.lotte.flower.user.social.entity.SocialUser;
import com.bit.lotte.flower.user.social.mapper.SocialUserMapper;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByLongIdService;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByIdService;
import com.bit.lotte.flower.user.social.repository.SocialUserJpaRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,12 +13,12 @@
@Service
public class SoftDeleteStrategyService implements UserWithdrawalService<UserId> {

private final FindSocialUserByLongIdService findSocialUserByLongIdService;
private final FindSocialUserByIdService findSocialUserByIdService;
private final SocialUserJpaRepository repository;

@Override
public void userWithdrawal(UserId userId) {
SocialUser socialUser = findSocialUserByLongIdService.findUserElseThrowError(userId.getValue());
SocialUser socialUser = findSocialUserByIdService.findUserByUserIdElseThrowError(userId.getValue());

SocialUser notSoftDeletedUser = getNotSoftDeletedUser(repository
.findAllByOauthId(socialUser.getOauthId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import com.bit.lotte.flower.user.social.entity.SocialUser;
import com.bit.lotte.flower.user.social.exception.SocialUserDomainException;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByLongIdService;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByIdService;
import com.bit.lotte.flower.user.social.repository.SocialUserJpaRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -24,20 +24,20 @@ class UpdateUserServiceTest {
@Mock
private SocialUserJpaRepository socialUserJpaRepository;
@Mock
private FindSocialUserByLongIdService findSocialUserByLongIdService;
private FindSocialUserByIdService findSocialUserByIdService;
private SocialUserUpdateServiceImpl socialUpdateUserService;


@BeforeEach
public void setUp() {
socialUpdateUserService = new SocialUserUpdateServiceImpl(findSocialUserByLongIdService,
socialUpdateUserService = new SocialUserUpdateServiceImpl(findSocialUserByIdService,
socialUserJpaRepository);
}

@DisplayName("유저가 존재하지 않을 때 SocialUserDomainException Throw")
@Test
void UpdateUser_WhenUserIsNotExist_ThrowSocialUserDomainException() {
when(findSocialUserByLongIdService.findUserElseThrowError(1L)).thenThrow(SocialUserDomainException.class);
when(findSocialUserByIdService.findUserByUserIdElseThrowError(1L)).thenThrow(SocialUserDomainException.class);

assertThrows(SocialUserDomainException.class, () -> {
socialUpdateUserService.updateUserInfo(1L, "nickname", "email", "phoneNumber");
Expand All @@ -50,7 +50,7 @@ void UpdateUser_WhenUserIsNotExist_ThrowSocialUserDomainException() {
@Test
void UpdateUser_WhenUserIsExist_SaveHappenedOnce() {
SocialUser socialUser = SocialUser.builder().id(1L).build();
when(findSocialUserByLongIdService.findUserElseThrowError(1L)).thenReturn(socialUser);
when(findSocialUserByIdService.findUserByUserIdElseThrowError(1L)).thenReturn(socialUser);

socialUpdateUserService.updateUserInfo(1L, "nickname", "email", "phoneNumber");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -12,8 +11,7 @@
import com.bit.lotte.flower.user.common.valueobject.UserId;
import com.bit.lotte.flower.user.social.entity.SocialUser;
import com.bit.lotte.flower.user.social.exception.SocialUserDomainException;
import com.bit.lotte.flower.user.social.mapper.SocialUserMapper;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByLongIdService;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByIdService;
import com.bit.lotte.flower.user.social.repository.SocialUserJpaRepository;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -29,21 +27,21 @@ class UserWithdrawalServiceTest {

private SoftDeleteStrategyService softDeleteStrategyService;
@Mock
private FindSocialUserByLongIdService findSocialUserByLongIdService;
private FindSocialUserByIdService findSocialUserByIdService;
@Mock
private SocialUserJpaRepository socialUserJpaRepository;


@BeforeEach
void init() {
softDeleteStrategyService = new SoftDeleteStrategyService(findSocialUserByLongIdService,
softDeleteStrategyService = new SoftDeleteStrategyService(findSocialUserByIdService,
socialUserJpaRepository);
}

@DisplayName("유저 존재하지 않을 때 Throw SocialUserDomainException")
@Test
void UserWithdrawal_WhenUserIsNotExisted_ThrowSocialUserDomainException() {
when(findSocialUserByLongIdService.findUserElseThrowError(1L)).thenThrow(
when(findSocialUserByIdService.findUserByUserIdElseThrowError(1L)).thenThrow(
SocialUserDomainException.class);

assertThrows(SocialUserDomainException.class, () -> {
Expand All @@ -57,7 +55,7 @@ void UserWithdrawal_WhenUserIsExist_UserIsDeletedTrue() {
SocialUser socialUserNotDeleted = SocialUser.builder().isDeleted(false).id(1L).oauthId(1L)
.build();

when(findSocialUserByLongIdService.findUserElseThrowError(1L)).thenReturn(socialUserNotDeleted);
when(findSocialUserByIdService.findUserByUserIdElseThrowError(1L)).thenReturn(socialUserNotDeleted);
when(socialUserJpaRepository.findAllByOauthId(1L)).thenReturn(List.of(socialUserNotDeleted));

softDeleteStrategyService.userWithdrawal(new UserId(1L));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import com.bit.lotte.flower.user.social.entity.SocialUser;
import com.bit.lotte.flower.user.social.exception.SocialUserDomainException;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByLongIdService;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByIdService;
import com.bit.lotte.flower.user.social.repository.SocialUserJpaRepository;
import java.util.Optional;
import org.junit.jupiter.api.Test;
Expand All @@ -18,20 +18,20 @@
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class FindSocialUserByLongIdServiceTest {
public class FindSocialUserByIdServiceTest {


@Mock
SocialUserJpaRepository repository;
@InjectMocks
FindSocialUserByLongIdService findSocialUserByLongIdService;
FindSocialUserByIdService findSocialUserByIdService;

@Test
void FindUser_WhenUserIsExist_GetUser() {
SocialUser mockUser = mock(SocialUser.class);
Mockito.when(repository.findById(anyLong())).thenReturn(Optional.of(mockUser));

SocialUser result = findSocialUserByLongIdService.findUserElseThrowError(1L);
SocialUser result = findSocialUserByIdService.findUserByUserIdElseThrowError(1L);

assertNotNull(result);
}
Expand All @@ -41,7 +41,7 @@ void FindUser_WhenUserIsNotExist_ThrowSocialUserDomainException() {
Mockito.when(repository.findById(anyLong())).thenThrow(SocialUserDomainException.class);

assertThrowsExactly(SocialUserDomainException.class, () -> {
findSocialUserByLongIdService.findUserElseThrowError(1L);
findSocialUserByIdService.findUserByUserIdElseThrowError(1L);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.bit.lotte.flower.user.common.valueobject.UserId;
import com.bit.lotte.flower.user.social.entity.SocialUser;
import com.bit.lotte.flower.user.social.exception.SocialUserDomainException;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByLongIdService;
import com.bit.lotte.flower.user.social.service.GetUserInfoServiceImpl;
import com.bit.lotte.flower.user.social.repository.FindSocialUserByIdService;
import com.bit.lotte.flower.user.social.service.GetUserInfoServiceByUserId;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -23,17 +23,17 @@
class GetUserInfoServiceTest {

@Mock
FindSocialUserByLongIdService findSocialUserByLongIdService;
FindSocialUserByIdService findSocialUserByIdService;
@InjectMocks
GetUserInfoServiceImpl getUserInfoService;
GetUserInfoServiceByUserId getUserInfoService;


@DisplayName("유저가 존재할 때 UserDataDto 반환")
@Test
void UserDataServiceTest_WhenUserIsExist_GetUserDto() {
SocialUser mockUser = mock(SocialUser.class);

when(findSocialUserByLongIdService.findUserElseThrowError(anyLong())).thenReturn(mockUser);
when(findSocialUserByIdService.findUserByUserIdElseThrowError(anyLong())).thenReturn(mockUser);

assertDoesNotThrow(()->{
getUserInfoService.getUserdata(new UserId(mockUser.getId()));
Expand All @@ -45,7 +45,7 @@ void UserDataServiceTest_WhenUserIsExist_GetUserDto() {
@DisplayName("유저가 존재하지 않을 때 Throw SocialUserException")
@Test
void UserDataServiceTest_WhenUserIsNotExist_ThrowSocialUserException() {
Mockito.when(findSocialUserByLongIdService.findUserElseThrowError(anyLong()))
Mockito.when(findSocialUserByIdService.findUserByUserIdElseThrowError(anyLong()))
.thenThrow(SocialUserDomainException.class);

assertThrowsExactly(SocialUserDomainException.class, () -> {
Expand Down

0 comments on commit c5961b7

Please sign in to comment.