BatchProcessor { public <T> void process(final String collectionName, final Function<List<String>, String> batchQueryFn, final Collection<String> primaryEntries, final Class<T> resultClass, final Consumer<T> consumer) { List<String> asList = new ArrayList<>(); asList.addAll(primaryEntries); final List<List<String>> batches = splitList(this.batchSize, asList); batches.forEach(batch -> { final List<T> dbEntry = repoHandler.getListByQuery(collectionName, batchQueryFn.apply(batch), resultClass); if(dbEntry.size() < batch.size()) { LOG.warn(String.format("Got fewer results %s < %s", dbEntry.size(), batch.size())); LOG.warn("There are referred dependencies not related to known artifacts"); } dbEntry.forEach(consumer); }); } BatchProcessor(RepositoryHandler handler); void process(final String collectionName,
                            final Function<List<String>, String> batchQueryFn,
                            final Collection<String> primaryEntries,
                            final Class<T> resultClass,
                            final Consumer<T> consumer); void setBatchSize(int value);  }
@Test public void consumerIsUsedOnAllReturnedEntities() { List<String> fullList = generateList(4); List<String> batch = generateList(1); final BatchProcessor sut = makeSut(batch, 1); Consumer<String> consumerMock = mock(Consumer.class); sut.process("Some collection", b -> "", fullList, String.class, consumerMock); verify(consumerMock, times(fullList.size())).accept(anyString()); }
@Test public void missingEntriesGenerateLogMessages() throws NoSuchFieldException, IllegalAccessException { List<String> fullList = generateList(4); List<String> batch = generateList(3); final BatchProcessor sut = makeSut(batch, fullList.size()); final Logger fakeLogger = mock(Logger.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); InjectionUtils.injectField(sut, BatchProcessor.class, "LOG", fakeLogger); sut.process("Some collection", b -> "", fullList, String.class, mock(Consumer.class)); final List<String> allValues = captor.getAllValues(); verify(fakeLogger, times(2)).warn(captor.capture()); assertEquals("Got fewer results 3 < 4", allValues.get(0)); assertEquals("There are referred dependencies not related to known artifacts", allValues.get(1)); }
@Test public void splitListWorksCorrectExact() throws NoSuchFieldException, IllegalAccessException { final List<String> fullList = generateList(6); final List<String> batch = generateList(3); final BatchProcessor sut1 = makeSut(batch, batch.size()); final Function<List<String>, String> fnMock = mock(Function.class); sut1.process("Some collection", fnMock, fullList, String.class, mock(Consumer.class)); verify(fnMock, times(getExpectedBatchCount(fullList, batch))).apply(anyListOf(String.class)); }
@Test public void splitListWorksCorrectReminder() throws NoSuchFieldException, IllegalAccessException { final List<String> fullList = generateList(5); final List<String> batch = generateList(2); final BatchProcessor sut1 = makeSut(batch, batch.size()); final Function<List<String>, String> fnMock = mock(Function.class); sut1.process("Some collection", fnMock, fullList, String.class, mock(Consumer.class)); verify(fnMock, times(getExpectedBatchCount(fullList, batch))).apply(anyListOf(String.class)); }