@Test public void apply() { final LocalDhisResourceRepository<TrackedEntityInstance> r1 = container.getRepository( TrackedEntityInstance.class, teiLocalDhisRepositoryPersistCallback ); final LocalDhisResourceRepository<Enrollment> r2 = container.getRepository( Enrollment.class, enrollmentLocalDhisRepositoryPersistCallback ); Mockito.doAnswer( invocation -> { final Collection<? extends DhisResource> resources = invocation.getArgument( 0 ); final Consumer<LocalDhisRepositoryPersistResult> consumer = invocation.getArgument( 2 ); resources.forEach( r -> consumer.accept( new LocalDhisRepositoryPersistResult( LocalDhisRepositoryPersistStatus.SUCCESS, r.getId() ) ) ); return null; } ).when( teiLocalDhisRepositoryPersistCallback ).persistSave( Mockito.anyCollection(), Mockito.anyBoolean(), Mockito.any() ); Mockito.doAnswer( invocation -> { final Collection<String> ids = invocation.getArgument( 0 ); final Consumer<LocalDhisRepositoryPersistResult> consumer = invocation.getArgument( 1 ); ids.forEach( id -> consumer.accept( new LocalDhisRepositoryPersistResult( LocalDhisRepositoryPersistStatus.SUCCESS, id ) ) ); return null; } ).when( teiLocalDhisRepositoryPersistCallback ).persistDeleteById( Mockito.anyCollection(), Mockito.any() ); Mockito.doAnswer( invocation -> { final Collection<? extends DhisResource> resources = invocation.getArgument( 0 ); final Consumer<LocalDhisRepositoryPersistResult> consumer = invocation.getArgument( 2 ); resources.forEach( r -> consumer.accept( new LocalDhisRepositoryPersistResult( LocalDhisRepositoryPersistStatus.SUCCESS, r.getId() ) ) ); return null; } ).when( enrollmentLocalDhisRepositoryPersistCallback ).persistSave( Mockito.anyCollection(), Mockito.anyBoolean(), Mockito.any() ); Mockito.doAnswer( invocation -> { final Collection<String> ids = invocation.getArgument( 0 ); final Consumer<LocalDhisRepositoryPersistResult> consumer = invocation.getArgument( 1 ); ids.forEach( id -> consumer.accept( new LocalDhisRepositoryPersistResult( LocalDhisRepositoryPersistStatus.SUCCESS, id ) ) ); return null; } ).when( enrollmentLocalDhisRepositoryPersistCallback ).persistDeleteById( Mockito.anyCollection(), Mockito.any() ); final TrackedEntityInstance trackedEntityInstance1 = new TrackedEntityInstance(); trackedEntityInstance1.setId( "A1234567890" ); r1.save( trackedEntityInstance1, null ); r1.deleteById( "A1234567891", null, TrackedEntityInstance::new ); final Enrollment enrollment1 = new Enrollment(); enrollment1.setId( "B1234567890" ); r2.save( enrollment1, null ); r2.deleteById( "B1234567891", null, Enrollment::new ); final List<String> ids = new ArrayList<>(); container.apply( ( resource, resourceKey, result ) -> ids.add( resource.getId() ) ); Assert.assertThat( ids, Matchers.contains( "A1234567890", "B1234567890", "B1234567891", "A1234567891" ) ); }
@Override public void apply( @Nonnull LocalDhisRepositoryResultCallback resultCallback ) { supportedResourceClasses.stream().map( repositoryItems::get ).filter( Objects::nonNull ) .forEach( ri -> ri.applySaves( resultCallback ) ); Lists.reverse( new ArrayList<>( supportedResourceClasses ) ).stream().map( repositoryItems::get ).filter( Objects::nonNull ) .forEach( ri -> ri.applyDeletes( resultCallback ) ); }
LocalDhisResourceRepositoryContainerImpl implements LocalDhisResourceRepositoryContainer { @Override public void apply( @Nonnull LocalDhisRepositoryResultCallback resultCallback ) { supportedResourceClasses.stream().map( repositoryItems::get ).filter( Objects::nonNull ) .forEach( ri -> ri.applySaves( resultCallback ) ); Lists.reverse( new ArrayList<>( supportedResourceClasses ) ).stream().map( repositoryItems::get ).filter( Objects::nonNull ) .forEach( ri -> ri.applyDeletes( resultCallback ) ); } }
LocalDhisResourceRepositoryContainerImpl implements LocalDhisResourceRepositoryContainer { @Override public void apply( @Nonnull LocalDhisRepositoryResultCallback resultCallback ) { supportedResourceClasses.stream().map( repositoryItems::get ).filter( Objects::nonNull ) .forEach( ri -> ri.applySaves( resultCallback ) ); Lists.reverse( new ArrayList<>( supportedResourceClasses ) ).stream().map( repositoryItems::get ).filter( Objects::nonNull ) .forEach( ri -> ri.applyDeletes( resultCallback ) ); } LocalDhisResourceRepositoryContainerImpl( @Nonnull Set<Class<? extends DhisResource>> supportedResourceClasses ); }
LocalDhisResourceRepositoryContainerImpl implements LocalDhisResourceRepositoryContainer { @Override public void apply( @Nonnull LocalDhisRepositoryResultCallback resultCallback ) { supportedResourceClasses.stream().map( repositoryItems::get ).filter( Objects::nonNull ) .forEach( ri -> ri.applySaves( resultCallback ) ); Lists.reverse( new ArrayList<>( supportedResourceClasses ) ).stream().map( repositoryItems::get ).filter( Objects::nonNull ) .forEach( ri -> ri.applyDeletes( resultCallback ) ); } LocalDhisResourceRepositoryContainerImpl( @Nonnull Set<Class<? extends DhisResource>> supportedResourceClasses ); @Nonnull @Override @SuppressWarnings( "unchecked" ) LocalDhisResourceRepository<T> getRepository(
        @Nonnull Class<T> resourceClass, @Nonnull LocalDhisRepositoryPersistCallback<T> persistCallback ); @Override void apply( @Nonnull LocalDhisRepositoryResultCallback resultCallback ); }
LocalDhisResourceRepositoryContainerImpl implements LocalDhisResourceRepositoryContainer { @Override public void apply( @Nonnull LocalDhisRepositoryResultCallback resultCallback ) { supportedResourceClasses.stream().map( repositoryItems::get ).filter( Objects::nonNull ) .forEach( ri -> ri.applySaves( resultCallback ) ); Lists.reverse( new ArrayList<>( supportedResourceClasses ) ).stream().map( repositoryItems::get ).filter( Objects::nonNull ) .forEach( ri -> ri.applyDeletes( resultCallback ) ); } LocalDhisResourceRepositoryContainerImpl( @Nonnull Set<Class<? extends DhisResource>> supportedResourceClasses ); @Nonnull @Override @SuppressWarnings( "unchecked" ) LocalDhisResourceRepository<T> getRepository(
        @Nonnull Class<T> resourceClass, @Nonnull LocalDhisRepositoryPersistCallback<T> persistCallback ); @Override void apply( @Nonnull LocalDhisRepositoryResultCallback resultCallback );  }