|
| 1 | +package com.loopers.application.ranking; |
| 2 | + |
| 3 | +import com.loopers.application.ranking.RankingInfo.RankingItemInfo; |
| 4 | +import com.loopers.application.ranking.RankingInfo.RankingPageInfo; |
| 5 | +import com.loopers.domain.brand.Brand; |
| 6 | +import com.loopers.domain.brand.repository.BrandRepository; |
| 7 | +import com.loopers.domain.common.vo.Money; |
| 8 | +import com.loopers.domain.product.Product; |
| 9 | +import com.loopers.domain.product.repository.ProductRepository; |
| 10 | +import com.loopers.infrastructure.cache.ProductRankingCache; |
| 11 | +import com.loopers.infrastructure.cache.ProductRankingCache.RankingEntry; |
| 12 | +import org.junit.jupiter.api.DisplayName; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | +import org.mockito.InjectMocks; |
| 16 | +import org.mockito.Mock; |
| 17 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import static org.mockito.ArgumentMatchers.*; |
| 24 | +import static org.mockito.Mockito.*; |
| 25 | + |
| 26 | +@ExtendWith(MockitoExtension.class) |
| 27 | +class RankingFacadeTest { |
| 28 | + |
| 29 | + @Mock |
| 30 | + private ProductRankingCache productRankingCache; |
| 31 | + |
| 32 | + @Mock |
| 33 | + private ProductRepository productRepository; |
| 34 | + |
| 35 | + @Mock |
| 36 | + private BrandRepository brandRepository; |
| 37 | + |
| 38 | + @InjectMocks |
| 39 | + private RankingFacade rankingFacade; |
| 40 | + |
| 41 | + private static final String TODAY_DATE = "20251226"; |
| 42 | + |
| 43 | + @Test |
| 44 | + @DisplayName("getRankings - 상품 정보가 Aggregation 된다") |
| 45 | + void getRankings_aggregatesProductInfo() { |
| 46 | + // given |
| 47 | + List<RankingEntry> rankingEntries = List.of( |
| 48 | + new RankingEntry(1, 100L, 50.0), |
| 49 | + new RankingEntry(2, 200L, 40.0) |
| 50 | + ); |
| 51 | + |
| 52 | + Product product1 = createProduct(100L, "상품A", 1L, 10000); |
| 53 | + Product product2 = createProduct(200L, "상품B", 2L, 20000); |
| 54 | + Brand brand1 = createBrand(1L, "브랜드A"); |
| 55 | + Brand brand2 = createBrand(2L, "브랜드B"); |
| 56 | + |
| 57 | + when(productRankingCache.getTodayDate()).thenReturn(TODAY_DATE); |
| 58 | + when(productRankingCache.getTopRankings(TODAY_DATE, 0, 10)).thenReturn(rankingEntries); |
| 59 | + when(productRankingCache.getTotalCount(TODAY_DATE)).thenReturn(2L); |
| 60 | + when(productRepository.findByIdIn(List.of(100L, 200L))).thenReturn(List.of(product1, product2)); |
| 61 | + when(brandRepository.findByIdIn(List.of(1L, 2L))).thenReturn(List.of(brand1, brand2)); |
| 62 | + |
| 63 | + // when |
| 64 | + RankingPageInfo result = rankingFacade.getRankings(null, 0, 10); |
| 65 | + |
| 66 | + // then |
| 67 | + assertThat(result.rankings()).hasSize(2); |
| 68 | + |
| 69 | + RankingItemInfo first = result.rankings().get(0); |
| 70 | + assertThat(first.rank()).isEqualTo(1); |
| 71 | + assertThat(first.productId()).isEqualTo(100L); |
| 72 | + assertThat(first.productName()).isEqualTo("상품A"); |
| 73 | + assertThat(first.brandName()).isEqualTo("브랜드A"); |
| 74 | + assertThat(first.score()).isEqualTo(50.0); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + @DisplayName("getRankings - 존재하지 않는 상품은 필터링된다") |
| 79 | + void getRankings_filtersNonExistentProducts() { |
| 80 | + // given |
| 81 | + List<RankingEntry> rankingEntries = List.of( |
| 82 | + new RankingEntry(1, 100L, 50.0), |
| 83 | + new RankingEntry(2, 999L, 40.0) // 존재하지 않는 상품 |
| 84 | + ); |
| 85 | + |
| 86 | + Product product1 = createProduct(100L, "상품A", 1L, 10000); |
| 87 | + Brand brand1 = createBrand(1L, "브랜드A"); |
| 88 | + |
| 89 | + when(productRankingCache.getTodayDate()).thenReturn(TODAY_DATE); |
| 90 | + when(productRankingCache.getTopRankings(TODAY_DATE, 0, 10)).thenReturn(rankingEntries); |
| 91 | + when(productRankingCache.getTotalCount(TODAY_DATE)).thenReturn(2L); |
| 92 | + when(productRepository.findByIdIn(anyList())).thenReturn(List.of(product1)); |
| 93 | + when(brandRepository.findByIdIn(anyList())).thenReturn(List.of(brand1)); |
| 94 | + |
| 95 | + // when |
| 96 | + RankingPageInfo result = rankingFacade.getRankings(null, 0, 10); |
| 97 | + |
| 98 | + // then |
| 99 | + assertThat(result.rankings()).hasSize(1); |
| 100 | + assertThat(result.rankings().get(0).productId()).isEqualTo(100L); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @DisplayName("getRankings - 날짜 미지정 시 오늘 날짜 사용") |
| 105 | + void getRankings_usesTodayDateWhenNotSpecified() { |
| 106 | + // given |
| 107 | + when(productRankingCache.getTodayDate()).thenReturn(TODAY_DATE); |
| 108 | + when(productRankingCache.getTopRankings(TODAY_DATE, 0, 10)).thenReturn(Collections.emptyList()); |
| 109 | + |
| 110 | + // when |
| 111 | + RankingPageInfo result = rankingFacade.getRankings(null, 0, 10); |
| 112 | + |
| 113 | + // then |
| 114 | + assertThat(result.date()).isEqualTo(TODAY_DATE); |
| 115 | + verify(productRankingCache).getTopRankings(eq(TODAY_DATE), anyInt(), anyInt()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + @DisplayName("getRankings - 페이지 정보가 정확하다") |
| 120 | + void getRankings_pageInfoIsCorrect() { |
| 121 | + // given - 첫 페이지에 10개 상품이 있고, 전체 25개 |
| 122 | + List<RankingEntry> rankingEntries = List.of( |
| 123 | + new RankingEntry(1, 100L, 50.0) |
| 124 | + ); |
| 125 | + Product product = createProduct(100L, "상품A", 1L, 10000); |
| 126 | + Brand brand = createBrand(1L, "브랜드A"); |
| 127 | + |
| 128 | + when(productRankingCache.getTodayDate()).thenReturn(TODAY_DATE); |
| 129 | + when(productRankingCache.getTopRankings(TODAY_DATE, 0, 10)).thenReturn(rankingEntries); |
| 130 | + when(productRankingCache.getTotalCount(TODAY_DATE)).thenReturn(25L); |
| 131 | + when(productRepository.findByIdIn(anyList())).thenReturn(List.of(product)); |
| 132 | + when(brandRepository.findByIdIn(anyList())).thenReturn(List.of(brand)); |
| 133 | + |
| 134 | + // when |
| 135 | + RankingPageInfo result = rankingFacade.getRankings(null, 0, 10); |
| 136 | + |
| 137 | + // then |
| 138 | + assertThat(result.page()).isEqualTo(0); |
| 139 | + assertThat(result.size()).isEqualTo(10); |
| 140 | + assertThat(result.totalCount()).isEqualTo(25); |
| 141 | + assertThat(result.totalPages()).isEqualTo(3); // ceil(25/10) |
| 142 | + assertThat(result.hasNext()).isTrue(); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + @DisplayName("getRankings - 빈 결과 시 빈 리스트 반환") |
| 147 | + void getRankings_returnsEmptyListWhenNoResults() { |
| 148 | + // given |
| 149 | + when(productRankingCache.getTodayDate()).thenReturn(TODAY_DATE); |
| 150 | + when(productRankingCache.getTopRankings(TODAY_DATE, 0, 10)).thenReturn(Collections.emptyList()); |
| 151 | + |
| 152 | + // when |
| 153 | + RankingPageInfo result = rankingFacade.getRankings(null, 0, 10); |
| 154 | + |
| 155 | + // then |
| 156 | + assertThat(result.rankings()).isEmpty(); |
| 157 | + assertThat(result.totalCount()).isEqualTo(0); |
| 158 | + assertThat(result.hasNext()).isFalse(); |
| 159 | + } |
| 160 | + |
| 161 | + private Product createProduct(Long id, String name, Long brandId, int price) { |
| 162 | + Product product = mock(Product.class); |
| 163 | + when(product.getId()).thenReturn(id); |
| 164 | + when(product.getName()).thenReturn(name); |
| 165 | + when(product.getBrandId()).thenReturn(brandId); |
| 166 | + when(product.getPrice()).thenReturn(Money.of(price)); |
| 167 | + when(product.getLikeCount()).thenReturn(10); |
| 168 | + return product; |
| 169 | + } |
| 170 | + |
| 171 | + private Brand createBrand(Long id, String name) { |
| 172 | + Brand brand = mock(Brand.class); |
| 173 | + when(brand.getId()).thenReturn(id); |
| 174 | + when(brand.getName()).thenReturn(name); |
| 175 | + return brand; |
| 176 | + } |
| 177 | +} |
0 commit comments