Minor - Use explicit dependencies in ProcessService

- Replace AuthMe in favor of PermissionsManager
- Create missing tests for ProcessService
This commit is contained in:
ljacqu 2016-05-24 18:25:38 +02:00
parent a7d3e60abe
commit 8de1897412
4 changed files with 55 additions and 37 deletions

View File

@ -1,15 +1,17 @@
package fr.xephi.authme.process; package fr.xephi.authme.process;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages; import fr.xephi.authme.output.Messages;
import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.domain.Property; import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.util.ValidationService; import fr.xephi.authme.util.ValidationService;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.plugin.PluginManager;
import javax.inject.Inject; import javax.inject.Inject;
/** /**
@ -24,11 +26,14 @@ public class ProcessService {
private Messages messages; private Messages messages;
@Inject @Inject
private AuthMe plugin; private PluginManager pluginManager;
@Inject @Inject
private ValidationService validationService; private ValidationService validationService;
@Inject
private PermissionsManager permissionsManager;
/** /**
* Retrieve a property's value. * Retrieve a property's value.
* *
@ -96,16 +101,7 @@ public class ProcessService {
* @param event the event to emit * @param event the event to emit
*/ */
public void callEvent(Event event) { public void callEvent(Event event) {
plugin.getServer().getPluginManager().callEvent(event); pluginManager.callEvent(event);
}
/**
* Return the plugin instance.
*
* @return AuthMe instance
*/
public AuthMe getAuthMe() {
return plugin;
} }
/** /**
@ -128,7 +124,7 @@ public class ProcessService {
} }
public boolean hasPermission(Player player, PermissionNode node) { public boolean hasPermission(Player player, PermissionNode node) {
return plugin.getPermissionsManager().hasPermission(player, node); return permissionsManager.hasPermission(player, node);
} }
} }

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.process.register; package fr.xephi.authme.process.register;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.cache.limbo.LimboPlayer;
@ -33,6 +34,9 @@ public class ProcessSyncEmailRegister implements SynchronousProcess {
@Inject @Inject
private BukkitService bukkitService; private BukkitService bukkitService;
@Inject
private AuthMe authMe;
public ProcessSyncEmailRegister() { } public ProcessSyncEmailRegister() { }
public void processEmailRegister(Player player) { public void processEmailRegister(Player player) {
@ -47,7 +51,7 @@ public class ProcessSyncEmailRegister implements SynchronousProcess {
if (limbo != null) { if (limbo != null) {
if (time != 0) { if (time != 0) {
BukkitTask id = bukkitService.runTaskLater(new TimeoutTask(service.getAuthMe(), name, player), time); BukkitTask id = bukkitService.runTaskLater(new TimeoutTask(authMe, name, player), time);
limbo.setTimeoutTask(id); limbo.setTimeoutTask(id);
} }
BukkitTask messageTask = bukkitService.runTask(new MessageTask( BukkitTask messageTask = bukkitService.runTask(new MessageTask(

View File

@ -79,7 +79,7 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
int interval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL); int interval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
BukkitTask task; BukkitTask task;
if (delay != 0) { if (delay != 0) {
task = bukkitService.runTaskLater(new TimeoutTask(service.getAuthMe(), name, player), delay); task = bukkitService.runTaskLater(new TimeoutTask(plugin, name, player), delay);
cache.getLimboPlayer(name).setTimeoutTask(task); cache.getLimboPlayer(name).setTimeoutTask(task);
} }
task = bukkitService.runTask(new MessageTask(bukkitService, plugin.getMessages(), task = bukkitService.runTask(new MessageTask(bukkitService, plugin.getMessages(),

View File

@ -1,16 +1,17 @@
package fr.xephi.authme.process; package fr.xephi.authme.process;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages; import fr.xephi.authme.output.Messages;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.ValidationService; import fr.xephi.authme.util.ValidationService;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginManager;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
@ -31,22 +32,21 @@ public class ProcessServiceTest {
@InjectMocks @InjectMocks
private ProcessService processService; private ProcessService processService;
@Mock @Mock
private ValidationService validationService; private ValidationService validationService;
@Mock @Mock
private NewSetting settings; private NewSetting settings;
@Mock @Mock
private Messages messages; private Messages messages;
@Mock @Mock
private AuthMe authMe; private PluginManager pluginManager;
@Mock @Mock
private DataSource dataSource; private PermissionsManager permissionsManager;
@Mock
private SpawnLoader spawnLoader;
@Mock
private PluginHooks pluginHooks;
@Mock
private BukkitService bukkitService;
@Test @Test
public void shouldGetProperty() { public void shouldGetProperty() {
@ -127,15 +127,6 @@ public class ProcessServiceTest {
verify(messages).retrieveSingle(key); verify(messages).retrieveSingle(key);
} }
@Test
public void shouldReturnAuthMeInstance() {
// given / when
AuthMe result = processService.getAuthMe();
// then
assertThat(result, equalTo(authMe));
}
@Test @Test
public void shouldValidatePassword() { public void shouldValidatePassword() {
// given // given
@ -180,4 +171,31 @@ public class ProcessServiceTest {
assertThat(result, equalTo(true)); assertThat(result, equalTo(true));
verify(validationService).isEmailFreeForRegistration(email, sender); verify(validationService).isEmailFreeForRegistration(email, sender);
} }
@Test
public void shouldEmitEvent() {
// given
Event event = mock(Event.class);
// when
processService.callEvent(event);
// then
verify(pluginManager).callEvent(event);
}
@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));
}
} }