Inject PostLoginActivities and refactor

This commit is contained in:
Sankaranarayanan Viswanathan
2014-10-08 22:18:16 -04:00
parent 67d7315003
commit 64b5d64709
3 changed files with 17 additions and 17 deletions

View File

@@ -34,9 +34,10 @@ public class UserService {
private final UserDAO userDAO; private final UserDAO userDAO;
private final UserSettingsDAO userSettingsDAO; private final UserSettingsDAO userSettingsDAO;
private final FeedSubscriptionService feedSubscriptionService;
private final PasswordEncryptionService encryptionService; private final PasswordEncryptionService encryptionService;
private final CommaFeedConfiguration config; private final CommaFeedConfiguration config;
private final PostLoginActivities postLoginActivities;
/** /**
* try to log in with given credentials * try to log in with given credentials
@@ -103,11 +104,9 @@ public class UserService {
/** /**
* should triggers after successful login * should triggers after successful login
*
* Note: Visibility changed to package private to enabled spying on this method
*/ */
void afterLogin(User user) { private void afterLogin(User user) {
new PostLoginActivities(userDAO, feedSubscriptionService, config).afterLogin(user); postLoginActivities.executeFor(user);
} }
public User register(String name, String password, String email, Collection<Role> roles) { public User register(String name, String password, String email, Collection<Role> roles) {

View File

@@ -3,6 +3,7 @@ package com.commafeed.backend.service.internal;
import java.util.Date; import java.util.Date;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -14,13 +15,14 @@ import com.commafeed.backend.model.User;
import com.commafeed.backend.service.FeedSubscriptionService; import com.commafeed.backend.service.FeedSubscriptionService;
@RequiredArgsConstructor(onConstructor = @__({ @Inject })) @RequiredArgsConstructor(onConstructor = @__({ @Inject }))
@Singleton
public class PostLoginActivities { public class PostLoginActivities {
private final UserDAO userDAO; private final UserDAO userDAO;
private final FeedSubscriptionService feedSubscriptionService; private final FeedSubscriptionService feedSubscriptionService;
private final CommaFeedConfiguration config; private final CommaFeedConfiguration config;
public void afterLogin(User user) { public void executeFor(User user) {
Date lastLogin = user.getLastLogin(); Date lastLogin = user.getLastLogin();
Date now = new Date(); Date now = new Date();

View File

@@ -2,20 +2,19 @@ package com.commafeed.backend.service;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; 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.Assert;
import org.junit.Test; import org.junit.Test;
import com.commafeed.backend.dao.UserDAO; import com.commafeed.backend.dao.UserDAO;
import com.commafeed.backend.model.User; import com.commafeed.backend.model.User;
import com.commafeed.backend.service.internal.PostLoginActivities;
import com.google.common.base.Optional; import com.google.common.base.Optional;
public class UserServiceTest { public class UserServiceTest {
@Test public void @Test public void
@@ -99,7 +98,7 @@ public class UserServiceTest {
when(encryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(false); when(encryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(false);
// Create service with mocks // 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 // Try to login as the user
service.login("test", "password"); service.login("test", "password");
@@ -130,7 +129,7 @@ public class UserServiceTest {
when(encryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(false); when(encryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(false);
// Create service with mocks // 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 // Try to login as the user
Optional<User> authenticatedUser = service.login("test", "password"); Optional<User> authenticatedUser = service.login("test", "password");
@@ -160,15 +159,15 @@ public class UserServiceTest {
PasswordEncryptionService encryptionService = mock(PasswordEncryptionService.class); PasswordEncryptionService encryptionService = mock(PasswordEncryptionService.class);
when(encryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(true); when(encryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(true);
// Create service with mocks // Mock PostLoginActivities to do nothing
UserService service = new UserService(null, dao, null, null, encryptionService, null); PostLoginActivities postLoginActivities = mock(PostLoginActivities.class);
doNothing().when(postLoginActivities).executeFor(any(User.class));
// Skip afterLogin activities // Create service with mocks
UserService spy = spy(service); UserService service = new UserService(null, dao, null, encryptionService, null, postLoginActivities);
doNothing().when(spy).afterLogin(any(User.class));
// Try to login as the user // Try to login as the user
Optional<User> authenticatedUser = spy.login("test", "password"); Optional<User> authenticatedUser = service.login("test", "password");
Assert.assertTrue(authenticatedUser.isPresent()); Assert.assertTrue(authenticatedUser.isPresent());
Assert.assertEquals(user, authenticatedUser.get()); Assert.assertEquals(user, authenticatedUser.get());