LoginCommand implements Command { @Override public void execute(Session session, Message msg) { if (session.getSessionUser() != null) { log.info("User {} already logged in.", session.getSessionUser()); return; } else { LoginMessage loginMsg = (LoginMessage) msg; User user = userStore.getUser(loginMsg.getLogin(), loginMsg.getPass()); if (user == null) { sendAcknowledge(session, "Login Failed"); return; } session.setSessionUser(user); sessionManager.registerUser(user.getId(), session.getId()); log.info("Success login: {}", user); sendAcknowledge(session, "Login Ok"); } } LoginCommand(UserStore userStore, SessionManager sessionManager); @Override void execute(Session session, Message msg);  }
@Test public void successLogin() throws Exception { LoginCommand loginCommand = new LoginCommand(userStore, sessionManager); LoginMessage login = new LoginMessage(); login.setSender(123L); login.setLogin("Jack"); login.setPass("qwerty"); Session session = new Session(); loginCommand.execute(session, login); assertEquals(session.getSessionUser(), defaultUser); }
@Test public void badLogin() throws Exception { LoginCommand loginCommand = new LoginCommand(userStore, sessionManager); LoginMessage login = new LoginMessage(); login.setSender(123L); login.setLogin("Jack"); login.setPass("*****"); Session session = new Session(); loginCommand.execute(session, login); assertEquals(session.getSessionUser(), defaultUser); }