diff --git a/src/main/java/space/space_spring/controller/PayController.java b/src/main/java/space/space_spring/controller/PayController.java new file mode 100644 index 00000000..cd54ce3c --- /dev/null +++ b/src/main/java/space/space_spring/controller/PayController.java @@ -0,0 +1,49 @@ +package space.space_spring.controller; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth; +import space.space_spring.dto.pay.GetPayViewResponse; +import space.space_spring.dto.pay.PayReceiveInfoDto; +import space.space_spring.dto.pay.PayRequestInfoDto; +import space.space_spring.entity.UserSpace; +import space.space_spring.response.BaseResponse; +import space.space_spring.service.PayService; +import space.space_spring.util.userSpace.UserSpaceUtils; + +import java.util.List; +import java.util.Optional; + +@RestController +@RequiredArgsConstructor +@Slf4j +public class PayController { + + private final PayService payService; + private final UserSpaceUtils userSpaceUtils; + + @GetMapping("/space/{spaceId}/pay") + public BaseResponse showPayListForUser(@JwtLoginAuth Long userId, @PathVariable Long spaceId) { + // TODO 1. 유저가 스페이스에 속하는 지 검증 + validateIsUserInSpace(userId, spaceId); + + // TODO 2. 유저가 요청한 정산 리스트 get + List payRequestInfoDtoList = payService.getPayRequestInfoForUser(userId, spaceId); + + // TODO 3. 유저가 요청받은 정산 리스트 get + List payReceiveInfoDtoList = payService.getPayReceiveInfoForUser(userId, spaceId); + + return new BaseResponse<>(new GetPayViewResponse(payRequestInfoDtoList, payReceiveInfoDtoList)); + } + + private void validateIsUserInSpace(Long userId, Long spaceId) { + // 유저가 스페이스에 속할 경우 exception이 터지지 않을 것임 + // 그렇지 않을 경우, USER_IS_NOT_IN_SPACE 예외가 터질 것임 -> 추후 exception handling 과정 필요 + userSpaceUtils.isUserInSpace(userId, spaceId); + } + +} diff --git a/src/main/java/space/space_spring/controller/SpaceController.java b/src/main/java/space/space_spring/controller/SpaceController.java index 88594cdb..fd3d707a 100644 --- a/src/main/java/space/space_spring/controller/SpaceController.java +++ b/src/main/java/space/space_spring/controller/SpaceController.java @@ -38,6 +38,9 @@ public BaseResponse createSpace(@JwtLoginAuth Long user return new BaseResponse<>(spaceService.createSpace(userId, postSpaceCreateRequest)); } + /** + * 테스트 용 + */ @GetMapping("/{spaceId}") public BaseResponse getSpaceHome(@JwtLoginAuth Long userId, @PathVariable Long spaceId) { Optional userInSpace = userSpaceUtils.isUserInSpace(userId, spaceId); diff --git a/src/main/java/space/space_spring/controller/TestController.java b/src/main/java/space/space_spring/controller/TestController.java index 25ca6e65..54fcb0c1 100644 --- a/src/main/java/space/space_spring/controller/TestController.java +++ b/src/main/java/space/space_spring/controller/TestController.java @@ -1,5 +1,6 @@ package space.space_spring.controller; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.messaging.handler.annotation.DestinationVariable; import org.springframework.messaging.handler.annotation.MessageMapping; @@ -7,9 +8,11 @@ import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.messaging.simp.annotation.SubscribeMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth; +import space.space_spring.dao.PayDao; import space.space_spring.dto.chat.ChatTestRequest; import space.space_spring.dto.chat.ChatTestResponse; import space.space_spring.dto.jwt.JwtPayloadDto; diff --git a/src/main/java/space/space_spring/controller/UserController.java b/src/main/java/space/space_spring/controller/UserController.java index e3f2453b..fc7f3be9 100644 --- a/src/main/java/space/space_spring/controller/UserController.java +++ b/src/main/java/space/space_spring/controller/UserController.java @@ -58,8 +58,8 @@ public BaseResponse login(@Validated @RequestBody PostUse */ @GetMapping("/space-choice") public BaseResponse showUserSpaceList(@JwtLoginAuth Long userId, - @RequestParam int size, - @RequestParam Long lastUserSpaceId) { + @RequestParam int size, + @RequestParam Long lastUserSpaceId) { log.info("userId = {}", userId); diff --git a/src/main/java/space/space_spring/dao/PayDao.java b/src/main/java/space/space_spring/dao/PayDao.java new file mode 100644 index 00000000..5b5b54d2 --- /dev/null +++ b/src/main/java/space/space_spring/dao/PayDao.java @@ -0,0 +1,55 @@ +package space.space_spring.dao; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.hibernate.validator.internal.constraintvalidators.bv.AssertFalseValidator; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; +import space.space_spring.entity.PayRequest; +import space.space_spring.entity.PayRequestTarget; +import space.space_spring.entity.Space; +import space.space_spring.entity.User; + +import java.util.List; + +@Repository +public class PayDao { + + @PersistenceContext + private EntityManager em; + + public List findPayRequestListByUser(User user, Space space) { + // 유저가 해당 스페이스에서 요청한 정산 목록만을 select + // 아직 정산이 완료되지 않은 payRequest 엔티티만 select + String jpql = "SELECT pr FROM PayRequest pr WHERE pr.payCreateUser = :user AND pr.space = :space AND pr.isComplete = false"; + + return em.createQuery(jpql, PayRequest.class) + .setParameter("user", user) + .setParameter("space", space) + .getResultList(); + } + + + public List findPayRequestTargetListByPayRequest(PayRequest payRequest) { + // 해당 정산 요청의 모든 타겟 엔티티를 select + String jpql = "SELECT prt FROM PayRequestTarget prt WHERE prt.payRequest = :payRequest"; + + return em.createQuery(jpql, PayRequestTarget.class) + .setParameter("payRequest", payRequest) + .getResultList(); + } + + public List findPayRequestTargetListByUser(User user, Space space) { + // 유저가 해당 스페이스에서 요청받은 정산 목록만을 select + // 유저가 요청받은 정산 중 아직 완료되지 않은 payRequestTarget 엔티티만 select + String jpql = "SELECT prt FROM PayRequestTarget prt " + + "JOIN prt.payRequest pr " + + "WHERE prt.targetUserId = :userId AND pr.space = :space AND pr.isComplete = false"; + + return em.createQuery(jpql, PayRequestTarget.class) + .setParameter("userId", user.getUserId()) + .setParameter("space", space) + .getResultList(); + } + +} diff --git a/src/main/java/space/space_spring/dto/pay/GetPayViewResponse.java b/src/main/java/space/space_spring/dto/pay/GetPayViewResponse.java new file mode 100644 index 00000000..2d2b7305 --- /dev/null +++ b/src/main/java/space/space_spring/dto/pay/GetPayViewResponse.java @@ -0,0 +1,16 @@ +package space.space_spring.dto.pay; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.List; + +@Getter +@AllArgsConstructor +public class GetPayViewResponse { + + private List payRequestInfoDtoList = new ArrayList<>(); + + private List payReceiveInfoDtoList = new ArrayList<>(); +} diff --git a/src/main/java/space/space_spring/dto/pay/PayReceiveInfoDto.java b/src/main/java/space/space_spring/dto/pay/PayReceiveInfoDto.java new file mode 100644 index 00000000..0d57aeb2 --- /dev/null +++ b/src/main/java/space/space_spring/dto/pay/PayReceiveInfoDto.java @@ -0,0 +1,13 @@ +package space.space_spring.dto.pay; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class PayReceiveInfoDto { + + private String payCreatorName; + + private int requestAmount; +} diff --git a/src/main/java/space/space_spring/dto/pay/PayRequestInfoDto.java b/src/main/java/space/space_spring/dto/pay/PayRequestInfoDto.java new file mode 100644 index 00000000..92dfa440 --- /dev/null +++ b/src/main/java/space/space_spring/dto/pay/PayRequestInfoDto.java @@ -0,0 +1,17 @@ +package space.space_spring.dto.pay; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class PayRequestInfoDto { + + private int totalAmount; // 정산 총 금액 + + private int receiveAmount; // 현재까지 받은 금액 + + private int totalTargetNum; // 정산 요청한 사람 수 + + private int receiveTargetNum; // 그 중, 돈 보낸 사람 수 +} diff --git a/src/main/java/space/space_spring/entity/PayRequest.java b/src/main/java/space/space_spring/entity/PayRequest.java new file mode 100644 index 00000000..2b0030c6 --- /dev/null +++ b/src/main/java/space/space_spring/entity/PayRequest.java @@ -0,0 +1,44 @@ +package space.space_spring.entity; + +import jakarta.persistence.*; +import lombok.Getter; + +@Getter +@Entity +@Table(name = "Pay_Request") +public class PayRequest extends BaseEntity { + + @Id + @GeneratedValue + @Column(name = "pay_request_id") + private Long payRequestId; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id") + private User payCreateUser; // 정산을 시작한 유저 + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "space_id") + private Space space; // 정산이 이루어지는 스페이스 + + @Column(name = "total_amount") + private int totalAmount; // 정산의 총 금액 + + @Column(name = "pay_request_bank") + private String bankName; // 정산 받을 은행 이름 + + @Column(name = "bank_account_num") + private String bankAccountNum; // 정산 받을 은행 계좌번호 + + @Column(name = "is_complete") + private boolean isComplete; + + public void savePayRequest(User payCreateUser, Space space, int totalAmount, String bankName, String bankAccountNum, boolean isComplete) { + this.payCreateUser = payCreateUser; + this.space = space; + this.totalAmount = totalAmount; + this.bankName = bankName; + this.bankAccountNum = bankAccountNum; + this.isComplete = isComplete; + } +} diff --git a/src/main/java/space/space_spring/entity/PayRequestTarget.java b/src/main/java/space/space_spring/entity/PayRequestTarget.java new file mode 100644 index 00000000..e2c8d8e1 --- /dev/null +++ b/src/main/java/space/space_spring/entity/PayRequestTarget.java @@ -0,0 +1,34 @@ +package space.space_spring.entity; + +import jakarta.persistence.*; +import lombok.Getter; + +@Entity +@Getter +public class PayRequestTarget extends BaseEntity { + + @Id + @GeneratedValue + @Column(name = "pay_request_target_id") + private Long payRequestTargetId; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "pay_request_id") + private PayRequest payRequest; + + @Column(name = "target_user_id") + private Long targetUserId; // User 객체를 필드로 가지는 것보다 이게 더 낫나?? + + @Column(name = "request_amount") + private int requestAmount; + + @Column(name = "is_complete") + private boolean isComplete; + + public void savePayRequestTarget(PayRequest payRequest, Long targetUserId, int requestAmount, boolean isComplete) { + this.payRequest = payRequest; + this.targetUserId = targetUserId; + this.requestAmount = requestAmount; + this.isComplete = isComplete; + } +} diff --git a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java index 2caf1e3c..ec401a56 100644 --- a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java +++ b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java @@ -53,7 +53,7 @@ public enum BaseExceptionResponseStatus implements ResponseStatus { * 6000: Space 오류 */ INVALID_SPACE_CREATE(6000, HttpStatus.BAD_REQUEST.value(), "스페이스 생성 요청에서 잘못된 값이 존재합니다."), - safd(6001, HttpStatus.BAD_REQUEST.value(), "이미 존재하는 이메일입니다."), + SPACE_NOT_FOUND(6001, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 스페이스입니다."), adff(6002, HttpStatus.BAD_REQUEST.value(), "이미 존재하는 닉네임입니다."), baab(6003, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 회원입니다."), nff(6004, HttpStatus.BAD_REQUEST.value(), "비밀번호가 일치하지 않습니다."), diff --git a/src/main/java/space/space_spring/service/PayService.java b/src/main/java/space/space_spring/service/PayService.java new file mode 100644 index 00000000..448969c7 --- /dev/null +++ b/src/main/java/space/space_spring/service/PayService.java @@ -0,0 +1,90 @@ +package space.space_spring.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import space.space_spring.dao.PayDao; +import space.space_spring.dto.pay.PayReceiveInfoDto; +import space.space_spring.dto.pay.PayRequestInfoDto; +import space.space_spring.entity.PayRequest; +import space.space_spring.entity.PayRequestTarget; +import space.space_spring.entity.Space; +import space.space_spring.entity.User; +import space.space_spring.util.space.SpaceUtils; +import space.space_spring.util.user.UserUtils; + +import java.util.ArrayList; +import java.util.List; + +@Service +@RequiredArgsConstructor +public class PayService { + + private final PayDao payDao; + private final UserUtils userUtils; + private final SpaceUtils spaceUtils; + + public List getPayRequestInfoForUser(Long userId, Long spaceId) { + // TODO 1. userId에 해당하는 user find + User userByUserId = userUtils.findUserByUserId(userId); + + // TODO 2. spaceId에 해당하는 space find + Space spaceBySpaceId = spaceUtils.findSpaceBySpaceId(spaceId); + + // TODO 3. 유저가 요청한 정산 중 진행 중인 정산 리스트 select + List payRequestListByUser = payDao.findPayRequestListByUser(userByUserId, spaceBySpaceId); + + // TODO 4. return 타입 구성 + // 3-1. 각 payRequest 에 해당하는 모든 payRequestTarget 을 loop로 돌면서 데이터 수집 + List payRequestInfoDtoList = new ArrayList<>(); + + for (PayRequest payRequest : payRequestListByUser) { + // 하나의 정산 요청에 대하여 ,,, + int totalAmount = payRequest.getTotalAmount(); + int receiveAmount = 0; + int totalTargetNum = 0; + int receiveTargetNum = 0; + + List payRequestTargetList = payDao.findPayRequestTargetListByPayRequest(payRequest); + for (PayRequestTarget payRequestTarget : payRequestTargetList) { + if (payRequestTarget.isComplete()) { + // 해당 타겟이 돈을 낸 경우 + receiveAmount += payRequestTarget.getRequestAmount(); + receiveTargetNum++; + } + + totalTargetNum++; + } + + PayRequestInfoDto payRequestInfoDto = new PayRequestInfoDto(totalAmount, receiveAmount, totalTargetNum, receiveTargetNum); + payRequestInfoDtoList.add(payRequestInfoDto); + } + + return payRequestInfoDtoList; + } + + public List getPayReceiveInfoForUser(Long userId, Long spaceId) { + // TODO 1. userId에 해당하는 유저 find + User userByUserId = userUtils.findUserByUserId(userId); + + // TODO 2. spaceId에 해당하는 space find + Space spaceBySpaceId = spaceUtils.findSpaceBySpaceId(spaceId); + + // TODO 3. 유저가 요청받은 정산 중 진행 중인 정산 리스트 select + List payRequestTargetListByUser = payDao.findPayRequestTargetListByUser(userByUserId, spaceBySpaceId); + + // TODO 4. return 타입 구성 + // 3-1. 각 payRequestTarget 에 해당하는 정산 요청자, 정산 요청 금액 을 loop를 돌면서 데이터 수집 + List payReceiveInfoDtoList = new ArrayList<>(); + + for (PayRequestTarget payRequestTarget : payRequestTargetListByUser) { + String payCreatorName = payRequestTarget.getPayRequest().getPayCreateUser().getUserName(); // 리펙토링 필요 + int requestAmount = payRequestTarget.getRequestAmount(); + + PayReceiveInfoDto payReceiveInfoDto = new PayReceiveInfoDto(payCreatorName, requestAmount); + + payReceiveInfoDtoList.add(payReceiveInfoDto); + } + + return payReceiveInfoDtoList; + } +} diff --git a/src/main/java/space/space_spring/service/UserService.java b/src/main/java/space/space_spring/service/UserService.java index 2b87090d..4b20794f 100644 --- a/src/main/java/space/space_spring/service/UserService.java +++ b/src/main/java/space/space_spring/service/UserService.java @@ -11,6 +11,7 @@ import space.space_spring.entity.User; import space.space_spring.exception.UserException; import space.space_spring.response.BaseResponse; +import space.space_spring.util.user.UserUtils; import java.util.List; import java.util.Map; @@ -25,6 +26,7 @@ public class UserService { private final UserDao userDao; private final JwtLoginProvider jwtLoginProvider; private final UserSpaceDao userSpaceDao; + private final UserUtils userUtils; @Transactional public PostUserSignupResponse signup(PostUserSignupRequest postUserSignupRequest) { @@ -52,7 +54,7 @@ private void validateEmail(String email) { @Transactional public String login(PostUserLoginRequest postUserLoginRequest) { // TODO 1. 이메일 존재 여부 확인(아이디 존재 여부 확인) - User userByEmail = findUserByEmail(postUserLoginRequest.getEmail()); + User userByEmail = userUtils.findUserByEmail(postUserLoginRequest.getEmail()); log.info("userByEmail.getUserId: {}", userByEmail.getUserId()); // TODO 2. 비밀번호 일치 여부 확인 @@ -67,14 +69,6 @@ public String login(PostUserLoginRequest postUserLoginRequest) { return jwtLogin; } - private User findUserByEmail(String email) { - User findUser = userDao.getUserByEmail(email); - if (findUser == null) { - throw new UserException(EMAIL_NOT_FOUND); - } - return findUser; - } - private void validatePassword(User userByEmail, String password) { if (!userByEmail.passwordMatch(password)) { throw new UserException(PASSWORD_NO_MATCH); @@ -84,7 +78,7 @@ private void validatePassword(User userByEmail, String password) { @Transactional public GetSpaceInfoForUserResponse getSpaceListForUser(Long userId, int size, Long lastUserSpaceId) { // TODO 1. userId로 User find - User userByUserId = findUserByUserId(userId); + User userByUserId = userUtils.findUserByUserId(userId); // TODO 2. user가 속한 스페이스가 없는 경우 -> 예외처리 ?? // (현재 lastUserSpaceId가 -1 & 스페이스 info list는 빈 껍데기로 response가 전달됨) @@ -105,12 +99,4 @@ private void validateSpaceListForUser(User userByUserId) { } - private User findUserByUserId(Long userId) { - User userByUserId = userDao.findUserByUserId(userId); - if (userByUserId == null) { - throw new UserException(USER_NOT_FOUND); - } - return userByUserId; - } - } diff --git a/src/main/java/space/space_spring/util/space/SpaceUtils.java b/src/main/java/space/space_spring/util/space/SpaceUtils.java new file mode 100644 index 00000000..2943bcbc --- /dev/null +++ b/src/main/java/space/space_spring/util/space/SpaceUtils.java @@ -0,0 +1,27 @@ +package space.space_spring.util.space; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; +import space.space_spring.dao.SpaceDao; +import space.space_spring.entity.Space; +import space.space_spring.exception.SpaceException; + +import static space.space_spring.response.status.BaseExceptionResponseStatus.SPACE_NOT_FOUND; + +@Component +@RequiredArgsConstructor +public class SpaceUtils { + + private final SpaceDao spaceDao; + + @Transactional + public Space findSpaceBySpaceId(Long spaceId) { + Space spaceBySpaceId = spaceDao.findSpaceBySpaceId(spaceId); + if (spaceBySpaceId == null) { + throw new SpaceException(SPACE_NOT_FOUND); + } + return spaceBySpaceId; + } + +} diff --git a/src/main/java/space/space_spring/util/user/UserUtils.java b/src/main/java/space/space_spring/util/user/UserUtils.java new file mode 100644 index 00000000..af643deb --- /dev/null +++ b/src/main/java/space/space_spring/util/user/UserUtils.java @@ -0,0 +1,36 @@ +package space.space_spring.util.user; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; +import space.space_spring.dao.UserDao; +import space.space_spring.entity.User; +import space.space_spring.exception.UserException; + +import static space.space_spring.response.status.BaseExceptionResponseStatus.EMAIL_NOT_FOUND; +import static space.space_spring.response.status.BaseExceptionResponseStatus.USER_NOT_FOUND; + +@Component +@RequiredArgsConstructor +public class UserUtils { + + private final UserDao userDao; + + @Transactional + public User findUserByEmail(String email) { + User findUser = userDao.getUserByEmail(email); + if (findUser == null) { + throw new UserException(EMAIL_NOT_FOUND); + } + return findUser; + } + + @Transactional + public User findUserByUserId(Long userId) { + User userByUserId = userDao.findUserByUserId(userId); + if (userByUserId == null) { + throw new UserException(USER_NOT_FOUND); + } + return userByUserId; + } +} diff --git a/src/test/java/space/space_spring/service/PayServiceTest.java b/src/test/java/space/space_spring/service/PayServiceTest.java new file mode 100644 index 00000000..09cdde40 --- /dev/null +++ b/src/test/java/space/space_spring/service/PayServiceTest.java @@ -0,0 +1,113 @@ +package space.space_spring.service; + +import org.junit.jupiter.api.BeforeEach; +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 space.space_spring.dao.PayDao; +import space.space_spring.dto.pay.PayReceiveInfoDto; +import space.space_spring.dto.pay.PayRequestInfoDto; +import space.space_spring.entity.PayRequest; +import space.space_spring.entity.PayRequestTarget; +import space.space_spring.entity.Space; +import space.space_spring.entity.User; +import space.space_spring.util.space.SpaceUtils; +import space.space_spring.util.user.UserUtils; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +class PayServiceTest { + + @InjectMocks + private PayService payService; + + @Mock + private PayDao payDao; + + @Mock + private UserUtils userUtils; + + @Mock + private SpaceUtils spaceUtils; + + private User user1; + private User user2; + private Space testSpace; + private PayRequest testPayRequest; + private PayRequestTarget testPayRequestTarget; + + @BeforeEach + public void 테스트_셋업() { + /** + * user2이 같은 스페이스에 속한 user1에게 정산을 요청한 상황 가정 + */ + user1 = new User(); + user1.saveUser("test1@test.com", "abcDEF123!@", "user1"); + + user2 = new User(); + user2.saveUser("test2@test.com", "abcDEF123!@", "user2"); + + testSpace = new Space(); + testSpace.saveSpace("testSpace", "test_profile_img_url"); + + testPayRequest = new PayRequest(); + testPayRequest.savePayRequest(user2, testSpace, 30000, "우리은행", "111-111-111", false); + + testPayRequestTarget = new PayRequestTarget(); + testPayRequestTarget.savePayRequestTarget(testPayRequest, user1.getUserId(), 10000, false); + } + + @Test + @DisplayName("getPayRequestInfoForUser_메서드_테스트") + void 유저가_요청한_정산_중_진행중인_정산리스트_찾는_메서드_테스트() throws Exception { + //given + when(userUtils.findUserByUserId(user2.getUserId())).thenReturn(user2); + when(spaceUtils.findSpaceBySpaceId(testSpace.getSpaceId())).thenReturn(testSpace); + when(payDao.findPayRequestListByUser(user2, testSpace)).thenReturn(List.of(testPayRequest)); + when(payDao.findPayRequestTargetListByPayRequest(testPayRequest)).thenReturn(List.of(testPayRequestTarget)); + + //when + List payRequestInfoForUser = payService.getPayRequestInfoForUser(user2.getUserId(), testSpace.getSpaceId()); + + //then + assertThat(payRequestInfoForUser.size()).isEqualTo(1); + + for (PayRequestInfoDto payRequestInfoDto : payRequestInfoForUser) { + assertThat(payRequestInfoDto.getTotalAmount()).isEqualTo(30000); + assertThat(payRequestInfoDto.getReceiveAmount()).isEqualTo(0); + assertThat(payRequestInfoDto.getTotalTargetNum()).isEqualTo(1); + assertThat(payRequestInfoDto.getReceiveTargetNum()).isEqualTo(0); + } + } + + @Test + @DisplayName("Test name") + void 유저가_요청받은_정산_중_진행중인_정산리스트_찾는_메서드_테스트() throws Exception { + //given + when(userUtils.findUserByUserId(user1.getUserId())).thenReturn(user1); + when(spaceUtils.findSpaceBySpaceId(testSpace.getSpaceId())).thenReturn(testSpace); + when(payDao.findPayRequestTargetListByUser(user1, testSpace)).thenReturn(List.of(testPayRequestTarget)); + + //when + List payReceiveInfoForUser = payService.getPayReceiveInfoForUser(user1.getUserId(), testSpace.getSpaceId()); + + //then + assertThat(payReceiveInfoForUser.size()).isEqualTo(1); + for (PayReceiveInfoDto payReceiveInfoDto : payReceiveInfoForUser) { + assertThat(payReceiveInfoDto.getPayCreatorName()).isEqualTo(user2.getUserName()); + assertThat(payReceiveInfoDto.getRequestAmount()).isEqualTo(10000); + } + } + + + + +} \ No newline at end of file