Skip to content

Commit

Permalink
Test: 카프카 메시지(클릭)에 uuid 프로퍼티 추가로 인한 테스트 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
KimChanJin97 committed Oct 12, 2024
1 parent a5058f0 commit dae472e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cjkimhello97.toy.crashMyServer.service.click;

import static org.junit.jupiter.api.Assertions.*;

import cjkimhello97.toy.crashMyServer.IntegrationTest;
import cjkimhello97.toy.crashMyServer.auth.infrastructure.JwtProvider;
import cjkimhello97.toy.crashMyServer.auth.service.AuthService;
Expand All @@ -14,6 +16,9 @@
import cjkimhello97.toy.crashMyServer.member.repository.MemberRepository;
import cjkimhello97.toy.crashMyServer.service.auth.testdata.AuthServiceTestDataBuilder;
import cjkimhello97.toy.crashMyServer.service.click.testdata.ClickServiceFixtureObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -107,8 +112,13 @@ void beforeEach() {
Mockito.when(clickRepository.findByMemberMemberId(memberB.getMemberId()))
.thenReturn(Optional.of(clickOfB));

List<Click> clicks = new ArrayList<>();
clicks.add(clickOfA);
clicks.add(clickOfB);
Collections.sort(clicks);

Mockito.when(clickRepository.findTop10ClicksByCountDesc(PageRequest.of(0, 10)))
.thenReturn(List.of(clickOfA, clickOfB));
.thenReturn(clicks);
}

@Test
Expand All @@ -125,8 +135,10 @@ void beforeEach() {
ArgumentCaptor<KafkaClickRequest> clickRequestCaptor = ArgumentCaptor.forClass(KafkaClickRequest.class);
Mockito.verify(kafkaClickRequestKafkaTemplate).send(clickTopicCaptor.capture(), clickRequestCaptor.capture());

Assertions.assertEquals("click", clickTopicCaptor.getValue());
Assertions.assertEquals(kafkaClickRequest, clickRequestCaptor.getValue());
assertEquals("click", clickTopicCaptor.getValue());

assertEquals(kafkaClickRequest.getNickname(), clickRequestCaptor.getValue().getNickname());
assertEquals(kafkaClickRequest.getCount(), clickRequestCaptor.getValue().getCount());
}

@Test
Expand All @@ -143,7 +155,15 @@ void beforeEach() {
ArgumentCaptor<KafkaClickRankRequest> clickRankRequestCaptor = ArgumentCaptor.forClass(KafkaClickRankRequest.class);
Mockito.verify(kafkaClickRankRequestKafkaTemplate).send(clickRankTopicCaptor.capture(), clickRankRequestCaptor.capture());

Assertions.assertEquals("click-rank", clickRankTopicCaptor.getValue());
Assertions.assertEquals(kafkaClickRankRequest, clickRankRequestCaptor.getValue());
assertEquals("click-rank", clickRankTopicCaptor.getValue());

assertEquals(
kafkaClickRankRequest.getClickRank().get(memberA.getNickname()),
clickRankRequestCaptor.getValue().getClickRank().get(memberA.getNickname())
);
assertEquals(
kafkaClickRankRequest.getClickRank().get(memberB.getNickname()),
clickRankRequestCaptor.getValue().getClickRank().get(memberB.getNickname())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void beforeEach() {
@Test
@DisplayName("[ CLICK ] MYSQL TEST 2")
void 클릭하면_그에_따라_클릭_랭킹이_업데이트된다() {
// given & when: A가 한번 클릭
// given & when: A가 1번 클릭
clickService.click(memberA.getMemberId());

Click clickOfA = clickService.getTopTenClicks().get(0); // 내림차순 정렬이므로 A가 1등
Expand All @@ -79,13 +79,13 @@ void beforeEach() {
Member memberB = clickOfB.getMember();
Double countOfB = clickOfB.getCount();

// then: 총 클릭을 1번한 A가 1등, 총 클릭을 0번한 B가 2등이다
// then: 총 클릭을 1번한 A가 클릭 횟수 2회로 1등, 총 클릭을 0번한 클릭 횟수 1회로 B가 2등이다
Assertions.assertEquals(this.memberA, memberA);
Assertions.assertEquals(countOfA, 1D);
Assertions.assertEquals(countOfA, 2D);
Assertions.assertEquals(this.memberB, memberB);
Assertions.assertEquals(countOfB, 0D);
Assertions.assertEquals(countOfB, 1D);

// given & when: B가 두번 클릭
// given & when: B가 2번 클릭
clickService.click(this.memberB.getMemberId());
clickService.click(this.memberB.getMemberId());

Expand All @@ -97,10 +97,10 @@ void beforeEach() {
memberA = clickOfA.getMember();
countOfA = clickOfA.getCount();

// then: 총 클릭을 2번한 B가 1등, 총 클릭을 1번한 A가 2등이다
// then: 총 클릭을 2번한 B가 클릭 횟수 3회로 1등, 총 클릭을 1번한 클릭 횟수 2회로 A가 2등이다
Assertions.assertEquals(this.memberB, memberB);
Assertions.assertEquals(countOfB, 2D);
Assertions.assertEquals(countOfB, 3D);
Assertions.assertEquals(this.memberA, memberA);
Assertions.assertEquals(countOfA, 1D);
Assertions.assertEquals(countOfA, 2D);
}
}

0 comments on commit dae472e

Please sign in to comment.