From 8a19ad7509acd56a39704c62b9bad3e9fa179fc4 Mon Sep 17 00:00:00 2001 From: Asep Rojali Date: Fri, 4 Nov 2022 15:09:10 +0700 Subject: [PATCH] change validation set to collection --- src/main/java/io/github/avew/CsvResultReader.java | 5 ++--- .../java/io/github/avew/CsvValuesSerializer.java | 4 ++-- src/main/java/io/github/avew/ValidationCsvDTO.java | 9 +++++++++ src/main/java/io/github/avew/reader/CsvReader.java | 7 +++++-- src/test/java/io/github/avew/CsvParserTest.java | 14 +++++++++++++- src/test/java/io/github/avew/CsvUserValueDTO.java | 9 +++++++++ src/test/resources/csv/user-failed.csv | 10 ++++++++++ 7 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 src/test/resources/csv/user-failed.csv diff --git a/src/main/java/io/github/avew/CsvResultReader.java b/src/main/java/io/github/avew/CsvResultReader.java index aa2410a..808163c 100644 --- a/src/main/java/io/github/avew/CsvResultReader.java +++ b/src/main/java/io/github/avew/CsvResultReader.java @@ -3,9 +3,8 @@ import lombok.*; import java.util.ArrayList; -import java.util.HashSet; +import java.util.Collection; import java.util.List; -import java.util.Set; @Getter @Setter @@ -19,7 +18,7 @@ public class CsvResultReader { private List values = new ArrayList<>(); @Builder.Default - private Set validations = new HashSet<>(); + private Collection validations = new ArrayList<>(); @Builder.Default private boolean error = false; diff --git a/src/main/java/io/github/avew/CsvValuesSerializer.java b/src/main/java/io/github/avew/CsvValuesSerializer.java index ffb65ce..98379af 100644 --- a/src/main/java/io/github/avew/CsvValuesSerializer.java +++ b/src/main/java/io/github/avew/CsvValuesSerializer.java @@ -1,13 +1,13 @@ package io.github.avew; -import java.util.Set; +import java.util.Collection; @FunctionalInterface public interface CsvValuesSerializer { void apply(int line, String[] columns, - Set validations, + Collection validations, T value ) throws Exception; diff --git a/src/main/java/io/github/avew/ValidationCsvDTO.java b/src/main/java/io/github/avew/ValidationCsvDTO.java index f7c8a3d..696734b 100644 --- a/src/main/java/io/github/avew/ValidationCsvDTO.java +++ b/src/main/java/io/github/avew/ValidationCsvDTO.java @@ -28,4 +28,13 @@ public ValidationCsvDTO(int line, boolean error, String message) { private String code; + @Override + public String toString() { + return "ValidationCsvDTO{" + + "line=" + line + + ", error=" + error + + ", message='" + message + '\'' + + ", code='" + code + '\'' + + '}'; + } } diff --git a/src/main/java/io/github/avew/reader/CsvReader.java b/src/main/java/io/github/avew/reader/CsvReader.java index ff9058c..ae6fb4c 100644 --- a/src/main/java/io/github/avew/reader/CsvReader.java +++ b/src/main/java/io/github/avew/reader/CsvReader.java @@ -11,6 +11,7 @@ import java.lang.reflect.ParameterizedType; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; import static java.nio.charset.StandardCharsets.UTF_8; @@ -83,11 +84,13 @@ protected CsvResultReader read( } } - result.setValidations(validations); result.setValues(values); result.setCount(values.size()); - if (validations.size() > 0) result.setError(true); + if (validations.size() > 0) { + result.setError(true); + result.setValidations(validations.stream().sorted(Comparator.comparing(ValidationCsvDTO::getLine)).collect(Collectors.toList())); + } } catch (IOException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException ex) { ex.printStackTrace(); diff --git a/src/test/java/io/github/avew/CsvParserTest.java b/src/test/java/io/github/avew/CsvParserTest.java index d146fde..b1ee1e2 100644 --- a/src/test/java/io/github/avew/CsvParserTest.java +++ b/src/test/java/io/github/avew/CsvParserTest.java @@ -16,7 +16,7 @@ public class CsvParserTest extends CsvReader { }; @Test - public void testRead() { + public void testReadSuccess() { InputStream is = this.getClass().getResourceAsStream("/csv/user.csv"); CsvResultReader read = read(is); Assert.assertFalse(read.isError()); @@ -27,6 +27,18 @@ public void testRead() { } } + @Test + public void testReadFailed() { + InputStream is = this.getClass().getResourceAsStream("/csv/user-failed.csv"); + CsvResultReader read = read(is); + Assert.assertTrue(read.isError()); + if (read.isError()) { + read.getValidations().forEach(validationCsvDTO -> { + System.out.println(validationCsvDTO.toString()); + }); + } + } + @Override public CsvResultReader read(InputStream is) { CsvParseUser csvParseUser = new CsvParseUser(); diff --git a/src/test/java/io/github/avew/CsvUserValueDTO.java b/src/test/java/io/github/avew/CsvUserValueDTO.java index 4420c15..1088b7f 100644 --- a/src/test/java/io/github/avew/CsvUserValueDTO.java +++ b/src/test/java/io/github/avew/CsvUserValueDTO.java @@ -14,4 +14,13 @@ public class CsvUserValueDTO extends CsvValue { private String firstname; private String lastname; + @Override + public String toString() { + return "CsvUserValueDTO{" + + "username='" + username + '\'' + + ", email='" + email + '\'' + + ", firstname='" + firstname + '\'' + + ", lastname='" + lastname + '\'' + + '}'; + } } diff --git a/src/test/resources/csv/user-failed.csv b/src/test/resources/csv/user-failed.csv new file mode 100644 index 0000000..5ee17cf --- /dev/null +++ b/src/test/resources/csv/user-failed.csv @@ -0,0 +1,10 @@ +username;email;firstname;lastname +john;john@m;john;doe +john;john@m.com;john;doe +john;john@m.com;john;doe +john;john@m;john;doe +john;john@m.com;john;doe +john;john@m.com;john;doe +john;john@m.com;john;doe +john;john@m.com;john;doe +john;john@m.com;john;doe \ No newline at end of file