Skip to content

DATACMNS-1663 - Support domain events for deletions. #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@
import org.springframework.util.ReflectionUtils;

/**
* {@link RepositoryProxyPostProcessor} to register a {@link MethodInterceptor} to intercept the
* {@link CrudRepository#save(Object)} method and publish events potentially exposed via a method annotated with
* {@link DomainEvents}. If no such method can be detected on the aggregate root, no interceptor is added. Additionally,
* {@link RepositoryProxyPostProcessor} to register a {@link MethodInterceptor} to intercept
* {@link CrudRepository#save(Object)} and {@link CrudRepository#delete(Object)} methods and publish events potentially
* exposed via a method annotated with {@link DomainEvents}. If no such method can be detected on the aggregate root, no
* interceptor is added. Additionally,
* the aggregate root can expose a method annotated with {@link AfterDomainEventPublication}. If present, the method
* will be invoked after all events have been published.
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Yuki Yoshida
* @author Réda Housni Alaoui
* @since 1.13
* @soundtrack Henrik Freischlader Trio - Master Plan (Openness)
*/
Expand All @@ -73,7 +75,7 @@ public void postProcess(ProxyFactory factory, RepositoryInformation repositoryIn
}

/**
* {@link MethodInterceptor} to publish events exposed an aggregate on calls to a save method on the repository.
* {@link MethodInterceptor} to publish events exposed an aggregate on calls to a save or delete method on the repository.
*
* @author Oliver Gierke
* @since 1.13
Expand All @@ -94,12 +96,16 @@ public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) thro
Object[] arguments = invocation.getArguments();
Object result = invocation.proceed();

if (!invocation.getMethod().getName().startsWith("save")) {
String methodName = invocation.getMethod().getName();
Object eventSource;
if (methodName.startsWith("save")) {
eventSource = arguments.length == 1 ? arguments[0] : result;
} else if ((methodName.equals("delete") || methodName.equals("deleteAll")) && arguments.length == 1) {
eventSource = arguments[0];
} else {
return result;
}

Object eventSource = arguments.length == 1 ? arguments[0] : result;

eventMethod.publishEventsFrom(eventSource, publisher);

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* @author Oliver Gierke
* @author Mark Paluch
* @author Yuki Yoshida
* @author Réda Housni Alaoui
* @soundtrack Henrik Freischlader Trio - Nobody Else To Blame (Openness)
*/
@RunWith(MockitoJUnitRunner.Silent.class)
Expand Down Expand Up @@ -121,7 +122,20 @@ public void interceptsSaveMethod() throws Throwable {

verify(publisher).publishEvent(event);
}

@Test // DATACMNS-1663
public void interceptsDeleteMethod() throws Throwable {
SomeEvent event = new SomeEvent();
MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event));
mockInvocation(invocation, SampleRepository.class.getMethod("delete", Object.class), sample);

EventPublishingMethodInterceptor//
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
.invoke(invocation);

verify(publisher).publishEvent(event);
}

@Test // DATACMNS-928
public void doesNotInterceptNonSaveMethod() throws Throwable {

Expand All @@ -134,6 +148,18 @@ public void doesNotInterceptNonSaveMethod() throws Throwable {
verify(publisher, never()).publishEvent(any());
}

@Test // DATACMNS-1663
public void doesNotInterceptDeleteByIdMethod() throws Throwable {

doReturn(SampleRepository.class.getMethod("deleteById", Object.class)).when(invocation).getMethod();

EventPublishingMethodInterceptor//
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
.invoke(invocation);

verify(publisher, never()).publishEvent(any());
}

@Test // DATACMNS-928
public void registersAdviceIfDomainTypeExposesEvents() {

Expand Down Expand Up @@ -174,6 +200,20 @@ public void publishesEventsForCallToSaveWithIterable() throws Throwable {
verify(publisher).publishEvent(any(SomeEvent.class));
}

@Test // DATACMNS-1663
public void publishesEventsForCallToDeleteWithIterable() throws Throwable {

SomeEvent event = new SomeEvent();
MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event));
mockInvocation(invocation, SampleRepository.class.getMethod("deleteAll", Iterable.class), sample);

EventPublishingMethodInterceptor//
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
.invoke(invocation);

verify(publisher).publishEvent(any(SomeEvent.class));
}

@Test // DATACMNS-975
public void publishesEventsAfterSaveInvocation() throws Throwable {

Expand Down