diff --git a/src/main/java/codesquad/fineants/domain/stock/domain/entity/Stock.java b/src/main/java/codesquad/fineants/domain/stock/domain/entity/Stock.java index 52f6bb3c..149ecf50 100644 --- a/src/main/java/codesquad/fineants/domain/stock/domain/entity/Stock.java +++ b/src/main/java/codesquad/fineants/domain/stock/domain/entity/Stock.java @@ -16,6 +16,7 @@ import codesquad.fineants.domain.kis.repository.CurrentPriceRepository; import codesquad.fineants.domain.purchasehistory.domain.entity.PurchaseHistory; import codesquad.fineants.domain.stock.converter.MarketConverter; +import codesquad.fineants.infra.s3.service.AmazonS3StockService; import jakarta.persistence.Convert; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -220,7 +221,7 @@ public List getStockDividendNotInRange(LocalDate from, LocalDate } public String toCsvLineString() { - return String.join(",", + return String.join(AmazonS3StockService.CSV_SEPARATOR, stockCode, tickerSymbol, companyName, diff --git a/src/main/java/codesquad/fineants/domain/stock/service/StockService.java b/src/main/java/codesquad/fineants/domain/stock/service/StockService.java index 96ff619c..e6b825f0 100644 --- a/src/main/java/codesquad/fineants/domain/stock/service/StockService.java +++ b/src/main/java/codesquad/fineants/domain/stock/service/StockService.java @@ -61,14 +61,15 @@ public StockResponse getDetailedStock(String tickerSymbol) { @Scheduled(cron = "0 0 8 * * ?") // 매일 오전 8시 (초, 분, 시간) @Transactional public void scheduledReloadStocks() { - StockReloadResponse response = stockAndDividendManager.reloadStocks(); - log.info("refreshStocks response : {}", response); - amazonS3StockService.writeStocks(stockRepository.findAll()); - amazonS3DividendService.writeDividends(stockDividendRepository.findAll()); + reloadStocks(); } @Transactional public StockReloadResponse reloadStocks() { - return stockAndDividendManager.reloadStocks(); + StockReloadResponse response = stockAndDividendManager.reloadStocks(); + log.info("refreshStocks response : {}", response); + amazonS3StockService.writeStocks(stockRepository.findAll()); + amazonS3DividendService.writeDividends(stockDividendRepository.findAll()); + return response; } } diff --git a/src/main/java/codesquad/fineants/infra/s3/service/AmazonS3StockService.java b/src/main/java/codesquad/fineants/infra/s3/service/AmazonS3StockService.java index 87b841d4..de309879 100644 --- a/src/main/java/codesquad/fineants/infra/s3/service/AmazonS3StockService.java +++ b/src/main/java/codesquad/fineants/infra/s3/service/AmazonS3StockService.java @@ -33,7 +33,7 @@ @Slf4j public class AmazonS3StockService { - public static final String CSV_SEPARATOR = ","; + public static final String CSV_SEPARATOR = ":"; private final AmazonS3 amazonS3; @Value("${aws.s3.bucket}") diff --git a/src/test/java/codesquad/fineants/domain/stock/domain/entity/StockTest.java b/src/test/java/codesquad/fineants/domain/stock/domain/entity/StockTest.java new file mode 100644 index 00000000..80fc8337 --- /dev/null +++ b/src/test/java/codesquad/fineants/domain/stock/domain/entity/StockTest.java @@ -0,0 +1,21 @@ +package codesquad.fineants.domain.stock.domain.entity; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class StockTest { + + @DisplayName("csv에 저장하기 위해서 쉼표(,)로 구분한 한줄의 문자열로 변환한다") + @Test + void toCsvLineString() { + // given + Stock stock = Stock.of("000370", "한화손해보험보통주", "Hanwha General Insurance Co.,Ltd.", "KR7000370007", "보험", + Market.KOSPI); + // when + String result = stock.toCsvLineString(); + // then + String expected = "KR7000370007:000370:한화손해보험보통주:Hanwha General Insurance Co.,Ltd.:KOSPI:보험"; + Assertions.assertThat(result).isEqualTo(expected); + } +} diff --git a/src/test/java/codesquad/fineants/infra/s3/service/AmazonS3StockServiceTest.java b/src/test/java/codesquad/fineants/infra/s3/service/AmazonS3StockServiceTest.java new file mode 100644 index 00000000..0157c56a --- /dev/null +++ b/src/test/java/codesquad/fineants/infra/s3/service/AmazonS3StockServiceTest.java @@ -0,0 +1,37 @@ +package codesquad.fineants.infra.s3.service; + +import java.util.List; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import codesquad.fineants.AbstractContainerBaseTest; +import codesquad.fineants.domain.stock.domain.entity.Market; +import codesquad.fineants.domain.stock.domain.entity.Stock; + +class AmazonS3StockServiceTest extends AbstractContainerBaseTest { + + @Autowired + private AmazonS3StockService amazonS3StockService; + + @DisplayName("종목 정보를 csv 파일에 저장한다") + @Test + void writeStocks() { + // given + Stock stock = Stock.of("000370", "한화손해보험보통주", "Hanwha General Insurance Co.,Ltd.", "KR7000370007", "보험", + Market.KOSPI); + // when + amazonS3StockService.writeStocks(List.of(stock)); + // then + Stock findStock = amazonS3StockService.fetchStocks().stream() + .findAny() + .orElseThrow(); + Assertions.assertThat(findStock) + .extracting(Stock::getTickerSymbol, Stock::getCompanyName, Stock::getCompanyNameEng, Stock::getStockCode, + Stock::getSector, Stock::getMarket) + .containsExactly("000370", "한화손해보험보통주", "Hanwha General Insurance Co.,Ltd.", "KR7000370007", "보험", + Market.KOSPI); + } +}