@Test public void summarizeCollapsesRepeatedErrors() { List<ErrorRecordBase> errs = IntStream.range(1, 102) .boxed() .map(i -> new ErrorRecordBase(String.format("error %s", i % 2 == 0 ? "foo" : "bar"), i, true)) .collect(Collectors.toList()); String[] results = ErrorRecordsException.summarize(errs) .split(ErrorRecordsException.ERROR_SUMMARY_ITEM_DELIM); Assert.assertArrayEquals(new String[]{"error bar - 51 rows", "error foo - 50 rows"}, results); }
static String summarize(List<ErrorRecordBase> errorRecords) { List<String> lines = errorRecords .stream() .collect(Collectors.groupingBy(ErrorRecordBase::getMessage)) .entrySet() .stream() .sorted(Comparator.comparingInt(a -> -a.getValue().size())) .map(entry -> String.format("%s - %d rows", entry.getKey(), entry.getValue().size())) .collect(Collectors.toList()); if (lines.size() > MAX_NUM_ERRORS_IN_SUMMARY) { lines = lines.subList(0, MAX_NUM_ERRORS_IN_SUMMARY - 1); lines.add("... and other errors hidden for brevity"); } return String.join(ERROR_SUMMARY_ITEM_DELIM, lines); }
ErrorRecordsException extends RuntimeException { static String summarize(List<ErrorRecordBase> errorRecords) { List<String> lines = errorRecords .stream() .collect(Collectors.groupingBy(ErrorRecordBase::getMessage)) .entrySet() .stream() .sorted(Comparator.comparingInt(a -> -a.getValue().size())) .map(entry -> String.format("%s - %d rows", entry.getKey(), entry.getValue().size())) .collect(Collectors.toList()); if (lines.size() > MAX_NUM_ERRORS_IN_SUMMARY) { lines = lines.subList(0, MAX_NUM_ERRORS_IN_SUMMARY - 1); lines.add("... and other errors hidden for brevity"); } return String.join(ERROR_SUMMARY_ITEM_DELIM, lines); } }
ErrorRecordsException extends RuntimeException { static String summarize(List<ErrorRecordBase> errorRecords) { List<String> lines = errorRecords .stream() .collect(Collectors.groupingBy(ErrorRecordBase::getMessage)) .entrySet() .stream() .sorted(Comparator.comparingInt(a -> -a.getValue().size())) .map(entry -> String.format("%s - %d rows", entry.getKey(), entry.getValue().size())) .collect(Collectors.toList()); if (lines.size() > MAX_NUM_ERRORS_IN_SUMMARY) { lines = lines.subList(0, MAX_NUM_ERRORS_IN_SUMMARY - 1); lines.add("... and other errors hidden for brevity"); } return String.join(ERROR_SUMMARY_ITEM_DELIM, lines); } ErrorRecordsException(List<ErrorRecordBase> errorRecords); }
ErrorRecordsException extends RuntimeException { static String summarize(List<ErrorRecordBase> errorRecords) { List<String> lines = errorRecords .stream() .collect(Collectors.groupingBy(ErrorRecordBase::getMessage)) .entrySet() .stream() .sorted(Comparator.comparingInt(a -> -a.getValue().size())) .map(entry -> String.format("%s - %d rows", entry.getKey(), entry.getValue().size())) .collect(Collectors.toList()); if (lines.size() > MAX_NUM_ERRORS_IN_SUMMARY) { lines = lines.subList(0, MAX_NUM_ERRORS_IN_SUMMARY - 1); lines.add("... and other errors hidden for brevity"); } return String.join(ERROR_SUMMARY_ITEM_DELIM, lines); } ErrorRecordsException(List<ErrorRecordBase> errorRecords); List<ErrorRecordBase> getErrorRecords(); }
ErrorRecordsException extends RuntimeException { static String summarize(List<ErrorRecordBase> errorRecords) { List<String> lines = errorRecords .stream() .collect(Collectors.groupingBy(ErrorRecordBase::getMessage)) .entrySet() .stream() .sorted(Comparator.comparingInt(a -> -a.getValue().size())) .map(entry -> String.format("%s - %d rows", entry.getKey(), entry.getValue().size())) .collect(Collectors.toList()); if (lines.size() > MAX_NUM_ERRORS_IN_SUMMARY) { lines = lines.subList(0, MAX_NUM_ERRORS_IN_SUMMARY - 1); lines.add("... and other errors hidden for brevity"); } return String.join(ERROR_SUMMARY_ITEM_DELIM, lines); } ErrorRecordsException(List<ErrorRecordBase> errorRecords); List<ErrorRecordBase> getErrorRecords();  }