Skip to content

Commit

Permalink
✅ Test: 리포트 생성 및 조회 테스트 케이스 작성 (#68)
Browse files Browse the repository at this point in the history
* 🐞 BugFix: 주간 일기 작성 쿼리 타입 LocalDate로 변경

* ✅ Test: 특정 연도, 월, 주 일기 목록 조회 테스트

* 🐞 BugFix: Test Code 일기 date 타입 LocalDate로 변경

* ✅ Test: ReportCommandServiceTest 작성

* ✅ Test: ReportQueryServiceTest 작성

* ✅ Test: ReportController 작성
  • Loading branch information
ahnsugyeong authored May 7, 2024
1 parent dd33ece commit 5c447ef
Show file tree
Hide file tree
Showing 12 changed files with 709 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public Report createReport(User user, int year, int month, int week) {
diary.getDiaryEmotions().forEach(diaryEmotion -> {
Emotion emotion = diaryEmotion.getEmotion();
emotionCounts.merge(emotion, 1, Integer::sum);
if (emotion.getPolarity() == EmotionPolarity.POSITIVE) {
if (emotion.getPolarity().equals(EmotionPolarity.POSITIVE)) {
positiveCount.getAndIncrement();
} else if (emotion.getPolarity() == EmotionPolarity.NEGATIVE) {
} else {
negativeCount.getAndIncrement();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.List;
import java.util.NoSuchElementException;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -77,7 +77,7 @@ void tearDown() {
@Test
void createDiary() {
// given
DiaryCreateRequest request = buildValidDiaryCreateRequest(LocalDateTime.of(2024, 1, 1, 0, 0));
DiaryCreateRequest request = buildValidDiaryCreateRequest(LocalDate.of(2024, 1, 1));

// when
Diary createdDiary = diaryCommandService.createDiary(user, request, "#FFFFFF", emotions);
Expand All @@ -87,7 +87,7 @@ void createDiary() {
assertThat(createdDiary.getId()).isNotNull();
assertThat(createdDiary.getContent()).isEqualTo("content");
assertThat(createdDiary.getPainting().getColor()).isEqualTo("#FFFFFF");
assertThat(createdDiary.getDate()).isEqualTo(LocalDateTime.of(2024, 1, 1, 0, 0));
assertThat(createdDiary.getDate()).isEqualTo(LocalDate.of(2024, 1, 1));

assertThat(createdDiary.getDiaryEmotions())
.hasSize(2)
Expand All @@ -99,7 +99,7 @@ void createDiary() {
@Test
void shouldNotCreateDiaryWithFutureDate() {
// given
LocalDateTime futureDate = LocalDateTime.now().plusDays(1);
LocalDate futureDate = LocalDate.now().plusDays(1);
DiaryCreateRequest request = DiaryCreateRequest.builder()
.date(futureDate)
.content("content")
Expand All @@ -116,7 +116,7 @@ void shouldNotCreateDiaryWithFutureDate() {
@Test
void shouldNotCreateDiaryWithDuplicatedDate() {
// given
DiaryCreateRequest request = buildValidDiaryCreateRequest(LocalDateTime.of(2024, 1, 1, 0, 0));
DiaryCreateRequest request = buildValidDiaryCreateRequest(LocalDate.of(2024, 1, 1));
diaryCommandService.createDiary(user, request, "#FFFFFF", emotions);

// when & then
Expand All @@ -129,12 +129,12 @@ void shouldNotCreateDiaryWithDuplicatedDate() {
@Test
void updateDiary() {
// given
DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDateTime.of(2024, 1, 1, 0, 0));
DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDate.of(2024, 1, 1));
Diary createdDiary = diaryCommandService.createDiary(user, createRequest, "#FFFFFF", emotions);

DiaryUpdateRequest updateRequest = buildValidDiaryUpdateRequest(
"updated content",
LocalDateTime.of(2023, 1, 1, 0, 0)
LocalDate.of(2024, 1, 1)
);

// when
Expand All @@ -145,7 +145,7 @@ void updateDiary() {
assertThat(updatedDiary.getId()).isNotNull();
assertThat(updatedDiary.getContent()).isEqualTo("updated content");
assertThat(updatedDiary.getPainting().getColor()).isEqualTo("#FFFFFF");
assertThat(updatedDiary.getDate()).isEqualTo(LocalDateTime.of(2023, 1, 1, 0, 0));
assertThat(updatedDiary.getDate()).isEqualTo(LocalDate.of(2024, 1, 1));

assertThat(createdDiary.getDiaryEmotions())
.hasSize(2)
Expand All @@ -157,7 +157,7 @@ void updateDiary() {
@Test
void updateDiaryContentOnly() {
// given
DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDateTime.of(2024, 1, 1, 0, 0));
DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDate.of(2024, 1, 1));
Diary createdDiary = diaryCommandService.createDiary(user, createRequest, "#FFFFFF", emotions);

DiaryUpdateRequest updateRequest = buildValidDiaryUpdateRequest("updated content", null);
Expand All @@ -170,7 +170,7 @@ void updateDiaryContentOnly() {
assertThat(updatedDiary.getId()).isNotNull();
assertThat(updatedDiary.getContent()).isEqualTo("updated content");
assertThat(updatedDiary.getPainting().getColor()).isEqualTo("#FFFFFF");
assertThat(updatedDiary.getDate()).isEqualTo(LocalDateTime.of(2024, 1, 1, 0, 0));
assertThat(updatedDiary.getDate()).isEqualTo(LocalDate.of(2024, 1, 1));

assertThat(createdDiary.getDiaryEmotions())
.hasSize(2)
Expand All @@ -182,10 +182,10 @@ void updateDiaryContentOnly() {
@Test
void updateDiaryDateOnly() {
// given
DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDateTime.of(2024, 1, 1, 0, 0));
DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDate.of(2024, 1, 1));
Diary createdDiary = diaryCommandService.createDiary(user, createRequest, "#FFFFFF", emotions);

DiaryUpdateRequest updateRequest = buildValidDiaryUpdateRequest(null, LocalDateTime.of(2023, 1, 1, 0, 0));
DiaryUpdateRequest updateRequest = buildValidDiaryUpdateRequest(null, LocalDate.of(2024, 1, 1));

// when
Diary updatedDiary = diaryCommandService.updateDiary(user, createdDiary.getId(), updateRequest);
Expand All @@ -195,7 +195,7 @@ void updateDiaryDateOnly() {
assertThat(updatedDiary.getId()).isNotNull();
assertThat(updatedDiary.getContent()).isEqualTo("content");
assertThat(updatedDiary.getPainting().getColor()).isEqualTo("#FFFFFF");
assertThat(updatedDiary.getDate()).isEqualTo(LocalDateTime.of(2023, 1, 1, 0, 0));
assertThat(updatedDiary.getDate()).isEqualTo(LocalDate.of(2024, 1, 1));

assertThat(createdDiary.getDiaryEmotions())
.hasSize(2)
Expand All @@ -208,7 +208,7 @@ void updateDiaryDateOnly() {
void throwExceptionWhenUPDATENonExistentDiary() {
// given
Long nonExistentDiaryId = 999L; // 존재하지 않는 ID
DiaryUpdateRequest request = buildValidDiaryUpdateRequest(null, LocalDateTime.of(2023, 1, 1, 0, 0));
DiaryUpdateRequest request = buildValidDiaryUpdateRequest(null, LocalDate.of(2024, 1, 1));

// when & then
assertThrows(DiaryNotFoundException.class, () -> {
Expand All @@ -220,10 +220,10 @@ void throwExceptionWhenUPDATENonExistentDiary() {
@Test
void shouldNotUpdateDiaryWithFutureDate() {
// given
DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDateTime.of(2024, 1, 1, 0, 0));
DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDate.of(2024, 1, 1));
Diary createdDiary = diaryCommandService.createDiary(user, createRequest, "#FFFFFF", emotions);

DiaryUpdateRequest updateRequest = buildValidDiaryUpdateRequest(null, LocalDateTime.now().plusDays(1));
DiaryUpdateRequest updateRequest = buildValidDiaryUpdateRequest(null, LocalDate.now().plusDays(1));

// when & then
assertThatThrownBy(() -> diaryCommandService.updateDiary(user, createdDiary.getId(), updateRequest))
Expand All @@ -235,13 +235,13 @@ void shouldNotUpdateDiaryWithFutureDate() {
@Test
void shouldNotUpdateDiaryWithDuplicatedDate() {
// given
DiaryCreateRequest createRequest1 = buildValidDiaryCreateRequest(LocalDateTime.of(2024, 1, 1, 0, 0));
DiaryCreateRequest createRequest1 = buildValidDiaryCreateRequest(LocalDate.of(2024, 1, 1));
diaryCommandService.createDiary(user, createRequest1, "#FFFFFF", emotions);

DiaryCreateRequest createRequest2 = buildValidDiaryCreateRequest(LocalDateTime.of(2023, 1, 1, 0, 0));
DiaryCreateRequest createRequest2 = buildValidDiaryCreateRequest(LocalDate.of(2023, 1, 1));
Diary createdDiary = diaryCommandService.createDiary(user, createRequest2, "#FFFFFF", emotions);

DiaryUpdateRequest updateRequest = buildValidDiaryUpdateRequest(null, LocalDateTime.of(2024, 1, 1, 0, 0));
DiaryUpdateRequest updateRequest = buildValidDiaryUpdateRequest(null, LocalDate.of(2024, 1, 1));

// when & then
assertThatThrownBy(() -> diaryCommandService.updateDiary(user, createdDiary.getId(), updateRequest))
Expand All @@ -256,10 +256,10 @@ void shouldThrowAccessDeniedWhenUpdatingOtherUsersDiary() {
User otherUser = User.builder().build();
userRepository.save(otherUser);

DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDateTime.of(2024, 1, 1, 0, 0));
DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDate.of(2024, 1, 1));
Diary diary = diaryCommandService.createDiary(user, createRequest, "#FFFFFF", emotions);

DiaryUpdateRequest updateRequest = buildValidDiaryUpdateRequest("updated content", LocalDateTime.now());
DiaryUpdateRequest updateRequest = buildValidDiaryUpdateRequest("updated content", LocalDate.now());

// when & then
assertThrows(DiaryAccessDeniedException.class, () -> {
Expand All @@ -272,7 +272,7 @@ void shouldThrowAccessDeniedWhenUpdatingOtherUsersDiary() {
@DisplayName("일기를 성공적으로 삭제한다.")
void deleteDiarySuccessfully() {
// given
DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDateTime.of(2024, 1, 1, 0, 0));
DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDate.of(2024, 1, 1));
Diary createdDiary = diaryCommandService.createDiary(user, createRequest, "#FFFFFF", emotions);

// when
Expand Down Expand Up @@ -303,7 +303,7 @@ void shouldThrowAccessDeniedWhenDeletingOtherUsersDiary() {
User otherUser = User.builder().build();
userRepository.save(otherUser);

DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDateTime.of(2024, 1, 1, 0, 0));
DiaryCreateRequest createRequest = buildValidDiaryCreateRequest(LocalDate.of(2024, 1, 1));
Diary diary = diaryCommandService.createDiary(user, createRequest, "#FFFFFF", emotions);

// when & then
Expand All @@ -312,15 +312,15 @@ void shouldThrowAccessDeniedWhenDeletingOtherUsersDiary() {
});
}

private DiaryCreateRequest buildValidDiaryCreateRequest(LocalDateTime date) {
private DiaryCreateRequest buildValidDiaryCreateRequest(LocalDate date) {
return DiaryCreateRequest.builder()
.content("content")
.date(date)
.imageUrl("imageUrl")
.build();
}

private DiaryUpdateRequest buildValidDiaryUpdateRequest(String content, LocalDateTime date) {
private DiaryUpdateRequest buildValidDiaryUpdateRequest(String content, LocalDate date) {
return DiaryUpdateRequest.builder()
.content(content)
.date(date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -50,7 +50,7 @@ void tearDown() {
@Test
void getDiaryByUser() {
// given
Diary diary = buildDiary(LocalDateTime.of(2024, 4, 1, 0, 0));
Diary diary = buildDiary(LocalDate.of(2024, 4, 1));
diary = diaryRepository.save(diary);

// when
Expand All @@ -60,17 +60,17 @@ void getDiaryByUser() {
assertThat(result).isNotNull();
assertThat(result.getId()).isEqualTo(diary.getId());
assertThat(result.getContent()).isEqualTo("content");
assertThat(result.getDate()).isEqualTo(LocalDateTime.of(2024, 4, 1, 0, 0));
assertThat(result.getDate()).isEqualTo(LocalDate.of(2024, 4, 1));
}

@DisplayName("특정 사용자의 특정 연도와 월에 대한 일기 목록을 조회한다.")
@Test
void getMonthlyDiariesByUser() {
// given
Diary diary1 = buildDiary(LocalDateTime.of(2024, 4, 1, 0, 0));
Diary diary2 = buildDiary(LocalDateTime.of(2024, 4, 30, 23, 59));
Diary diary3 = buildDiary(LocalDateTime.of(2024, 5, 1, 0, 0));
Diary diary4 = buildDiary(LocalDateTime.of(2024, 3, 31, 23, 59));
Diary diary1 = buildDiary(LocalDate.of(2024, 4, 1));
Diary diary2 = buildDiary(LocalDate.of(2024, 4, 30));
Diary diary3 = buildDiary(LocalDate.of(2024, 5, 1));
Diary diary4 = buildDiary(LocalDate.of(2024, 3, 31));
diaryRepository.saveAll(List.of(diary1, diary2, diary3, diary4));

int year = 2024;
Expand All @@ -84,15 +84,15 @@ void getMonthlyDiariesByUser() {
assertThat(result).hasSize(2);
assertThat(result).extracting("date")
.containsExactlyInAnyOrder(
LocalDateTime.of(2024, 4, 1, 0, 0),
LocalDateTime.of(2024, 4, 30, 23, 59));
LocalDate.of(2024, 4, 1),
LocalDate.of(2024, 4, 30));
}

@DisplayName("다른 사용자의 일기를 조회하려 할 때 접근 거부 예외를 발생시킨다.")
@Test
void accessDeniedWhenUserIsNotOwner() {
// given
Diary diary = buildDiary(LocalDateTime.of(2024, 4, 1, 0, 0));
Diary diary = buildDiary(LocalDate.of(2024, 4, 1));
diaryRepository.save(diary);
User otherUser = User.builder().build();

Expand All @@ -114,7 +114,7 @@ void throwExceptionWhenGetNonExistentDiary() {
});
}

private Diary buildDiary(LocalDateTime date) {
private Diary buildDiary(LocalDate date) {
return Diary.builder()
.user(user)
.content("content")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -62,7 +61,7 @@ void createDiary() throws Exception {
// given
DiaryCreateRequest request = DiaryCreateRequest.builder()
.content("content")
.date(LocalDateTime.of(2024, 1, 1, 0, 0))
.date(LocalDate.of(2024, 1, 1))
.imageUrl("imageUrl")
.build();

Expand Down Expand Up @@ -129,7 +128,7 @@ void createDiaryWithoutContent() throws Exception {
// given
DiaryCreateRequest request = DiaryCreateRequest.builder()
.content("")
.date(LocalDateTime.of(2024, 1, 1, 0, 0))
.date(LocalDate.of(2024, 1, 1))
.imageUrl("imageUrl")
.build();

Expand Down Expand Up @@ -164,7 +163,7 @@ void updateDiary() throws Exception {
// given
DiaryUpdateRequest request = DiaryUpdateRequest.builder()
.content("content")
.date(LocalDateTime.of(2024, 1, 1, 0, 0))
.date(LocalDate.of(2024, 1, 1))
.build();

User user = User.builder()
Expand Down Expand Up @@ -224,7 +223,7 @@ void getDiaryByUser() throws Exception {
// given
DiaryResponse diaryResponse = DiaryResponse.builder()
.id(1L)
.date(LocalDateTime.of(2024, 1, 1, 0, 0))
.date(LocalDate.of(2024, 1, 1))
.content("content")
.imageUrl("imageUrl")
.color("#FFFFFF")
Expand Down
Loading

0 comments on commit 5c447ef

Please sign in to comment.