Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ public CursorPageResponseInterestDto getInterests(Long userId,

Slice<Interest> slices = interestRepository.findAll(
keyword, orderBy, direction, cursor, after, limit);
log.info("REQ userId={}, keyword={}, orderBy={}, direction={}, cursor={}, after={}, limit={}",
userId, keyword, orderBy, direction, cursor, after, limit);

List<Interest> interests = slices.getContent();

Expand All @@ -126,9 +124,6 @@ public CursorPageResponseInterestDto getInterests(Long userId,
boolean subscribedByMe = subscribedIds.contains(interest.getId());
InterestDto dto = interestMapper.toInterestDto(interest, keywords, subscribedByMe,
subscriberCount);

log.info("DBG dto id={}, name={}, subscriberCount={} subscribedByMe={}",
dto.id(), dto.name(), dto.subscriberCount(), dto.subscribedByMe());
interestDtos.add(dto);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.monew.monew_api.interest;

import com.monew.monew_api.interest.entity.Interest;
import com.monew.monew_api.interest.entity.Keyword;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.test.util.ReflectionTestUtils;

public class TestInterestForm {

// interestId Long 생성기
private static Long generatedId(){
return new AtomicLong(1).getAndIncrement();
}

public static Interest create(String name, List<String> keywords) {
Interest interest = Interest.create(name);

for (String keyword : keywords) {
interest.addKeyword(new Keyword(keyword));
}
ReflectionTestUtils.setField(interest, "id", generatedId());
return interest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.monew.monew_api.interest.repository;

import static org.assertj.core.api.Assertions.assertThat;

import com.monew.monew_api.common.config.QuerydslConfig;
import com.monew.monew_api.interest.dto.InterestOrderBy;
import com.monew.monew_api.interest.entity.Interest;
import com.monew.monew_api.interest.entity.Keyword;
import com.querydsl.core.types.Order;
import java.time.LocalDateTime;
import java.util.Comparator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.Slice;
import org.springframework.test.context.ActiveProfiles;


@DataJpaTest
@ActiveProfiles("test")
@Import(QuerydslConfig.class)
public class InterestRepositoryCustomTest {

@Qualifier("interestRepositoryCustomImpl")
@Autowired
InterestRepositoryCustom interestRepositoryCustom;

@Autowired
TestEntityManager em;

@BeforeEach
void setUp() {
Interest i1 = Interest.create("interest1");
Interest i2 = Interest.create("interest2");
Interest i3 = Interest.create("interest3");

// i1: 3명 i2: 2명 i3: 1명 구독
i1.addSubscriberCount();
i1.addSubscriberCount();
i1.addSubscriberCount();
i2.addSubscriberCount();
i2.addSubscriberCount();
i3.addSubscriberCount();

em.persist(i1);
em.persist(i2);
em.persist(i3);

Keyword k1 = new Keyword("keyword1");
Keyword k2 = new Keyword("keyword2");
Keyword k3 = new Keyword("keyword3");
Keyword k4 = new Keyword("keyword4");

// i1: k1,k2 i2: k2,k3 i3: k4
i1.addKeyword(k1);
i1.addKeyword(k2);
i2.addKeyword(k2);
i2.addKeyword(k3);
i3.addKeyword(k4);

em.persist(k1);
em.persist(k2);
em.persist(k3);
em.persist(k4);

em.flush();
em.clear();
}

@Test
@DisplayName("관심사 전체 조회 - name ASC")
void testFindAllNameASC() {
String keyword = null;
InterestOrderBy orderBy = InterestOrderBy.name;
Order direction = Order.ASC;
String cursor = null;
LocalDateTime after = null;
int limit = 2;

Slice<Interest> result = interestRepositoryCustom.findAll(
keyword, orderBy, direction, cursor, after, limit
);

assertThat(result).hasSize(2);
assertThat(result.hasNext()).isTrue();
assertThat(result.getContent())
.isSortedAccordingTo(Comparator.comparing(Interest::getName));

}

@Test
@DisplayName("검색어로 관심사 조회 - subscriberCount DESC")
void testFindAllSubscriberCountDESC() {
String keyword = "interest1";
InterestOrderBy orderBy = InterestOrderBy.subscriberCount;
Order direction = Order.DESC;
int limit = 6;

Slice<Interest> result = interestRepositoryCustom.findAll(
keyword, orderBy, direction, null, null, limit
);

assertThat(result).isNotEmpty();
assertThat(result.getContent())
.allMatch(i -> i.getName().contains("interest1"));
}

@Test
@DisplayName("커서 조회 확인 - subscriberCount ASC")
void testFindAllSubscriberCountASCWithCursor() {
Slice<Interest> firstSlice = interestRepositoryCustom.findAll(
null, InterestOrderBy.subscriberCount, Order.ASC, null, null, 2
);
assertThat(firstSlice).hasSize(2);
assertThat(firstSlice.hasNext()).isTrue();

Interest last = firstSlice.getContent().get(1);
String nextCursor = String.valueOf(last.getSubscriberCount());
LocalDateTime after = last.getCreatedAt();

Slice<Interest> secondSlice = interestRepositoryCustom.findAll(
null, InterestOrderBy.subscriberCount, Order.DESC, nextCursor, after, 2
);
assertThat(secondSlice).isNotEmpty();
}

@Test
@DisplayName("관심사 전체 카운트")
void testFindAllSubscriberCount() {
long count = interestRepositoryCustom.countFilteredTotalElements(null);
assertThat(count).isEqualTo(3);
}

@Test
@DisplayName("검색어로 관심사 카운트")
void testCountFilteredTotalElementsWithKeyword() {
long count = interestRepositoryCustom.countFilteredTotalElements("keyword1");
assertThat(count).isEqualTo(1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.monew.monew_api.interest.repository;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

import com.monew.monew_api.common.config.QuerydslConfig;
import com.monew.monew_api.interest.entity.Interest;
import com.monew.monew_api.interest.entity.Keyword;
import jakarta.persistence.EntityManager;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;

@DataJpaTest
@ActiveProfiles("test")
@Import(QuerydslConfig.class)
public class KeywordRepositoryTest {

@Autowired
KeywordRepository keywordRepository;

@Autowired
InterestRepository interestRepository;

@Autowired
EntityManager em;

@DisplayName("관심사 안에 포함되지 않는 키워드 조회")
@Test
public void findOrphanKeywordsIn() {
Keyword keyword1 = keywordRepository.save(new Keyword("keyword1"));
Keyword keyword2 = keywordRepository.save(new Keyword("keyword2"));
Keyword keyword3 = keywordRepository.save(new Keyword("keyword3")); // 고아 키워드

Interest interest = Interest.create("interest1");
interest.addKeyword(keyword1);
interest.addKeyword(keyword2);
interestRepository.saveAndFlush(interest);

em.flush();
em.clear();

List<Keyword> orphanKeywords = keywordRepository.findOrphanKeywordsIn(
List.of(keyword1, keyword2, keyword3));

assertThat(orphanKeywords).hasSize(1);
assertThat(orphanKeywords.get(0).getKeyword()).isEqualTo("keyword3");
}
}
Loading