package com.jenjinstudios.server.message;

import com.jenjinstudios.core.io.Message;
import com.jenjinstudios.core.io.MessageRegistry;
import com.jenjinstudios.server.net.AuthServer;
import com.jenjinstudios.server.net.ClientHandler;
import com.jenjinstudios.server.net.User;
import com.jenjinstudios.server.sql.Authenticator;
import org.testng.annotations.Test;

import static org.mockito.Mockito.*;

/**
 * @author Caleb Brinkman
 */
public class ExecutableLoginRequestTest
{
	@Test
	public void testMessageExecution() throws Exception {
		Message message = MessageRegistry.getInstance().createMessage("LoginRequest");
		message.setArgument("username", "foo");
		message.setArgument("password", "bar");

		User user = new User();
		user.setUsername("foo");
		user.setUsername("bar");
		user.setLoggedIn(true);
		ClientHandler clientHandler = mock(ClientHandler.class);
		AuthServer server = mock(AuthServer.class);
		ServerMessageFactory serverMessageFactory = new ServerMessageFactory(clientHandler,
			  MessageRegistry.getInstance());
		Authenticator authenticator = mock(Authenticator.class);
		when(server.getAuthenticator()).thenReturn(authenticator);
		when(server.getCycleStartTime()).thenReturn(12345l);
		when(authenticator.logInUser("foo", "bar")).thenReturn(user);
		when(clientHandler.getServer()).thenReturn(server);
		when(clientHandler.getMessageRegistry()).thenReturn(MessageRegistry.getInstance());
		when(clientHandler.getMessageFactory()).thenReturn(serverMessageFactory);

		ExecutableLoginRequest executableLoginRequest = new ExecutableLoginRequest(clientHandler, message);
		executableLoginRequest.runImmediate();
		executableLoginRequest.runDelayed();

		verify(server).associateUsernameWithClientHandler("foo", clientHandler);
	}
}

@@ -320,16 +320,68 @@
 icator;%0A
+import com.jenjinstudios.server.sql.LoginException;%0A
 import o
@@ -1801,11 +1801,1399 @@
 ler);%0A%09%7D
+%0A%0A%09@Test%0A%09public void testFailedLogin() throws Exception %7B%0A%09%09Message message = MessageRegistry.getInstance().createMessage(%22LoginRequest%22);%0A%09%09message.setArgument(%22username%22, %22foo-dapoo%22);%0A%09%09message.setArgument(%22password%22, %22bar%22);%0A%0A%09%09User user = new User();%0A%09%09user.setUsername(%22foo%22);%0A%09%09user.setUsername(%22bar%22);%0A%09%09user.setLoggedIn(true);%0A%09%09ClientHandler clientHandler = mock(ClientHandler.class);%0A%09%09AuthServer server = mock(AuthServer.class);%0A%09%09ServerMessageFactory serverMessageFactory = new ServerMessageFactory(clientHandler,%0A%09%09%09  MessageRegistry.getInstance());%0A%09%09Authenticator authenticator = mock(Authenticator.class);%0A%09%09when(server.getAuthenticator()).thenReturn(authenticator);%0A%09%09when(server.getCycleStartTime()).thenReturn(12345l);%0A%09%09when(authenticator.logInUser(%22foo%22, %22bar%22)).thenReturn(user);%0A%09%09when(authenticator.logInUser(%22foo-dapoo%22, %22bar%22)).thenThrow(new LoginException(%22Nope%22));%0A%09%09when(clientHandler.getServer()).thenReturn(server);%0A%09%09when(clientHandler.getMessageRegistry()).thenReturn(MessageRegistry.getInstance());%0A%09%09when(clientHandler.getMessageFactory()).thenReturn(serverMessageFactory);%0A%0A%09%09ExecutableLoginRequest executableLoginRequest = new ExecutableLoginRequest(clientHandler, message);%0A%09%09executableLoginRequest.runImmediate();%0A%09%09executableLoginRequest.runDelayed();%0A%0A%09%09verify(server, times(0)).associateUsernameWithClientHandler(%22foo-dapoo%22, clientHandler);%0A%09%7D
 %0A%7D%0A

Test improvementification