Skip to content

Commit

Permalink
Updated Tests to accomodate for changes in CSV parsing in Mass Import
Browse files Browse the repository at this point in the history
  • Loading branch information
BartChris committed Jul 10, 2024
1 parent ec817cd commit 7f1df5a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public List<CsvRecord> parseLines(List<String> lines, String separator) throws I
}
List<CsvCell> cells = new LinkedList<>();
for (String value : entries) {
cells.add(new CsvCell(value));
cells.add(new CsvCell(value.trim()));
}
records.add(new CsvRecord(cells));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -32,9 +34,11 @@ public class MassImportTest {
private static final String PLACE = "Place";
private static final List<String> 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<String> CSV_LINES = Arrays.asList(CSV_FIRST_LINE, CSV_SECOND_LINE, CSV_THIRD_LINE);
private static final List<String> CSV_LINES_WITH_COMMA = Arrays.asList(CSV_FIRST_LINE_WITH_COMMA, CSV_SECOND_LINE, CSV_THIRD_LINE);
private static final List<String> 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";
Expand All @@ -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<CsvRecord> csvRecords = service.parseLines(CSV_LINES, StringConstants.COMMA_DELIMITER);
Expand Down Expand Up @@ -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<CsvRecord> csvRecords = service.parseLines(CSV_LINES_WITH_COMMA, StringConstants.COMMA_DELIMITER);
List<CsvCell> 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<CsvRecord> 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<CsvRecord> newCsvRecords = service.updateSeparator(oldCsvRecords, StringConstants.SEMICOLON_DELIMITER, StringConstants.COMMA_DELIMITER);
List<CsvRecord> 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));
Expand All @@ -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<CsvRecord> csvRecords = service.parseLines(CSV_LINES, StringConstants.COMMA_DELIMITER);
Map<String, Map<String, List<String>>> metadata = service.prepareMetadata(METADATA_KEYS, csvRecords);
Expand Down

0 comments on commit 7f1df5a

Please sign in to comment.