From 71977c9309788a3cd28d86314776a743cff1d394 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 15 May 2017 15:39:52 +0200 Subject: [PATCH] DATACMNS-1067 - Polishing. Revert change to only invoke cleanup method if events have been exposed. We now again invoke the cleanup method for every aggregate. Changed the publication of events from the aggregate instances that were handed into the method to the ones the save method returns as the save call might return different object instances. Cleanups in the unit tests. Moved newly introduced methods to the bottom of the test case class. Extracted method to set up mock method invocation. Original pull request: #216. --- ...ublishingRepositoryProxyPostProcessor.java | 10 +- ...RepositoryProxyPostProcessorUnitTests.java | 91 ++++++++++--------- 2 files changed, 50 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java b/src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java index 2cea045a8..d7e13152b 100644 --- a/src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java +++ b/src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java @@ -94,9 +94,7 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr return result; } - for (Object argument : invocation.getArguments()) { - eventMethod.publishEventsFrom(argument, publisher); - } + eventMethod.publishEventsFrom(result, publisher); return result; } @@ -165,12 +163,12 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr } for (Object aggregateRoot : asCollection(object)) { - Collection events = asCollection(ReflectionUtils.invokeMethod(publishingMethod, aggregateRoot)); - for (Object event : events) { + + for (Object event : asCollection(ReflectionUtils.invokeMethod(publishingMethod, aggregateRoot))) { publisher.publishEvent(event); } - if (clearingMethod != null && !events.isEmpty()) { + if (clearingMethod != null) { ReflectionUtils.invokeMethod(clearingMethod, aggregateRoot); } } diff --git a/src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java index 2ebbdb5dc..802d89130 100644 --- a/src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java @@ -52,7 +52,7 @@ import org.springframework.data.repository.core.support.EventPublishingRepositor * @author Yuki Yoshida * @soundtrack Henrik Freischlader Trio - Nobody Else To Blame (Openness) */ -@RunWith(MockitoJUnitRunner.class) +@RunWith(MockitoJUnitRunner.Silent.class) public class EventPublishingRepositoryProxyPostProcessorUnitTests { @Mock ApplicationEventPublisher publisher; @@ -102,40 +102,6 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests { 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 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(); @@ -144,11 +110,9 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests { @Test // DATACMNS-928 public void interceptsSaveMethod() throws Throwable { - doReturn(SampleRepository.class.getMethod("save", Object.class)).when(invocation).getMethod(); - SomeEvent event = new SomeEvent(); MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event)); - doReturn(new Object[] { sample }).when(invocation).getArguments(); + mockInvocation(invocation, SampleRepository.class.getMethod("save", Object.class), sample); EventPublishingMethodInterceptor// .of(EventPublishingMethod.of(MultipleEvents.class), publisher)// @@ -200,9 +164,7 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests { SomeEvent event = new SomeEvent(); MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event)); - doReturn(new Object[] { Collections.singletonList(sample) }).when(invocation).getArguments(); - - doReturn(SampleRepository.class.getMethod("saveAll", Iterable.class)).when(invocation).getMethod(); + mockInvocation(invocation, SampleRepository.class.getMethod("saveAll", Iterable.class), sample); EventPublishingMethodInterceptor// .of(EventPublishingMethod.of(MultipleEvents.class), publisher)// @@ -228,12 +190,9 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests { @Test // DATACMNS-1113 public void invokesEventsForMethodsThatStartsWithSave() throws Throwable { - Method method = SampleRepository.class.getMethod("saveAndFlush", MultipleEvents.class); - doReturn(method).when(invocation).getMethod(); - SomeEvent event = new SomeEvent(); MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event)); - doReturn(new Object[] { sample }).when(invocation).getArguments(); + mockInvocation(invocation, SampleRepository.class.getMethod("saveAndFlush", MultipleEvents.class), sample); EventPublishingMethodInterceptor// .of(EventPublishingMethod.of(MultipleEvents.class), publisher)// @@ -242,6 +201,48 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests { verify(publisher).publishEvent(event); } + @Test // DATACMNS-1067 + public void clearsEventsEvenIfNoneWereExposedToPublish() { + + EventsWithClearing entity = spy(EventsWithClearing.of(Collections.emptyList())); + + EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher); + + verify(entity, times(1)).clearDomainEvents(); + } + + @Test // DATACMNS-1067 + public void clearsEventsIfThereWereSomeToBePublished() { + + 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 clearsEventsForOperationOnMutlipleAggregates() { + + EventsWithClearing firstEntity = spy(EventsWithClearing.of(Collections.emptyList())); + EventsWithClearing secondEntity = spy(EventsWithClearing.of(Collections.singletonList(new SomeEvent()))); + + Collection entities = Arrays.asList(firstEntity, secondEntity); + + EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entities, publisher); + + verify(firstEntity, times(1)).clearDomainEvents(); + verify(secondEntity, times(1)).clearDomainEvents(); + } + + private static void mockInvocation(MethodInvocation invocation, Method method, Object parameterAndReturnValue) + throws Throwable { + + doReturn(method).when(invocation).getMethod(); + doReturn(new Object[] { parameterAndReturnValue }).when(invocation).getArguments(); + doReturn(parameterAndReturnValue).when(invocation).proceed(); + } + @Value(staticConstructor = "of") static class MultipleEvents { @Getter(onMethod = @__(@DomainEvents)) Collection events;