-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
전략 패턴을 참고하여, 사용자 입력에 따라 적절한 구현체를 사용하는 파일 출력기 전략 클래스를 구현. 새로운 파일 출력기는 `MockDataFileExporter`를 구현하여 스프링 빈으로 등록하는 것만으로 자연스럽게 로직에 합쳐지게 되는 설계임.
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/uno/fastcampus/testdata/service/exporter/MockDataFileExporterContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package uno.fastcampus.testdata.service.exporter; | ||
|
||
import org.springframework.stereotype.Service; | ||
import uno.fastcampus.testdata.domain.constant.ExportFileType; | ||
import uno.fastcampus.testdata.dto.TableSchemaDto; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class MockDataFileExporterContext { | ||
|
||
private final Map<ExportFileType, MockDataFileExporter> mockDataFileExporterMap; | ||
|
||
public MockDataFileExporterContext(List<MockDataFileExporter> mockDataFileExporters) { | ||
this.mockDataFileExporterMap = mockDataFileExporters.stream() | ||
.collect(Collectors.toMap(MockDataFileExporter::getType, Function.identity())); | ||
} | ||
|
||
public String export(ExportFileType fileType, TableSchemaDto dto, Integer rowCount) { | ||
MockDataFileExporter fileExporter = mockDataFileExporterMap.get(fileType); | ||
|
||
return fileExporter.export(dto, rowCount); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/uno/fastcampus/testdata/service/exporter/MockDataFileExporterContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package uno.fastcampus.testdata.service.exporter; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import uno.fastcampus.testdata.domain.constant.ExportFileType; | ||
import uno.fastcampus.testdata.domain.constant.MockDataType; | ||
import uno.fastcampus.testdata.dto.SchemaFieldDto; | ||
import uno.fastcampus.testdata.dto.TableSchemaDto; | ||
|
||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@ActiveProfiles("test") | ||
@DisplayName("[IntegrationTest] 파일 출력기 컨텍스트 테스트") | ||
@SpringBootTest | ||
record MockDataFileExporterContextTest(@Autowired MockDataFileExporterContext sut) { | ||
|
||
@DisplayName("파일 형식과 테이블 스키마와 행 수가 주어지면, 파일 형식에 맞게 변환한 문자열을 리턴한다.") | ||
@Test | ||
void givenFileTypeAndTableSchemaAndRowCount_whenExporting_thenReturnsFileFormattedString() { | ||
// Given | ||
ExportFileType exportFileType = ExportFileType.CSV; | ||
TableSchemaDto dto = TableSchemaDto.of( | ||
"test_schema", | ||
"uno", | ||
null, | ||
Set.of( | ||
SchemaFieldDto.of("id", MockDataType.ROW_NUMBER, 1, 0, null, null), | ||
SchemaFieldDto.of("name", MockDataType.NAME, 2, 0, null, null), | ||
SchemaFieldDto.of("age", MockDataType.NUMBER, 3, 0, null, null), | ||
SchemaFieldDto.of("car", MockDataType.CAR, 4, 0, null, null), | ||
SchemaFieldDto.of("created_at", MockDataType.DATETIME, 5, 0, null, null) | ||
) | ||
); | ||
int rowCount = 10; | ||
|
||
// When | ||
String result = sut.export(exportFileType, dto, rowCount); | ||
|
||
// Then | ||
System.out.println(result); // 관찰용 | ||
assertThat(result).startsWith("id,name,age,car,created_at"); | ||
} | ||
|
||
} |