diff --git a/dsl/src/main/java/be/sddevelopment/validation/dsl/CsvFile.java b/dsl/src/main/java/be/sddevelopment/validation/dsl/CsvFile.java index b8ec4f3..017d7cf 100644 --- a/dsl/src/main/java/be/sddevelopment/validation/dsl/CsvFile.java +++ b/dsl/src/main/java/be/sddevelopment/validation/dsl/CsvFile.java @@ -1,7 +1,9 @@ package be.sddevelopment.validation.dsl; import java.nio.file.Path; +import java.util.List; import java.util.Vector; +import java.util.stream.Collectors; import java.util.stream.Stream; public record CsvFile ( @@ -9,6 +11,8 @@ public record CsvFile ( Vector headerFields, Vector> lines ) { + private static final String DEFAULT = ".*\\.csv"; + public CsvFile { if (headerFields == null || lines == null) { throw new IllegalArgumentException("Header fields and lines must not be null"); @@ -19,11 +23,29 @@ public Vector line(int lineNumber) { return lines.get(lineNumber); } - public static CsvFile fromLines(Stream lines) { + public static CsvFile fromLines(List lines) { + if(lines.isEmpty()) { + throw new IllegalArgumentException("No lines provided. A data file requires at least one line"); + } + + var header = parseHeader(lines.getFirst()); + var dataLines = lines.stream().skip(1).map(CsvFile::parseLine).toList(); + return new CsvFile(DEFAULT, header, new Vector<>(dataLines)); + } + + private static Vector parseHeader(String s) { + return null; + } + + private static Vector parseLine(String s) { return null; } public static CsvFile fromFile(Path dataFile) { return null; } + + public boolean isEmpty() { + return this.lines.isEmpty(); + } } diff --git a/dsl/src/test/java/be/sddevelopment/validation/dsl/CsvFileTest.java b/dsl/src/test/java/be/sddevelopment/validation/dsl/CsvFileTest.java index 541ef0f..f03f039 100644 --- a/dsl/src/test/java/be/sddevelopment/validation/dsl/CsvFileTest.java +++ b/dsl/src/test/java/be/sddevelopment/validation/dsl/CsvFileTest.java @@ -4,15 +4,21 @@ import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import java.util.List; + @DisplayName("Comma Separated Values File") @DisplayNameGeneration(ReplaceUnderscoredCamelCasing.class) class CsvFileTest implements WithAssertions { - @Test - void canBeCreatedFromLines() { - var dataWithHeader = """ + @Nested + class LineBasedParsing { + + @Test + void dataIsAccessibleAfterParsing() { + var dataWithHeader = """ NAME,HEIGHT,SPECIES Luke Skywalker,172,Human C-3PO,167,Droid @@ -20,12 +26,34 @@ void canBeCreatedFromLines() { Boba Fett,183, Human """; - var csvFile = CsvFile.fromLines(dataWithHeader.lines()); + var csvFile = CsvFile.fromLines(dataWithHeader.lines().toList()); + + assertThat(csvFile).isNotNull() + .extracting(CsvFile::headerFields) + .asInstanceOf(LIST) + .containsExactly("NAME", "HEIGHT", "SPECIES"); + assertThat(csvFile.line(0)).containsExactly("Luke Skywalker", "172", "Human"); + } + + @Test + void parsingRequiresAtLeastAHeaderLine() { + assertThatException().isThrownBy(() -> CsvFile.fromLines(List.of())) + .isInstanceOf(IllegalArgumentException.class) + .withMessage("No lines provided. A data file requires at least one line") + .withNoCause(); + } + + @Test + void canHandleEmptyDataSets() { + var headerOnly = List.of("NAME,HEIGHT,SPECIES"); + + var csvFile = CsvFile.fromLines(headerOnly); - assertThat(csvFile).isNotNull() - .extracting(CsvFile::headerFields) - .asInstanceOf(LIST) - .containsExactly("NAME", "HEIGHT", "SPECIES"); - assertThat(csvFile.line(0)).containsExactly("Luke Skywalker", "172", "Human"); + assertThat(csvFile).isNotNull(); + assertThat(csvFile.headerFields()) + .asInstanceOf(LIST) + .containsExactly("NAME", "HEIGHT", "SPECIES"); + assertThat(csvFile).matches(CsvFile::isEmpty); + } } } \ No newline at end of file diff --git a/dsl/src/test/java/be/sddevelopment/validation/dsl/FileValidatorParserTest.java b/dsl/src/test/java/be/sddevelopment/validation/dsl/FileValidatorParserTest.java index b6cbd6b..b6caf3b 100644 --- a/dsl/src/test/java/be/sddevelopment/validation/dsl/FileValidatorParserTest.java +++ b/dsl/src/test/java/be/sddevelopment/validation/dsl/FileValidatorParserTest.java @@ -62,7 +62,7 @@ void canCheckFieldExistence() { C-3PO,167,Droid R2-D2,96,Droid Boba Fett,183, Human - """.lines() + """.lines().toList() ); var rule = "FieldExists('HOMEWORLD')"; var ruleAdder = FileValidatorParser.toRuleAdder(rule); diff --git a/pom.xml b/pom.xml index 87c16e7..4d6cd2f 100644 --- a/pom.xml +++ b/pom.xml @@ -60,7 +60,7 @@ sddevelopment_modular-validators ${jacoco.build.dir}/jacoco.xml - 1.0.0-SNAPSHOT + 1.0.0 5.36.0