Skip to content

Commit

Permalink
test: GetTrackerService의 테스트를 작성한다
Browse files Browse the repository at this point in the history
  • Loading branch information
vectorch9 committed Nov 6, 2023
1 parent 046aab7 commit e241f1c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package daybyquest.post.application;

import static daybyquest.support.fixture.UserFixtures.ALICE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;

import daybyquest.post.dto.response.TrackerResponse;
import daybyquest.post.query.PostDao;
import daybyquest.post.query.SimplePostData;
import daybyquest.user.domain.Users;
import java.time.LocalDateTime;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.BDDMockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class GetTrackerServiceTest {

private final Users users = BDDMockito.mock(Users.class);

private final PostDao postDao = BDDMockito.mock(PostDao.class);

private final GetTrackerService getTrackerService = new GetTrackerService(users, postDao, 5L);

@Test
void 트래커를_조회한다() {
// given
final Long loginId = 1L;
final Long aliceId = 2L;
final LocalDateTime now = LocalDateTime.now();
final List<SimplePostData> simplePostData = List.of(
new SimplePostData(1L, now),
new SimplePostData(2L, now.minusDays(1)),
new SimplePostData(3L, now.minusDays(1)),
new SimplePostData(4L, now.minusDays(3)),
new SimplePostData(4L, now.minusDays(4))
);
final List<Long> expected = List.of(1L, 2L, 0L, 1L, 1L);

given(users.getUserIdByUsername(ALICE.username)).willReturn(aliceId);
given(postDao.findAllBySuccessAndUploadedAtAfter(eq(aliceId), any())).willReturn(simplePostData);

// when
final TrackerResponse response = getTrackerService.invoke(loginId, ALICE.username);
final List<Long> tracker = response.tracker();

// then
assertThat(tracker).containsExactlyElementsOf(expected);
}
}
28 changes: 28 additions & 0 deletions src/test/java/daybyquest/post/query/PostDaoQuerydslImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static daybyquest.support.fixture.UserFixtures.ALICE;
import static daybyquest.support.fixture.UserFixtures.BOB;
import static daybyquest.support.fixture.UserFixtures.CHARLIE;
import static daybyquest.support.util.PostUtils.n일_전에_업로드;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

Expand All @@ -17,6 +18,7 @@
import daybyquest.relation.domain.Follow;
import daybyquest.support.test.QuerydslTest;
import daybyquest.user.domain.User;
import java.time.LocalDateTime;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -118,4 +120,30 @@ public class PostDaoQuerydslImplTest extends QuerydslTest {
// then
assertThat(postData).hasSize(3);
}

@Test
void 특정_날짜_이후에_업로드하고_성공한_게시물들을_조회한다() {
// given
final User bob = 저장한다(BOB.생성());
final Post post1 = 저장한다(POST_1.생성(bob));
final Post post2 = 저장한다(POST_2.생성(bob));
final Post post3 = 저장한다(POST_3.생성(bob));
final Post post4 = 저장한다(POST_4.생성(bob));
n일_전에_업로드(post2, 1);
n일_전에_업로드(post3, 2);
n일_전에_업로드(post4, 6);

final List<Long> expected = List.of(post1.getId(), post2.getId(), post3.getId());
final LocalDateTime time = LocalDateTime.now().minusDays(5);

// when
final List<SimplePostData> postData = postDao.findAllBySuccessAndUploadedAtAfter(bob.getId(), time);
final List<Long> actual = postData.stream().map(SimplePostData::id).toList();

// then
assertAll(() -> {
assertThat(postData).hasSize(3);
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
});
}
}
13 changes: 13 additions & 0 deletions src/test/java/daybyquest/support/util/PostUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package daybyquest.support.util;

import daybyquest.post.domain.Post;
import java.time.LocalDateTime;
import org.springframework.test.util.ReflectionTestUtils;

public class PostUtils {

public static void n일_전에_업로드(final Post post, final long day) {
final LocalDateTime dayBefore = LocalDateTime.now().minusDays(day);
ReflectionTestUtils.setField(post, "uploadedAt", dayBefore);
}
}

0 comments on commit e241f1c

Please sign in to comment.