@SuppressWarnings("unchecked") @Test void testDeleteAll() { Entity permittedEntity = mock(Entity.class); Entity notPermittedEntity = mock(Entity.class); doAnswer( invocation -> { Consumer<List<Entity>> consumer = invocation.getArgument(0); consumer.accept(asList(permittedEntity, notPermittedEntity)); return null; }) .when(delegateRepository) .forEachBatched(any(), eq(1000)); when(permissionChecker.isDeleteAllowed(permittedEntity)).thenReturn(true); permissionCheckingDecorator.deleteAll(); ArgumentCaptor<Stream<Entity>> entityStreamCaptor = ArgumentCaptor.forClass(Stream.class); verify(delegateRepository).delete(entityStreamCaptor.capture()); assertEquals(singletonList(permittedEntity), entityStreamCaptor.getValue().collect(toList())); }
@Transactional @Override public void deleteAll(Stream<Object> ids) { partition(ids.iterator(), BATCH_SIZE) .forEachRemaining( batchIds -> { List<E> entities = delegate().findAll(batchIds.stream()).collect(toList()); delegate() .deleteAll( entities.stream() .filter(permissionChecker::isDeleteAllowed) .map(Entity::getIdValue)); }); }
PermissionCheckingDecorator extends AbstractRepositoryDecorator<E> { @Transactional @Override public void deleteAll(Stream<Object> ids) { partition(ids.iterator(), BATCH_SIZE) .forEachRemaining( batchIds -> { List<E> entities = delegate().findAll(batchIds.stream()).collect(toList()); delegate() .deleteAll( entities.stream() .filter(permissionChecker::isDeleteAllowed) .map(Entity::getIdValue)); }); } }
PermissionCheckingDecorator extends AbstractRepositoryDecorator<E> { @Transactional @Override public void deleteAll(Stream<Object> ids) { partition(ids.iterator(), BATCH_SIZE) .forEachRemaining( batchIds -> { List<E> entities = delegate().findAll(batchIds.stream()).collect(toList()); delegate() .deleteAll( entities.stream() .filter(permissionChecker::isDeleteAllowed) .map(Entity::getIdValue)); }); } PermissionCheckingDecorator(
      Repository<E> delegateRepository, PermissionChecker<E> permissionChecker); }
PermissionCheckingDecorator extends AbstractRepositoryDecorator<E> { @Transactional @Override public void deleteAll(Stream<Object> ids) { partition(ids.iterator(), BATCH_SIZE) .forEachRemaining( batchIds -> { List<E> entities = delegate().findAll(batchIds.stream()).collect(toList()); delegate() .deleteAll( entities.stream() .filter(permissionChecker::isDeleteAllowed) .map(Entity::getIdValue)); }); } PermissionCheckingDecorator(
      Repository<E> delegateRepository, PermissionChecker<E> permissionChecker); @Override @Nonnull Iterator<E> iterator(); @Override void forEachBatched(Fetch fetch, Consumer<List<E>> consumer, int batchSize); @Override long count(); @Override long count(Query<E> q); @Override Stream<E> findAll(Query<E> q); @Override E findOne(Query<E> q); @Override E findOneById(Object id); @Override E findOneById(Object id, Fetch fetch); @Override Stream<E> findAll(Stream<Object> ids); @Override Stream<E> findAll(Stream<Object> ids, Fetch fetch); @Override void update(E entity); @Override void update(Stream<E> entities); @Override void delete(E entity); @Override void delete(Stream<E> entities); @Override void deleteById(Object id); @Transactional @Override void deleteAll(Stream<Object> ids); @Transactional @Override void deleteAll(); @Override void add(E entity); @Override Integer add(Stream<E> entities); @Override AggregateResult aggregate(AggregateQuery aggregateQuery); }
PermissionCheckingDecorator extends AbstractRepositoryDecorator<E> { @Transactional @Override public void deleteAll(Stream<Object> ids) { partition(ids.iterator(), BATCH_SIZE) .forEachRemaining( batchIds -> { List<E> entities = delegate().findAll(batchIds.stream()).collect(toList()); delegate() .deleteAll( entities.stream() .filter(permissionChecker::isDeleteAllowed) .map(Entity::getIdValue)); }); } PermissionCheckingDecorator(
      Repository<E> delegateRepository, PermissionChecker<E> permissionChecker); @Override @Nonnull Iterator<E> iterator(); @Override void forEachBatched(Fetch fetch, Consumer<List<E>> consumer, int batchSize); @Override long count(); @Override long count(Query<E> q); @Override Stream<E> findAll(Query<E> q); @Override E findOne(Query<E> q); @Override E findOneById(Object id); @Override E findOneById(Object id, Fetch fetch); @Override Stream<E> findAll(Stream<Object> ids); @Override Stream<E> findAll(Stream<Object> ids, Fetch fetch); @Override void update(E entity); @Override void update(Stream<E> entities); @Override void delete(E entity); @Override void delete(Stream<E> entities); @Override void deleteById(Object id); @Transactional @Override void deleteAll(Stream<Object> ids); @Transactional @Override void deleteAll(); @Override void add(E entity); @Override Integer add(Stream<E> entities); @Override AggregateResult aggregate(AggregateQuery aggregateQuery);  }