@@ -105,40 +105,6 @@ public void doesNotExposeNullEvent() {
105
105
verify (publisher , times (0 )).publishEvent (any ());
106
106
}
107
107
108
- @ Test // DATACMNS-1067
109
- public void clearEventsDoesNotExposedByEntity () {
110
-
111
- EventsWithClearing entity = spy (EventsWithClearing .of (Collections .emptyList ()));
112
-
113
- EventPublishingMethod .of (EventsWithClearing .class ).publishEventsFrom (entity , publisher );
114
-
115
- verify (entity , times (0 )).clearDomainEvents ();
116
- }
117
-
118
- @ Test // DATACMNS-1067
119
- public void clearEventsExposedByEntity () {
120
-
121
- EventsWithClearing entity = spy (EventsWithClearing .of (Collections .singletonList (new SomeEvent ())));
122
-
123
- EventPublishingMethod .of (EventsWithClearing .class ).publishEventsFrom (entity , publisher );
124
-
125
- verify (entity , times (1 )).clearDomainEvents ();
126
- }
127
-
128
- @ Test // DATACMNS-1067
129
- public void clearEventsExposedByEntities () {
130
-
131
- EventsWithClearing firstEntity = spy (EventsWithClearing .of (Collections .emptyList ()));
132
- EventsWithClearing secondEntity = spy (EventsWithClearing .of (Collections .singletonList (new SomeEvent ())));
133
-
134
- Collection <EventsWithClearing > entities = Arrays .asList (firstEntity , secondEntity );
135
-
136
- EventPublishingMethod .of (EventsWithClearing .class ).publishEventsFrom (entities , publisher );
137
-
138
- verify (firstEntity , times (0 )).clearDomainEvents ();
139
- verify (secondEntity , times (1 )).clearDomainEvents ();
140
- }
141
-
142
108
@ Test // DATACMNS-928
143
109
public void doesNotCreatePublishingMethodIfNoAnnotationDetected () {
144
110
assertThat (EventPublishingMethod .of (Object .class ), is (nullValue ()));
@@ -147,11 +113,9 @@ public void doesNotCreatePublishingMethodIfNoAnnotationDetected() {
147
113
@ Test // DATACMNS-928
148
114
public void interceptsSaveMethod () throws Throwable {
149
115
150
- doReturn (SampleRepository .class .getMethod ("save" , Object .class )).when (invocation ).getMethod ();
151
-
152
116
SomeEvent event = new SomeEvent ();
153
- MultipleEvents sample = MultipleEvents .of (Arrays . asList (event ));
154
- doReturn ( new Object [] { sample }). when ( invocation ). getArguments ( );
117
+ MultipleEvents sample = MultipleEvents .of (Collections . singletonList (event ));
118
+ mockInvocation ( invocation , SampleRepository . class . getMethod ( "save" , Object . class ), sample );
155
119
156
120
EventPublishingMethodInterceptor //
157
121
.of (EventPublishingMethod .of (MultipleEvents .class ), publisher )//
@@ -202,10 +166,8 @@ public void doesNotAddAdviceIfDomainTypeDoesNotExposeEvents() {
202
166
public void publishesEventsForCallToSaveWithIterable () throws Throwable {
203
167
204
168
SomeEvent event = new SomeEvent ();
205
- MultipleEvents sample = MultipleEvents .of (Arrays .asList (event ));
206
- doReturn (new Object [] { Arrays .asList (sample ) }).when (invocation ).getArguments ();
207
-
208
- doReturn (SampleRepository .class .getMethod ("save" , Iterable .class )).when (invocation ).getMethod ();
169
+ MultipleEvents sample = MultipleEvents .of (Collections .singletonList (event ));
170
+ mockInvocation (invocation , SampleRepository .class .getMethod ("save" , Iterable .class ), sample );
209
171
210
172
EventPublishingMethodInterceptor //
211
173
.of (EventPublishingMethod .of (MultipleEvents .class ), publisher )//
@@ -234,12 +196,9 @@ public void publishesEventsAfterSaveInvocation() throws Throwable {
234
196
@ Test // DATACMNS-1113
235
197
public void invokesEventsForMethodsThatStartsWithSave () throws Throwable {
236
198
237
- Method method = SampleRepository .class .getMethod ("saveAndFlush" , MultipleEvents .class );
238
- doReturn (method ).when (invocation ).getMethod ();
239
-
240
199
SomeEvent event = new SomeEvent ();
241
200
MultipleEvents sample = MultipleEvents .of (Collections .singletonList (event ));
242
- doReturn ( new Object [] { sample }). when ( invocation ). getArguments ( );
201
+ mockInvocation ( invocation , SampleRepository . class . getMethod ( "saveAndFlush" , MultipleEvents . class ), sample );
243
202
244
203
EventPublishingMethodInterceptor //
245
204
.of (EventPublishingMethod .of (MultipleEvents .class ), publisher )//
@@ -248,6 +207,48 @@ public void invokesEventsForMethodsThatStartsWithSave() throws Throwable {
248
207
verify (publisher ).publishEvent (event );
249
208
}
250
209
210
+ @ Test // DATACMNS-1067
211
+ public void clearsEventsEvenIfNoneWereExposedToPublish () {
212
+
213
+ EventsWithClearing entity = spy (EventsWithClearing .of (Collections .emptyList ()));
214
+
215
+ EventPublishingMethod .of (EventsWithClearing .class ).publishEventsFrom (entity , publisher );
216
+
217
+ verify (entity , times (1 )).clearDomainEvents ();
218
+ }
219
+
220
+ @ Test // DATACMNS-1067
221
+ public void clearsEventsIfThereWereSomeToBePublished () {
222
+
223
+ EventsWithClearing entity = spy (EventsWithClearing .of (Collections .singletonList (new SomeEvent ())));
224
+
225
+ EventPublishingMethod .of (EventsWithClearing .class ).publishEventsFrom (entity , publisher );
226
+
227
+ verify (entity , times (1 )).clearDomainEvents ();
228
+ }
229
+
230
+ @ Test // DATACMNS-1067
231
+ public void clearsEventsForOperationOnMutlipleAggregates () {
232
+
233
+ EventsWithClearing firstEntity = spy (EventsWithClearing .of (Collections .emptyList ()));
234
+ EventsWithClearing secondEntity = spy (EventsWithClearing .of (Collections .singletonList (new SomeEvent ())));
235
+
236
+ Collection <EventsWithClearing > entities = Arrays .asList (firstEntity , secondEntity );
237
+
238
+ EventPublishingMethod .of (EventsWithClearing .class ).publishEventsFrom (entities , publisher );
239
+
240
+ verify (firstEntity , times (1 )).clearDomainEvents ();
241
+ verify (secondEntity , times (1 )).clearDomainEvents ();
242
+ }
243
+
244
+ private static void mockInvocation (MethodInvocation invocation , Method method , Object parameterAndReturnValue )
245
+ throws Throwable {
246
+
247
+ doReturn (method ).when (invocation ).getMethod ();
248
+ doReturn (new Object [] { parameterAndReturnValue }).when (invocation ).getArguments ();
249
+ doReturn (parameterAndReturnValue ).when (invocation ).proceed ();
250
+ }
251
+
251
252
@ Value (staticConstructor = "of" )
252
253
static class MultipleEvents {
253
254
@ Getter (onMethod = @ __ (@ DomainEvents )) Collection <? extends Object > events ;
0 commit comments