Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 환불하는데 돈이 오히려 차감되던 버그 수정과 시니어 등록, 수정 시 폰 번호 중복 확인 로직 추가 #166

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface SeniorRepository extends JpaRepository<Senior, Long> {
Optional<Senior> findByPhoneNumber(String phoneNumber);

List<Senior> findAllByMember(Member member);

boolean existsByPhoneNumber(String phoneNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public void createSenior(Long memberId, SeniorRequest seniorRequest) {
);
if (member.isSinitto()) throw new BadRequestException("보호자만 이용할 수 있습니다.");

if(seniorRepository.existsByPhoneNumber(seniorRequest.seniorPhoneNumber())) {
throw new BadRequestException("이미 등록되어 있는 전화번호 입니다.");
}

Senior senior = new Senior(seniorRequest.seniorName(), seniorRequest.seniorPhoneNumber(), member);

seniorRepository.save(senior);
Expand Down Expand Up @@ -86,6 +90,10 @@ public void updateSenior(Long memberId, Long seniorId, SeniorRequest seniorReque
() -> new NotFoundException("이메일에 해당하는 시니어를 찾을 수 없습니다.")
);

if(seniorRepository.existsByPhoneNumber(seniorRequest.seniorPhoneNumber())) {
throw new BadRequestException("이미 등록되어 있는 전화번호 입니다.");
}

senior.updateSenior(seniorRequest.seniorName(), seniorRequest.seniorPhoneNumber());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void refundPointByDelete(Long memberId, int price, PointLog.Content conte
throw new BadRequestException("포인트가 부족합니다.");
}

point.deduct(price);
point.earn(price);

pointLogRepository.save(
new PointLog(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

@MockitoSettings
public class GuardServiceTest {
Expand Down Expand Up @@ -115,6 +115,20 @@ void createSeniorTestWhenSinittoDoThis() {
assertThrows(BadRequestException.class, () -> guardService.createSenior(memberId, seniorRequest));
}

@Test
@DisplayName("createSenior 테스트 - 이미 등록된 번호로 시니어 생성하면 예외를 발생시켜야한다.")
void createSeniorTestFailWhenDuplicatedPhoneNumber() {
// Given
Long memberId = 1L;
Member member = new Member("testName", "01012345678", "[email protected]", false);
SeniorRequest seniorRequest = new SeniorRequest("testSeniorName", "01011111111");
when(memberRepository.findById(memberId)).thenReturn(Optional.of(member));
when(seniorRepository.existsByPhoneNumber("01011111111")).thenReturn(true);

// When, Then
assertThrows(BadRequestException.class, () -> guardService.createSenior(memberId, seniorRequest));
}

@Test
@DisplayName("readSeniors 메소드 테스트")
void readSeniorsTest() {
Expand Down Expand Up @@ -177,6 +191,22 @@ void updateSeniorTest() {
verify(senior, times(1)).updateSenior(request.seniorName(), request.seniorPhoneNumber());
}

@Test
@DisplayName("updateSenior 메소드 테스트 - 이미 등록된 번호로 수정시 예외가 발생해야한다.")
void updateSeniorTestFailWhenDuplicatedPhoneNumber() {
//given
Long memberId = 1L;
Long seniorId = 2L;
Member member = new Member("testName", "01012345678", "[email protected]", true);
Senior senior = mock(Senior.class);
SeniorRequest request = new SeniorRequest("newSeniorName", "01011111111");

when(seniorRepository.findByIdAndMemberId(seniorId, memberId)).thenReturn(Optional.of(senior));
when(seniorRepository.existsByPhoneNumber("01011111111")).thenReturn(true);
//when then
assertThrows(BadRequestException.class, () -> guardService.updateSenior(memberId, seniorId, request));
}

@Test
@DisplayName("deleteSenior 메소드 테스트")
void deleteSeniorTest() {
Expand Down
120 changes: 120 additions & 0 deletions src/test/java/com/example/sinitto/point/service/PointServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,124 @@ void savePointWithdrawRequest4() {
assertThrows(BadRequestException.class, () -> pointService.savePointWithdrawRequest(1L, 10000));
}
}

@Nested
@DisplayName("포인트 적립 테스트")
class EarnPointTest {
@Test
@DisplayName("포인트 적립 성공한다.")
void earnPoint1() {
//given
Point point = mock(Point.class);
when(pointRepository.findByMemberId(1L)).thenReturn(Optional.of(point));

//when
pointService.earnPoint(1L, 10000, PointLog.Content.COMPLETE_HELLO_CALL_AND_EARN);

//then
verify(point).earn(10000);
verify(pointLogRepository).save(any(PointLog.class));
}

@Test
@DisplayName("멤버에 연관된 포인트가 없으면 예외를 발생시켜야한다.")
void earnPoint2() {
//given
when(pointRepository.findByMemberId(1L)).thenReturn(Optional.empty());


//when then
assertThrows(NotFoundException.class, () -> pointService.earnPoint(1L, 10000, PointLog.Content.COMPLETE_CALLBACK_AND_EARN));
}
}

@Nested
@DisplayName("포인트 차감 테스트")
class DeductPointTest {

@Test
@DisplayName("포인트 차감 성공한다.")
void deductPoint1() {
//given
Point point = mock(Point.class);
when(pointRepository.findByMemberIdWithWriteLock(1L)).thenReturn(Optional.of(point));
when(point.isSufficientForDeduction(10000)).thenReturn(true);

//when
pointService.deductPoint(1L, 10000, PointLog.Content.SPEND_COMPLETE_CALLBACK);

//then
verify(point).deduct(10000);
verify(pointLogRepository).save(any(PointLog.class));
}

@Test
@DisplayName("멤버에 연관된 포인트가 없으면 예외를 발생시켜야한다.")
void deductPoint2() {
//given
when(pointRepository.findByMemberIdWithWriteLock(1L)).thenReturn(Optional.empty());

//when then

assertThrows(NotFoundException.class, () -> pointService.deductPoint(1L, 10000, PointLog.Content.SPEND_COMPLETE_CALLBACK));
}

@Test
@DisplayName("포인트가 부족하면 예외를 발생시켜야한다.")
void deductPoint3() {

//given
Point point = mock(Point.class);
when(pointRepository.findByMemberIdWithWriteLock(1L)).thenReturn(Optional.of(point));
when(point.isSufficientForDeduction(10000)).thenReturn(false);

//when then
assertThrows(BadRequestException.class, () -> pointService.deductPoint(1L, 10000, PointLog.Content.SPEND_COMPLETE_HELLO_CALL));
}
}

@Nested
@DisplayName("포인트 환불 테스트")
class RefundPointByDeleteTest {

@Test
@DisplayName("포인트 환불 성공한다. 성공하면 포인트가 되돌아 온다(적립)")
void refundPointByDelete1() {
//given
Point point = mock(Point.class);
when(pointRepository.findByMemberIdWithWriteLock(1L)).thenReturn(Optional.of(point));
when(point.isSufficientForDeduction(10000)).thenReturn(true);

//when
pointService.refundPointByDelete(1L, 10000, PointLog.Content.SPEND_CANCEL_HELLO_CALL);

//then
verify(point).earn(10000);
verify(pointLogRepository).save(any(PointLog.class));
}


@Test
@DisplayName("멤버에 연관된 포인트가 없으면 예외를 발생시켜야한다.")
void refundPointByDelete2() {
//given
when(pointRepository.findByMemberIdWithWriteLock(1L)).thenReturn(Optional.empty());

//when then
assertThrows(NotFoundException.class, () -> pointService.refundPointByDelete(1L, 10000, PointLog.Content.SPEND_CANCEL_HELLO_CALL));
}

@Test
@DisplayName("포인트가 부족하면 예외를 발생시켜야한다.")
void refundPointByDelete3() {
//given
Point point = mock(Point.class);
when(pointRepository.findByMemberIdWithWriteLock(1L)).thenReturn(Optional.of(point));
when(point.isSufficientForDeduction(10000)).thenReturn(false);

//when then
assertThrows(BadRequestException.class, () -> pointService.refundPointByDelete(1L, 10000, PointLog.Content.SPEND_CANCEL_HELLO_CALL));
}
}

}