Skip to content

DATACMNS-1067 - Fix not to call @AfterDomainEventPublication method from Collection of entities. #216

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
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 @@ -44,6 +44,7 @@
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Yuki Yoshida
* @since 1.13
* @soundtrack Henrik Freischlader Trio - Master Plan (Openness)
*/
Expand Down Expand Up @@ -164,13 +165,14 @@ public void publishEventsFrom(Object object, ApplicationEventPublisher publisher
}

for (Object aggregateRoot : asCollection(object)) {
for (Object event : asCollection(ReflectionUtils.invokeMethod(publishingMethod, aggregateRoot))) {
Collection<Object> events = asCollection(ReflectionUtils.invokeMethod(publishingMethod, aggregateRoot));
for (Object event : events) {
publisher.publishEvent(event);
}
}

if (clearingMethod != null) {
ReflectionUtils.invokeMethod(clearingMethod, object);
if (clearingMethod != null && !events.isEmpty()) {
ReflectionUtils.invokeMethod(clearingMethod, aggregateRoot);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.mockito.Mockito.*;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Value;

import java.lang.reflect.Method;
Expand All @@ -36,6 +37,7 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.AfterDomainEventPublication;
import org.springframework.data.domain.DomainEvents;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.RepositoryInformation;
Expand All @@ -47,6 +49,7 @@
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Yuki Yoshida
* @soundtrack Henrik Freischlader Trio - Nobody Else To Blame (Openness)
*/
@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -99,6 +102,40 @@ public void doesNotExposeNullEvent() {
verify(publisher, times(0)).publishEvent(any());
}

@Test // DATACMNS-1067
public void clearEventsDoesNotExposedByEntity() {

EventsWithClearing entity = spy(EventsWithClearing.of(Collections.emptyList()));

EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher);

verify(entity, times(0)).clearDomainEvents();
}

@Test // DATACMNS-1067
public void clearEventsExposedByEntity() {

EventsWithClearing entity = spy(EventsWithClearing.of(Collections.singletonList(new SomeEvent())));

EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher);

verify(entity, times(1)).clearDomainEvents();
}

@Test // DATACMNS-1067
public void clearEventsExposedByEntities() {

EventsWithClearing firstEntity = spy(EventsWithClearing.of(Collections.emptyList()));
EventsWithClearing secondEntity = spy(EventsWithClearing.of(Collections.singletonList(new SomeEvent())));

Collection<EventsWithClearing> entities = Arrays.asList(firstEntity, secondEntity);

EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entities, publisher);

verify(firstEntity, times(0)).clearDomainEvents();
verify(secondEntity, times(1)).clearDomainEvents();
}

@Test // DATACMNS-928
public void doesNotCreatePublishingMethodIfNoAnnotationDetected() {
assertThat(EventPublishingMethod.of(Object.class)).isNull();
Expand Down Expand Up @@ -210,6 +247,14 @@ static class MultipleEvents {
@Getter(onMethod = @__(@DomainEvents)) Collection<? extends Object> events;
}

@RequiredArgsConstructor(staticName = "of")
static class EventsWithClearing {
@Getter(onMethod = @__(@DomainEvents)) final Collection<? extends Object> events;

@AfterDomainEventPublication
void clearDomainEvents() {}
}

@Value(staticConstructor = "of")
static class OneEvent {
@Getter(onMethod = @__(@DomainEvents)) Object event;
Expand Down