From b514748f9f0147efc882d38fcac50f24fed7d9fd Mon Sep 17 00:00:00 2001 From: Caramel1004 Date: Tue, 7 May 2024 11:58:12 +0900 Subject: [PATCH 1/6] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20[refactor]=20=ED=8C=8C?= =?UTF-8?q?=EB=9D=BC=EB=AF=B8=ED=84=B0=20=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit == --- .../remittance/v2/RemittanceQuoteV2DTO.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/moin/remittance/domain/dto/remittance/v2/RemittanceQuoteV2DTO.java b/src/main/java/com/moin/remittance/domain/dto/remittance/v2/RemittanceQuoteV2DTO.java index ca32d39..5e1dcf6 100644 --- a/src/main/java/com/moin/remittance/domain/dto/remittance/v2/RemittanceQuoteV2DTO.java +++ b/src/main/java/com/moin/remittance/domain/dto/remittance/v2/RemittanceQuoteV2DTO.java @@ -56,19 +56,19 @@ public RemittanceQuoteEntityV2 toEntity (RemittanceQuoteV2DTO dto) { .build(); } - public static RemittanceQuoteV2DTO of (RemittanceQuoteEntityV2 dto) { - if(dto == null) { + public static RemittanceQuoteV2DTO of (RemittanceQuoteEntityV2 entity) { + if(entity == null) { throw new NullPointerQuotationException(BAD_NOT_MATCH_QUOTATION); } return RemittanceQuoteV2DTO.builder() - .sourceAmount(dto.getSourceAmount())// 원화 - .fee(new BigDecimal(String.valueOf(dto.getFee())))// 수수료 - .usdExchangeRate(dto.getUsdExchangeRate()) - .usdAmount(dto.getUsdAmount())// USD 송금액 - .targetCurrency(dto.getTargetCurrency()) - .targetAmount(dto.getTargetAmount())// 받는 금액 - .exchangeRate(dto.getExchangeRate()) - .expireTime(dto.getExpireTime())// 송금 견적서 만료 기간 + .sourceAmount(entity.getSourceAmount())// 원화 + .fee(new BigDecimal(String.valueOf(entity.getFee())))// 수수료 + .usdExchangeRate(entity.getUsdExchangeRate()) + .usdAmount(entity.getUsdAmount())// USD 송금액 + .targetCurrency(entity.getTargetCurrency()) + .targetAmount(entity.getTargetAmount())// 받는 금액 + .exchangeRate(entity.getExchangeRate()) + .expireTime(entity.getExpireTime())// 송금 견적서 만료 기간 .build(); } From ceb3ec69dee5c99bc78c78e4f2e216f9774f1316 Mon Sep 17 00:00:00 2001 From: Caramel1004 Date: Wed, 8 May 2024 18:39:17 +0900 Subject: [PATCH 2/6] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20[refactor]=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=EC=9D=84=20=EC=9C=84=ED=95=9C=20toString=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EC=9E=AC=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ♻️ 테스트 데이터 확인을 위한 toString 메소드 재정의 --- .../domain/entity/member/v2/MemberEntityV2.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/com/moin/remittance/domain/entity/member/v2/MemberEntityV2.java b/src/main/java/com/moin/remittance/domain/entity/member/v2/MemberEntityV2.java index 87ba90d..c0b2bb9 100644 --- a/src/main/java/com/moin/remittance/domain/entity/member/v2/MemberEntityV2.java +++ b/src/main/java/com/moin/remittance/domain/entity/member/v2/MemberEntityV2.java @@ -36,4 +36,16 @@ public class MemberEntityV2 { @Column(name = "ID_VALUE", nullable = false) private String idValue; + + @Override + public String toString() { + return "MemberEntityV2 {" + "\n" + + "\tindex: " + index + ",\n" + + "\tuserId: '" + userId + '\''+ ",\n" + + "\tpassword: '" + password+ '\'' + ",\n" + + "\tname: '" + name+ '\'' + ",\n" + + "\tidType: '" + idType+ '\'' + ",\n" + + "\tidValue: '" + idValue + '\'' + ",\n" + + '}'; + } } From 6cb54325ec59c3076d76102a814f486542adbdd6 Mon Sep 17 00:00:00 2001 From: Caramel1004 Date: Wed, 8 May 2024 18:40:48 +0900 Subject: [PATCH 3/6] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20[refactor]=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=EC=9D=84=20=EC=9C=84=ED=95=9C=20toString=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EC=9E=AC=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ♻️ 테스트 데이터 확인을 위한 toString메소드 재정의 --- .../v2/RemittanceQuoteEntityV2.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/com/moin/remittance/domain/entity/remittance/v2/RemittanceQuoteEntityV2.java b/src/main/java/com/moin/remittance/domain/entity/remittance/v2/RemittanceQuoteEntityV2.java index 4419146..6e074f0 100644 --- a/src/main/java/com/moin/remittance/domain/entity/remittance/v2/RemittanceQuoteEntityV2.java +++ b/src/main/java/com/moin/remittance/domain/entity/remittance/v2/RemittanceQuoteEntityV2.java @@ -1,6 +1,7 @@ package com.moin.remittance.domain.entity.remittance.v2; import jakarta.persistence.*; +import jakarta.validation.constraints.Positive; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -24,6 +25,7 @@ public class RemittanceQuoteEntityV2 { // 송금 할 금액(원화) @Column(name = "source_amount", nullable = false) + @Positive private long sourceAmount; // 수수료 = 보내는금액(amount: 원화) * 수수료율 + 고정 수수료 @@ -36,6 +38,7 @@ public class RemittanceQuoteEntityV2 { // USD 송금액 @Column(name = "usd_amount", nullable = false) + @Positive private BigDecimal usdAmount; // 받는 환율 정보 @@ -48,6 +51,7 @@ public class RemittanceQuoteEntityV2 { // 받는 금액 @Column(name = "target_amount", nullable = false) + @Positive private BigDecimal targetAmount; // 만료 기간 @@ -56,4 +60,20 @@ public class RemittanceQuoteEntityV2 { @Column(name = "user_id", nullable = false) private String userId; + + @Override + public String toString() { + return "RemittanceQuoteEntityV2 {" + "\n" + + "\tquoteId: " + quoteId + ",\n" + + "\tsourceAmount: " + sourceAmount + ",\n" + + "\tfee: " + fee + ",\n" + + "\tusdExchangeRate: " + usdExchangeRate + ",\n" + + "\tusdAmount: " + usdAmount + ",\n" + + "\ttargetCurrency: '" + targetCurrency + '\'' + ",\n" + + "\ttargetAmount: " + targetAmount + ",\n" + + "\texchangeRate: " + exchangeRate + ",\n" + + "\texpireTime: " + expireTime + ",\n" + + "\tuserId: '" + userId + '\'' + ",\n" + + '}'; + } } From 03a4d099cd5070053d462de46126d626d856e3d1 Mon Sep 17 00:00:00 2001 From: Caramel1004 Date: Thu, 9 May 2024 20:47:59 +0900 Subject: [PATCH 4/6] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20[refactor]=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=EC=9D=84=20=EC=9C=84=ED=95=9C=20toString=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EC=9E=AC=EC=A0=95=EC=9D=98=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ♻️ [refactor] 테스트 데이터 확인을 위한 toString 메소드 재정의 코드 추가 --- .../remittance/v2/RemittanceLogEntityV2.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/com/moin/remittance/domain/entity/remittance/v2/RemittanceLogEntityV2.java b/src/main/java/com/moin/remittance/domain/entity/remittance/v2/RemittanceLogEntityV2.java index e537469..7c3278d 100644 --- a/src/main/java/com/moin/remittance/domain/entity/remittance/v2/RemittanceLogEntityV2.java +++ b/src/main/java/com/moin/remittance/domain/entity/remittance/v2/RemittanceLogEntityV2.java @@ -56,4 +56,20 @@ public class RemittanceLogEntityV2 { @Column(name = "user_id", nullable = false) private String userId; + + @Override + public String toString() { + return "RemittanceLogEntityV2 {" + "\n" + + "\tlogId: " + logId + ",\n" + + "\tsourceAmount: " + sourceAmount + ",\n" + + "\tfee: " + fee + ",\n" + + "\tusdExchangeRate: " + usdExchangeRate + ",\n" + + "\tusdAmount: " + usdAmount + ",\n" + + "\ttargetCurrency: '" + targetCurrency + '\'' + ",\n" + + "\ttargetAmount: " + targetAmount + ",\n" + + "\texchangeRate: " + exchangeRate + ",\n" + + "\texpireTime: " + requestedDate + ",\n" + + "\tuserId: '" + userId + '\'' + ",\n" + + '}'; + } } From 9c98a36c359ab35a0fe4e89d4bad23adfc2cfd29 Mon Sep 17 00:00:00 2001 From: Caramel1004 Date: Thu, 9 May 2024 21:01:26 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20[refactor]=20Repositor?= =?UTF-8?q?y=20Test=EC=97=90=20=EC=82=AC=EC=9A=A9=ED=95=A0=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EC=9A=A9=20JPA=EC=BF=BC=EB=A6=AC=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [refactor] Repository Test에 사용할 테스트용 JPA쿼리 코드 추가 --- .../com/moin/remittance/repository/v2/MemberRepositoryV2.java | 4 ++++ .../remittance/repository/v2/RemittanceLogRepositoryV2.java | 3 +++ .../moin/remittance/repository/v2/RemittanceRepositoryV2.java | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/src/main/java/com/moin/remittance/repository/v2/MemberRepositoryV2.java b/src/main/java/com/moin/remittance/repository/v2/MemberRepositoryV2.java index be98b93..11af330 100644 --- a/src/main/java/com/moin/remittance/repository/v2/MemberRepositoryV2.java +++ b/src/main/java/com/moin/remittance/repository/v2/MemberRepositoryV2.java @@ -5,6 +5,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.lang.reflect.Member; import java.util.UUID; @Repository @@ -17,4 +18,7 @@ public interface MemberRepositoryV2 extends JpaRepository @Query(value = "select name from member_v2 where user_id = :userId", nativeQuery = true) String getNameOfMemberByUserId(String userId); + + /* TEST Query Method*/ + MemberEntityV2 findByUserIdAndPassword(String userId, String password); } diff --git a/src/main/java/com/moin/remittance/repository/v2/RemittanceLogRepositoryV2.java b/src/main/java/com/moin/remittance/repository/v2/RemittanceLogRepositoryV2.java index 50b9600..286adfb 100644 --- a/src/main/java/com/moin/remittance/repository/v2/RemittanceLogRepositoryV2.java +++ b/src/main/java/com/moin/remittance/repository/v2/RemittanceLogRepositoryV2.java @@ -10,4 +10,7 @@ @Repository public interface RemittanceLogRepositoryV2 extends JpaRepository { List findByUserId(String userId); + + /* 테스트 용 쿼리 메소드 */ + boolean existsByUserId(String userId); } diff --git a/src/main/java/com/moin/remittance/repository/v2/RemittanceRepositoryV2.java b/src/main/java/com/moin/remittance/repository/v2/RemittanceRepositoryV2.java index 58367b4..4a636ed 100644 --- a/src/main/java/com/moin/remittance/repository/v2/RemittanceRepositoryV2.java +++ b/src/main/java/com/moin/remittance/repository/v2/RemittanceRepositoryV2.java @@ -10,4 +10,8 @@ public interface RemittanceRepositoryV2 extends JpaRepository { // 송금 견적서 조회 RemittanceQuoteEntityV2 findByQuoteIdAndUserId(UUID quoteId, String userId); + + boolean existsByQuoteIdAndUserId(UUID quoteId, String userId); + + } From 0fe6cdbf9ff03b7e36ef1ae9f6047d461008f815 Mon Sep 17 00:00:00 2001 From: Caramel1004 Date: Thu, 9 May 2024 21:02:56 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=E2=9C=85=20[TEST]=20Repository=20Test=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ [TEST] Repository Test 코드 추가 --- .../repository/v2/MemberRepositoryV2Test.java | 119 +++++++++++ .../v2/RemittanceLogRepositoryV2Test.java | 169 ++++++++++++++++ .../v2/RemittanceRepositoryV2Test.java | 187 ++++++++++++++++++ 3 files changed, 475 insertions(+) create mode 100644 src/test/java/com/moin/remittance/repository/v2/MemberRepositoryV2Test.java create mode 100644 src/test/java/com/moin/remittance/repository/v2/RemittanceLogRepositoryV2Test.java diff --git a/src/test/java/com/moin/remittance/repository/v2/MemberRepositoryV2Test.java b/src/test/java/com/moin/remittance/repository/v2/MemberRepositoryV2Test.java new file mode 100644 index 0000000..d7cbcbc --- /dev/null +++ b/src/test/java/com/moin/remittance/repository/v2/MemberRepositoryV2Test.java @@ -0,0 +1,119 @@ +package com.moin.remittance.repository.v2; + +import com.moin.remittance.domain.entity.member.v2.MemberEntityV2; +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.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + + +@DataJpaTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY) // 가짜 DB로 테스트 +@ExtendWith(SpringExtension.class) +public class MemberRepositoryV2Test { + + @Autowired + private MemberRepositoryV2 memberRepositoryV2; + + private MemberEntityV2 createMemberTestCase( + String userId, String password, String name, String idType, String idValue + ) { + return MemberEntityV2.builder() + .userId(userId) + .password(password) + .name(name) + .idType(idType) + .idValue(idValue) + .build(); + } + + /** + * 회원가입 + * 유저데이터 저장 -> 유저 조회 where(유저아이디, 비밀번호) + */ + @BeforeEach + @Test + @DisplayName("회원 저장") + void saveUserTest() { + String REG_NO = "REG_NO"; + String BUSINESS_NO = "BUSINESS_NO"; + // given: 회원 엔티티 + MemberEntityV2 member1 = createMemberTestCase( + "test@test.com", "1234", "카라멜프라프치노", REG_NO, "111111-1111111" + ); + MemberEntityV2 member2 = createMemberTestCase( + "test2@test.com", "3333", "자바칩프라푸치노", REG_NO, "222222-2222222" + ); + MemberEntityV2 member3 = createMemberTestCase( + "test3@test.com", "3333", "(주) 컴포즈", BUSINESS_NO, "222222-2222222" + ); + + // when: 회원 저장 + MemberEntityV2 savedMember1 = memberRepositoryV2.save(member1); + MemberEntityV2 savedMember2 = memberRepositoryV2.save(member2); + MemberEntityV2 savedMember3 = memberRepositoryV2.save(member3); + + // then: junit 메소드로 값 비교 + assertEquals(member1, savedMember1); + assertEquals(member2, savedMember2); + assertEquals(member3, savedMember3); + + // then: PK가 잘 생성되었는지 확인 + assertNotNull(savedMember1.getIndex()); + assertNotNull(savedMember2.getIndex()); + assertNotNull(savedMember3.getIndex()); + assertNotEquals(member1, savedMember2); + assertNotEquals(member2, savedMember1); + assertNotEquals(savedMember1, savedMember2); + assertNotEquals(savedMember1, savedMember3); + } + + /** + * 회원가입 -> 로그인 + */ + @Test + @DisplayName("Parameter: 유저아이디, 비밀번호 => 회원 조회") + void findByUserIdAndPasswordTest() { + // then: 회원 조회 + assertTrue(memberRepositoryV2.existsByUserIdAndPassword("test@test.com", "1234")); // true && true == true + assertFalse(memberRepositoryV2.existsByUserIdAndPassword("test@test.com", "123")); // true && false == false + assertFalse(memberRepositoryV2.existsByUserIdAndPassword("test@test2.com", "1234")); // false && true == false + assertFalse(memberRepositoryV2.existsByUserIdAndPassword("test@test2.com", "123")); // false && false == false + + // Test Data 확인 + System.out.println("====** 회원 조회 **===="); + System.out.println("====↑↑ Test Data ↑↑===="); + System.out.println(memberRepositoryV2.findByUserIdAndPassword("test@test.com", "1234")); + } + + @Test + @DisplayName("모든 회원 조회") + void findAllMember() { + List dataAll = memberRepositoryV2.findAll(); + + // then: Not Null 체크 + dataAll.forEach(data -> { + assertNotNull(data); + assertNotNull(data.getIndex()); + assertNotNull(data.getUserId()); + assertNotNull(data.getPassword()); + assertNotNull(data.getName()); + assertNotNull(data.getIdType()); + assertNotNull(data.getIdValue()); + }); + + // Test Data 확인 + System.out.println("====** 모든 회원 조회 **===="); + System.out.println("====↑↑ Test Data ↑↑===="); + dataAll.forEach(System.out::println); + } + +} diff --git a/src/test/java/com/moin/remittance/repository/v2/RemittanceLogRepositoryV2Test.java b/src/test/java/com/moin/remittance/repository/v2/RemittanceLogRepositoryV2Test.java new file mode 100644 index 0000000..743074f --- /dev/null +++ b/src/test/java/com/moin/remittance/repository/v2/RemittanceLogRepositoryV2Test.java @@ -0,0 +1,169 @@ +package com.moin.remittance.repository.v2; + +import com.moin.remittance.application.v2.transfer.impl.estimating.calculating.ExchangeRateCalculator; +import com.moin.remittance.application.v2.transfer.impl.estimating.policy.RemittanceFeePolicy; +import com.moin.remittance.domain.entity.remittance.v2.RemittanceLogEntityV2; +import com.moin.remittance.domain.entity.remittance.v2.RemittanceQuoteEntityV2; +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.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.math.BigDecimal; +import java.time.OffsetDateTime; +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +@DataJpaTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY) +@ExtendWith(SpringExtension.class) +public class RemittanceLogRepositoryV2Test { + + @Autowired + private RemittanceLogRepositoryV2 remittanceLogRepositoryV2; + + private String userIdTestCase; + + private RemittanceLogEntityV2 createRemittanceLogTestCase( + long sourceAmount, int currencyUnit, BigDecimal usdBasePrice, BigDecimal basePrice, String currencyCode, String userId + ) { + ExchangeRateCalculator exchangeRateCalculator = new ExchangeRateCalculator(); + RemittanceFeePolicy feePolicy = new RemittanceFeePolicy(); + + BigDecimal fee = feePolicy.calculateRemittanceFee(sourceAmount); + + return RemittanceLogEntityV2.builder()// 송금 견적서 DTO + .sourceAmount(sourceAmount) // 원화 + .fee(fee) // 수수료 + .usdExchangeRate(usdBasePrice) // USD 환율 + .usdAmount(exchangeRateCalculator.calculateExchangeRate( + sourceAmount, + fee, + 1, + usdBasePrice, + "USD") + ) // USD 송금액 + .targetCurrency(currencyCode) // 타겟 통화 + .exchangeRate(basePrice) // 환율 + .targetAmount(exchangeRateCalculator.calculateExchangeRate( + sourceAmount, + fee, + currencyUnit, + basePrice, + currencyCode) + ) // 받는 금액 + .requestedDate(OffsetDateTime.now()) // 송금 요청 시간 + .userId(userId) + .build(); + } + + + @Test + @DisplayName("송금 거래 이력 저장") + @BeforeEach + void saveTest() { + // given: 저장할 송금 견적서 엔티티 + RemittanceLogEntityV2 log1 = createRemittanceLogTestCase( + 50000, 1, new BigDecimal("1362.50"), new BigDecimal("1362.50"), "USD", "test1@test.com" + ); + RemittanceLogEntityV2 log2 = createRemittanceLogTestCase( + 70000, 100, new BigDecimal("1362.50"), new BigDecimal("884.67"), "JPY", "test1@test.com" + ); + RemittanceLogEntityV2 log3 = createRemittanceLogTestCase( + 800000, 1, new BigDecimal("1380.00"), new BigDecimal("1380.00"), "USD", "test2@test.com" + ); + + // given: 저장하지 않을 송금 견적서 엔티티 + RemittanceLogEntityV2 logCopy1 = createRemittanceLogTestCase( + 50000, 1, new BigDecimal("1362.50"), new BigDecimal("1362.50"), "USD", "test1@test.com" + ); + RemittanceLogEntityV2 logCopy2 = createRemittanceLogTestCase( + 70000, 100, new BigDecimal("1362.50"), new BigDecimal("884.67"), "JPY", "test2@test.com" + ); + + // when: 송금 견적서 저장 + RemittanceLogEntityV2 savedLog1 = remittanceLogRepositoryV2.saveAndFlush(log1); + RemittanceLogEntityV2 savedLog2 = remittanceLogRepositoryV2.saveAndFlush(log2); + RemittanceLogEntityV2 savedLog3 = remittanceLogRepositoryV2.saveAndFlush(log3); + + // then: 생성한 엔티티와 그 생성한 엔티티를 저장한 데이터가 같은지 확인 + assertEquals(log1, savedLog1); + assertEquals(log2, savedLog2); + assertEquals(log3, savedLog3); + + // then: PK가 잘 생성되었는지 확인 == 모든 송금 견적서는 서로 다름 + assertNotNull(savedLog1.getLogId()); + assertNotNull(savedLog2.getLogId()); + assertNotNull(savedLog3.getLogId()); + assertNotEquals(log1, savedLog2); + assertNotEquals(log2, savedLog1); + assertNotEquals(savedLog1, savedLog2); + assertNotEquals(savedLog1, savedLog3); + + /* + * then: DB 저장하지않은 엔티티는 quoteId가 null == 저장한 견적서와 내용이 같더라도 저장하지 않았으므로 서로 다른 견적서 + * */ + assertNull(logCopy1.getLogId()); + assertNull(logCopy2.getLogId()); + assertNotEquals(logCopy1, savedLog1); + assertNotEquals(logCopy2, savedLog2); + + this.userIdTestCase = savedLog1.getUserId(); + } + + @Test + @DisplayName("모든 유저의 송금 거래 이력 조회") + void findAllRemittanceLogList() { + // when: 송금 거래 리스트 + List dataAll = remittanceLogRepositoryV2.findAll(); + + // then: Not Null 체크 + dataAll.forEach(data -> { + assertNotNull(data); + assertNotNull(data.getLogId()); + assertTrue(data.getSourceAmount() > 0); + assertNotNull(data.getFee()); + assertNotNull(data.getUsdExchangeRate()); + assertNotNull(data.getUsdAmount()); + assertNotNull(data.getTargetAmount()); + assertNotNull(data.getExchangeRate()); + assertNotNull(data.getUserId()); + assertNotNull(data.getRequestedDate()); + }); + + // Test Data 확인 + System.out.println("====** 모든 유저의 송금 거래 내역 조회 **===="); + System.out.println("====↑↑ Test Data ↑↑===="); + System.out.println(dataAll); + } + + @Test + @DisplayName("특정 유저의 송금 거래 이력 조회") + void findRemittanceLogListByUserId() { + // when: 견적서 번호와 유저아이디가 일치하는 발행한 견적서 조회 + List remittanceLog = remittanceLogRepositoryV2.findByUserId(this.userIdTestCase); + + // then: 조회 결과값 확인 + remittanceLog.forEach(log -> assertEquals(this.userIdTestCase, log.getUserId()));// 요소가 거래 이력 객체인 리스트 안에 유저 아이디가 같은지 확인 + assertEquals(0, remittanceLogRepositoryV2.findByUserId("test@test2.com").size());// 존재하지 않으므로 리스트 사이즈가 0 이어야함 + + assertTrue(remittanceLogRepositoryV2.existsByUserId(this.userIdTestCase));// 일치하는 거래 이력 + + assertFalse(remittanceLog.isEmpty());// 리스트가 비어있으면 안됨 + assertFalse(remittanceLogRepositoryV2.existsByUserId("test@test2.com"));// 해당 유저의 데이터가 존재하지 않는 경우 + + assertNotNull(remittanceLog); + assertNotNull(remittanceLogRepositoryV2.findByUserId("test@test2.com"));// 사이즈가 0인 리스트가 리턴되어야함 + + // Test Data 확인 + System.out.println("====** 특정 유저의 송금 거래 이력 조회 **===="); + System.out.println(remittanceLog); + } +} diff --git a/src/test/java/com/moin/remittance/repository/v2/RemittanceRepositoryV2Test.java b/src/test/java/com/moin/remittance/repository/v2/RemittanceRepositoryV2Test.java index c9dfa47..c90fe6f 100644 --- a/src/test/java/com/moin/remittance/repository/v2/RemittanceRepositoryV2Test.java +++ b/src/test/java/com/moin/remittance/repository/v2/RemittanceRepositoryV2Test.java @@ -1,4 +1,191 @@ package com.moin.remittance.repository.v2; +import com.moin.remittance.application.v2.transfer.impl.estimating.calculating.ExchangeRateCalculator; +import com.moin.remittance.application.v2.transfer.impl.estimating.policy.RemittanceFeePolicy; +import com.moin.remittance.domain.entity.remittance.v2.RemittanceQuoteEntityV2; + +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.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.math.BigDecimal; +import java.time.OffsetDateTime; +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +@DataJpaTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY) // 가짜 DB로 테스트 +@ExtendWith(SpringExtension.class) public class RemittanceRepositoryV2Test { + + @Autowired + private RemittanceRepositoryV2 remittanceRepositoryV2; + + private UUID quoteIdTestCase; + private String userIdTestCase; + + private RemittanceQuoteEntityV2 createQuotationTestCase(long sourceAmount, int currencyUnit, BigDecimal usdBasePrice, BigDecimal basePrice, String currencyCode) { + ExchangeRateCalculator exchangeRateCalculator = new ExchangeRateCalculator(); + RemittanceFeePolicy feePolicy = new RemittanceFeePolicy(); + + BigDecimal fee = feePolicy.calculateRemittanceFee(sourceAmount); + + return RemittanceQuoteEntityV2.builder()// 송금 견적서 DTO + .sourceAmount(sourceAmount) // 원화 + .fee(fee) // 수수료 + .usdExchangeRate(usdBasePrice) // USD 환율 + .usdAmount(exchangeRateCalculator.calculateExchangeRate( + sourceAmount, + fee, + 1, + usdBasePrice, + "USD") + ) // USD 송금액 + .targetCurrency(currencyCode) // 타겟 통화 + .exchangeRate(basePrice) // 환율 + .targetAmount(exchangeRateCalculator.calculateExchangeRate( + sourceAmount, + fee, + currencyUnit, + basePrice, + currencyCode) + ) // 받는 금액 + .expireTime(OffsetDateTime.now().plusMinutes(10)) // 송금 견적서 만료 기간 + .userId("test@test.com") + .build(); + } + + @BeforeEach + @Test + @DisplayName("@BeforeEach: 송금 견적서 저장") + void saveSuccessfulTest() { + // given: 저장할 송금 견적서 엔티티 + RemittanceQuoteEntityV2 quote1 = createQuotationTestCase( + 50000, 1, new BigDecimal("1362.50"), new BigDecimal("1362.50"), "USD" + ); + RemittanceQuoteEntityV2 quote2 = createQuotationTestCase( + 70000, 100, new BigDecimal("1362.50"), new BigDecimal("884.67"), "JPY" + ); + RemittanceQuoteEntityV2 quote3 = createQuotationTestCase( + 800000, 1, new BigDecimal("1380.00"), new BigDecimal("1380.00"), "USD" + ); + + // given: 저장하지 않을 송금 견적서 엔티티 + RemittanceQuoteEntityV2 quoteCopy1 = createQuotationTestCase( + 50000, 1, new BigDecimal("1362.50"), new BigDecimal("1362.50"), "USD" + ); + RemittanceQuoteEntityV2 quoteCopy2 = createQuotationTestCase( + 70000, 100, new BigDecimal("1362.50"), new BigDecimal("884.67"), "JPY" + ); + + // when: 송금 견적서 저장 + RemittanceQuoteEntityV2 savedQuote1 = remittanceRepositoryV2.saveAndFlush(quote1); + RemittanceQuoteEntityV2 savedQuote2 = remittanceRepositoryV2.saveAndFlush(quote2); + RemittanceQuoteEntityV2 savedQuote3 = remittanceRepositoryV2.saveAndFlush(quote3); + + // then: 생성한 엔티티와 그 생성한 엔티티를 저장한 데이터가 같은지 확인 + assertEquals(quote1, savedQuote1); + assertEquals(quote2, savedQuote2); + assertEquals(quote3, savedQuote3); + + // then: PK가 잘 생성되었는지 확인 == 모든 송금 견적서는 서로 다름 + assertNotNull(savedQuote1.getQuoteId()); + assertNotNull(savedQuote2.getQuoteId()); + assertNotNull(savedQuote3.getQuoteId()); + assertNotEquals(quote1, savedQuote2); + assertNotEquals(quote2, savedQuote1); + assertNotEquals(savedQuote1, savedQuote2); + assertNotEquals(savedQuote1, savedQuote3); + + /* + * then: DB 저장하지않은 엔티티는 quoteId가 null == 저장한 견적서와 내용이 같더라도 저장하지 않았으므로 서로 다른 견적서 + * */ + assertNull(quoteCopy1.getQuoteId()); + assertNull(quoteCopy2.getQuoteId()); + assertNotEquals(quoteCopy1, savedQuote1); + assertNotEquals(quoteCopy2, savedQuote2); + + this.quoteIdTestCase = quote1.getQuoteId(); + this.userIdTestCase = quote1.getUserId(); + } + + @Test + @DisplayName("모든 송금 견적서 조회") + void findAllQuotationTest() { + // when: 송금 견적서 리스트 + List dataAll = remittanceRepositoryV2.findAll(); + + // then: Not Null 체크 + dataAll.forEach(data -> { + assertNotNull(data); + assertNotNull(data.getQuoteId()); + assertTrue(data.getSourceAmount() > 0); + assertNotNull(data.getFee()); + assertNotNull(data.getUsdExchangeRate()); + assertNotNull(data.getUsdAmount()); + assertNotNull(data.getTargetAmount()); + assertNotNull(data.getExchangeRate()); + assertNotNull(data.getUserId()); + assertNotNull(data.getExpireTime()); + }); + + // Test Data 확인 + System.out.println("====** 모든 송금 견적서 조회 **===="); + System.out.println("====↑↑ Test Data ↑↑===="); + dataAll.stream() + .map(RemittanceQuoteEntityV2::toString) + .forEach(System.out::println); + } + + @Test + @DisplayName("견적서 번호와 유저아이디가 일치하는 발행한 견적서 조회") + void findAllByQuoteIdAndUserIdTest() { + // when: 견적서 번호와 유저아이디가 일치하는 발행한 견적서 조회 + RemittanceQuoteEntityV2 quotation = remittanceRepositoryV2.findByQuoteIdAndUserId(this.quoteIdTestCase, this.userIdTestCase); + + // then: 조회 결과값 확인 + assertEquals(this.quoteIdTestCase, quotation.getQuoteId()); + assertEquals(this.userIdTestCase, quotation.getUserId()); + assertTrue(remittanceRepositoryV2.existsByQuoteIdAndUserId(this.quoteIdTestCase, this.userIdTestCase)); + assertTrue(remittanceRepositoryV2.existsByQuoteIdAndUserId(quotation.getQuoteId(), quotation.getUserId())); + + assertFalse(remittanceRepositoryV2.existsByQuoteIdAndUserId(this.quoteIdTestCase, "test2@test.com")); + assertFalse(remittanceRepositoryV2.existsByQuoteIdAndUserId(null, "test@test.com")); + assertFalse(remittanceRepositoryV2.existsByQuoteIdAndUserId(UUID.fromString("2aeb538f-e05c-49ff-bb64-ae6fa93d494f"), "test@test.com")); + assertFalse(remittanceRepositoryV2.existsByQuoteIdAndUserId(null, "test2@test.com")); + + assertNull(remittanceRepositoryV2.findByQuoteIdAndUserId(this.quoteIdTestCase, "test2@test.com"));// 일치하는 유저가 없는 경우 + assertNull(remittanceRepositoryV2.findByQuoteIdAndUserId(null, "test@test.com"));// 견적서 아이디값이 null일 경우 + assertNull(remittanceRepositoryV2.findByQuoteIdAndUserId(UUID.fromString("2aeb538f-e05c-49ff-bb64-ae6fa93d494f"), "test@test.com"));// 일치하는 견적서 아이디값이 없는 경우 + assertNull(remittanceRepositoryV2.findByQuoteIdAndUserId(null, "test2@test.com"));// 둘다 일치하지 않는 경우 + + // Test Data 확인 + System.out.println("====** 견적서 번호로 발행한 견적서 조회 **===="); + System.out.println(quotation); + } + + @Test + @DisplayName("송금 견적서 삭제") + void removeTest() { + System.out.println("====** 삭제 전 **===="); + System.out.println(remittanceRepositoryV2.findAll()); + + // when + remittanceRepositoryV2.deleteById(this.quoteIdTestCase); + + // then + assertFalse(remittanceRepositoryV2.findById(this.quoteIdTestCase).isPresent()); + assertFalse(remittanceRepositoryV2.existsById(this.quoteIdTestCase)); + + System.out.println("====** 삭제 후 **===="); + System.out.println(remittanceRepositoryV2.findAll()); + } }