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

test: 청약 중과 청약 종료된 상품에 대한 테스트 코드 작성 #19

Merged
merged 4 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'

// db
// mysql
runtimeOnly 'com.mysql:mysql-connector-j'

// h2
testRuntimeOnly 'com.h2database:h2'

// devtools
developmentOnly 'org.springframework.boot:spring-boot-devtools'

Expand All @@ -60,6 +63,9 @@ dependencies {

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'com.squareup.okhttp3:mockwebserver:4.10.0'
testImplementation "org.testcontainers:junit-jupiter:1.16.3"
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.wl2c.elswhereproductservice.domain.product.repository;

import com.wl2c.elswhereproductservice.domain.product.model.ProductState;
import com.wl2c.elswhereproductservice.domain.product.model.ProductType;
import com.wl2c.elswhereproductservice.domain.product.model.entity.Product;
import com.wl2c.elswhereproductservice.mock.ProductMock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.math.BigDecimal;
import java.time.LocalDate;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@DataJpaTest
public class ProductRepositoryTest {

@Autowired
private ProductRepository productRepository;

@BeforeEach
void before() {
productRepository.deleteAll();

Product product1 = ProductMock.create(
3L,
"AA증권",
"1호",
"삼성전자 / S&P500 / KOSPI200",
3,
LocalDate.now().minusDays(1),
LocalDate.now().plusYears(3),
new BigDecimal("10.423"),
LocalDate.now().minusDays(14),
LocalDate.now().minusDays(1),
"95-90-85-80-75-50",
45,
ProductType.STEP_DOWN,
ProductState.ACTIVE);

Product product2 = ProductMock.create(
2L,
"BB증권",
"2호",
"Tesla / HSCEI / NVIDIA",
3,
LocalDate.now().plusDays(1),
LocalDate.now().plusYears(3),
new BigDecimal("15.34"),
LocalDate.now().minusDays(14),
LocalDate.now(),
"95-90-80-75-70-65",
40,
ProductType.STEP_DOWN,
ProductState.ACTIVE);

productRepository.save(product1);
productRepository.save(product2);
}

@Test
@DisplayName("청약 중인 상품인지를 잘 구분하는지 확인")
void findOnSale() {
// given & when
Page<Product> result = productRepository.listByOnSale("knock-in", Pageable.unpaged());

// then
assertThat(result.getTotalElements()).isEqualTo(1);
assertThat(result.getContent().stream().anyMatch(product -> product.getName().equals("2호"))).isTrue();
}

@Test
@DisplayName("청약 종료인 상품인지를 잘 구분하는지 확인")
void findEndSale() {
// given & when
Page<Product> result = productRepository.listByEndSale("knock-in", Pageable.unpaged());

// then
assertThat(result.getTotalElements()).isEqualTo(1);
assertThat(result.getContent().stream().anyMatch(product -> product.getName().equals("1호"))).isTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.wl2c.elswhereproductservice.global.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@TestConfiguration
@EnableJpaAuditing
public class QueryDslConfig {
@PersistenceContext
private EntityManager em;

@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(em);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.wl2c.elswhereproductservice.mock;

import com.wl2c.elswhereproductservice.domain.product.model.ProductState;
import com.wl2c.elswhereproductservice.domain.product.model.ProductType;
import com.wl2c.elswhereproductservice.domain.product.model.entity.Product;

import java.math.BigDecimal;
import java.time.LocalDate;

public class ProductMock {

public static Product create(Long productId,
String publisher,
String name,
String equities,
int equityCount,
LocalDate issuedDate,
LocalDate maturityDate,
BigDecimal yieldIfConditionsMet,
LocalDate subscriptionStartDate,
LocalDate subscriptionEndDate,
String productInfo,
Integer knockIn,
ProductType productType,
ProductState productState) {
return Product.builder()
.publisher(publisher)
.name(name)
.equities(equities)
.equityCount(equityCount)
.issuedDate(issuedDate)
.maturityDate(maturityDate)
.yieldIfConditionsMet(yieldIfConditionsMet)
.subscriptionStartDate(subscriptionStartDate)
.subscriptionEndDate(subscriptionEndDate)
.productInfo(productInfo)
.knockIn(knockIn)
.productType(productType)
.productState(productState)
.maximumLossRate(BigDecimal.valueOf(100))
.productFullInfo("")
.link("")
.remarks("")
.summaryInvestmentProspectusLink("")
.earlyRepaymentEvaluationDates("")
.volatilites("")
.initialBasePriceEvaluationDate(null)
.build();
}
}
Loading