#707 Convert async processes as services

(work in progress - rough, untested changes)
This commit is contained in:
ljacqu
2016-05-17 19:49:06 +02:00
parent 3ad76b8ec5
commit f5c89e897f
14 changed files with 359 additions and 412 deletions
@@ -140,33 +140,6 @@ public class ProcessServiceTest {
assertThat(result, equalTo(authMe));
}
@Test
public void shouldReturnPluginHooks() {
// given / when
PluginHooks result = processService.getPluginHooks();
// then
assertThat(result, equalTo(pluginHooks));
}
@Test
public void shouldReturnSpawnLoader() {
// given / when
SpawnLoader result = processService.getSpawnLoader();
// then
assertThat(result, equalTo(spawnLoader));
}
@Test
public void shouldReturnDatasource() {
// given / when
DataSource result = processService.getDataSource();
// then
assertThat(result, equalTo(dataSource));
}
@Test
public void shouldComputeHash() {
// given
@@ -11,6 +11,7 @@ import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@@ -26,12 +27,18 @@ import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class AsyncAddEmailTest {
@InjectMocks
private AsyncAddEmail asyncAddEmail;
@Mock
private Player player;
@Mock
private DataSource dataSource;
@Mock
private PlayerCache playerCache;
@Mock
private ProcessService service;
@@ -44,7 +51,6 @@ public class AsyncAddEmailTest {
public void shouldAddEmail() {
// given
String email = "my.mail@example.org";
AsyncAddEmail process = createProcess(email);
given(player.getName()).willReturn("testEr");
given(playerCache.isAuthenticated("tester")).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
@@ -55,7 +61,7 @@ public class AsyncAddEmailTest {
given(service.isEmailFreeForRegistration(email, player)).willReturn(true);
// when
process.run();
asyncAddEmail.addEmail(player, email);
// then
verify(dataSource).updateEmail(auth);
@@ -68,7 +74,6 @@ public class AsyncAddEmailTest {
public void shouldReturnErrorWhenMailCannotBeSaved() {
// given
String email = "my.mail@example.org";
AsyncAddEmail process = createProcess(email);
given(player.getName()).willReturn("testEr");
given(playerCache.isAuthenticated("tester")).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
@@ -80,7 +85,7 @@ public class AsyncAddEmailTest {
given(service.isEmailFreeForRegistration(email, player)).willReturn(true);
// when
process.run();
asyncAddEmail.addEmail(player, email);
// then
verify(dataSource).updateEmail(auth);
@@ -90,7 +95,6 @@ public class AsyncAddEmailTest {
@Test
public void shouldNotAddMailIfPlayerAlreadyHasEmail() {
// given
AsyncAddEmail process = createProcess("some.mail@example.org");
given(player.getName()).willReturn("my_Player");
given(playerCache.isAuthenticated("my_player")).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
@@ -98,7 +102,7 @@ public class AsyncAddEmailTest {
given(playerCache.getAuth("my_player")).willReturn(auth);
// when
process.run();
asyncAddEmail.addEmail(player, "some.mail@example.org");
// then
verify(service).send(player, MessageKey.USAGE_CHANGE_EMAIL);
@@ -109,7 +113,6 @@ public class AsyncAddEmailTest {
public void shouldNotAddMailIfItIsInvalid() {
// given
String email = "invalid_mail";
AsyncAddEmail process = createProcess(email);
given(player.getName()).willReturn("my_Player");
given(playerCache.isAuthenticated("my_player")).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
@@ -118,7 +121,7 @@ public class AsyncAddEmailTest {
given(service.validateEmail(email)).willReturn(false);
// when
process.run();
asyncAddEmail.addEmail(player, email);
// then
verify(service).send(player, MessageKey.INVALID_EMAIL);
@@ -129,7 +132,6 @@ public class AsyncAddEmailTest {
public void shouldNotAddMailIfAlreadyUsed() {
// given
String email = "player@mail.tld";
AsyncAddEmail process = createProcess(email);
given(player.getName()).willReturn("TestName");
given(playerCache.isAuthenticated("testname")).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
@@ -139,7 +141,7 @@ public class AsyncAddEmailTest {
given(service.isEmailFreeForRegistration(email, player)).willReturn(false);
// when
process.run();
asyncAddEmail.addEmail(player, email);
// then
verify(service).send(player, MessageKey.EMAIL_ALREADY_USED_ERROR);
@@ -149,13 +151,12 @@ public class AsyncAddEmailTest {
@Test
public void shouldShowLoginMessage() {
// given
AsyncAddEmail process = createProcess("test@mail.com");
given(player.getName()).willReturn("Username12");
given(playerCache.isAuthenticated("username12")).willReturn(false);
given(dataSource.isAuthAvailable("Username12")).willReturn(true);
// when
process.run();
asyncAddEmail.addEmail(player, "test@mail.com");
// then
verify(service).send(player, MessageKey.LOGIN_MESSAGE);
@@ -165,14 +166,13 @@ public class AsyncAddEmailTest {
@Test
public void shouldShowEmailRegisterMessage() {
// given
AsyncAddEmail process = createProcess("test@mail.com");
given(player.getName()).willReturn("user");
given(playerCache.isAuthenticated("user")).willReturn(false);
given(dataSource.isAuthAvailable("user")).willReturn(false);
given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
// when
process.run();
asyncAddEmail.addEmail(player, "test@mail.com");
// then
verify(service).send(player, MessageKey.REGISTER_EMAIL_MESSAGE);
@@ -182,28 +182,17 @@ public class AsyncAddEmailTest {
@Test
public void shouldShowRegularRegisterMessage() {
// given
AsyncAddEmail process = createProcess("test@mail.com");
given(player.getName()).willReturn("user");
given(playerCache.isAuthenticated("user")).willReturn(false);
given(dataSource.isAuthAvailable("user")).willReturn(false);
given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(false);
// when
process.run();
asyncAddEmail.addEmail(player, "test@mail.com");
// then
verify(service).send(player, MessageKey.REGISTER_MESSAGE);
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
}
/**
* Create an instance of {@link AsyncAddEmail} with the class' mocks.
*
* @param email The email to use
* @return The created process
*/
private AsyncAddEmail createProcess(String email) {
return new AsyncAddEmail(player, email, dataSource, playerCache, service);
}
}
@@ -5,11 +5,11 @@ import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
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.runners.MockitoJUnitRunner;
@@ -26,12 +26,18 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class AsyncChangeEmailTest {
@InjectMocks
private AsyncChangeEmail process;
@Mock
private Player player;
@Mock
private PlayerCache playerCache;
@Mock
private DataSource dataSource;
@Mock
private ProcessService service;
@@ -39,7 +45,6 @@ public class AsyncChangeEmailTest {
public void shouldAddEmail() {
// given
String newEmail = "new@mail.tld";
AsyncChangeEmail process = createProcess("old@mail.tld", newEmail);
given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("old@mail.tld");
@@ -49,7 +54,7 @@ public class AsyncChangeEmailTest {
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
// when
process.run();
process.changeEmail(player, "old@mail.tld", newEmail);
// then
verify(dataSource).updateEmail(auth);
@@ -61,7 +66,6 @@ public class AsyncChangeEmailTest {
public void shouldShowErrorIfSaveFails() {
// given
String newEmail = "new@mail.tld";
AsyncChangeEmail process = createProcess("old@mail.tld", newEmail);
given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("old@mail.tld");
@@ -71,7 +75,7 @@ public class AsyncChangeEmailTest {
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
// when
process.run();
process.changeEmail(player, "old@mail.tld", newEmail);
// then
verify(dataSource).updateEmail(auth);
@@ -82,14 +86,13 @@ public class AsyncChangeEmailTest {
@Test
public void shouldShowAddEmailUsage() {
// given
AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld");
given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail(null);
given(playerCache.getAuth("bobby")).willReturn(auth);
// when
process.run();
process.changeEmail(player, "old@mail.tld", "new@mailt.tld");
// then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@@ -101,7 +104,6 @@ public class AsyncChangeEmailTest {
public void shouldRejectInvalidNewMail() {
// given
String newEmail = "bogus";
AsyncChangeEmail process = createProcess("old@mail.tld", newEmail);
given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("old@mail.tld");
@@ -109,7 +111,7 @@ public class AsyncChangeEmailTest {
given(service.validateEmail(newEmail)).willReturn(false);
// when
process.run();
process.changeEmail(player, "old@mail.tld", newEmail);
// then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@@ -121,7 +123,6 @@ public class AsyncChangeEmailTest {
public void shouldRejectInvalidOldEmail() {
// given
String newEmail = "new@mail.tld";
AsyncChangeEmail process = createProcess("old@mail.tld", newEmail);
given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("other@address.email");
@@ -131,7 +132,7 @@ public class AsyncChangeEmailTest {
// when
process.run();
process.changeEmail(player, "old@mail.tld", newEmail);
// then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@@ -143,7 +144,6 @@ public class AsyncChangeEmailTest {
public void shouldRejectAlreadyUsedEmail() {
// given
String newEmail = "new@example.com";
AsyncChangeEmail process = createProcess("old@example.com", newEmail);
given(player.getName()).willReturn("Username");
given(playerCache.isAuthenticated("username")).willReturn(true);
PlayerAuth auth = authWithMail("old@example.com");
@@ -152,7 +152,7 @@ public class AsyncChangeEmailTest {
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(false);
// when
process.run();
process.changeEmail(player, "old@example.com", newEmail);
// then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@@ -163,13 +163,12 @@ public class AsyncChangeEmailTest {
@Test
public void shouldSendLoginMessage() {
// given
AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld");
given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(false);
given(dataSource.isAuthAvailable("Bobby")).willReturn(true);
// when
process.run();
process.changeEmail(player, "old@mail.tld", "new@mail.tld");
// then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@@ -180,14 +179,13 @@ public class AsyncChangeEmailTest {
@Test
public void shouldShowEmailRegistrationMessage() {
// given
AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld");
given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(false);
given(dataSource.isAuthAvailable("Bobby")).willReturn(false);
given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
// when
process.run();
process.changeEmail(player, "old@mail.tld", "new@mail.tld");
// then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@@ -198,14 +196,13 @@ public class AsyncChangeEmailTest {
@Test
public void shouldShowRegistrationMessage() {
// given
AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld");
given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(false);
given(dataSource.isAuthAvailable("Bobby")).willReturn(false);
given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(false);
// when
process.run();
process.changeEmail(player, "old@mail.tld", "new@mail.tld");
// then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
@@ -219,8 +216,4 @@ public class AsyncChangeEmailTest {
return auth;
}
private AsyncChangeEmail createProcess(String oldEmail, String newEmail) {
given(service.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(5);
return new AsyncChangeEmail(player, oldEmail, newEmail, dataSource, playerCache, service);
}
}