|
| 1 | +package com.monew.monew_api.interest.repository; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.monew.monew_api.common.config.QuerydslConfig; |
| 6 | +import com.monew.monew_api.interest.dto.InterestOrderBy; |
| 7 | +import com.monew.monew_api.interest.entity.Interest; |
| 8 | +import com.monew.monew_api.interest.entity.Keyword; |
| 9 | +import com.querydsl.core.types.Order; |
| 10 | +import java.time.LocalDateTime; |
| 11 | +import java.util.Comparator; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.DisplayName; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 17 | +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
| 18 | +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; |
| 19 | +import org.springframework.context.annotation.Import; |
| 20 | +import org.springframework.data.domain.Slice; |
| 21 | +import org.springframework.test.context.ActiveProfiles; |
| 22 | + |
| 23 | + |
| 24 | +@DataJpaTest |
| 25 | +@ActiveProfiles("test") |
| 26 | +@Import(QuerydslConfig.class) |
| 27 | +public class InterestRepositoryCustomTest { |
| 28 | + |
| 29 | + @Qualifier("interestRepositoryCustomImpl") |
| 30 | + @Autowired |
| 31 | + InterestRepositoryCustom interestRepositoryCustom; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + TestEntityManager em; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setUp() { |
| 38 | + Interest i1 = Interest.create("interest1"); |
| 39 | + Interest i2 = Interest.create("interest2"); |
| 40 | + Interest i3 = Interest.create("interest3"); |
| 41 | + |
| 42 | + // i1: 3명 i2: 2명 i3: 1명 구독 |
| 43 | + i1.addSubscriberCount(); |
| 44 | + i1.addSubscriberCount(); |
| 45 | + i1.addSubscriberCount(); |
| 46 | + i2.addSubscriberCount(); |
| 47 | + i2.addSubscriberCount(); |
| 48 | + i3.addSubscriberCount(); |
| 49 | + |
| 50 | + em.persist(i1); |
| 51 | + em.persist(i2); |
| 52 | + em.persist(i3); |
| 53 | + |
| 54 | + Keyword k1 = new Keyword("keyword1"); |
| 55 | + Keyword k2 = new Keyword("keyword2"); |
| 56 | + Keyword k3 = new Keyword("keyword3"); |
| 57 | + Keyword k4 = new Keyword("keyword4"); |
| 58 | + |
| 59 | + // i1: k1,k2 i2: k2,k3 i3: k4 |
| 60 | + i1.addKeyword(k1); |
| 61 | + i1.addKeyword(k2); |
| 62 | + i2.addKeyword(k2); |
| 63 | + i2.addKeyword(k3); |
| 64 | + i3.addKeyword(k4); |
| 65 | + |
| 66 | + em.persist(k1); |
| 67 | + em.persist(k2); |
| 68 | + em.persist(k3); |
| 69 | + em.persist(k4); |
| 70 | + |
| 71 | + em.flush(); |
| 72 | + em.clear(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @DisplayName("관심사 전체 조회 - name ASC") |
| 77 | + void testFindAllNameASC() { |
| 78 | + String keyword = null; |
| 79 | + InterestOrderBy orderBy = InterestOrderBy.name; |
| 80 | + Order direction = Order.ASC; |
| 81 | + String cursor = null; |
| 82 | + LocalDateTime after = null; |
| 83 | + int limit = 2; |
| 84 | + |
| 85 | + Slice<Interest> result = interestRepositoryCustom.findAll( |
| 86 | + keyword, orderBy, direction, cursor, after, limit |
| 87 | + ); |
| 88 | + |
| 89 | + assertThat(result).hasSize(2); |
| 90 | + assertThat(result.hasNext()).isTrue(); |
| 91 | + assertThat(result.getContent()) |
| 92 | + .isSortedAccordingTo(Comparator.comparing(Interest::getName)); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + @DisplayName("검색어로 관심사 조회 - subscriberCount DESC") |
| 98 | + void testFindAllSubscriberCountDESC() { |
| 99 | + String keyword = "interest1"; |
| 100 | + InterestOrderBy orderBy = InterestOrderBy.subscriberCount; |
| 101 | + Order direction = Order.DESC; |
| 102 | + int limit = 6; |
| 103 | + |
| 104 | + Slice<Interest> result = interestRepositoryCustom.findAll( |
| 105 | + keyword, orderBy, direction, null, null, limit |
| 106 | + ); |
| 107 | + |
| 108 | + assertThat(result).isNotEmpty(); |
| 109 | + assertThat(result.getContent()) |
| 110 | + .allMatch(i -> i.getName().contains("interest1")); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + @DisplayName("커서 조회 확인 - subscriberCount ASC") |
| 115 | + void testFindAllSubscriberCountASCWithCursor() { |
| 116 | + Slice<Interest> firstSlice = interestRepositoryCustom.findAll( |
| 117 | + null, InterestOrderBy.subscriberCount, Order.ASC, null, null, 2 |
| 118 | + ); |
| 119 | + assertThat(firstSlice).hasSize(2); |
| 120 | + assertThat(firstSlice.hasNext()).isTrue(); |
| 121 | + |
| 122 | + Interest last = firstSlice.getContent().get(1); |
| 123 | + String nextCursor = String.valueOf(last.getSubscriberCount()); |
| 124 | + LocalDateTime after = last.getCreatedAt(); |
| 125 | + |
| 126 | + Slice<Interest> secondSlice = interestRepositoryCustom.findAll( |
| 127 | + null, InterestOrderBy.subscriberCount, Order.DESC, nextCursor, after, 2 |
| 128 | + ); |
| 129 | + assertThat(secondSlice).isNotEmpty(); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + @DisplayName("관심사 전체 카운트") |
| 134 | + void testFindAllSubscriberCount() { |
| 135 | + long count = interestRepositoryCustom.countFilteredTotalElements(null); |
| 136 | + assertThat(count).isEqualTo(3); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + @DisplayName("검색어로 관심사 카운트") |
| 141 | + void testCountFilteredTotalElementsWithKeyword() { |
| 142 | + long count = interestRepositoryCustom.countFilteredTotalElements("keyword1"); |
| 143 | + assertThat(count).isEqualTo(1); |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments