Skip to content

Commit

Permalink
✅ [TEST] Repository Test 코드 추가
Browse files Browse the repository at this point in the history
✅ [TEST] Repository Test 코드 추가
  • Loading branch information
Caramel1004 committed May 9, 2024
1 parent 9c98a36 commit 0fe6cdb
Show file tree
Hide file tree
Showing 3 changed files with 475 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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(
"[email protected]", "1234", "카라멜프라프치노", REG_NO, "111111-1111111"
);
MemberEntityV2 member2 = createMemberTestCase(
"[email protected]", "3333", "자바칩프라푸치노", REG_NO, "222222-2222222"
);
MemberEntityV2 member3 = createMemberTestCase(
"[email protected]", "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("[email protected]", "1234")); // true && true == true
assertFalse(memberRepositoryV2.existsByUserIdAndPassword("[email protected]", "123")); // true && false == false
assertFalse(memberRepositoryV2.existsByUserIdAndPassword("[email protected]", "1234")); // false && true == false
assertFalse(memberRepositoryV2.existsByUserIdAndPassword("[email protected]", "123")); // false && false == false

// Test Data 확인
System.out.println("====** 회원 조회 **====");
System.out.println("====↑↑ Test Data ↑↑====");
System.out.println(memberRepositoryV2.findByUserIdAndPassword("[email protected]", "1234"));
}

@Test
@DisplayName("모든 회원 조회")
void findAllMember() {
List<MemberEntityV2> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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", "[email protected]"
);
RemittanceLogEntityV2 log2 = createRemittanceLogTestCase(
70000, 100, new BigDecimal("1362.50"), new BigDecimal("884.67"), "JPY", "[email protected]"
);
RemittanceLogEntityV2 log3 = createRemittanceLogTestCase(
800000, 1, new BigDecimal("1380.00"), new BigDecimal("1380.00"), "USD", "[email protected]"
);

// given: 저장하지 않을 송금 견적서 엔티티
RemittanceLogEntityV2 logCopy1 = createRemittanceLogTestCase(
50000, 1, new BigDecimal("1362.50"), new BigDecimal("1362.50"), "USD", "[email protected]"
);
RemittanceLogEntityV2 logCopy2 = createRemittanceLogTestCase(
70000, 100, new BigDecimal("1362.50"), new BigDecimal("884.67"), "JPY", "[email protected]"
);

// 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<RemittanceLogEntityV2> 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<RemittanceLogEntityV2> remittanceLog = remittanceLogRepositoryV2.findByUserId(this.userIdTestCase);

// then: 조회 결과값 확인
remittanceLog.forEach(log -> assertEquals(this.userIdTestCase, log.getUserId()));// 요소가 거래 이력 객체인 리스트 안에 유저 아이디가 같은지 확인
assertEquals(0, remittanceLogRepositoryV2.findByUserId("[email protected]").size());// 존재하지 않으므로 리스트 사이즈가 0 이어야함

assertTrue(remittanceLogRepositoryV2.existsByUserId(this.userIdTestCase));// 일치하는 거래 이력

assertFalse(remittanceLog.isEmpty());// 리스트가 비어있으면 안됨
assertFalse(remittanceLogRepositoryV2.existsByUserId("[email protected]"));// 해당 유저의 데이터가 존재하지 않는 경우

assertNotNull(remittanceLog);
assertNotNull(remittanceLogRepositoryV2.findByUserId("[email protected]"));// 사이즈가 0인 리스트가 리턴되어야함

// Test Data 확인
System.out.println("====** 특정 유저의 송금 거래 이력 조회 **====");
System.out.println(remittanceLog);
}
}
Loading

0 comments on commit 0fe6cdb

Please sign in to comment.