Skip to content

Commit

Permalink
[fix] s3 stocks.csv 버그 해결 (#424)
Browse files Browse the repository at this point in the history
* test: toCsvLineString 테스트 추가

* test: writeStocks 테스트 추가

* fix: csv 구부낮 콜론으로 변경

* feat: reloadStocks 위임하는 방식으로 변경
  • Loading branch information
yonghwankim-dev authored Aug 10, 2024
1 parent af3d2b9 commit 4ffd6c9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -220,7 +221,7 @@ public List<StockDividend> getStockDividendNotInRange(LocalDate from, LocalDate
}

public String toCsvLineString() {
return String.join(",",
return String.join(AmazonS3StockService.CSV_SEPARATOR,
stockCode,
tickerSymbol,
companyName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 4ffd6c9

Please sign in to comment.