DATACMNS-928 - Refinements for aggregate root domain event publication.

We now publish events for all methods named "save" on the repository. Fundamentally, that's in place to capture calls to CrudRepository.save(Iterable entities), too.

Some minor refactorings to the internal setup of EventPublishingMethodInterceptor. More JavaDoc.
This commit is contained in:
Oliver Gierke
2016-11-30 15:45:26 +01:00
parent c52c45741a
commit f6db714206
2 changed files with 43 additions and 13 deletions

View File

@@ -25,7 +25,6 @@ import lombok.Getter;
import lombok.Value;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;
@@ -129,14 +128,14 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests {
@Test
public void interceptsSaveMethod() throws Throwable {
Method saveMethod = SampleRepository.class.getMethod("save", Object.class);
doReturn(saveMethod).when(invocation).getMethod();
doReturn(SampleRepository.class.getMethod("save", Object.class)).when(invocation).getMethod();
SomeEvent event = new SomeEvent();
MultipleEvents sample = MultipleEvents.of(Arrays.asList(event));
doReturn(new Object[] { sample }).when(invocation).getArguments();
new EventPublishingMethodInterceptor(saveMethod, EventPublishingMethod.of(MultipleEvents.class), publisher)
EventPublishingMethodInterceptor//
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
.invoke(invocation);
verify(publisher).publishEvent(event);
@@ -148,10 +147,10 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests {
@Test
public void doesNotInterceptNonSaveMethod() throws Throwable {
Method saveMethod = SampleRepository.class.getMethod("save", Object.class);
doReturn(SampleRepository.class.getMethod("findOne", Serializable.class)).when(invocation).getMethod();
new EventPublishingMethodInterceptor(saveMethod, EventPublishingMethod.of(MultipleEvents.class), publisher)
EventPublishingMethodInterceptor//
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
.invoke(invocation);
verify(publisher, never()).publishEvent(any());
@@ -189,6 +188,25 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests {
verify(factory, never()).addAdvice(any(Advice.class));
}
/**
* @see DATACMNS-928
*/
@Test
public void publishesEventsForCallToSaveWithIterable() throws Throwable {
SomeEvent event = new SomeEvent();
MultipleEvents sample = MultipleEvents.of(Arrays.asList(event));
doReturn(new Object[] { Arrays.asList(sample) }).when(invocation).getArguments();
doReturn(SampleRepository.class.getMethod("save", Iterable.class)).when(invocation).getMethod();
EventPublishingMethodInterceptor//
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
.invoke(invocation);
verify(publisher).publishEvent(any(SomeEvent.class));
}
@Value(staticConstructor = "of")
static class MultipleEvents {
@Getter(onMethod = @__(@DomainEvents)) Collection<? extends Object> events;