From 44d1a0089a5bdd24021a5309b8846332ee933763 Mon Sep 17 00:00:00 2001 From: kimyu0218 Date: Thu, 19 Dec 2024 13:57:28 +0900 Subject: [PATCH] =?UTF-8?q?test:=20MissionRetryMessageRepository=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...MissionRetryMessageRepositoryImplTest.java | 169 +++++++++++------- 1 file changed, 103 insertions(+), 66 deletions(-) diff --git a/src/test/java/com/nexters/goalpanzi/infrastructure/mission/MissionRetryMessageRepositoryImplTest.java b/src/test/java/com/nexters/goalpanzi/infrastructure/mission/MissionRetryMessageRepositoryImplTest.java index ba046530..8cb9b025 100644 --- a/src/test/java/com/nexters/goalpanzi/infrastructure/mission/MissionRetryMessageRepositoryImplTest.java +++ b/src/test/java/com/nexters/goalpanzi/infrastructure/mission/MissionRetryMessageRepositoryImplTest.java @@ -1,66 +1,103 @@ -//package com.nexters.goalpanzi.infrastructure.mission; -// -//import com.nexters.goalpanzi.domain.mission.repository.MissionRetryMessageRepository; -//import org.junit.jupiter.api.BeforeEach; -//import org.junit.jupiter.api.Test; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.boot.test.autoconfigure.data.redis.DataRedisTest; -//import org.springframework.boot.test.context.TestConfiguration; -//import org.springframework.context.annotation.Bean; -//import org.springframework.data.redis.core.RedisTemplate; -// -//import java.time.LocalDate; -//import java.util.Set; -// -//import static org.assertj.core.api.Assertions.assertThat; -// -//@DataRedisTest -//class MissionRetryMessageRepositoryImplTest { -// -// @Autowired -// private MissionRetryMessageRepository missionRetryMessageRepository; -// -// @Autowired -// private RedisTemplate redisTemplate; -// -// @TestConfiguration -// static class RedisTestConfig { -// @Bean -// public MissionRetryMessageRepository missionRetryMessageRepository(RedisTemplate redisTemplate) { -// return new MissionRetryMessageRepositoryImpl(redisTemplate); -// } -// } -// -// @BeforeEach -// public void setUp() { -// redisTemplate.getConnectionFactory().getConnection().serverCommands().flushAll(); -// } -// -// @Test -// void memberId로_key를_만들고_key에_해당하는_device_token을_조회한다() { -// missionRetryMessageRepository.save("memberId", "deviceToken", 6000); -// -// String foundToken = missionRetryMessageRepository.find("memberId"); -// -// assertThat(foundToken).isEqualTo("deviceToken"); -// } -// -// @Test -// void key에_7일_후의_날짜가_포함된_key_집합을_조회한다() { -// missionRetryMessageRepository.save("memberId1", "deviceToken", 6000); -// missionRetryMessageRepository.save("memberId2", "deviceToken", 6000); -// -// Set keys = missionRetryMessageRepository.keys(LocalDate.now().plusDays(7)); -// -// assertThat(keys.size()).isEqualTo(2); -// } -// -// @Test -// void memberId로_key를_만들고_key에_해당하는_device_token을_삭제한다() { -// missionRetryMessageRepository.save("memberId", "deviceToken", 6000); -// -// Boolean deleteResult = missionRetryMessageRepository.delete("memberId"); -// -// assertThat(deleteResult).isTrue(); -// } -//} \ No newline at end of file +package com.nexters.goalpanzi.infrastructure.mission; + +import com.nexters.goalpanzi.config.redis.RedisInitializer; +import com.nexters.goalpanzi.domain.mission.repository.MissionRetryMessageRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.data.redis.DataRedisTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.test.context.ContextConfiguration; + +import java.time.LocalDate; +import java.util.List; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +@DataRedisTest +@ContextConfiguration( + initializers = {RedisInitializer.class} +) +class MissionRetryMessageRepositoryImplTest { + + @Autowired + private MissionRetryMessageRepository missionRetryMessageRepository; + + @Autowired + private RedisTemplate redisTemplate; + + @TestConfiguration + static class RedisTestConfig { + @Bean + public MissionRetryMessageRepository missionRetryMessageRepository(RedisTemplate redisTemplate) { + return new MissionRetryMessageRepositoryImpl(redisTemplate); + } + } + + @BeforeEach + public void setUp() { + redisTemplate.getConnectionFactory().getConnection().serverCommands().flushAll(); + } + + @Test + void 특정_멤버와_관련있는_device_token을_모두_조회한다() { + missionRetryMessageRepository.save("memberId", "deviceToken1", 6000); + missionRetryMessageRepository.save("memberId", "deviceToken2", 6000); + + List tokens = missionRetryMessageRepository.findAllByMemberId("memberId"); + + assertAll( + () -> assertThat(tokens.size()).isEqualTo(2), + () -> assertThat(tokens).containsExactlyInAnyOrder("deviceToken1", "deviceToken2") + ); + } + + @Test + void 특정_디바이스와_관련있는_device_token을_모두_조회한다() { + missionRetryMessageRepository.save("memberId1", "deviceToken", 6000); + missionRetryMessageRepository.save("memberId2", "deviceToken", 6000); + + List tokens = missionRetryMessageRepository.findAllByDeviceToken("deviceToken"); + + assertAll( + () -> assertThat(tokens.size()).isEqualTo(2), + () -> assertThat(tokens).allMatch(it -> it.equals("deviceToken")) + ); + } + + @Test + void key에_7일_후의_날짜가_포함된_key_집합을_조회한다() { + missionRetryMessageRepository.save("memberId1", "deviceToken", 6000); + missionRetryMessageRepository.save("memberId2", "deviceToken", 6000); + + Set keys = missionRetryMessageRepository.keys(LocalDate.now().plusDays(7)); + + assertThat(keys.size()).isEqualTo(2); + } + + @Test + void 특정_멤버와_관련있는_device_token을_모두_삭제한다() { + missionRetryMessageRepository.save("memberId1", "deviceToken1", 6000); + missionRetryMessageRepository.save("memberId1", "deviceToken2", 6000); + missionRetryMessageRepository.save("memberId2", "deviceToken2", 6000); + + Long deletions = missionRetryMessageRepository.deleteAllByMemberId("memberId1"); + + assertThat(deletions).isEqualTo(2); + } + + @Test + void 특정_디바이스와_관련있는_device_token을_모두_삭제한다() { + missionRetryMessageRepository.save("memberId1", "deviceToken1", 6000); + missionRetryMessageRepository.save("memberId1", "deviceToken2", 6000); + missionRetryMessageRepository.save("memberId2", "deviceToken2", 6000); + + Long deletions = missionRetryMessageRepository.deleteAllByDeviceToken("deviceToken1"); + + assertThat(deletions).isEqualTo(1); + } +} \ No newline at end of file