FilterTwoSensPerDayProcessor implements ItemProcessor<BatchExcelRow, BatchExcelRow>, InitializingBean { @Override public BatchExcelRow process(final BatchExcelRow item) throws Exception { BatchExcelRow result = item; Language lang = Language.valueOf(language.toUpperCase(Locale.US)); StationBasedExcelRowComparator comparator = new StationBasedExcelRowComparator(lang); LOGGER.trace("item", item); try { outputReader.open(executionContext); try { for (BatchExcelRow matchingExcelRow = outputReader.read(); matchingExcelRow != null; matchingExcelRow = outputReader.read()) { if (!isEmpty(matchingExcelRow)) { if (comparator.compare(item, matchingExcelRow) == 0) { if (item.getDelay() > matchingExcelRow.getDelay()) { result = item; if (matchingExcelRow.getIndex() != null) { result.setIndex(matchingExcelRow.getIndex()); LOGGER.trace("replace_matching", matchingExcelRow); } else { throw new IllegalArgumentException("We don't know the current index of this Excel row. We cannot replace it!"); } } else { result = null; LOGGER.trace("not_replace_matching", matchingExcelRow); } LOGGER.debug("stop_searching", result); break; } else if (item.getDate().isBefore(matchingExcelRow.getDate())) { result = item; LOGGER.debug("item_before_matching", matchingExcelRow); break; } } else { result = item; result.setIndex(null); LOGGER.debug("first_empty_row", item); break; } } } finally { outputReader.close(); } } catch (ItemStreamException e) { LOGGER.warn("Error when opening ResourceAwareItemReaderItemStream. Maybe the resource is not available yet.", e); result = item; } LOGGER.trace("result", result); return result; }  @Override void afterPropertiesSet(); @Override BatchExcelRow process(final BatchExcelRow item); void setLanguage(String language); void setOutputReader(ItemStreamReader<BatchExcelRow> outputReader);  }
@Test public void testProcessReturnNew() throws Exception { BatchExcelRow excelRow = processor.process(new Builder(LocalDate.parse("2000-01-05"), Sens.ARRIVAL) .departureStation(stationB) .arrivalStation(stationA) .expectedTrain1(new TrainLine.Builder(578L).build()) .expectedArrivalTime(LocalTime.parse("22:00")) .expectedDepartureTime(LocalTime.parse("22:05")) .effectiveTrain1(new TrainLine.Builder(578L).build()) .effectiveDepartureTime(LocalTime.parse("16:03")) .effectiveArrivalTime(LocalTime.parse("16:10")) .delay(5L) .build()); Assert.assertNotNull(excelRow); Assert.assertNull(excelRow.getIndex()); }
@Test public void testProcessReachEof() throws Exception { BatchExcelRow expected = new Builder(LocalDate.parse("2000-01-02"), Sens.ARRIVAL) .departureStation(stationB) .arrivalStation(stationA) .expectedTrain1(new TrainLine.Builder(578L).build()) .expectedArrivalTime(LocalTime.parse("17:00")) .expectedDepartureTime(LocalTime.parse("17:05")) .effectiveTrain1(new TrainLine.Builder(578L).build()) .effectiveDepartureTime(LocalTime.parse("16:03")) .effectiveArrivalTime(LocalTime.parse("16:10")) .delay(25L) .build(); BatchExcelRow excelRow = processor.process(expected); Assert.assertEquals(expected, excelRow); }
@Test public void testProcessReturnReplace() throws Exception { BatchExcelRow excelRow = processor.process(new Builder(LocalDate.parse("2000-01-02"), Sens.ARRIVAL) .departureStation(stationB) .arrivalStation(stationA) .expectedTrain1(new TrainLine.Builder(578L).build()) .expectedArrivalTime(LocalTime.parse("17:00")) .expectedDepartureTime(LocalTime.parse("17:05")) .effectiveTrain1(new TrainLine.Builder(578L).build()) .effectiveDepartureTime(LocalTime.parse("16:03")) .effectiveArrivalTime(LocalTime.parse("16:10")) .delay(25L) .build()); Assert.assertNotNull(excelRow); Assert.assertNotNull(excelRow.getIndex()); }
@Test public void testProcessReturnSkip() throws Exception { BatchExcelRow excelRow = processor.process(new Builder(LocalDate.parse("2000-01-02"), Sens.ARRIVAL) .departureStation(stationB) .arrivalStation(stationA) .expectedTrain1(new TrainLine.Builder(578L).build()) .expectedArrivalTime(LocalTime.parse("16:00")) .expectedDepartureTime(LocalTime.parse("16:05")) .effectiveTrain1(new TrainLine.Builder(466L).build()) .effectiveDepartureTime(LocalTime.parse("16:03")) .effectiveArrivalTime(LocalTime.parse("16:10")) .delay(0L) .build()); Assert.assertNull(excelRow); }
@Test(expected = IllegalArgumentException.class) public void testProcessNoIndex() throws Exception { list.forEach(row -> row.setIndex(null)); processor.process(new Builder(LocalDate.parse("2000-01-02"), Sens.ARRIVAL) .departureStation(stationB) .arrivalStation(stationA) .expectedTrain1(new TrainLine.Builder(578L).build()) .expectedArrivalTime(LocalTime.parse("17:00")) .expectedDepartureTime(LocalTime.parse("17:05")) .effectiveTrain1(new TrainLine.Builder(578L).build()) .effectiveDepartureTime(LocalTime.parse("16:03")) .effectiveArrivalTime(LocalTime.parse("16:10")) .delay(25L) .build()); }
@Test public void testProcessBefore() throws Exception { BatchExcelRow excelRow = processor.process(new Builder(LocalDate.parse("1999-12-31"), Sens.ARRIVAL) .departureStation(stationB) .arrivalStation(stationA) .expectedTrain1(new TrainLine.Builder(578L).build()) .expectedArrivalTime(LocalTime.parse("22:00")) .expectedDepartureTime(LocalTime.parse("22:05")) .effectiveTrain1(new TrainLine.Builder(578L).build()) .effectiveDepartureTime(LocalTime.parse("16:03")) .effectiveArrivalTime(LocalTime.parse("16:10")) .delay(5L) .build()); Assert.assertNotNull(excelRow); }