-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
src/test/java/com/dnd/accompany/domain/user/service/UserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.dnd.accompany.domain.user.service; | ||
|
||
import com.dnd.accompany.domain.auth.dto.AuthUserInfo; | ||
import com.dnd.accompany.domain.auth.oauth.dto.OAuthUserDataResponse; | ||
import com.dnd.accompany.domain.auth.oauth.dto.OAuthUserInfo; | ||
import com.dnd.accompany.domain.user.entity.User; | ||
import com.dnd.accompany.domain.user.infrastructure.UserRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class UserServiceTest { | ||
|
||
@Mock | ||
private UserRepository userRepository; | ||
|
||
@InjectMocks | ||
private UserService userService; | ||
|
||
private OAuthUserDataResponse oAuthUserDataResponse; | ||
|
||
private OAuthUserInfo oauthUserInfo; | ||
|
||
@BeforeEach | ||
void setup() { | ||
oAuthUserDataResponse = OAuthUserDataResponse.builder() | ||
.provider("KAKAO") | ||
.nickname("TESTER1") | ||
.oauthId("KA-123") | ||
.email("[email protected]") | ||
.build(); | ||
|
||
oauthUserInfo = OAuthUserInfo.from(oAuthUserDataResponse); | ||
} | ||
|
||
@Test | ||
@DisplayName("getOrRegister 호출 시 User 정보가 없다면 유저 데이터를 저장한다.") | ||
void success() { | ||
//given | ||
User newUser = User.of( | ||
oauthUserInfo.getEmail(), | ||
oauthUserInfo.getNickname(), | ||
oauthUserInfo.getProvider(), | ||
oauthUserInfo.getOauthId() | ||
); | ||
|
||
ReflectionTestUtils.setField(newUser, "id", 1L); | ||
|
||
when(userRepository.findUserByProviderAndOauthId(oauthUserInfo.getProvider(), oauthUserInfo.getOauthId())).thenReturn(Optional.empty()); | ||
when(userRepository.save(any(User.class))).thenReturn(newUser); | ||
|
||
//when | ||
AuthUserInfo result = userService.getOrRegister(oauthUserInfo); | ||
|
||
//then | ||
verify(userRepository).save(any(User.class)); // Ensure save was called | ||
assertEquals(newUser.getId(), result.getUserId()); | ||
} | ||
|
||
@Test | ||
@DisplayName("getOrRegister 호출 시 User 정보가 있다면 유저 데이터를 저장하지 않는다.") | ||
void success2() { | ||
//given | ||
User existingUser = User.of( | ||
oauthUserInfo.getEmail(), | ||
oauthUserInfo.getNickname(), | ||
oauthUserInfo.getProvider(), | ||
oauthUserInfo.getOauthId() | ||
); | ||
|
||
ReflectionTestUtils.setField(existingUser, "id", 1L); | ||
|
||
when(userRepository.findUserByProviderAndOauthId(oauthUserInfo.getProvider(), oauthUserInfo.getOauthId())) | ||
.thenReturn(Optional.of(existingUser)); | ||
|
||
//when | ||
AuthUserInfo result = userService.getOrRegister(oauthUserInfo); | ||
|
||
//then | ||
verify(userRepository, never()).save(any(User.class)); | ||
assertEquals(existingUser.getId(), result.getUserId()); | ||
} | ||
} |