22
33import static org .assertj .core .api .Assertions .assertThat ;
44import static org .mockito .ArgumentMatchers .any ;
5+ import static org .mockito .ArgumentMatchers .anyLong ;
6+ import static org .mockito .ArgumentMatchers .anyString ;
57import static org .mockito .Mockito .doAnswer ;
68import static org .mockito .Mockito .timeout ;
79import static org .mockito .Mockito .verify ;
@@ -88,38 +90,39 @@ void registrationEvent_triggersEmailService() throws Exception {
8890 doAnswer (invocation -> {
8991 latch .countDown ();
9092 return null ;
91- }).when (userEmailService ).sendRegistrationVerificationEmail (any (), any ());
93+ }).when (userEmailService ).sendRegistrationVerificationEmail (anyLong (), anyString ());
9294
9395 // When
94- OnRegistrationCompleteEvent event = OnRegistrationCompleteEvent .builder ().user (testUser ).locale (locale ).appUrl (appUrl ).build ();
96+ OnRegistrationCompleteEvent event = OnRegistrationCompleteEvent .builder ().userId (testUser .getId ()).userEmail (testUser .getEmail ())
97+ .userEnabled (testUser .isEnabled ()).locale (locale ).appUrl (appUrl ).build ();
9598 eventPublisher .publishEvent (event );
9699
97100 // Then
98101 assertThat (latch .await (5 , TimeUnit .SECONDS )).isTrue ();
99- verify (userEmailService ).sendRegistrationVerificationEmail (testUser , appUrl );
102+ verify (userEmailService ).sendRegistrationVerificationEmail (testUser . getId () , appUrl );
100103 assertThat (eventCapture .getCapturedEvents ()).filteredOn (e -> e instanceof OnRegistrationCompleteEvent ).hasSize (1 );
101104 }
102105
103106 @ Test
104107 @ DisplayName ("Multiple registration events are handled independently" )
105108 void multipleRegistrationEvents_handledIndependently () throws Exception {
106109 // Given
107- User user1 = UserTestDataBuilder .aUser ().withEmail ("user1@example.com" ).build ();
108- User user2 = UserTestDataBuilder .aUser ().withEmail ("user2@example.com" ).build ();
110+ User user1 = UserTestDataBuilder .aUser ().withId ( 10L ). withEmail ("user1@example.com" ).build ();
111+ User user2 = UserTestDataBuilder .aUser ().withId ( 20L ). withEmail ("user2@example.com" ).build ();
109112 CountDownLatch latch = new CountDownLatch (2 );
110113 doAnswer (invocation -> {
111114 latch .countDown ();
112115 return null ;
113- }).when (userEmailService ).sendRegistrationVerificationEmail (any (), any ());
116+ }).when (userEmailService ).sendRegistrationVerificationEmail (anyLong (), anyString ());
114117
115118 // When
116- eventPublisher .publishEvent (new OnRegistrationCompleteEvent (user1 , Locale .ENGLISH , "app1" ));
117- eventPublisher .publishEvent (new OnRegistrationCompleteEvent (user2 , Locale .FRENCH , "app2" ));
119+ eventPublisher .publishEvent (new OnRegistrationCompleteEvent (user1 . getId (), user1 . getEmail (), user1 . isEnabled () , Locale .ENGLISH , "app1" ));
120+ eventPublisher .publishEvent (new OnRegistrationCompleteEvent (user2 . getId (), user2 . getEmail (), user2 . isEnabled () , Locale .FRENCH , "app2" ));
118121
119122 // Then
120123 assertThat (latch .await (5 , TimeUnit .SECONDS )).isTrue ();
121- verify (userEmailService ).sendRegistrationVerificationEmail (user1 , "app1" );
122- verify (userEmailService ).sendRegistrationVerificationEmail (user2 , "app2" );
124+ verify (userEmailService ).sendRegistrationVerificationEmail (user1 . getId () , "app1" );
125+ verify (userEmailService ).sendRegistrationVerificationEmail (user2 . getId () , "app2" );
123126 }
124127 }
125128
@@ -167,14 +170,14 @@ class UserDeletionEventFlowTests {
167170 @ DisplayName ("UserPreDeleteEvent is captured correctly" )
168171 void userPreDeleteEvent_capturedCorrectly () {
169172 // When
170- UserPreDeleteEvent event = new UserPreDeleteEvent (this , testUser );
173+ UserPreDeleteEvent event = new UserPreDeleteEvent (this , testUser . getId (), testUser . getEmail () );
171174 eventPublisher .publishEvent (event );
172175
173176 // Then
174177 assertThat (eventCapture .getCapturedEvents ()).filteredOn (e -> e instanceof UserPreDeleteEvent ).hasSize (1 ).first ().satisfies (e -> {
175178 UserPreDeleteEvent deleteEvent = (UserPreDeleteEvent ) e ;
176- assertThat (deleteEvent .getUser ()).isEqualTo (testUser );
177179 assertThat (deleteEvent .getUserId ()).isEqualTo (1L );
180+ assertThat (deleteEvent .getUserEmail ()).isEqualTo (testUser .getEmail ());
178181 });
179182 }
180183 }
@@ -217,7 +220,7 @@ void events_processedInOrder() throws Exception {
217220 processedEvents .add ("registration" );
218221 latch .countDown ();
219222 return null ;
220- }).when (userEmailService ).sendRegistrationVerificationEmail (any (), any ());
223+ }).when (userEmailService ).sendRegistrationVerificationEmail (anyLong (), anyString ());
221224
222225 doAnswer (invocation -> {
223226 processedEvents .add ("login-success" );
@@ -226,9 +229,9 @@ void events_processedInOrder() throws Exception {
226229 }).when (loginAttemptService ).loginSucceeded (any ());
227230
228231 // When
229- eventPublisher .publishEvent (new OnRegistrationCompleteEvent (testUser , Locale .ENGLISH , "app" ));
232+ eventPublisher .publishEvent (new OnRegistrationCompleteEvent (testUser . getId (), testUser . getEmail (), testUser . isEnabled () , Locale .ENGLISH , "app" ));
230233 eventPublisher .publishEvent (new AuthenticationSuccessEvent (new UsernamePasswordAuthenticationToken ("user" , "pass" )));
231- eventPublisher .publishEvent (new UserPreDeleteEvent (this , testUser ));
234+ eventPublisher .publishEvent (new UserPreDeleteEvent (this , testUser . getId (), testUser . getEmail () ));
232235 processedEvents .add ("delete" );
233236 latch .countDown ();
234237
0 commit comments