JSONBatchEngineImportTaskItemReader implements BatchEngineImportTaskItemReader { @Override public Map<String, Object> read() throws Exception { if (_jsonParser.nextToken() == JsonToken.START_OBJECT) { return _objectMapper.readValue( _jsonParser, new TypeReference<Map<String, Object>>() { }); } return null; } JSONBatchEngineImportTaskItemReader(InputStream inputStream); @Override void close(); @Override Map<String, Object> read();  }
@Test public void testColumnMapping() throws Exception { try (JSONBatchEngineImportTaskItemReader jsonBatchEngineImportTaskItemReader = _getJSONBatchEngineImportTaskItemReader( new String[] { "createDate1", "description1", "id1", "name1" }, new Object[][] { { "\"" + createDateString + "\"", "\"sample description\"", 1, "{\"en\": \"sample name\", \"hr\": \"naziv\"}" } })) { validate( createDateString, "sample description", 1L, HashMapBuilder.put( "createDate1", "createDate" ).put( "description1", "description" ).put( "id1", "id" ).put( "name1", "name" ).build(), jsonBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name" ).put( "hr", "naziv" ).build()); } }
@Test public void testColumnMappingWithUndefinedColumn() throws Exception { try (JSONBatchEngineImportTaskItemReader jsonBatchEngineImportTaskItemReader = _getJSONBatchEngineImportTaskItemReader( new String[] { "createDate1", "description1", "id1", "name1" }, new Object[][] { { "\"" + createDateString + "\"", "\"sample description\"", 1, "{\"en\": \"sample name\", \"hr\": \"naziv\"}" } })) { validate( createDateString, "sample description", 1L, HashMapBuilder.put( "createDate1", "createDate" ).put( "description1", "description" ).put( "id1", "id" ).build(), jsonBatchEngineImportTaskItemReader.read(), null); } }
@Test public void testColumnMappingWithUndefinedTargetColumn() throws Exception { try (JSONBatchEngineImportTaskItemReader jsonBatchEngineImportTaskItemReader = _getJSONBatchEngineImportTaskItemReader( new String[] { "createDate1", "description1", "id1", "name1" }, new Object[][] { { "\"" + createDateString + "\"", "\"sample description\"", 1, "{\"en\": \"sample name\", \"hr\": \"naziv\"}" } })) { validate( createDateString, "sample description", 1L, new HashMap<String, String>() { { put("createDate1", "createDate"); put("description1", "description"); put("id1", "id"); put("name1", null); } }, jsonBatchEngineImportTaskItemReader.read(), null); } }
@Test public void testInvalidColumnMapping() throws Exception { try (JSONBatchEngineImportTaskItemReader jsonBatchEngineImportTaskItemReader = _getJSONBatchEngineImportTaskItemReader( new String[] { "createDate1", "description1", "id1", "name1" }, new Object[][] { { "\"" + createDateString + "\"", "\"sample description\"", 1, "{\"en\": \"sample name\", \"hr\": \"naziv\"}" } })) { try { validate( createDateString, "sample description", null, HashMapBuilder.put( "createDate1", "description" ).put( "description1", "createDate" ).put( "id1", "id" ).put( "name1", "name" ).build(), jsonBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name" ).put( "hr", "naziv" ).build()); Assert.fail(); } catch (IllegalArgumentException illegalArgumentException) { } } }
@Test public void testReadInvalidRow() throws Exception { try (JSONBatchEngineImportTaskItemReader jsonBatchEngineImportTaskItemReader = _getJSONBatchEngineImportTaskItemReader( _FIELD_NAMES, new Object[][] { { null, "\"sample description\"", 1, "{\"en\": \"sample name\", \"hr\": \"naziv\"}", "\"unknown column\"" } })) { try { validate( createDateString, "sample description", null, Collections.emptyMap(), jsonBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name" ).put( "hr", "naziv" ).build()); Assert.fail(); } catch (NoSuchFieldException noSuchFieldException) { } } }
@Test public void testReadMultipleRows() throws Exception { try (JSONBatchEngineImportTaskItemReader jsonBatchEngineImportTaskItemReader = _getJSONBatchEngineImportTaskItemReader( _FIELD_NAMES, new Object[][] { { "\"" + createDateString + "\"", "\"sample description 1\"", 1, "{\"en\": \"sample name 1\", \"hr\": \"naziv " + "1\"}" }, { "\"" + createDateString + "\"", "\"sample description 2\"", 2, "{\"en\": \"sample name 2\", \"hr\": \"naziv " + "2\"}" } })) { for (int i = 1; i < 3; i++) { long rowCount = i; validate( createDateString, "sample description " + rowCount, rowCount, Collections.emptyMap(), jsonBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name " + rowCount ).put( "hr", "naziv " + rowCount ).build()); } } }
@Test public void testReadRowsWithCommaInsideQuotes() throws Exception { try (JSONBatchEngineImportTaskItemReader jsonBatchEngineImportTaskItemReader = _getJSONBatchEngineImportTaskItemReader( _FIELD_NAMES, new Object[][] { { "\"" + createDateString + "\"", "\"hey, here is comma inside\"", 1, "{\"en\": \"sample name\", \"hr\": \"naziv\"}" } })) { validate( createDateString, "hey, here is comma inside", 1L, Collections.emptyMap(), jsonBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name" ).put( "hr", "naziv" ).build()); } }
@Test public void testReadRowsWithLessValues() throws Exception { try (JSONBatchEngineImportTaskItemReader jsonBatchEngineImportTaskItemReader = _getJSONBatchEngineImportTaskItemReader( _FIELD_NAMES, new Object[][] {{"null", "null", 1}})) { validate( null, null, 1L, Collections.emptyMap(), jsonBatchEngineImportTaskItemReader.read(), null); } }
@Test public void testReadRowsWithNullValues() throws Exception { try (JSONBatchEngineImportTaskItemReader jsonBatchEngineImportTaskItemReader = _getJSONBatchEngineImportTaskItemReader( _FIELD_NAMES, new Object[][] { { "\"" + createDateString + "\"", "null", 1, "{\"hr\": \"naziv 1\"}" }, { "\"" + createDateString + "\"", "\"sample description 2\"", 2, "{\"en\": \"sample name 2\", \"hr\": \"naziv " + "2\"}" } })) { validate( createDateString, null, 1L, Collections.emptyMap(), jsonBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "hr", "naziv 1" ).build()); validate( createDateString, "sample description 2", 2L, Collections.emptyMap(), jsonBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name 2" ).put( "hr", "naziv 2" ).build()); } }