Skip to content

Commit

Permalink
change validation set to collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Asep Rojali committed Nov 4, 2022
1 parent 34051e3 commit 8a19ad7
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/main/java/io/github/avew/CsvResultReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,7 +18,7 @@ public class CsvResultReader<T extends CsvValue> {
private List<T> values = new ArrayList<>();

@Builder.Default
private Set<ValidationCsvDTO> validations = new HashSet<>();
private Collection<ValidationCsvDTO> validations = new ArrayList<>();

@Builder.Default
private boolean error = false;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/github/avew/CsvValuesSerializer.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.github.avew;

import java.util.Set;
import java.util.Collection;

@FunctionalInterface
public interface CsvValuesSerializer<T extends CsvValue> {

void apply(int line,
String[] columns,
Set<ValidationCsvDTO> validations,
Collection<ValidationCsvDTO> validations,
T value
) throws Exception;

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/github/avew/ValidationCsvDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '\'' +
'}';
}
}
7 changes: 5 additions & 2 deletions src/main/java/io/github/avew/reader/CsvReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -83,11 +84,13 @@ protected CsvResultReader<T> 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();
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/io/github/avew/CsvParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CsvParserTest extends CsvReader<CsvUserValueDTO> {
};

@Test
public void testRead() {
public void testReadSuccess() {
InputStream is = this.getClass().getResourceAsStream("/csv/user.csv");
CsvResultReader<CsvUserValueDTO> read = read(is);
Assert.assertFalse(read.isError());
Expand All @@ -27,6 +27,18 @@ public void testRead() {
}
}

@Test
public void testReadFailed() {
InputStream is = this.getClass().getResourceAsStream("/csv/user-failed.csv");
CsvResultReader<CsvUserValueDTO> read = read(is);
Assert.assertTrue(read.isError());
if (read.isError()) {
read.getValidations().forEach(validationCsvDTO -> {
System.out.println(validationCsvDTO.toString());
});
}
}

@Override
public CsvResultReader<CsvUserValueDTO> read(InputStream is) {
CsvParseUser csvParseUser = new CsvParseUser();
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/io/github/avew/CsvUserValueDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '\'' +
'}';
}
}
10 changes: 10 additions & 0 deletions src/test/resources/csv/user-failed.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
username;email;firstname;lastname
john;john@m;john;doe
john;[email protected];john;doe
john;[email protected];john;doe
john;john@m;john;doe
john;[email protected];john;doe
john;[email protected];john;doe
john;[email protected];john;doe
john;[email protected];john;doe
john;[email protected];john;doe

0 comments on commit 8a19ad7

Please sign in to comment.