Skip to content

Commit

Permalink
fix skip header
Browse files Browse the repository at this point in the history
  • Loading branch information
avew committed Jun 17, 2023
1 parent d6a0403 commit d82dc6a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/java/io/github/avew/reader/CsvewReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ protected CsvewResultReader<T> read(

BufferedReader br = new BufferedReader(new InputStreamReader(is, UTF_8));


try {

/* dont validate header */
if (!skipHeader) {
String[] contentHeader = getHeader(br.readLine(), delimeter);
CsvewValidationDTO headerValidation = headerValidation(typeHeader, contentHeader);
Expand All @@ -61,12 +64,13 @@ protected CsvewResultReader<T> read(

if (startAt == 0 || startAt == 1) {
startAt = 1;
log.debug("START LINE={}", startAt);
} else log.debug("SKIP LINE CURRENT READ={}", startAt);
log.debug("READ LINE {}", startAt);
} else {
log.debug("SKIP LINE CURRENT READ {}", startAt);
}

AtomicInteger index = new AtomicInteger(startAt);
for (int x = 1; x < startAt; x++) br.readLine();
if (skipHeader) br.readLine();

while ((lineContent = br.readLine()) != null) {
String[] x = lineContent.split(delimeter, -1);
Expand All @@ -76,7 +80,7 @@ protected CsvewResultReader<T> read(
value.setLine(line);
value.setRaw(List.of(x));

if (skipHeader) {
if (!skipHeader) {
if (x.length > typeHeader.length) {
validations.add(CsvewValidationDTO.builder()
.line(value.getLine())
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/io/github/avew/CsvSftpValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.avew;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CsvSftpValue extends CsvewValue {

private String identityId;
private String identityType;
private String identityName;
private String docNo;
private String docDate;
private String docTemplate;
private String docKopur;
private String docPassword;
private String docReference;
}
57 changes: 57 additions & 0 deletions src/test/java/io/github/avew/CsvewSftpParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.github.avew;

import io.github.avew.reader.CsvewReader;
import org.junit.Assert;
import org.junit.Test;

import java.io.InputStream;

public class CsvewSftpParserTest extends CsvewReader<CsvSftpValue> {

static String[] HEADER = {
"Identity Number",
"Identity Type",
"Identity Name",
"Document Number",
"Document Date",
"Template Code",
"Transaction Value",
"Password",
"Document Reference",
};

@Test
public void testReadSuccess() {
InputStream is = this.getClass().getResourceAsStream("/csv/1.csv");
CsvewResultReader<CsvSftpValue> read = process(0, is);
System.out.println(read.getValues().toString());
}


@Override
public CsvewResultReader<CsvSftpValue> process(int startAt, InputStream is) {
CsvewParser csvParseUser = new CsvewParser();
return read(true,
startAt,
is,
HEADER,
";",
(line, columns, validations, value) -> {
csvParseUser.parseString(line, 0, HEADER[0], columns[0], true, validations, value::setIdentityId);
csvParseUser.parseString(line, 1, HEADER[1], columns[1], true, validations, value::setIdentityType);
csvParseUser.parseString(line, 2, HEADER[2], columns[2], true, validations, value::setIdentityName);
csvParseUser.parseString(line, 3, HEADER[3], columns[3], true, validations, value::setDocNo);
csvParseUser.parseString(line, 4, HEADER[4], columns[4], true, validations, value::setDocDate);
csvParseUser.parseString(line, 5, HEADER[5], columns[5], true, validations, value::setDocTemplate);
csvParseUser.parseString(line, 6, HEADER[6], columns[6], false, validations, value::setDocKopur);

try {
csvParseUser.parseString(line, 7, HEADER[7], columns[7], false, validations, value::setDocPassword);
csvParseUser.parseString(line, 8, HEADER[8], columns[8], false, validations, value::setDocReference);
} catch (IndexOutOfBoundsException ignore) {
}
});
}


}
1 change: 1 addition & 0 deletions src/test/resources/csv/1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000111122224444;NOW;RAAM TEST;0614_19112019_000001_082343;19-11-20192;E001;4897546;;;;true;FIELD_VALIDATION;Identity Type should only be NIK (Citizenship ID Number) or NPWP (Taxpayer ID Number)

0 comments on commit d82dc6a

Please sign in to comment.