Skip to content

Commit 889ae01

Browse files
reda-alaouiodrotbohm
authored andcommitted
DATACMNS-1663 - Support domain events for deletions.
Domain events are now also published on calls to CrudRepository.delete(…) and ….deleteAll(…). Original pull request: #436.
1 parent 00d77d0 commit 889ae01

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@
3838
import org.springframework.util.ReflectionUtils;
3939

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

7779
/**
78-
* {@link MethodInterceptor} to publish events exposed an aggregate on calls to a save method on the repository.
80+
* {@link MethodInterceptor} to publish events exposed an aggregate on calls to a save or delete method on the repository.
7981
*
8082
* @author Oliver Gierke
8183
* @since 1.13
@@ -106,12 +108,16 @@ public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) thro
106108
Object[] arguments = invocation.getArguments();
107109
Object result = invocation.proceed();
108110

109-
if (!invocation.getMethod().getName().startsWith("save")) {
111+
String methodName = invocation.getMethod().getName();
112+
Object eventSource;
113+
if (methodName.startsWith("save")) {
114+
eventSource = arguments.length == 1 ? arguments[0] : result;
115+
} else if ((methodName.equals("delete") || methodName.equals("deleteAll")) && arguments.length == 1) {
116+
eventSource = arguments[0];
117+
} else {
110118
return result;
111119
}
112120

113-
Object eventSource = arguments.length == 1 ? arguments[0] : result;
114-
115121
eventMethod.publishEventsFrom(eventSource, publisher);
116122

117123
return result;

src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java

+40
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
* @author Oliver Gierke
5454
* @author Mark Paluch
5555
* @author Yuki Yoshida
56+
* @author Réda Housni Alaoui
5657
* @soundtrack Henrik Freischlader Trio - Nobody Else To Blame (Openness)
5758
*/
5859
@ExtendWith(MockitoExtension.class)
@@ -124,7 +125,20 @@ void interceptsSaveMethod() throws Throwable {
124125

125126
verify(publisher).publishEvent(event);
126127
}
128+
129+
@Test // DATACMNS-1663
130+
public void interceptsDeleteMethod() throws Throwable {
131+
SomeEvent event = new SomeEvent();
132+
MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event));
133+
mockInvocation(invocation, SampleRepository.class.getMethod("delete", Object.class), sample);
127134

135+
EventPublishingMethodInterceptor//
136+
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
137+
.invoke(invocation);
138+
139+
verify(publisher).publishEvent(event);
140+
}
141+
128142
@Test // DATACMNS-928
129143
void doesNotInterceptNonSaveMethod() throws Throwable {
130144

@@ -137,6 +151,18 @@ void doesNotInterceptNonSaveMethod() throws Throwable {
137151
verify(publisher, never()).publishEvent(any());
138152
}
139153

154+
@Test // DATACMNS-1663
155+
public void doesNotInterceptDeleteByIdMethod() throws Throwable {
156+
157+
doReturn(SampleRepository.class.getMethod("deleteById", Object.class)).when(invocation).getMethod();
158+
159+
EventPublishingMethodInterceptor//
160+
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
161+
.invoke(invocation);
162+
163+
verify(publisher, never()).publishEvent(any());
164+
}
165+
140166
@Test // DATACMNS-928
141167
void registersAdviceIfDomainTypeExposesEvents() {
142168

@@ -177,6 +203,20 @@ void publishesEventsForCallToSaveWithIterable() throws Throwable {
177203
verify(publisher).publishEvent(any(SomeEvent.class));
178204
}
179205

206+
@Test // DATACMNS-1663
207+
public void publishesEventsForCallToDeleteWithIterable() throws Throwable {
208+
209+
SomeEvent event = new SomeEvent();
210+
MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event));
211+
mockInvocation(invocation, SampleRepository.class.getMethod("deleteAll", Iterable.class), sample);
212+
213+
EventPublishingMethodInterceptor//
214+
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
215+
.invoke(invocation);
216+
217+
verify(publisher).publishEvent(any(SomeEvent.class));
218+
}
219+
180220
@Test // DATACMNS-975
181221
void publishesEventsAfterSaveInvocation() throws Throwable {
182222

0 commit comments

Comments
 (0)