EventPublishingDataStore implements DataStore<K, E>, DataStoreEventPublisher { @Override public boolean delete(K key) { if (!delegate.hasKey(key)) { log.info("Attempted to delete entity with key " + key + " but found nothing"); return false; } final E entity = delegate.retrieve(key); log.info("About to delete an entity with key " + key); publishEvent(new BeforeDeleteDataStoreEvent(repositoryMetadata, this, entity)); delegate.delete(key); log.info("Finished deleting the entity with key " + key); publishEvent(new AfterDeleteDataStoreEvent(repositoryMetadata, this, entity)); return true; } EventPublishingDataStore(DataStore<K, E> delegate, RepositoryMetadata repositoryMetadata,
                                    DataStoreEventListenerContext listenerContext); @Override boolean hasKey(K key); @Override boolean save(K key, E entity); @Override boolean delete(K key); @Override E retrieve(K key); @Override Collection<K> keys(); @Override Collection<E> retrieveAll(); @Override Class<E> getEntityType(); @Override void truncate(); @Override void publishEvent(DataStoreEvent event);  }
@Test public void testDeletingNonExistentKey() { dataStore.delete("key"); assertThat(delegateSpy.getRequests(), hasSize(1)); assertThat(listenerContext.getEvents(), is(Matchers.empty())); }
@Test public void testDelete() { final String key = "k"; final Person entity = new Person(); dataStore.save(key, entity); listenerContext.reset(); delegateSpy.reset(); dataStore.delete(key); assertThat(listenerContext.getEvents(), hasSize(2)); assertThat(listenerContext.getEvents().get(0).getEvent(), is(instanceOf(BeforeDeleteDataStoreEvent.class))); assertThat(listenerContext.getEvents().get(1).getEvent(), is(instanceOf(AfterDeleteDataStoreEvent.class))); assertThat(delegateSpy.getRequests(), hasSize(3)); final OperationRequest request = delegateSpy.getRequests().get(2); assertThat(request.getOperation(), is(Operation.DELETE)); assertThat(request.getKey(), Matchers.is(key)); assertThat(request.getEntity(), is(nullValue())); final EventTrigger before = listenerContext.getEvents().get(0); final EventTrigger after = listenerContext.getEvents().get(1); assertThat(before.getTimestamp(), is(lessThan(request.getTimestamp()))); assertThat(request.getTimestamp(), is(lessThan(after.getTimestamp()))); assertThat(before.getEvent().getDataStore(), Matchers.is(dataStore)); assertThat(after.getEvent().getDataStore(), Matchers.is(dataStore)); }