@Test void updateEntitiesTest() { String repoFullName = "EntityRepo"; Entity entity1 = Mockito.mock(Entity.class); Entity entity2 = Mockito.mock(Entity.class); entityListenersService.register(repoFullName); EntityListener entityListener1 = Mockito.when(Mockito.mock(EntityListener.class).getEntityId()).thenReturn(1).getMock(); EntityListener entityListener2 = Mockito.when(Mockito.mock(EntityListener.class).getEntityId()).thenReturn(2).getMock(); Mockito.when(entity1.getIdValue()).thenReturn(1).getMock(); Mockito.when(entity2.getIdValue()).thenReturn(2).getMock(); entityListenersService.addEntityListener(repoFullName, entityListener1); entityListenersService.addEntityListener(repoFullName, entityListener2); entityListenersService .updateEntities(repoFullName, Stream.of(entity1, entity2)) .collect(Collectors.toList()); Mockito.verify(entityListener1).postUpdate(entity1); Mockito.verify(entityListener2).postUpdate(entity2); entityListenersService.removeEntityListener(repoFullName, entityListener1); entityListenersService.removeEntityListener(repoFullName, entityListener2); assertTrue(entityListenersService.isEmpty(repoFullName)); }
Stream<Entity> updateEntities(String repoFullName, Stream<Entity> entities) { lock.readLock().lock(); try { verifyRepoRegistered(repoFullName); SetMultimap<Object, EntityListener> entityListeners = this.entityListenersByRepo.get(repoFullName); return entities.filter( entity -> { Set<EntityListener> entityEntityListeners = entityListeners.get(entity.getIdValue()); entityEntityListeners.forEach(entityListener -> entityListener.postUpdate(entity)); return true; }); } finally { lock.readLock().unlock(); } }
EntityListenersService { Stream<Entity> updateEntities(String repoFullName, Stream<Entity> entities) { lock.readLock().lock(); try { verifyRepoRegistered(repoFullName); SetMultimap<Object, EntityListener> entityListeners = this.entityListenersByRepo.get(repoFullName); return entities.filter( entity -> { Set<EntityListener> entityEntityListeners = entityListeners.get(entity.getIdValue()); entityEntityListeners.forEach(entityListener -> entityListener.postUpdate(entity)); return true; }); } finally { lock.readLock().unlock(); } } }
EntityListenersService { Stream<Entity> updateEntities(String repoFullName, Stream<Entity> entities) { lock.readLock().lock(); try { verifyRepoRegistered(repoFullName); SetMultimap<Object, EntityListener> entityListeners = this.entityListenersByRepo.get(repoFullName); return entities.filter( entity -> { Set<EntityListener> entityEntityListeners = entityListeners.get(entity.getIdValue()); entityEntityListeners.forEach(entityListener -> entityListener.postUpdate(entity)); return true; }); } finally { lock.readLock().unlock(); } }  }
EntityListenersService { Stream<Entity> updateEntities(String repoFullName, Stream<Entity> entities) { lock.readLock().lock(); try { verifyRepoRegistered(repoFullName); SetMultimap<Object, EntityListener> entityListeners = this.entityListenersByRepo.get(repoFullName); return entities.filter( entity -> { Set<EntityListener> entityEntityListeners = entityListeners.get(entity.getIdValue()); entityEntityListeners.forEach(entityListener -> entityListener.postUpdate(entity)); return true; }); } finally { lock.readLock().unlock(); } }  void addEntityListener(String repoFullName, EntityListener entityListener); boolean removeEntityListener(String repoFullName, EntityListener entityListener); }
EntityListenersService { Stream<Entity> updateEntities(String repoFullName, Stream<Entity> entities) { lock.readLock().lock(); try { verifyRepoRegistered(repoFullName); SetMultimap<Object, EntityListener> entityListeners = this.entityListenersByRepo.get(repoFullName); return entities.filter( entity -> { Set<EntityListener> entityEntityListeners = entityListeners.get(entity.getIdValue()); entityEntityListeners.forEach(entityListener -> entityListener.postUpdate(entity)); return true; }); } finally { lock.readLock().unlock(); } }  void addEntityListener(String repoFullName, EntityListener entityListener); boolean removeEntityListener(String repoFullName, EntityListener entityListener);  }