diff --git a/src/main/java/uno/fastcampus/testdata/service/exporter/JsonFileExporter.java b/src/main/java/uno/fastcampus/testdata/service/exporter/JsonFileExporter.java index bcef0bf..d28d62f 100644 --- a/src/main/java/uno/fastcampus/testdata/service/exporter/JsonFileExporter.java +++ b/src/main/java/uno/fastcampus/testdata/service/exporter/JsonFileExporter.java @@ -11,7 +11,6 @@ import uno.fastcampus.testdata.service.generator.MockDataGeneratorContext; import java.util.*; -import java.util.stream.Collectors; import java.util.stream.IntStream; @Slf4j @@ -37,12 +36,22 @@ public String export(TableSchemaDto dto, Integer rowCount) { dto.schemaFields().stream() .sorted(Comparator.comparing(SchemaFieldDto::fieldOrder)) .forEach(field -> { - map.put(field.fieldName(), mockDataGeneratorContext.generate( + String generatedValue = mockDataGeneratorContext.generate( field.mockDataType(), field.blankPercent(), field.typeOptionJson(), field.forceValue() - )); + ); + if (generatedValue == null) { + map.put(field.fieldName(), null); + } else { + var jsonValue = switch (field.mockDataType().jsonType()) { + case NUMBER -> Long.valueOf(generatedValue); + case BOOLEAN -> Boolean.valueOf(generatedValue); + default -> generatedValue; + }; + map.put(field.fieldName(), jsonValue); + } }); list.add(map); }); diff --git a/src/test/java/uno/fastcampus/testdata/service/exporter/JsonFileExporterTest.java b/src/test/java/uno/fastcampus/testdata/service/exporter/JsonFileExporterTest.java index c8e248d..6b6548d 100644 --- a/src/test/java/uno/fastcampus/testdata/service/exporter/JsonFileExporterTest.java +++ b/src/test/java/uno/fastcampus/testdata/service/exporter/JsonFileExporterTest.java @@ -57,7 +57,11 @@ void givenSchemaAndRowCount_whenExporting_thenReturnsJsonFormattedString() throw ) ); int rowCount = 10; - given(mockDataGeneratorContext.generate(any(), any(), any(), any())).willReturn("test-value"); + given(mockDataGeneratorContext.generate(eq(MockDataType.ROW_NUMBER), any(), any(), any())).willReturn("1"); + given(mockDataGeneratorContext.generate(eq(MockDataType.NAME), any(), any(), any())).willReturn("test-name"); + given(mockDataGeneratorContext.generate(eq(MockDataType.DATETIME), any(), any(), any())).willReturn("2024-01-02T03:04:05"); + given(mockDataGeneratorContext.generate(eq(MockDataType.NUMBER), any(), any(), any())).willReturn(null); + given(mockDataGeneratorContext.generate(eq(MockDataType.CAR), any(), any(), any())).willReturn("test-횬다이"); // When String result = sut.export(dto, rowCount); @@ -68,7 +72,7 @@ void givenSchemaAndRowCount_whenExporting_thenReturnsJsonFormattedString() throw .startsWith("[") .endsWith("]") .contains(""" - {"id":"test-value","name":"test-value","age":"test-value","car":"test-value","created_at":"test-value"}""" + {"id":1,"name":"test-name","age":null,"car":"test-횬다이","created_at":"2024-01-02T03:04:05"}""" ); then(mockDataGeneratorContext).should(times(rowCount * dto.schemaFields().size())).generate(any(), any(), any(), any()); then(mapper).should().writeValueAsString(any()); @@ -78,7 +82,7 @@ void givenSchemaAndRowCount_whenExporting_thenReturnsJsonFormattedString() throw @Test void givenSchemaAndRowCount_whenFailingToJsonFormatting_thenReturnsEmptyString() throws Exception { // Given - TableSchemaDto dto = TableSchemaDto.of("test_schema", "uno", null, Set.of(SchemaFieldDto.of("id", MockDataType.ROW_NUMBER, 1, 0, null, null))); + TableSchemaDto dto = TableSchemaDto.of("test_schema", "uno", null, Set.of(SchemaFieldDto.of("name", MockDataType.NAME, 1, 0, null, null))); int rowCount = 10; given(mockDataGeneratorContext.generate(any(), any(), any(), any())).willReturn("test-value"); willThrow(JsonProcessingException.class).given(mapper).writeValueAsString(any());