diff --git a/src/main/java/com/somemore/domains/volunteerrecord/repository/VolunteerRankingRedisRepository.java b/src/main/java/com/somemore/domains/volunteerrecord/repository/VolunteerRankingRedisRepository.java index 474804e82..cd47366bf 100644 --- a/src/main/java/com/somemore/domains/volunteerrecord/repository/VolunteerRankingRedisRepository.java +++ b/src/main/java/com/somemore/domains/volunteerrecord/repository/VolunteerRankingRedisRepository.java @@ -1,5 +1,8 @@ package com.somemore.domains.volunteerrecord.repository; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; import com.somemore.domains.volunteerrecord.dto.response.VolunteerMonthlyRankingResponseDto; import com.somemore.domains.volunteerrecord.dto.response.VolunteerRankingResponseDto; import com.somemore.domains.volunteerrecord.dto.response.VolunteerTotalRankingResponseDto; @@ -31,13 +34,29 @@ public void saveRanking(VolunteerRankingResponseDto rankings) { @SuppressWarnings("unchecked") public Optional getRankings() { + ObjectMapper mapper = new ObjectMapper(); + + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); List totalRanking = - (List) redisTemplate.opsForValue().get(TOTAL_RANKING_KEY); + (List) Optional.ofNullable(redisTemplate.opsForValue().get(TOTAL_RANKING_KEY)) + .map(obj -> mapper.convertValue(obj, + mapper.getTypeFactory().constructCollectionType(List.class, VolunteerTotalRankingResponseDto.class))) + .orElse(null); + List monthlyRanking = - (List) redisTemplate.opsForValue().get(MONTHLY_RANKING_KEY); + (List) Optional.ofNullable(redisTemplate.opsForValue().get(MONTHLY_RANKING_KEY)) + .map(obj -> mapper.convertValue(obj, + mapper.getTypeFactory().constructCollectionType(List.class, VolunteerMonthlyRankingResponseDto.class))) + .orElse(null); + List weeklyRanking = - (List) redisTemplate.opsForValue().get(WEEKLY_RANKING_KEY); + (List) Optional.ofNullable(redisTemplate.opsForValue().get(WEEKLY_RANKING_KEY)) + .map(obj -> mapper.convertValue(obj, + mapper.getTypeFactory().constructCollectionType(List.class, VolunteerWeeklyRankingResponseDto.class))) + .orElse(null); if (totalRanking == null || monthlyRanking == null || weeklyRanking == null) { return Optional.empty(); @@ -49,4 +68,6 @@ public Optional getRankings() { weeklyRanking )); } + + } diff --git a/src/main/java/com/somemore/global/redis/config/RedisConfig.java b/src/main/java/com/somemore/global/redis/config/RedisConfig.java index 77c70102a..ecceb9e4a 100644 --- a/src/main/java/com/somemore/global/redis/config/RedisConfig.java +++ b/src/main/java/com/somemore/global/redis/config/RedisConfig.java @@ -1,5 +1,6 @@ package com.somemore.global.redis.config; +import com.somemore.domains.volunteerrecord.dto.response.VolunteerRankingResponseDto; import com.somemore.global.common.event.ServerEventType; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.StringUtils; @@ -51,6 +52,10 @@ public RedisTemplate redisTemplate(RedisConnectionFactory connec template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new CustomJacksonRedisSerializer<>(Object.class)); + + template.setValueSerializer(new CustomJacksonRedisSerializer<>(VolunteerRankingResponseDto.class)); + template.setHashValueSerializer(new CustomJacksonRedisSerializer<>(VolunteerRankingResponseDto.class)); + return template; } diff --git a/src/test/java/com/somemore/domains/volunteerrecord/service/GetVolunteerRankingServiceTest.java b/src/test/java/com/somemore/domains/volunteerrecord/service/GetVolunteerRankingServiceTest.java index e130567c2..3644ab530 100644 --- a/src/test/java/com/somemore/domains/volunteerrecord/service/GetVolunteerRankingServiceTest.java +++ b/src/test/java/com/somemore/domains/volunteerrecord/service/GetVolunteerRankingServiceTest.java @@ -78,24 +78,24 @@ void getVolunteerRanking_Success() { .hasSize(2) .extracting("volunteerId", "totalHours", "ranking", "nickname") .containsExactly( - tuple(id1.toString(), 100, 1, "봉사자1"), - tuple(id2.toString(), 90, 2, "봉사자2") + tuple(id1, 100, 1L, "봉사자1"), + tuple(id2, 90, 2L, "봉사자2") ); assertThat(result.volunteerMonthlyRankingResponse()) .hasSize(2) .extracting("volunteerId", "totalHours", "ranking", "nickname") .containsExactly( - tuple(id3.toString(), 50, 1, "봉사자3"), - tuple(id4.toString(), 40, 2, "봉사자4") + tuple(id3, 50, 1L, "봉사자3"), + tuple(id4, 40, 2L, "봉사자4") ); assertThat(result.volunteerWeeklyRankingResponse()) .hasSize(2) .extracting("volunteerId", "totalHours", "ranking", "nickname") .containsExactly( - tuple(id5.toString(), 30, 1, "봉사자5"), - tuple(id6.toString(), 20, 2, "봉사자6") + tuple(id5, 30, 1L, "봉사자5"), + tuple(id6, 20, 2L, "봉사자6") ); } diff --git a/src/test/java/com/somemore/domains/volunteerrecord/service/RankingCacheServiceTest.java b/src/test/java/com/somemore/domains/volunteerrecord/service/RankingCacheServiceTest.java index 8e43a91a0..333a266c6 100644 --- a/src/test/java/com/somemore/domains/volunteerrecord/service/RankingCacheServiceTest.java +++ b/src/test/java/com/somemore/domains/volunteerrecord/service/RankingCacheServiceTest.java @@ -73,24 +73,24 @@ void cacheRanking_Success() { .hasSize(2) .extracting("volunteerId", "totalHours", "ranking") .containsExactly( - tuple(id1.toString(), 100, 1), - tuple(id2.toString(), 90, 2) + tuple(id1, 100, 1L), + tuple(id2, 90, 2L) ); assertThat(dto.volunteerMonthlyRankingResponse()) .hasSize(2) .extracting("volunteerId", "totalHours", "ranking") .containsExactly( - tuple(id3.toString(), 50, 1), - tuple(id4.toString(), 40, 2) + tuple(id3, 50, 1L), + tuple(id4, 40, 2L) ); assertThat(dto.volunteerWeeklyRankingResponse()) .hasSize(2) .extracting("volunteerId", "totalHours", "ranking") .containsExactly( - tuple(id5.toString(), 30, 1), - tuple(id6.toString(), 20, 2) + tuple(id5, 30, 1L), + tuple(id6, 20, 2L) ); }); }