Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stijn-dejongh committed Nov 20, 2024
1 parent 8a2b7ec commit bf3cf91
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
24 changes: 23 additions & 1 deletion dsl/src/main/java/be/sddevelopment/validation/dsl/CsvFile.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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 (
String fileIdentifier,
Vector<String> headerFields,
Vector<Vector<String>> 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");
Expand All @@ -19,11 +23,29 @@ public Vector<String> line(int lineNumber) {
return lines.get(lineNumber);
}

public static CsvFile fromLines(Stream<String> lines) {
public static CsvFile fromLines(List<String> 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<String> parseHeader(String s) {
return null;
}

private static Vector<String> parseLine(String s) {
return null;
}

public static CsvFile fromFile(Path dataFile) {
return null;
}

public boolean isEmpty() {
return this.lines.isEmpty();
}
}
46 changes: 37 additions & 9 deletions dsl/src/test/java/be/sddevelopment/validation/dsl/CsvFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,56 @@
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
R2-D2,96,Droid
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.<CsvFile>toRuleAdder(rule);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<sonar.projectKey>sddevelopment_modular-validators</sonar.projectKey>
<sonar.coverage.jacoco.xmlReportPaths>${jacoco.build.dir}/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>

<code.utils.version>1.0.0-SNAPSHOT</code.utils.version>
<code.utils.version>1.0.0</code.utils.version>
<rewrite.version>5.36.0</rewrite.version>
</properties>
<dependencyManagement>
Expand Down

0 comments on commit bf3cf91

Please sign in to comment.