AuditDataEventListener implements DataStoreEventListener<DataStoreEvent> { @SuppressWarnings({"unchecked", "ConstantConditions"}) @Override public void onEvent(DataStoreEvent event) { if (event instanceof BeforeInsertDataStoreEvent) { final Object entity = ((BeforeInsertDataStoreEvent) event).getEntity(); final Auditable wrapper = getAuditable(entity, event.getRepositoryMetadata()); wrapper.setCreatedBy(auditorAware == null ? null : auditorAware.getCurrentAuditor().orElse(null)); wrapper.setCreatedDate(Instant.now()); } else if (event instanceof BeforeUpdateDataStoreEvent) { final Object entity = ((BeforeUpdateDataStoreEvent) event).getEntity(); final Auditable wrapper = getAuditable(entity, event.getRepositoryMetadata()); wrapper.setLastModifiedBy(auditorAware == null ? null : auditorAware.getCurrentAuditor().orElse(null)); wrapper.setLastModifiedDate(Instant.now()); } } AuditDataEventListener(AuditorAware auditorAware); @SuppressWarnings({"unchecked", "ConstantConditions"}) @Override void onEvent(DataStoreEvent event); AuditorAware getAuditorAware();  }
@Test public void testBeforeInsertForImplicitAuditing() { final AuditDataEventListener listener = new AuditDataEventListener(new SampleAuditorAware()); final RepositoryMetadata repositoryMetadata = new ImmutableRepositoryMetadata(String.class, ImplicitlyAuditableEntity.class, null, "id"); final ImplicitlyAuditableEntity entity = new ImplicitlyAuditableEntity(); final Date before = new Date(); listener.onEvent(new BeforeInsertDataStoreEvent(repositoryMetadata, null, entity)); final Date after = new Date(); assertThat(entity.getCreatedBy(), is(notNullValue())); assertThat(entity.getCreatedBy(), is(AUDITOR)); assertThat(entity.getCreatedDate(), is(notNullValue())); assertThat(entity.getCreatedDate().getTime(), is(greaterThanOrEqualTo(before.getTime()))); assertThat(entity.getCreatedDate().getTime(), is(lessThanOrEqualTo(after.getTime()))); assertThat(entity.getLastModifiedDate(), is(nullValue())); assertThat(entity.getLastModifiedBy(), is(nullValue())); }
@Test public void testBeforeUpdateForImplicitAuditing() { final AuditDataEventListener listener = new AuditDataEventListener(new SampleAuditorAware()); final RepositoryMetadata repositoryMetadata = new ImmutableRepositoryMetadata(String.class, ImplicitlyAuditableEntity.class, null, "id"); final ImplicitlyAuditableEntity entity = new ImplicitlyAuditableEntity(); final Date before = new Date(); listener.onEvent(new BeforeUpdateDataStoreEvent(repositoryMetadata, null, entity)); final Date after = new Date(); assertThat(entity.getLastModifiedBy(), is(notNullValue())); assertThat(entity.getLastModifiedBy(), is(AUDITOR)); assertThat(entity.getLastModifiedDate(), is(notNullValue())); assertThat(entity.getLastModifiedDate().getTime(), is(greaterThanOrEqualTo(before.getTime()))); assertThat(entity.getLastModifiedDate().getTime(), is(lessThanOrEqualTo(after.getTime()))); assertThat(entity.getCreatedDate(), is(nullValue())); assertThat(entity.getCreatedBy(), is(nullValue())); }
@Test public void testBeforeInsertForExplicitAuditing() { final AuditDataEventListener listener = new AuditDataEventListener(new SampleAuditorAware()); final RepositoryMetadata repositoryMetadata = new ImmutableRepositoryMetadata(String.class, AuditableEntity.class, null, "id"); final AuditableEntity entity = new AuditableEntity(); final Date before = new Date(); listener.onEvent(new BeforeInsertDataStoreEvent(repositoryMetadata, null, entity)); final Date after = new Date(); assertThat(entity.getCreatedBy().isPresent(), is(true)); assertThat(entity.getCreatedBy().get(), is(notNullValue())); assertThat(entity.getCreatedBy().get(), is(AUDITOR)); assertThat(entity.getCreatedDate().isPresent(), is(true)); assertThat(entity.getCreatedDate().get(), is(notNullValue())); assertThat(entity.getCreatedDate().get().toEpochMilli(), is(greaterThanOrEqualTo(before.getTime()))); assertThat(entity.getCreatedDate().get().toEpochMilli(), is(lessThanOrEqualTo(after.getTime()))); assertThat(entity.getLastModifiedDate(), is(Optional.empty())); assertThat(entity.getLastModifiedBy(), is(Optional.empty())); }
@Test public void testBeforeUpdateForExplicitAuditing() { final AuditDataEventListener listener = new AuditDataEventListener(new SampleAuditorAware()); final RepositoryMetadata repositoryMetadata = new ImmutableRepositoryMetadata(String.class, AuditableEntity.class, null, "id"); final AuditableEntity entity = new AuditableEntity(); final Date before = new Date(); listener.onEvent(new BeforeUpdateDataStoreEvent(repositoryMetadata, null, entity)); final Date after = new Date(); assertThat(entity.getLastModifiedBy().isPresent(), is(true)); assertThat(entity.getLastModifiedBy().get(), is(notNullValue())); assertThat(entity.getLastModifiedBy().get(), is(AUDITOR)); assertThat(entity.getLastModifiedDate().isPresent(), is(true)); assertThat(entity.getLastModifiedDate().get(), is(notNullValue())); assertThat(entity.getLastModifiedDate().get().toEpochMilli(), is(greaterThanOrEqualTo(before.getTime()))); assertThat(entity.getLastModifiedDate().get().toEpochMilli(), is(lessThanOrEqualTo(after.getTime()))); assertThat(entity.getCreatedDate(), is(Optional.empty())); assertThat(entity.getCreatedBy(), is(Optional.empty())); }