|
@@ -53,7 +53,7 @@ public class CsvProcessorTests extends ESTestCase {
|
|
|
separator = randomFrom(SEPARATORS);
|
|
|
}
|
|
|
|
|
|
- public void testExactNumberOfFields() throws Exception {
|
|
|
+ public void testExactNumberOfFields() {
|
|
|
int numItems = randomIntBetween(2, 10);
|
|
|
Map<String, String> items = new LinkedHashMap<>();
|
|
|
for (int i = 0; i < numItems; i++) {
|
|
@@ -67,7 +67,67 @@ public class CsvProcessorTests extends ESTestCase {
|
|
|
items.forEach((key, value) -> assertEquals(value, ingestDocument.getFieldValue(key, String.class)));
|
|
|
}
|
|
|
|
|
|
- public void testLessFieldsThanHeaders() throws Exception {
|
|
|
+ public void testEmptyValues() {
|
|
|
+ int numItems = randomIntBetween(5, 10);
|
|
|
+ Map<String, String> items = new LinkedHashMap<>();
|
|
|
+ for (int i = 0; i < 3; i++) {
|
|
|
+ items.put(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10));
|
|
|
+ }
|
|
|
+ String emptyKey = randomAlphaOfLengthBetween(5, 10);
|
|
|
+ items.put(emptyKey, "");
|
|
|
+ for (int i = 0; i < numItems - 4; i++) {
|
|
|
+ items.put(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10));
|
|
|
+ }
|
|
|
+ String[] headers = items.keySet().toArray(new String[numItems]);
|
|
|
+ String csv = items.values().stream().map(v -> quote + v + quote).collect(Collectors.joining(separator + ""));
|
|
|
+
|
|
|
+ IngestDocument ingestDocument = processDocument(headers, csv);
|
|
|
+
|
|
|
+ items.forEach((key, value) -> {
|
|
|
+ if (emptyKey.equals(key)) {
|
|
|
+ assertFalse(ingestDocument.hasField(key));
|
|
|
+ } else {
|
|
|
+ assertEquals(value, ingestDocument.getFieldValue(key, String.class));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testEmptyValuesReplace() {
|
|
|
+ int numItems = randomIntBetween(5, 10);
|
|
|
+ Map<String, String> items = new LinkedHashMap<>();
|
|
|
+ for (int i = 0; i < 3; i++) {
|
|
|
+ items.put(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10));
|
|
|
+ }
|
|
|
+ String emptyKey = randomAlphaOfLengthBetween(5, 10);
|
|
|
+ items.put(emptyKey, "");
|
|
|
+ for (int i = 0; i < numItems - 4; i++) {
|
|
|
+ items.put(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10));
|
|
|
+ }
|
|
|
+ String[] headers = items.keySet().toArray(new String[numItems]);
|
|
|
+ String csv = items.values().stream().map(v -> quote + v + quote).collect(Collectors.joining(separator + ""));
|
|
|
+
|
|
|
+ IngestDocument ingestDocument = processDocument(headers, csv, true, "");
|
|
|
+
|
|
|
+ items.forEach((key, value) -> {
|
|
|
+ if (emptyKey.equals(key)) {
|
|
|
+ assertEquals("", ingestDocument.getFieldValue(key, String.class));
|
|
|
+ } else {
|
|
|
+ assertEquals(value, ingestDocument.getFieldValue(key, String.class));
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ IngestDocument ingestDocument2 = processDocument(headers, csv, true, 0);
|
|
|
+
|
|
|
+ items.forEach((key, value) -> {
|
|
|
+ if (emptyKey.equals(key)) {
|
|
|
+ assertEquals(0, (int) ingestDocument2.getFieldValue(key, Integer.class));
|
|
|
+ } else {
|
|
|
+ assertEquals(value, ingestDocument2.getFieldValue(key, String.class));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testLessFieldsThanHeaders() {
|
|
|
int numItems = randomIntBetween(4, 10);
|
|
|
Map<String, String> items = new LinkedHashMap<>();
|
|
|
for (int i = 0; i < numItems; i++) {
|
|
@@ -82,7 +142,7 @@ public class CsvProcessorTests extends ESTestCase {
|
|
|
items.entrySet().stream().limit(3).forEach(e -> assertEquals(e.getValue(), ingestDocument.getFieldValue(e.getKey(), String.class)));
|
|
|
}
|
|
|
|
|
|
- public void testLessHeadersThanFields() throws Exception {
|
|
|
+ public void testLessHeadersThanFields() {
|
|
|
int numItems = randomIntBetween(5, 10);
|
|
|
Map<String, String> items = new LinkedHashMap<>();
|
|
|
for (int i = 0; i < numItems; i++) {
|
|
@@ -96,7 +156,7 @@ public class CsvProcessorTests extends ESTestCase {
|
|
|
items.entrySet().stream().limit(3).forEach(e -> assertEquals(e.getValue(), ingestDocument.getFieldValue(e.getKey(), String.class)));
|
|
|
}
|
|
|
|
|
|
- public void testSingleField() throws Exception {
|
|
|
+ public void testSingleField() {
|
|
|
String[] headers = new String[]{randomAlphaOfLengthBetween(5, 10)};
|
|
|
String value = randomAlphaOfLengthBetween(5, 10);
|
|
|
String csv = quote + value + quote;
|
|
@@ -106,7 +166,7 @@ public class CsvProcessorTests extends ESTestCase {
|
|
|
assertEquals(value, ingestDocument.getFieldValue(headers[0], String.class));
|
|
|
}
|
|
|
|
|
|
- public void testEscapedQuote() throws Exception {
|
|
|
+ public void testEscapedQuote() {
|
|
|
int numItems = randomIntBetween(2, 10);
|
|
|
Map<String, String> items = new LinkedHashMap<>();
|
|
|
for (int i = 0; i < numItems; i++) {
|
|
@@ -121,7 +181,7 @@ public class CsvProcessorTests extends ESTestCase {
|
|
|
items.forEach((key, value) -> assertEquals(value.replace(quote + quote, quote), ingestDocument.getFieldValue(key, String.class)));
|
|
|
}
|
|
|
|
|
|
- public void testQuotedStrings() throws Exception {
|
|
|
+ public void testQuotedStrings() {
|
|
|
assumeFalse("quote needed", quote.isEmpty());
|
|
|
int numItems = randomIntBetween(2, 10);
|
|
|
Map<String, String> items = new LinkedHashMap<>();
|
|
@@ -138,7 +198,7 @@ public class CsvProcessorTests extends ESTestCase {
|
|
|
String.class)));
|
|
|
}
|
|
|
|
|
|
- public void testEmptyFields() throws Exception {
|
|
|
+ public void testEmptyFields() {
|
|
|
int numItems = randomIntBetween(5, 10);
|
|
|
Map<String, String> items = new LinkedHashMap<>();
|
|
|
for (int i = 0; i < numItems; i++) {
|
|
@@ -167,7 +227,7 @@ public class CsvProcessorTests extends ESTestCase {
|
|
|
expectThrows(IllegalArgumentException.class, () -> processDocument(new String[]{"a"}, "abc\rabc"));
|
|
|
}
|
|
|
|
|
|
- public void testQuotedWhitespaces() throws Exception {
|
|
|
+ public void testQuotedWhitespaces() {
|
|
|
assumeFalse("quote needed", quote.isEmpty());
|
|
|
IngestDocument document = processDocument(new String[]{"a", "b", "c", "d"},
|
|
|
" abc " + separator + " def" + separator + "ghi " + separator + " " + quote + " ooo " + quote);
|
|
@@ -177,7 +237,7 @@ public class CsvProcessorTests extends ESTestCase {
|
|
|
assertEquals(" ooo ", document.getFieldValue("d", String.class));
|
|
|
}
|
|
|
|
|
|
- public void testUntrimmed() throws Exception {
|
|
|
+ public void testUntrimmed() {
|
|
|
assumeFalse("quote needed", quote.isEmpty());
|
|
|
IngestDocument document = processDocument(new String[]{"a", "b", "c", "d", "e", "f"},
|
|
|
" abc " + separator + " def" + separator + "ghi " + separator + " "
|
|
@@ -197,9 +257,9 @@ public class CsvProcessorTests extends ESTestCase {
|
|
|
if (ingestDocument.hasField(fieldName)) {
|
|
|
ingestDocument.removeField(fieldName);
|
|
|
}
|
|
|
- CsvProcessor processor = new CsvProcessor(randomAlphaOfLength(5), fieldName, new String[]{"a"}, false, ',', '"', true);
|
|
|
+ CsvProcessor processor = new CsvProcessor(randomAlphaOfLength(5), fieldName, new String[]{"a"}, false, ',', '"', true, null);
|
|
|
processor.execute(ingestDocument);
|
|
|
- CsvProcessor processor2 = new CsvProcessor(randomAlphaOfLength(5), fieldName, new String[]{"a"}, false, ',', '"', false);
|
|
|
+ CsvProcessor processor2 = new CsvProcessor(randomAlphaOfLength(5), fieldName, new String[]{"a"}, false, ',', '"', false, null);
|
|
|
expectThrows(IllegalArgumentException.class, () -> processor2.execute(ingestDocument));
|
|
|
}
|
|
|
|
|
@@ -209,24 +269,29 @@ public class CsvProcessorTests extends ESTestCase {
|
|
|
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "abc,abc");
|
|
|
HashMap<String, Object> metadata = new HashMap<>(ingestDocument.getSourceAndMetadata());
|
|
|
|
|
|
- CsvProcessor processor = new CsvProcessor(randomAlphaOfLength(5), fieldName, new String[0], false, ',', '"', false);
|
|
|
+ CsvProcessor processor = new CsvProcessor(randomAlphaOfLength(5), fieldName, new String[0], false, ',', '"', false, null);
|
|
|
|
|
|
processor.execute(ingestDocument);
|
|
|
|
|
|
assertEquals(metadata, ingestDocument.getSourceAndMetadata());
|
|
|
}
|
|
|
|
|
|
- private IngestDocument processDocument(String[] headers, String csv) throws Exception {
|
|
|
+ private IngestDocument processDocument(String[] headers, String csv) {
|
|
|
return processDocument(headers, csv, true);
|
|
|
}
|
|
|
|
|
|
- private IngestDocument processDocument(String[] headers, String csv, boolean trim) throws Exception {
|
|
|
+ private IngestDocument processDocument(String[] headers, String csv, boolean trim) {
|
|
|
+ return processDocument(headers, csv, trim, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ private IngestDocument processDocument(String[] headers, String csv, boolean trim, Object emptyValue) {
|
|
|
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
|
|
Arrays.stream(headers).filter(ingestDocument::hasField).forEach(ingestDocument::removeField);
|
|
|
|
|
|
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, csv);
|
|
|
char quoteChar = quote.isEmpty() ? '"' : quote.charAt(0);
|
|
|
- CsvProcessor processor = new CsvProcessor(randomAlphaOfLength(5), fieldName, headers, trim, separator, quoteChar, false);
|
|
|
+ CsvProcessor processor = new CsvProcessor(randomAlphaOfLength(5), fieldName, headers, trim, separator, quoteChar, false,
|
|
|
+ emptyValue);
|
|
|
|
|
|
processor.execute(ingestDocument);
|
|
|
|