Merge CommandService and ProcessService to CommonService

- Replace CommandService and ProcessService with CommonService: a service that offers our typical needs to work with settings, messages and permissions
- Remove validation methods from CommonService: inject ValidationService directly. Validation methods are not used very frequently and therefore don't belong in CommonService. Their presence was a relict from our architecture before injection was used.
This commit is contained in:
ljacqu
2016-12-03 12:10:30 +01:00
parent a38d3a25b8
commit c325d0db41
55 changed files with 317 additions and 553 deletions
@@ -1,189 +0,0 @@
package fr.xephi.authme.process;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.permission.AuthGroupHandler;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Test for {@link ProcessService}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ProcessServiceTest {
@InjectMocks
private ProcessService processService;
@Mock
private ValidationService validationService;
@Mock
private Settings settings;
@Mock
private Messages messages;
@Mock
private PermissionsManager permissionsManager;
@Mock
private AuthGroupHandler authGroupHandler;
@Test
public void shouldGetProperty() {
// given
given(settings.getProperty(SecuritySettings.CAPTCHA_LENGTH)).willReturn(8);
// when
int result = processService.getProperty(SecuritySettings.CAPTCHA_LENGTH);
// then
verify(settings).getProperty(SecuritySettings.CAPTCHA_LENGTH);
assertThat(result, equalTo(8));
}
@Test
public void shouldReturnSettings() {
// given/when
Settings result = processService.getSettings();
// then
assertThat(result, equalTo(settings));
}
@Test
public void shouldSendMessageToPlayer() {
// given
CommandSender sender = mock(CommandSender.class);
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
// when
processService.send(sender, key);
// then
verify(messages).send(sender, key);
}
@Test
public void shouldSendMessageWithReplacements() {
// given
CommandSender sender = mock(CommandSender.class);
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
String[] replacements = new String[]{"test", "toast"};
// when
processService.send(sender, key, replacements);
// then
verify(messages).send(sender, key, replacements);
}
@Test
public void shouldRetrieveMessage() {
// given
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
String[] lines = new String[]{"First message line", "second line"};
given(messages.retrieve(key)).willReturn(lines);
// when
String[] result = processService.retrieveMessage(key);
// then
assertThat(result, equalTo(lines));
verify(messages).retrieve(key);
}
@Test
public void shouldRetrieveSingleMessage() {
// given
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
String text = "Test text";
given(messages.retrieveSingle(key)).willReturn(text);
// when
String result = processService.retrieveSingleMessage(key);
// then
assertThat(result, equalTo(text));
verify(messages).retrieveSingle(key);
}
@Test
public void shouldValidateEmail() {
// given
String email = "test@example.tld";
given(validationService.validateEmail(email)).willReturn(true);
// when
boolean result = processService.validateEmail(email);
// then
assertThat(result, equalTo(true));
verify(validationService).validateEmail(email);
}
@Test
public void shouldCheckIfEmailCanBeUsed() {
// given
String email = "mail@example.com";
CommandSender sender = mock(CommandSender.class);
given(validationService.isEmailFreeForRegistration(email, sender))
.willReturn(true);
// when
boolean result = processService.isEmailFreeForRegistration(email, sender);
// then
assertThat(result, equalTo(true));
verify(validationService).isEmailFreeForRegistration(email, sender);
}
@Test
public void shouldCheckPermission() {
// given
Player player = mock(Player.class);
PermissionNode permission = PlayerPermission.CHANGE_PASSWORD;
given(permissionsManager.hasPermission(player, permission)).willReturn(true);
// when
boolean result = processService.hasPermission(player, permission);
// then
verify(permissionsManager).hasPermission(player, permission);
assertThat(result, equalTo(true));
}
@Test
public void shouldSetPermissionGroup() {
// given
Player player = mock(Player.class);
AuthGroupType type = AuthGroupType.LOGGED_IN;
given(authGroupHandler.setGroup(player, type)).willReturn(true);
// when
boolean result = processService.setGroup(player, type);
// then
verify(authGroupHandler).setGroup(player, type);
assertThat(result, equalTo(true));
}
}
@@ -5,7 +5,8 @@ import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
@@ -40,7 +41,10 @@ public class AsyncAddEmailTest {
private PlayerCache playerCache;
@Mock
private ProcessService service;
private CommonService service;
@Mock
private ValidationService validationService;
@BeforeClass
public static void setUp() {
@@ -57,8 +61,8 @@ public class AsyncAddEmailTest {
given(auth.getEmail()).willReturn(null);
given(playerCache.getAuth("tester")).willReturn(auth);
given(dataSource.updateEmail(any(PlayerAuth.class))).willReturn(true);
given(service.validateEmail(email)).willReturn(true);
given(service.isEmailFreeForRegistration(email, player)).willReturn(true);
given(validationService.validateEmail(email)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, player)).willReturn(true);
// when
asyncAddEmail.addEmail(player, email);
@@ -80,8 +84,8 @@ public class AsyncAddEmailTest {
given(auth.getEmail()).willReturn(null);
given(playerCache.getAuth("tester")).willReturn(auth);
given(dataSource.updateEmail(any(PlayerAuth.class))).willReturn(false);
given(service.validateEmail(email)).willReturn(true);
given(service.isEmailFreeForRegistration(email, player)).willReturn(true);
given(validationService.validateEmail(email)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, player)).willReturn(true);
// when
asyncAddEmail.addEmail(player, email);
@@ -117,7 +121,7 @@ public class AsyncAddEmailTest {
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getEmail()).willReturn(null);
given(playerCache.getAuth("my_player")).willReturn(auth);
given(service.validateEmail(email)).willReturn(false);
given(validationService.validateEmail(email)).willReturn(false);
// when
asyncAddEmail.addEmail(player, email);
@@ -136,8 +140,8 @@ public class AsyncAddEmailTest {
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getEmail()).willReturn(null);
given(playerCache.getAuth("testname")).willReturn(auth);
given(service.validateEmail(email)).willReturn(true);
given(service.isEmailFreeForRegistration(email, player)).willReturn(false);
given(validationService.validateEmail(email)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, player)).willReturn(false);
// when
asyncAddEmail.addEmail(player, email);
@@ -4,7 +4,8 @@ import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.entity.Player;
import org.junit.Test;
@@ -39,7 +40,10 @@ public class AsyncChangeEmailTest {
private DataSource dataSource;
@Mock
private ProcessService service;
private CommonService service;
@Mock
private ValidationService validationService;
@Test
public void shouldAddEmail() {
@@ -50,8 +54,8 @@ public class AsyncChangeEmailTest {
PlayerAuth auth = authWithMail("old@mail.tld");
given(playerCache.getAuth("bobby")).willReturn(auth);
given(dataSource.updateEmail(auth)).willReturn(true);
given(service.validateEmail(newEmail)).willReturn(true);
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
given(validationService.validateEmail(newEmail)).willReturn(true);
given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
// when
process.changeEmail(player, "old@mail.tld", newEmail);
@@ -71,8 +75,8 @@ public class AsyncChangeEmailTest {
PlayerAuth auth = authWithMail("old@mail.tld");
given(playerCache.getAuth("bobby")).willReturn(auth);
given(dataSource.updateEmail(auth)).willReturn(false);
given(service.validateEmail(newEmail)).willReturn(true);
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
given(validationService.validateEmail(newEmail)).willReturn(true);
given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
// when
process.changeEmail(player, "old@mail.tld", newEmail);
@@ -108,7 +112,7 @@ public class AsyncChangeEmailTest {
given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("old@mail.tld");
given(playerCache.getAuth("bobby")).willReturn(auth);
given(service.validateEmail(newEmail)).willReturn(false);
given(validationService.validateEmail(newEmail)).willReturn(false);
// when
process.changeEmail(player, "old@mail.tld", newEmail);
@@ -127,7 +131,7 @@ public class AsyncChangeEmailTest {
given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("other@address.email");
given(playerCache.getAuth("bobby")).willReturn(auth);
given(service.validateEmail(newEmail)).willReturn(true);
given(validationService.validateEmail(newEmail)).willReturn(true);
// when
process.changeEmail(player, "old@mail.tld", newEmail);
@@ -146,8 +150,8 @@ public class AsyncChangeEmailTest {
given(playerCache.isAuthenticated("username")).willReturn(true);
PlayerAuth auth = authWithMail("old@example.com");
given(playerCache.getAuth("username")).willReturn(auth);
given(service.validateEmail(newEmail)).willReturn(true);
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(false);
given(validationService.validateEmail(newEmail)).willReturn(true);
given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(false);
// when
process.changeEmail(player, "old@example.com", newEmail);
@@ -8,7 +8,7 @@ import fr.xephi.authme.events.AuthMeAsyncPreLoginEvent;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.HooksSettings;
@@ -56,7 +56,7 @@ public class AsynchronousLoginTest {
@Mock
private PlayerCache playerCache;
@Mock
private ProcessService processService;
private CommonService commonService;
@Mock
private LimboPlayerTaskManager limboPlayerTaskManager;
@Mock
@@ -81,7 +81,7 @@ public class AsynchronousLoginTest {
// then
verify(playerCache, only()).isAuthenticated(name);
verify(processService).send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
verify(commonService).send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
verifyZeroInteractions(dataSource);
}
@@ -98,7 +98,7 @@ public class AsynchronousLoginTest {
// then
verify(playerCache, only()).isAuthenticated(name);
verify(processService).send(player, MessageKey.USER_NOT_REGISTERED);
verify(commonService).send(player, MessageKey.USER_NOT_REGISTERED);
verify(dataSource, only()).getAuth(name);
}
@@ -111,15 +111,15 @@ public class AsynchronousLoginTest {
int groupId = 13;
PlayerAuth auth = PlayerAuth.builder().name(name).groupId(groupId).build();
given(dataSource.getAuth(name)).willReturn(auth);
given(processService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("group");
given(processService.getProperty(HooksSettings.NON_ACTIVATED_USERS_GROUP)).willReturn(groupId);
given(commonService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("group");
given(commonService.getProperty(HooksSettings.NON_ACTIVATED_USERS_GROUP)).willReturn(groupId);
// when
asynchronousLogin.forceLogin(player);
// then
verify(playerCache, only()).isAuthenticated(name);
verify(processService).send(player, MessageKey.ACCOUNT_NOT_ACTIVATED);
verify(commonService).send(player, MessageKey.ACCOUNT_NOT_ACTIVATED);
verify(dataSource, only()).getAuth(name);
}
@@ -133,7 +133,7 @@ public class AsynchronousLoginTest {
given(playerCache.isAuthenticated(name)).willReturn(false);
PlayerAuth auth = PlayerAuth.builder().name(name).build();
given(dataSource.getAuth(name)).willReturn(auth);
given(processService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("");
given(commonService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("");
doReturn(true).when(asynchronousLogin).hasReachedMaxLoggedInPlayersForIp(any(Player.class), anyString());
// when
@@ -141,7 +141,7 @@ public class AsynchronousLoginTest {
// then
verify(playerCache, only()).isAuthenticated(name);
verify(processService).send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
verify(commonService).send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
verify(dataSource, only()).getAuth(name);
verify(asynchronousLogin).hasReachedMaxLoggedInPlayersForIp(player, ip);
}
@@ -156,8 +156,8 @@ public class AsynchronousLoginTest {
given(playerCache.isAuthenticated(name)).willReturn(false);
PlayerAuth auth = PlayerAuth.builder().name(name).build();
given(dataSource.getAuth(name)).willReturn(auth);
given(processService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("");
given(processService.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
given(commonService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("");
given(commonService.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
doReturn(false).when(asynchronousLogin).hasReachedMaxLoggedInPlayersForIp(any(Player.class), anyString());
doAnswer(new Answer<Void>() {
@Override
@@ -181,7 +181,7 @@ public class AsynchronousLoginTest {
public void shouldPassMaxLoginPerIpCheck() {
// given
Player player = mockPlayer("Carl");
given(processService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(2);
given(commonService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(2);
given(permissionsManager.hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)).willReturn(false);
mockOnlinePlayersInBukkitService();
@@ -198,7 +198,7 @@ public class AsynchronousLoginTest {
public void shouldSkipIpCheckForZeroThreshold() {
// given
Player player = mockPlayer("Fiona");
given(processService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(0);
given(commonService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(0);
// when
boolean result = asynchronousLogin.hasReachedMaxLoggedInPlayersForIp(player, "192.168.0.1");
@@ -212,7 +212,7 @@ public class AsynchronousLoginTest {
public void shouldSkipIpCheckForPlayerWithMultipleAccountsPermission() {
// given
Player player = mockPlayer("Frank");
given(processService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(1);
given(commonService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(1);
given(permissionsManager.hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)).willReturn(true);
// when
@@ -228,7 +228,7 @@ public class AsynchronousLoginTest {
public void shouldFailIpCheckForIpWithTooManyPlayersOnline() {
// given
Player player = mockPlayer("Ian");
given(processService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(2);
given(commonService.getProperty(RestrictionSettings.MAX_LOGIN_PER_IP)).willReturn(2);
given(permissionsManager.hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)).willReturn(false);
mockOnlinePlayersInBukkitService();
@@ -8,7 +8,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.AuthGroupHandler;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.BukkitService;
@@ -45,7 +45,7 @@ public class AsynchronousUnregisterTest {
@Mock
private DataSource dataSource;
@Mock
private ProcessService service;
private CommonService service;
@Mock
private PasswordSecurity passwordSecurity;
@Mock