From fb15a808a56d85d50e66416e249e8be55b56b318 Mon Sep 17 00:00:00 2001 From: Choi YoungLyn Date: Tue, 28 May 2024 16:49:31 +0900 Subject: [PATCH 1/7] =?UTF-8?q?[Feat]=20#274=20-=20=EC=A7=84=EC=B2=99?= =?UTF-8?q?=EC=83=81=ED=99=A9=20=EA=B8=B0=EB=A1=9D=20=ED=9B=84=20=EC=B2=B4?= =?UTF-8?q?=ED=81=AC=EC=9D=B8=20=EB=A1=9C=EA=B7=B8=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../moonshot/log/service/LogServiceTest.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java diff --git a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java new file mode 100644 index 00000000..ba11bde2 --- /dev/null +++ b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java @@ -0,0 +1,84 @@ +package org.moonshot.log.service; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.moonshot.exception.BadRequestException; +import org.moonshot.keyresult.dto.request.KeyResultCreateRequestDto; +import org.moonshot.keyresult.dto.request.KeyResultCreateRequestInfoDto; +import org.moonshot.keyresult.dto.request.KeyResultModifyRequestDto; +import org.moonshot.keyresult.model.KeyResult; +import org.moonshot.keyresult.repository.KeyResultRepository; +import org.moonshot.log.dto.request.LogCreateRequestDto; +import org.moonshot.log.dto.response.AchieveResponseDto; +import org.moonshot.log.model.Log; +import org.moonshot.log.model.LogState; +import org.moonshot.log.repository.LogRepository; +import org.moonshot.objective.model.Objective; +import org.moonshot.user.model.User; + +import java.time.LocalDate; +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.times; + +@ExtendWith(MockitoExtension.class) +public class LogServiceTest { + + @Mock + private KeyResultRepository keyResultRepository; + @InjectMocks + private LogService logService; + @Mock + private LogRepository logRepository; + + private static User fakeUser; + + + @BeforeAll + static void setUp() { + Long fakeUserId = 1L; + String fakeUserNickname = "tester"; + fakeUser = User.buildWithId().id(fakeUserId).nickname(fakeUserNickname).build(); + } + + @Test + @DisplayName("KeyResult의 진척 상황을 기록하고 체크인 로그를 추가합니다.") + void KeyResult의_진척상황을_기록하고_체크인_로그를_추가합니다() { + // given + Objective testObjective = mock(Objective.class); + KeyResult testKeyResult = mock(KeyResult.class); + Log testLog = mock(Log.class); + Log testPrevLog = mock(Log.class); + LogCreateRequestDto request = new LogCreateRequestDto(1L, 1000L, "new log content"); + + given(keyResultRepository.findKeyResultAndObjective(request.keyResultId())).willReturn(Optional.of(testKeyResult)); + given(testKeyResult.getObjective()).willReturn(testObjective); + given(testObjective.getUser()).willReturn(fakeUser); + + given(logRepository.findLatestLogByKeyResultId(eq(LogState.RECORD), anyLong())).willReturn(Optional.of(testPrevLog)); + given(logRepository.save(any(Log.class))).willReturn(testLog); + given(testObjective.getKeyResultList()).willReturn(List.of(testKeyResult)); + + given(testObjective.getProgress()).willReturn((short)1); + + //when + Optional response = logService.createRecordLog(fakeUser.getId(), request); + + //then + verify(testKeyResult, times(1)).modifyProgress(anyShort()); + verify(testObjective, times(1)).modifyProgress(anyShort()); + assertThat(response.isEmpty()).isEqualTo(true); + } + +} \ No newline at end of file From 3d50bc4a7be7090cdb3a23d5b14e9b01e34a1647 Mon Sep 17 00:00:00 2001 From: Choi YoungLyn Date: Tue, 28 May 2024 16:52:06 +0900 Subject: [PATCH 2/7] =?UTF-8?q?[Feat]=20#274=20-=20=EC=A7=84=EC=B2=99?= =?UTF-8?q?=EC=83=81=ED=99=A9=20=EC=9E=85=EB=A0=A5=20=EC=8B=9C=20=EC=9D=B4?= =?UTF-8?q?=EC=A0=84=20=EA=B0=92=EA=B3=BC=20=EB=8F=99=EC=9D=BC=ED=95=9C=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20=EC=98=88=EC=99=B8=20=EB=B0=9C=EC=83=9D=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../moonshot/log/service/LogServiceTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java index ba11bde2..ed396d1e 100644 --- a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java +++ b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java @@ -81,4 +81,26 @@ static void setUp() { assertThat(response.isEmpty()).isEqualTo(true); } + @Test + @DisplayName("KeyResult의 진척 상황의 값이 이전 진척 상황 값과 동일하여 예외가 발생합니다.") + void KeyResult의_진척상황_값이_이전_진척상황_값과_동일하여_예외가_발생합니다() { + //given + Objective testObjective = mock(Objective.class); + KeyResult testKeyResult = mock(KeyResult.class); + Log testPrevLog = mock(Log.class); + LogCreateRequestDto request = new LogCreateRequestDto(1L, 1000L, "new log content"); + + given(keyResultRepository.findKeyResultAndObjective(request.keyResultId())).willReturn(Optional.of(testKeyResult)); + given(testKeyResult.getObjective()).willReturn(testObjective); + given(testObjective.getUser()).willReturn(fakeUser); + + given(logRepository.findLatestLogByKeyResultId(eq(LogState.RECORD), anyLong())).willReturn(Optional.of(testPrevLog)); + given(testPrevLog.getCurrNum()).willReturn(1000L); + + //when, then + assertThatThrownBy(() -> logService.createRecordLog(fakeUser.getId(), request)) + .isInstanceOf(BadRequestException.class) + .hasMessage("Log 입력값은 이전 값과 동일할 수 없습니다."); + } + } \ No newline at end of file From b5e045691a40747526cb813e514fdaaebc4b8d93 Mon Sep 17 00:00:00 2001 From: Choi YoungLyn Date: Tue, 28 May 2024 16:54:36 +0900 Subject: [PATCH 3/7] =?UTF-8?q?[Feat]=20#274=20-=20KR=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=20=ED=9B=84=20=EC=B2=B4=ED=81=AC=EC=9D=B8=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/moonshot/log/service/LogServiceTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java index ed396d1e..2a94ba6b 100644 --- a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java +++ b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java @@ -103,4 +103,18 @@ static void setUp() { .hasMessage("Log 입력값은 이전 값과 동일할 수 없습니다."); } + @Test + @DisplayName("KeyResult가 수정되면 체크인 로그를 추가합니다.") + void KeyResult가_수정되면_체크인_로그를_추가합니다() { + //given + KeyResult testKeyResult = mock(KeyResult.class); + KeyResultModifyRequestDto request = new KeyResultModifyRequestDto( + 1L, null, null, null, 100000L, null, "new check-in"); + //when + logService.createUpdateLog(request, testKeyResult); + + //then + verify(logRepository, times(1)).save(any(Log.class)); + } + } \ No newline at end of file From e2c9abc971b6e49857f93608a667be2c105ecfa8 Mon Sep 17 00:00:00 2001 From: Choi YoungLyn Date: Tue, 28 May 2024 16:55:51 +0900 Subject: [PATCH 4/7] =?UTF-8?q?[Feat]=20#274=20-=20KR=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=20=ED=9B=84=20=EC=B2=B4=ED=81=AC=EC=9D=B8=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../moonshot/log/service/LogServiceTest.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java index 2a94ba6b..8fa8557d 100644 --- a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java +++ b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java @@ -116,5 +116,38 @@ static void setUp() { //then verify(logRepository, times(1)).save(any(Log.class)); } - + + @Test + @DisplayName("Objective와 함께 KeyResult가 생성되면 체크인 로그를 추가합니다.") + void Objective와_함께_KeyResult가_생성되면_체크인_로그를_추가합니다() { + //given + KeyResult testKeyResult = mock(KeyResult.class); + Long testKeyResultId = 1L; + KeyResultCreateRequestInfoDto request = new KeyResultCreateRequestInfoDto("test KR", LocalDate.now(), LocalDate.now(), 0, 2000L, "건", null); + + given(keyResultRepository.findById(testKeyResultId)).willReturn(Optional.of(testKeyResult)); + + //when + logService.createKRLog(request, testKeyResultId); + + //then + verify(logRepository, times(1)).save(any(Log.class)); + } + + @Test + @DisplayName("KeyResult가 추가적으로 생성되면 체크인 로그를 추가합니다.") + void KeyResult가_추가적으로_생성되면_체크인_로그를_추가합니다() { + //given + KeyResult testKeyResult = mock(KeyResult.class); + Long testKeyResultId = 1L; + KeyResultCreateRequestDto request = new KeyResultCreateRequestDto(1L, "test KR", LocalDate.now(), LocalDate.now(), 0, 2000L, "건"); + + given(keyResultRepository.findById(testKeyResultId)).willReturn(Optional.of(testKeyResult)); + + //when + logService.createKRLog(request, testKeyResultId); + + //then + verify(logRepository, times(1)).save(any(Log.class)); + } } \ No newline at end of file From 026a97b3f9bcf8baf8bcf0b25bfd45c0cbedaf99 Mon Sep 17 00:00:00 2001 From: Choi YoungLyn Date: Tue, 28 May 2024 16:56:35 +0900 Subject: [PATCH 5/7] =?UTF-8?q?[Feat]=20#274=20-=20=EC=A7=84=EC=B2=99?= =?UTF-8?q?=EC=83=81=ED=99=A9=20=EC=9E=85=EB=A0=A5=20=ED=9B=84=20KR=20?= =?UTF-8?q?=EB=8B=AC=EC=84=B1=EB=A5=A0=20=EA=B3=84=EC=82=B0=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/moonshot/log/service/LogServiceTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java index 8fa8557d..5826127d 100644 --- a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java +++ b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java @@ -150,4 +150,19 @@ static void setUp() { //then verify(logRepository, times(1)).save(any(Log.class)); } + + @Test + @DisplayName("진척 상황값이 입력되면 KR 달성률을 계산합니다.") + void 진척상황_값이_입력되면_KR_달성률을_계산합니다() { + //given + KeyResult testKeyResult = mock(KeyResult.class); + Log testLog = mock(Log.class); + + given(testKeyResult.getTarget()).willReturn(1000L); + given(testLog.getCurrNum()).willReturn(100L); + + //when, then + assertThat(logService.calculateKRProgressBar(testLog, testKeyResult.getTarget())).isEqualTo((short)10); + } + } \ No newline at end of file From 76b86162495cfacd71c327a9109afc6caa396827 Mon Sep 17 00:00:00 2001 From: Choi YoungLyn Date: Tue, 28 May 2024 16:57:02 +0900 Subject: [PATCH 6/7] =?UTF-8?q?[Feat]=20#274=20-=20KR=EC=97=90=20=EB=8C=80?= =?UTF-8?q?=ED=95=9C=20Log=20=EC=A1=B0=ED=9A=8C=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/moonshot/log/service/LogServiceTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java index 5826127d..1248032c 100644 --- a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java +++ b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java @@ -165,4 +165,18 @@ static void setUp() { assertThat(logService.calculateKRProgressBar(testLog, testKeyResult.getTarget())).isEqualTo((short)10); } + @Test + @DisplayName("KeyResult에 대한 Log를 조회합니다.") + void KeyResult에_대한_Log를_조회합니다() { + //given + KeyResult testKeyResult = mock(KeyResult.class); + List testLogList = mock(List.class); + + given(testLogList.size()).willReturn(2); + given(logRepository.findAllByKeyResultOrderByIdDesc(testKeyResult)).willReturn(testLogList); + + //when,then + assertThat(logService.getLogList(testKeyResult).size()).isEqualTo(2); + } + } \ No newline at end of file From 90a91782bc8739cf4bf45189d0d7851648550c85 Mon Sep 17 00:00:00 2001 From: Choi YoungLyn Date: Tue, 28 May 2024 17:04:50 +0900 Subject: [PATCH 7/7] =?UTF-8?q?[Chore]=20#274=20-=20isKeyResultAchieved?= =?UTF-8?q?=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EB=A1=9C=EA=B7=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/moonshot/log/service/LogServiceTest.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java index 1248032c..7a0d06dd 100644 --- a/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java +++ b/moonshot-api/src/test/java/org/moonshot/log/service/LogServiceTest.java @@ -4,6 +4,8 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -52,9 +54,10 @@ static void setUp() { fakeUser = User.buildWithId().id(fakeUserId).nickname(fakeUserNickname).build(); } - @Test + @ParameterizedTest @DisplayName("KeyResult의 진척 상황을 기록하고 체크인 로그를 추가합니다.") - void KeyResult의_진척상황을_기록하고_체크인_로그를_추가합니다() { + @CsvSource(value = {"1, true", "100, false"}) + void KeyResult의_진척상황을_기록하고_체크인_로그를_추가합니다(short progress, boolean expected) { // given Objective testObjective = mock(Objective.class); KeyResult testKeyResult = mock(KeyResult.class); @@ -70,7 +73,7 @@ static void setUp() { given(logRepository.save(any(Log.class))).willReturn(testLog); given(testObjective.getKeyResultList()).willReturn(List.of(testKeyResult)); - given(testObjective.getProgress()).willReturn((short)1); + given(testObjective.getProgress()).willReturn((short)progress); //when Optional response = logService.createRecordLog(fakeUser.getId(), request); @@ -78,7 +81,7 @@ static void setUp() { //then verify(testKeyResult, times(1)).modifyProgress(anyShort()); verify(testObjective, times(1)).modifyProgress(anyShort()); - assertThat(response.isEmpty()).isEqualTo(true); + assertThat(response.isEmpty()).isEqualTo(expected); } @Test