-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from SWM-WeLike2Coding/test/userInfo
test: UserInfoRedisRepository에 대한 테스트 코드 작성
- Loading branch information
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
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
65 changes: 65 additions & 0 deletions
65
...com/wl2c/elswhereuserservice/domain/user/repository/impl/UserInfoRedisRepositoryTest.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,65 @@ | ||
package com.wl2c.elswhereuserservice.domain.user.repository.impl; | ||
|
||
import com.wl2c.elswhereuserservice.domain.user.model.UserInfo; | ||
import com.wl2c.elswhereuserservice.mock.UserInfoMock; | ||
import com.wl2c.elswhereuserservice.util.base.AbstractContainerRedisTest; | ||
import com.wl2c.elswhereuserservice.util.test.FullIntegrationTest; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.time.Instant; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest | ||
@FullIntegrationTest | ||
class UserInfoRedisRepositoryTest extends AbstractContainerRedisTest { | ||
|
||
@Autowired | ||
private UserInfoRedisRepository repository; | ||
|
||
private final Instant now = Instant.ofEpochSecond(123456789); | ||
|
||
|
||
@Test | ||
@DisplayName("유저 정보 저장 및 반환") | ||
void saveAndGetUserInfo() { | ||
// given | ||
UserInfo userInfo = UserInfoMock.create(); | ||
repository.setUserInfo(5L, userInfo, now); | ||
|
||
// when | ||
Optional<UserInfo> info = repository.getUserInfo(5L, now); | ||
|
||
// then | ||
assertThat(info.orElseThrow()).isEqualTo(userInfo); | ||
} | ||
|
||
@Test | ||
@DisplayName("유저 정보가 없으면 null 반환") | ||
void getUserInfoEmpty() { | ||
// when | ||
Optional<UserInfo> info = repository.getUserInfo(5L, now); | ||
|
||
// then | ||
assertThat(info).isEmpty(); | ||
} | ||
|
||
@Test | ||
@DisplayName("유저 정보 캐시 삭제") | ||
void removeUserInfo() { | ||
// given | ||
UserInfo userInfo = UserInfoMock.create(); | ||
repository.setUserInfo(5L, userInfo, now); | ||
|
||
// when | ||
repository.removeUserInfo(5L); | ||
|
||
// then | ||
Optional<UserInfo> info = repository.getUserInfo(5L, now); | ||
assertThat(info).isEmpty(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/com/wl2c/elswhereuserservice/mock/UserInfoMock.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,28 @@ | ||
package com.wl2c.elswhereuserservice.mock; | ||
|
||
import com.wl2c.elswhereuserservice.domain.user.model.SocialType; | ||
import com.wl2c.elswhereuserservice.domain.user.model.UserInfo; | ||
import com.wl2c.elswhereuserservice.domain.user.model.UserStatus; | ||
import com.wl2c.elswhereuserservice.domain.user.model.entity.User; | ||
import com.wl2c.elswhereuserservice.global.auth.role.UserRole; | ||
|
||
public class UserInfoMock { | ||
|
||
public static UserInfo create() { | ||
return new UserInfo(UserMock.createDummy()); | ||
} | ||
|
||
public static UserInfo create(String academicStatus) { | ||
User user = User.builder() | ||
.socialId("12345678") | ||
.socialType(SocialType.GOOGLE) | ||
.email("[email protected]") | ||
.name("name") | ||
.nickname("nickname") | ||
.userStatus(UserStatus.ACTIVE) | ||
.userRole(UserRole.USER) | ||
.build(); | ||
|
||
return new UserInfo(user); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -12,6 +12,17 @@ public class UserMock { | |
public static final String SOCIAL_ID = "12345678"; | ||
public static final String NAME = "username"; | ||
public static final String NICKNAME = "nickname"; | ||
public static final String EMAIL = "[email protected]"; | ||
public static final String SOCIAL_TYPE = String.valueOf(SocialType.GOOGLE); | ||
|
||
public static User createDummy() { | ||
return createDummy(RandomGen.nextLong()); | ||
} | ||
|
||
public static User createDummy(Long userId) { | ||
return create(userId, NAME, SocialType.valueOf(SOCIAL_TYPE), EMAIL, UserRole.USER); | ||
} | ||
|
||
|
||
public static User create(SocialType socialType, String email) { | ||
return create(RandomGen.nextLong(), NAME, socialType, email, UserRole.USER); | ||
|
41 changes: 41 additions & 0 deletions
41
src/test/java/com/wl2c/elswhereuserservice/util/base/AbstractContainerRedisTest.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,41 @@ | ||
package com.wl2c.elswhereuserservice.util.base; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
import java.util.Set; | ||
|
||
public abstract class AbstractContainerRedisTest { | ||
static final String REDIS_IMAGE = "redis:6-alpine"; | ||
static final GenericContainer REDIS_CONTAINER; | ||
|
||
@Autowired | ||
private StringRedisTemplate redisTemplate; | ||
|
||
static { | ||
REDIS_CONTAINER = new GenericContainer<>(REDIS_IMAGE) | ||
.withExposedPorts(6379) | ||
.withReuse(true); | ||
REDIS_CONTAINER.start(); | ||
} | ||
|
||
@BeforeEach | ||
void clearAll() { | ||
Set<String> keys = redisTemplate.keys("*"); | ||
if (keys != null) { | ||
for (String key : keys) { | ||
redisTemplate.delete(key); | ||
} | ||
} | ||
} | ||
|
||
@DynamicPropertySource | ||
public static void overrideProps(DynamicPropertyRegistry registry) { | ||
registry.add("spring.redis.host", REDIS_CONTAINER::getHost); | ||
registry.add("spring.redis.port", () -> String.valueOf(REDIS_CONTAINER.getMappedPort(6379))); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/test/java/com/wl2c/elswhereuserservice/util/test/FullIntegrationTest.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,16 @@ | ||
package com.wl2c.elswhereuserservice.util.test; | ||
|
||
import org.junit.jupiter.api.condition.EnabledIfSystemProperty; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* local환경에서만 실행되는 테스트임을 명시합니다. 이 annotation이 붙어있으면, Build/CI/CD시에 테스트가 수행되지 않습니다. | ||
* 통합 테스트는 local에서만 테스트합니다. (매번 SpringBootTest하기에는 속도가 너무 느리고 환경 구성의 어려움) | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@EnabledIfSystemProperty(named = "spring.profiles.active", matches = "local") | ||
public @interface FullIntegrationTest { | ||
} |