EventPublishingDataStore implements DataStore<K, E>, DataStoreEventPublisher { @Override public boolean save(K key, E entity) { if (key == null) { log.error("Cannot save an entity under a null key"); throw new CorruptDataException(getEntityType(), null, "Cannot save an entity with a null key"); } if (entity == null) { log.error("Cannot save a null value into the data store"); throw new CorruptDataException(getEntityType(), null, "Cannot save null into the data store"); } final boolean entityIsNew = !delegate.hasKey(key); if (entityIsNew) { log.info("About to insert a new entity in the data store under key " + key); publishEvent(new BeforeInsertDataStoreEvent(repositoryMetadata, this, entity)); } else { log.info("About to update the entity in the data store under key " + key); publishEvent(new BeforeUpdateDataStoreEvent(repositoryMetadata, this, entity)); } delegate.save(key, entity); if (entityIsNew) { log.info("Finished inserting the entity in the data store under key " + key); publishEvent(new AfterInsertDataStoreEvent(repositoryMetadata, this, entity)); return true; } else { log.info("Finished updating the entity under key " + key); publishEvent(new AfterUpdateDataStoreEvent(repositoryMetadata, this, entity)); return false; } } 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(expectedExceptions = CorruptDataException.class) public void testSavingNullKey() { dataStore.save(null, new Person()); }
@Test(expectedExceptions = CorruptDataException.class) public void testSavingNullEntity() { dataStore.save("", null); }
@Test public void testInsert() { final String key = "k"; final Person entity = new Person(); dataStore.save(key, entity); assertThat(listenerContext.getEvents(), hasSize(2)); assertThat(listenerContext.getEvents().get(0).getEvent(), is(instanceOf(BeforeInsertDataStoreEvent.class))); assertThat(listenerContext.getEvents().get(1).getEvent(), is(instanceOf(AfterInsertDataStoreEvent.class))); assertThat(delegateSpy.getRequests(), hasSize(2)); final OperationRequest save = delegateSpy.getRequests().get(1); assertThat(save.getOperation(), is(Operation.SAVE)); assertThat(save.getKey(), Matchers.is(key)); assertThat(save.getEntity(), Matchers.is(entity)); final EventTrigger before = listenerContext.getEvents().get(0); final EventTrigger after = listenerContext.getEvents().get(1); assertThat(before.getTimestamp(), is(lessThan(save.getTimestamp()))); assertThat(save.getTimestamp(), is(lessThan(after.getTimestamp()))); assertThat(before.getEvent().getDataStore(), Matchers.is(dataStore)); assertThat(after.getEvent().getDataStore(), Matchers.is(dataStore)); }
@Test public void testUpdate() { final String key = "k"; final Person entity = new Person(); dataStore.save(key, entity); listenerContext.reset(); delegateSpy.reset(); dataStore.save(key, entity); assertThat(listenerContext.getEvents(), hasSize(2)); assertThat(listenerContext.getEvents().get(0).getEvent(), is(instanceOf(BeforeUpdateDataStoreEvent.class))); assertThat(listenerContext.getEvents().get(1).getEvent(), is(instanceOf(AfterUpdateDataStoreEvent.class))); assertThat(delegateSpy.getRequests(), hasSize(2)); final OperationRequest request = delegateSpy.getRequests().get(1); assertThat(request.getOperation(), is(Operation.SAVE)); assertThat(request.getKey(), Matchers.is(key)); assertThat(request.getEntity(), Matchers.is(entity)); 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)); }