@SuppressWarnings("ResultOfMethodCallIgnored") @Test void testCleanCacheBeforeDeleteByIdStreamInversedBy() { Attribute attribute = mock(Attribute.class); EntityType entityType = mock(EntityType.class); when(entityType.getInversedByAttributes()).thenReturn(Stream.of(attribute)); Entity refEntity = when(mock(Entity.class).getIdValue()).thenReturn("MyRefEntityId").getMock(); Entity entity = when(mock(Entity.class).getIdValue()).thenReturn("MyEntityId").getMock(); when(entity.getEntity(attribute)).thenReturn(refEntity); Object entityId = mock(Object.class); when(l1Cache.get(entityType, entityId)).thenReturn(Optional.of(CacheHit.of(entity))); l1CacheJanitorImpl.cleanCacheBeforeDeleteById(entityType, Stream.of(entityId)).count(); verify(l1Cache).evict(refEntity); verifyNoMoreInteractions(l1Cache); }
@Override public void cleanCacheBeforeDeleteById(EntityType entityType, Object entityId) { Iterator<Attribute> iterator = entityType.getInversedByAttributes().iterator(); if (!iterator.hasNext()) { return; } Entity entity = l1Cache.get(entityType, entityId).map(CacheHit::getValue).orElse(null); do { cleanCacheBeforeDeleteById(iterator.next(), entity); } while (iterator.hasNext()); }
L1CacheJanitorImpl implements L1CacheJanitor { @Override public void cleanCacheBeforeDeleteById(EntityType entityType, Object entityId) { Iterator<Attribute> iterator = entityType.getInversedByAttributes().iterator(); if (!iterator.hasNext()) { return; } Entity entity = l1Cache.get(entityType, entityId).map(CacheHit::getValue).orElse(null); do { cleanCacheBeforeDeleteById(iterator.next(), entity); } while (iterator.hasNext()); } }
L1CacheJanitorImpl implements L1CacheJanitor { @Override public void cleanCacheBeforeDeleteById(EntityType entityType, Object entityId) { Iterator<Attribute> iterator = entityType.getInversedByAttributes().iterator(); if (!iterator.hasNext()) { return; } Entity entity = l1Cache.get(entityType, entityId).map(CacheHit::getValue).orElse(null); do { cleanCacheBeforeDeleteById(iterator.next(), entity); } while (iterator.hasNext()); } L1CacheJanitorImpl(L1Cache l1Cache); }
L1CacheJanitorImpl implements L1CacheJanitor { @Override public void cleanCacheBeforeDeleteById(EntityType entityType, Object entityId) { Iterator<Attribute> iterator = entityType.getInversedByAttributes().iterator(); if (!iterator.hasNext()) { return; } Entity entity = l1Cache.get(entityType, entityId).map(CacheHit::getValue).orElse(null); do { cleanCacheBeforeDeleteById(iterator.next(), entity); } while (iterator.hasNext()); } L1CacheJanitorImpl(L1Cache l1Cache); @Override void cleanCacheBeforeAdd(Entity entity); @Override Stream<Entity> cleanCacheBeforeAdd(EntityType entityType, Stream<Entity> entityStream); @Override void cleanCacheBeforeUpdate(Entity entity); @Override Stream<Entity> cleanCacheBeforeUpdate(EntityType entityType, Stream<Entity> entityStream); @Override void cleanCacheBeforeDelete(Entity entity); @Override Stream<Entity> cleanCacheBeforeDelete(EntityType entityType, Stream<Entity> entityStream); @Override void cleanCacheBeforeDeleteById(EntityType entityType, Object entityId); @Override Stream<Object> cleanCacheBeforeDeleteById(
      EntityType entityType, Stream<Object> entityIdStream); @Override void cleanCacheBeforeDeleteAll(EntityType entityType); }
L1CacheJanitorImpl implements L1CacheJanitor { @Override public void cleanCacheBeforeDeleteById(EntityType entityType, Object entityId) { Iterator<Attribute> iterator = entityType.getInversedByAttributes().iterator(); if (!iterator.hasNext()) { return; } Entity entity = l1Cache.get(entityType, entityId).map(CacheHit::getValue).orElse(null); do { cleanCacheBeforeDeleteById(iterator.next(), entity); } while (iterator.hasNext()); } L1CacheJanitorImpl(L1Cache l1Cache); @Override void cleanCacheBeforeAdd(Entity entity); @Override Stream<Entity> cleanCacheBeforeAdd(EntityType entityType, Stream<Entity> entityStream); @Override void cleanCacheBeforeUpdate(Entity entity); @Override Stream<Entity> cleanCacheBeforeUpdate(EntityType entityType, Stream<Entity> entityStream); @Override void cleanCacheBeforeDelete(Entity entity); @Override Stream<Entity> cleanCacheBeforeDelete(EntityType entityType, Stream<Entity> entityStream); @Override void cleanCacheBeforeDeleteById(EntityType entityType, Object entityId); @Override Stream<Object> cleanCacheBeforeDeleteById(
      EntityType entityType, Stream<Object> entityIdStream); @Override void cleanCacheBeforeDeleteAll(EntityType entityType);  }