From 53151a349ffaf0a8e1a897d85844a3ef1211a161 Mon Sep 17 00:00:00 2001 From: Seonghun Jeong <119427233+zzoe2346@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:10:58 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EC=BD=9C=EB=B0=B1=20=EB=8B=A8=EA=B1=B4?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=20=EB=A1=9C=EC=A7=81=20=EA=B0=9C=EC=84=A0?= =?UTF-8?q?=20(#123)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feat: 콜백 단건 조회 로직 개선 - 상태에 따른 검증 로직 추가 - 예외 처리 추가 - 가독성 향상 - 테스트 꼼꼼히 --- .../callback/service/CallbackService.java | 7 ++- .../callback/service/CallbackServiceTest.java | 46 +++++++++++++++---- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/example/sinitto/callback/service/CallbackService.java b/src/main/java/com/example/sinitto/callback/service/CallbackService.java index 6fcb06da..065a0be7 100644 --- a/src/main/java/com/example/sinitto/callback/service/CallbackService.java +++ b/src/main/java/com/example/sinitto/callback/service/CallbackService.java @@ -234,8 +234,11 @@ public CallbackForSinittoResponse getCallbackForSinitto(Long memberId, Long call Callback callback = callbackRepository.findById(callbackId) .orElseThrow(() -> new NotFoundException("해당 콜백 id에 해당하는 콜백이 없습니다.")); - if (callback.getAssignedMemberId().equals(memberId)) { - return new CallbackForSinittoResponse(callback.getId(), callback.getSeniorName(), callback.getPostTime(), callback.getStatus(), callback.getSeniorId(), true, callback.getSenior().getPhoneNumber()); + if (!callback.getStatus().equals(Callback.Status.WAITING.toString())) { + if (callback.getAssignedMemberId() != null && callback.getAssignedMemberId().equals(memberId)) { + return new CallbackForSinittoResponse(callback.getId(), callback.getSeniorName(), callback.getPostTime(), callback.getStatus(), callback.getSeniorId(), true, callback.getSenior().getPhoneNumber()); + } + throw new ForbiddenException("대기중인 콜백이 아닌경우 오직 할당받은 시니또만이 콜백을 조회할 수 있습니다."); } return new CallbackForSinittoResponse(callback.getId(), callback.getSeniorName(), callback.getPostTime(), callback.getStatus(), callback.getSeniorId(), false, ""); diff --git a/src/test/java/com/example/sinitto/callback/service/CallbackServiceTest.java b/src/test/java/com/example/sinitto/callback/service/CallbackServiceTest.java index 7641c458..18653c19 100644 --- a/src/test/java/com/example/sinitto/callback/service/CallbackServiceTest.java +++ b/src/test/java/com/example/sinitto/callback/service/CallbackServiceTest.java @@ -406,7 +406,7 @@ void localDateTimeBeforeCalculateTest() { } @Test - @DisplayName("시니또용 콜백 단건 조회 - api 호출한 시니또 본인이 할당된 콜백일 경우") + @DisplayName("시니또용 콜백 단건 조회 - 1.대기상태 아님 2.AssignedMemberId 이 null 아님 + 해당 콜백에 할당 된 시니또 맞음 ") void getCallbackForSinitto() { //given Long memberId = 1L; @@ -415,11 +415,12 @@ void getCallbackForSinitto() { Senior senior = mock(Senior.class); when(callbackRepository.findById(callbackId)).thenReturn(Optional.of(callback)); + + when(callback.getStatus()).thenReturn(Callback.Status.IN_PROGRESS.toString()); when(callback.getAssignedMemberId()).thenReturn(1L); when(callback.getId()).thenReturn(1L); when(callback.getSeniorName()).thenReturn("SeniorName"); when(callback.getPostTime()).thenReturn(LocalDateTime.now()); - when(callback.getStatus()).thenReturn(Callback.Status.WAITING.toString()); when(callback.getSeniorId()).thenReturn(1L); when(callback.getSenior()).thenReturn(senior); when(callback.getSenior().getPhoneNumber()).thenReturn("01012341234"); @@ -429,10 +430,11 @@ void getCallbackForSinitto() { //then assertTrue(result.isAssignedToSelf()); + assertEquals("01012341234", result.seniorPhoneNumber()); } @Test - @DisplayName("시니또용 콜백 단건 조회 - api 호출한 시니또 본인이 할당된 콜백이 아닌 경우") + @DisplayName("시니또용 콜백 단건 조회 - 1.대기상태 아님 2.AssignedMemberId 이 null 인 상황 ") void getCallbackForSinitto2() { //given Long memberId = 1L; @@ -440,17 +442,45 @@ void getCallbackForSinitto2() { Callback callback = mock(Callback.class); when(callbackRepository.findById(callbackId)).thenReturn(Optional.of(callback)); - when(callback.getAssignedMemberId()).thenReturn(999L); // 여기서 시니또 본인에게 할당된 콜백이 아닌걸 확인 - when(callback.getId()).thenReturn(1L); - when(callback.getSeniorName()).thenReturn("SeniorName"); - when(callback.getPostTime()).thenReturn(LocalDateTime.now()); + when(callback.getAssignedMemberId()).thenReturn(null); + when(callback.getStatus()).thenReturn(Callback.Status.IN_PROGRESS.toString()); + + //when then + assertThrows(ForbiddenException.class, () -> callbackService.getCallbackForSinitto(memberId, callbackId)); + } + + @Test + @DisplayName("시니또용 콜백 단건 조회 - 1.대기상태 아님 2.AssignedMemberId 이 null 은 아닌데 해당 콜백에 할당된 시니또는 아닌 상황") + void getCallbackForSinitto3() { + //given + Long memberId = 1L; + Long callbackId = 1L; + Callback callback = mock(Callback.class); + + when(callbackRepository.findById(callbackId)).thenReturn(Optional.of(callback)); + when(callback.getAssignedMemberId()).thenReturn(999L); + when(callback.getStatus()).thenReturn(Callback.Status.IN_PROGRESS.toString()); + + //when then + assertThrows(ForbiddenException.class, () -> callbackService.getCallbackForSinitto(memberId, callbackId)); + } + + @Test + @DisplayName("시니또용 콜백 단건 조회 - 1.콜백이 '대기상태' 인 경우(모든 시니또가 조회 가능하다)") + void getCallbackForSinitto4() { + //given + Long memberId = 1L; + Long callbackId = 1L; + Callback callback = mock(Callback.class); + + when(callbackRepository.findById(callbackId)).thenReturn(Optional.of(callback)); when(callback.getStatus()).thenReturn(Callback.Status.WAITING.toString()); - when(callback.getSeniorId()).thenReturn(1L); //when CallbackForSinittoResponse result = callbackService.getCallbackForSinitto(memberId, callbackId); //then assertFalse(result.isAssignedToSelf()); + assertEquals("", result.seniorPhoneNumber()); } }