From 7f1df5ad117c64c851e82b7df19407d6c0dc7344 Mon Sep 17 00:00:00 2001 From: BartChris Date: Wed, 10 Jul 2024 09:53:40 +0200 Subject: [PATCH] Updated Tests to accomodate for changes in CSV parsing in Mass Import --- .../services/data/MassImportService.java | 2 +- .../catalogimport/MassImportTest.java | 32 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Kitodo/src/main/java/org/kitodo/production/services/data/MassImportService.java b/Kitodo/src/main/java/org/kitodo/production/services/data/MassImportService.java index 13c634728f2..c2cf5f22e11 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/data/MassImportService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/data/MassImportService.java @@ -105,7 +105,7 @@ public List parseLines(List lines, String separator) throws I } List cells = new LinkedList<>(); for (String value : entries) { - cells.add(new CsvCell(value)); + cells.add(new CsvCell(value.trim())); } records.add(new CsvRecord(cells)); } diff --git a/Kitodo/src/test/java/org/kitodo/production/services/catalogimport/MassImportTest.java b/Kitodo/src/test/java/org/kitodo/production/services/catalogimport/MassImportTest.java index 910427e6a7e..0575b9461d9 100644 --- a/Kitodo/src/test/java/org/kitodo/production/services/catalogimport/MassImportTest.java +++ b/Kitodo/src/test/java/org/kitodo/production/services/catalogimport/MassImportTest.java @@ -12,6 +12,7 @@ package org.kitodo.production.services.catalogimport; +import com.opencsv.exceptions.CsvException; import org.junit.Assert; import org.junit.Test; import org.kitodo.constants.StringConstants; @@ -21,6 +22,7 @@ import org.kitodo.production.services.ServiceManager; import org.kitodo.production.services.data.MassImportService; +import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -32,9 +34,11 @@ public class MassImportTest { private static final String PLACE = "Place"; private static final List METADATA_KEYS = Arrays.asList(ID, TITLE, PLACE); private static final String CSV_FIRST_LINE = "123, Band 1, Hamburg"; + private static final String CSV_FIRST_LINE_WITH_COMMA = "123, \"Band 1,2\", Hamburg"; private static final String CSV_SECOND_LINE = "456, Band 2, Dresden"; private static final String CSV_THIRD_LINE = "789, Band 3, Berlin"; private static final List CSV_LINES = Arrays.asList(CSV_FIRST_LINE, CSV_SECOND_LINE, CSV_THIRD_LINE); + private static final List CSV_LINES_WITH_COMMA = Arrays.asList(CSV_FIRST_LINE_WITH_COMMA, CSV_SECOND_LINE, CSV_THIRD_LINE); private static final List METADATA_KEYS_MUTLIPLE_VALUES = Arrays.asList(ID, TITLE, PLACE, PLACE); private static final String CSV_FIRST_LINE_MUTLIPLE_VALUES = "321, Band 1, Hamburg, Berlin"; private static final String CSV_SECOND_LINE_MUTLIPLE_VALUES = "654, Band 2, Dresden, Hannover"; @@ -45,7 +49,7 @@ public class MassImportTest { * Tests parsing CSV lines into CSV records with multiple cells. */ @Test - public void shouldParseLines() { + public void shouldParseLines() throws IOException, CsvException { MassImportService service = ServiceManager.getMassImportService(); // test parsing CSV lines with correct delimiter List csvRecords = service.parseLines(CSV_LINES, StringConstants.COMMA_DELIMITER); @@ -75,17 +79,37 @@ public void shouldParseLines() { Assert.assertEquals("Wrong number of cells in third CSV record", 1, cells.size()); } + /** + * Tests parsing CSV lines with comma in values into CSV records with multiple cells. + */ + @Test + public void shouldParseLinesWithCommaInValues() throws IOException, CsvException { + MassImportService service = ServiceManager.getMassImportService(); + // test parsing CSV lines with correct delimiter + List csvRecords = service.parseLines(CSV_LINES_WITH_COMMA, StringConstants.COMMA_DELIMITER); + List cells = csvRecords.get(0).getCsvCells(); + Assert.assertEquals("Wrong number of CSV records", 3, csvRecords.size()); + Assert.assertEquals("Wrong number of cells in first CSV record", 3, cells.size()); + Assert.assertEquals("Wrong value in first cell of first CSV record", "123", cells.get(0).getValue()); + Assert.assertEquals("Wrong value in second cell of first CSV record", "Band 1,2", cells.get(1).getValue()); + Assert.assertEquals("Wrong value in third cell of first CSV record", "Hamburg", cells.get(2).getValue()); + csvRecords = service.parseLines(CSV_LINES, ";"); + cells = csvRecords.get(0).getCsvCells(); + Assert.assertEquals("Wrong number of cells in first CSV record", 1, cells.size()); + } + + /** * Tests whether updating CSV separator character from incorrect character to correct character keeps number of * CSV records, but changes number of cells per record. */ @Test - public void shouldUpdateSeparator() { + public void shouldParseCorrectlyAfterSeparatorUpdate() throws IOException, CsvException { MassImportService service = ServiceManager.getMassImportService(); List oldCsvRecords = service.parseLines(CSV_LINES, StringConstants.SEMICOLON_DELIMITER); Assert.assertTrue("CSV lines should not be separated into multiple records when using incorrect separator character", oldCsvRecords.stream().noneMatch(csvRecord -> csvRecord.getCsvCells().size() > 1)); - List newCsvRecords = service.updateSeparator(oldCsvRecords, StringConstants.SEMICOLON_DELIMITER, StringConstants.COMMA_DELIMITER); + List newCsvRecords = service.parseLines(CSV_LINES, StringConstants.COMMA_DELIMITER); Assert.assertEquals("Updating separator character should not alter number of CSV records", oldCsvRecords.size(), newCsvRecords.size()); Assert.assertTrue("CSV lines be separated into multiple records when using correct separator character", newCsvRecords.stream().allMatch(csvRecord -> csvRecord.getCsvCells().size() > 1)); @@ -95,7 +119,7 @@ public void shouldUpdateSeparator() { * Tests whether parsing data entered in mass import form succeeds or not. */ @Test - public void shouldPrepareMetadata() throws ImportException { + public void shouldPrepareMetadata() throws ImportException, IOException, CsvException { MassImportService service = ServiceManager.getMassImportService(); List csvRecords = service.parseLines(CSV_LINES, StringConstants.COMMA_DELIMITER); Map>> metadata = service.prepareMetadata(METADATA_KEYS, csvRecords);