Junit Test Application-36-계좌입금 기능

jaegeunsong97·2023년 8월 8일
0

Junit Bank Application 깃허브

Junit Bank Application 기록 노션

  • 서비스 코드
@Transactional // 인증 필요 X
     public AccountDepositResponseDto 계좌입금(AccountDepositRequestDto accountDepositRequestDto) { // ATM -> A계좌
          // 1. 0원 체크
          if (accountDepositRequestDto.getAmount() <= 0L) {
               throw new CustomApiException("0원 이하의 금액을 입금할 수 없습니다. ");
          }

          // 2. 입금계좌 확인
          Account depositAccountPS = accountRepository.findByNumber(accountDepositRequestDto.getNumber()).orElseThrow(
                    () -> new CustomApiException("계좌를 찾을 수 없습니다. "));

          // 3. 입금(해당 계좌 balance 조정 - update문 - 더티채킹)
          depositAccountPS.deposit(accountDepositRequestDto.getAmount());

          // 4. 거래내역 남기기
          Transaction transaction = Transaction.builder()
                    .depositAccount(depositAccountPS)
                    .withdrawAccount(null)
                    .depositAccountBalance(depositAccountPS.getBalance())
                    .withdrawAccountBalance(null)
                    .amount(accountDepositRequestDto.getAmount())
                    .gubun(TransactionEnum.DEPOSIT)
                    .sender("ATM")
                    .receiver(accountDepositRequestDto.getNumber() + "")
                    .tel(accountDepositRequestDto.getTel())
                    .build();

          Transaction transactionPS = transactionRepository.save(transaction);
          return new AccountDepositResponseDto(depositAccountPS, transactionPS);
     }
@NoArgsConstructor
@Getter
@EntityListeners(AuditingEntityListener.class)
@Table(name = "account_tb", indexes = {
          @Index(name = "idx_account_number", columnList = "number")
})
@Entity
public class Account {
.
.
.
public void deposit(Long amount) {
          balance += amount;
     }
  • dto
@Getter
     @Setter
     public static class AccountDepositResponseDto {

          private Long id; // 계좌 ID
          private Long number; // 계좌번호
          private TransactionDto transaction;

          public AccountDepositResponseDto(Account account, Transaction transaction) {
               this.id = account.getId();
               this.number = account.getNumber();
               this.transaction = new TransactionDto(transaction);
          }

          @Getter
          @Setter
          public class TransactionDto {
               private Long id;
               private String gubun;
               private String sender;
               private String receiver;
               private Long amount;
               @JsonIgnore // 주목!!
               private Long depositAccountBalance;
               private String tel;
               private String createdAt;

               public TransactionDto(Transaction transaction) {
                    this.id = transaction.getId();
                    this.gubun = transaction.getGubun().getValue();
                    this.sender = transaction.getSender();
                    this.receiver = transaction.getReceiver();
                    this.amount = transaction.getAmount();
                    this.depositAccountBalance = transaction.getDepositAccountBalance();
                    this.tel = transaction.getTel();
                    this.createdAt = CustomDateUtil.toStringFormat(transaction.getCreateAt());
               }
          }
     }

     @Getter
     @Setter
     public static class AccountDepositRequestDto {

          @NotNull
          @Digits(integer = 4, fraction = 4)
          private Long number;
          @NotNull
          private Long amount;
          @NotEmpty
          @Pattern(regexp = "DEPOSIT")
          private String gubun; // DEPOSIT
          @NotEmpty
          @Pattern(regexp = "^[0-9]{11}")
          private String tel;

     }
  • 컨트롤러 코드
@PostMapping("/account/deposit")
     public ResponseEntity<?> depositAccount(@RequestBody @Valid AccountDepositRequestDto accountDepositRequestDto,
               BindingResult bindingResult) {
          AccountDepositResponseDto accountDepositResponseDto = accountService.계좌입금(accountDepositRequestDto);
          return new ResponseEntity<>(new ResponseDto<>(1, "계좌입금 성공", accountDepositResponseDto), HttpStatus.CREATED);
     }
  • 포스트맨 테스트를 위한 더미 수정
@Configuration
public class DummyDevInit extends DummyObject {

     @Profile("dev") // dev만 동작, prod는 실행 안되야함
     @Bean
     CommandLineRunner init(UserRepository userRepository, AccountRepository accountRepository) {
          return (args) -> {
               // 서버 실행 시, 무조건 실행
               User ssar = userRepository.save(newUser("ssar", "쌀"));
               User cos = userRepository.save(newUser("cos", "코스"));

               Account ssarAccount1 = accountRepository.save(newAccount(1111L, ssar));
               Account cosAccount1 = accountRepository.save(newAccount(2222L, cos));
          };
     }
}

유효성 검사

profile
블로그 이전 : https://medium.com/@jaegeunsong97

0개의 댓글