From 64b5d647092da6b79c68b32b5065f818178920e7 Mon Sep 17 00:00:00 2001 From: Sankaranarayanan Viswanathan Date: Wed, 8 Oct 2014 22:18:16 -0400 Subject: [PATCH] Inject PostLoginActivities and refactor --- .../backend/service/UserService.java | 9 ++++---- .../service/internal/PostLoginActivities.java | 4 +++- .../backend/service/UserServiceTest.java | 21 +++++++++---------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/commafeed/backend/service/UserService.java b/src/main/java/com/commafeed/backend/service/UserService.java index 0a92704c..93b79eff 100644 --- a/src/main/java/com/commafeed/backend/service/UserService.java +++ b/src/main/java/com/commafeed/backend/service/UserService.java @@ -34,9 +34,10 @@ public class UserService { private final UserDAO userDAO; private final UserSettingsDAO userSettingsDAO; - private final FeedSubscriptionService feedSubscriptionService; private final PasswordEncryptionService encryptionService; private final CommaFeedConfiguration config; + + private final PostLoginActivities postLoginActivities; /** * try to log in with given credentials @@ -103,11 +104,9 @@ public class UserService { /** * should triggers after successful login - * - * Note: Visibility changed to package private to enabled spying on this method */ - void afterLogin(User user) { - new PostLoginActivities(userDAO, feedSubscriptionService, config).afterLogin(user); + private void afterLogin(User user) { + postLoginActivities.executeFor(user); } public User register(String name, String password, String email, Collection roles) { diff --git a/src/main/java/com/commafeed/backend/service/internal/PostLoginActivities.java b/src/main/java/com/commafeed/backend/service/internal/PostLoginActivities.java index 170ea7ef..92f0d3e7 100644 --- a/src/main/java/com/commafeed/backend/service/internal/PostLoginActivities.java +++ b/src/main/java/com/commafeed/backend/service/internal/PostLoginActivities.java @@ -3,6 +3,7 @@ package com.commafeed.backend.service.internal; import java.util.Date; import javax.inject.Inject; +import javax.inject.Singleton; import lombok.RequiredArgsConstructor; @@ -14,13 +15,14 @@ import com.commafeed.backend.model.User; import com.commafeed.backend.service.FeedSubscriptionService; @RequiredArgsConstructor(onConstructor = @__({ @Inject })) +@Singleton public class PostLoginActivities { private final UserDAO userDAO; private final FeedSubscriptionService feedSubscriptionService; private final CommaFeedConfiguration config; - public void afterLogin(User user) { + public void executeFor(User user) { Date lastLogin = user.getLastLogin(); Date now = new Date(); diff --git a/src/test/java/com/commafeed/backend/service/UserServiceTest.java b/src/test/java/com/commafeed/backend/service/UserServiceTest.java index d00561c5..20508143 100644 --- a/src/test/java/com/commafeed/backend/service/UserServiceTest.java +++ b/src/test/java/com/commafeed/backend/service/UserServiceTest.java @@ -2,20 +2,19 @@ package com.commafeed.backend.service; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.doNothing; import org.junit.Assert; import org.junit.Test; import com.commafeed.backend.dao.UserDAO; import com.commafeed.backend.model.User; +import com.commafeed.backend.service.internal.PostLoginActivities; import com.google.common.base.Optional; - public class UserServiceTest { @Test public void @@ -99,7 +98,7 @@ public class UserServiceTest { when(encryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(false); // Create service with mocks - UserService service = new UserService(null, dao, null, null, encryptionService, null); + UserService service = new UserService(null, dao, null, encryptionService, null, null); // Try to login as the user service.login("test", "password"); @@ -130,7 +129,7 @@ public class UserServiceTest { when(encryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(false); // Create service with mocks - UserService service = new UserService(null, dao, null, null, encryptionService, null); + UserService service = new UserService(null, dao, null, encryptionService, null, null); // Try to login as the user Optional authenticatedUser = service.login("test", "password"); @@ -160,15 +159,15 @@ public class UserServiceTest { PasswordEncryptionService encryptionService = mock(PasswordEncryptionService.class); when(encryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(true); - // Create service with mocks - UserService service = new UserService(null, dao, null, null, encryptionService, null); + // Mock PostLoginActivities to do nothing + PostLoginActivities postLoginActivities = mock(PostLoginActivities.class); + doNothing().when(postLoginActivities).executeFor(any(User.class)); - // Skip afterLogin activities - UserService spy = spy(service); - doNothing().when(spy).afterLogin(any(User.class)); + // Create service with mocks + UserService service = new UserService(null, dao, null, encryptionService, null, postLoginActivities); // Try to login as the user - Optional authenticatedUser = spy.login("test", "password"); + Optional authenticatedUser = service.login("test", "password"); Assert.assertTrue(authenticatedUser.isPresent()); Assert.assertEquals(user, authenticatedUser.get());