CSVBatchEngineImportTaskItemReader implements BatchEngineImportTaskItemReader { @Override public Map<String, Object> read() throws Exception { String line = _unsyncBufferedReader.readLine(); if (line == null) { return null; } Map<String, Object> fieldNameValueMap = new HashMap<>(); String[] values = StringUtil.split(line, _delimiter); for (int i = 0; i < values.length; i++) { String fieldName = _fieldNames[i]; if (fieldName == null) { continue; } String value = values[i].trim(); if (value.isEmpty()) { value = null; } int lastDelimiterIndex = fieldName.lastIndexOf('_'); if (lastDelimiterIndex == -1) { fieldNameValueMap.put(fieldName, value); } else { BatchEngineImportTaskItemReaderUtil.handleMapField( fieldName, fieldNameValueMap, lastDelimiterIndex, value); } } return fieldNameValueMap; } CSVBatchEngineImportTaskItemReader(
			String delimiter, InputStream inputStream); @Override void close(); @Override Map<String, Object> read();  }
@Test public void testColumnMapping() throws Exception { try (CSVBatchEngineImportTaskItemReader csvBatchEngineImportTaskItemReader = _getCSVBatchEngineImportTaskItemReader( new String[] { "createDate1", "description1", "id1", "name1_en", "name1_hr" }, StringPool.SEMICOLON, new Object[][] { { createDateString, "sample description", 1, "sample name", "naziv" } })) { validate( createDateString, "sample description", 1L, HashMapBuilder.put( "createDate1", "createDate" ).put( "description1", "description" ).put( "id1", "id" ).put( "name1", "name" ).build(), csvBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name" ).put( "hr", "naziv" ).build()); } }
@Test public void testColumnMappingWithUndefinedColumn() throws Exception { try (CSVBatchEngineImportTaskItemReader csvBatchEngineImportTaskItemReader = _getCSVBatchEngineImportTaskItemReader( new String[] { "createDate1", "description1", "id1", "name1_en", "name1_hr" }, StringPool.SEMICOLON, new Object[][] { { createDateString, "sample description", 1, "sample name", "naziv" } })) { validate( createDateString, "sample description", 1L, HashMapBuilder.put( "createDate1", "createDate" ).put( "description1", "description" ).put( "id1", "id" ).build(), csvBatchEngineImportTaskItemReader.read(), null); } }
@Test public void testColumnMappingWithUndefinedTargetColumn() throws Exception { try (CSVBatchEngineImportTaskItemReader csvBatchEngineImportTaskItemReader = _getCSVBatchEngineImportTaskItemReader( new String[] { "createDate1", "description1", "id1", "name1_en", "name1_hr" }, StringPool.SEMICOLON, new Object[][] { { createDateString, "sample description", 1, "sample name", "naziv" } })) { validate( createDateString, "sample description", 1L, new HashMap<String, String>() { { put("createDate1", "createDate"); put("description1", "description"); put("id1", "id"); put("name1", null); } }, csvBatchEngineImportTaskItemReader.read(), null); } }
@Test public void testInvalidColumnMapping() throws Exception { try (CSVBatchEngineImportTaskItemReader csvBatchEngineImportTaskItemReader = _getCSVBatchEngineImportTaskItemReader( new String[] { "createDate1", "description1", "id1", "name1_en", "name1_hr" }, StringPool.SEMICOLON, new Object[][] { { createDateString, "sample description", 1, "sample name", "naziv" } })) { try { validate( createDateString, "sample description", null, HashMapBuilder.put( "createDate1", "description" ).put( "description1", "createDate" ).put( "id1", "id" ).put( "name1", "name" ).build(), csvBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name" ).put( "hr", "naziv" ).build()); Assert.fail(); } catch (IllegalArgumentException illegalArgumentException) { } } }
@Test public void testReadInvalidRow() throws Exception { try (CSVBatchEngineImportTaskItemReader csvBatchEngineImportTaskItemReader = _getCSVBatchEngineImportTaskItemReader( FIELD_NAMES, StringPool.COMMA, new Object[][] { { "", "sample description", 1, "sample name", "naziv", "unknown column" } })) { try { csvBatchEngineImportTaskItemReader.read(); Assert.fail(); } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) { } } }
@Test public void testReadMultipleRows() throws Exception { try (CSVBatchEngineImportTaskItemReader csvBatchEngineImportTaskItemReader = _getCSVBatchEngineImportTaskItemReader( FIELD_NAMES, StringPool.COMMA, new Object[][] { { createDateString, "sample description 1", 1, "sample name 1", "naziv 1" }, { createDateString, "sample description 2", 2, "sample name 2", "naziv 2" } })) { for (int i = 1; i < 3; i++) { long rowCount = i; validate( createDateString, "sample description " + rowCount, rowCount, Collections.emptyMap(), csvBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name " + rowCount ).put( "hr", "naziv " + rowCount ).build()); } } }
@Test public void testReadRowsWithCommaInsideQuotes() throws Exception { try (CSVBatchEngineImportTaskItemReader csvBatchEngineImportTaskItemReader = _getCSVBatchEngineImportTaskItemReader( FIELD_NAMES, StringPool.SEMICOLON, new Object[][] { { createDateString, "hey, here is comma inside", 1, "sample name", "naziv" } })) { validate( createDateString, "hey, here is comma inside", 1L, Collections.emptyMap(), csvBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name" ).put( "hr", "naziv" ).build()); } }
@Test public void testReadRowsWithLessValues() throws Exception { try (CSVBatchEngineImportTaskItemReader csvBatchEngineImportTaskItemReader = _getCSVBatchEngineImportTaskItemReader( FIELD_NAMES, StringPool.COMMA, new Object[][] {{"", "", 1}})) { validate( null, null, 1L, Collections.emptyMap(), csvBatchEngineImportTaskItemReader.read(), null); } }
@Test public void testReadRowsWithNullValues() throws Exception { try (CSVBatchEngineImportTaskItemReader csvBatchEngineImportTaskItemReader = _getCSVBatchEngineImportTaskItemReader( FIELD_NAMES, StringPool.COMMA, new Object[][] { {createDateString, "", 1, "", "naziv 1"}, { createDateString, "sample description 2", 2, "sample name 2", "naziv 2" } })) { validate( createDateString, null, 1L, Collections.emptyMap(), csvBatchEngineImportTaskItemReader.read(), new HashMap<String, String>() { { put("en", null); put("hr", "naziv 1"); } }); validate( createDateString, "sample description 2", 2L, Collections.emptyMap(), csvBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name 2" ).put( "hr", "naziv 2" ).build()); } }