LoginDelegator extends BaseDelegator implements ILoginDelegator { @Override public void login(final String password) throws ConnectionException, RequestException, AccessException, ConfigException { login(password, false); } LoginDelegator(final IOptionsServer server); @Override String getLoginStatus(); @Override void login(final String password); @Override void login(final String password, final boolean allHosts); @Override boolean isDontWriteTicket(final String cmd, final String[] cmdArgs); @Override void login(final String password, final LoginOptions opts); @Override void login(final String password, final StringBuffer ticket, final LoginOptions opts); @Override void login(@Nonnull final IUser user, final StringBuffer ticket, final LoginOptions opts);  }
@Test public void testLoginUserOptTicketFromServer() throws P4JavaException { final int nameCalls = 3; IUser user = mock(IUser.class); when(user.getLoginName()).thenReturn(TEST_USER, TEST_USER, TEST_USER); LoginOptions options = new LoginOptions().setHost(TEST_HOST).setDontWriteTicket(true) .setAllHosts(true); when(server.execMapCmdList(eq(LOGIN.toString()), argThat(USER_OPT_MATCHER), eq((Map<String, Object>)null))) .thenReturn(buildLoggedIn()); when(server.getAuthTicket(TEST_USER)).thenReturn("auth"); StringBuffer ticketBuffer = new StringBuffer(); loginDelegator.login(user, ticketBuffer, options); verify(server).execMapCmdList(eq(LOGIN.toString()), argThat(USER_OPT_MATCHER), eq((Map<String, Object>)null)); verify(server).getAuthTicket(TEST_USER); verify(user, times(nameCalls)).getLoginName(); assertEquals("auth", ticketBuffer.toString()); }
@Test public void testLoginTicketFromServer() throws P4JavaException { Map<String, Object> pwdMap = buildPwd(); when(server.execMapCmdList(eq(LOGIN.toString()), argThat(EMPTY_MATCHER), eq(pwdMap))) .thenReturn(buildLoggedIn()); when(server.getAuthTicket()).thenReturn("auth"); StringBuffer ticketBuffer = new StringBuffer(); loginDelegator.login(TEST_PASSWORD, ticketBuffer, null); verify(server).execMapCmdList(eq(LOGIN.toString()), argThat(EMPTY_MATCHER), eq(pwdMap)); verify(server).getAuthTicket(); assertEquals("auth", ticketBuffer.toString()); }
@Test public void testLoginOptTicketFromServer() throws P4JavaException { LoginOptions options = new LoginOptions().setHost(TEST_HOST).setDontWriteTicket(true) .setAllHosts(true); Map<String, Object> pwdMap = buildPwd(); when(server.execMapCmdList(eq(LOGIN.toString()), argThat(OPT_MATCHER), eq(pwdMap))) .thenReturn(buildLoggedIn()); when(server.getAuthTicket()).thenReturn("auth"); StringBuffer ticketBuffer = new StringBuffer(); loginDelegator.login(TEST_PASSWORD, ticketBuffer, options); verify(server).execMapCmdList(eq(LOGIN.toString()), argThat(OPT_MATCHER), eq(pwdMap)); verify(server).getAuthTicket(); assertEquals("auth", ticketBuffer.toString()); }
@Test public void testLoginPwdOptFromServer() throws P4JavaException { LoginOptions options = new LoginOptions().setHost(TEST_HOST).setDontWriteTicket(true) .setAllHosts(true); Map<String, Object> pwdMap = buildPwd(); when(server.execMapCmdList(eq(LOGIN.toString()), argThat(OPT_MATCHER), eq(pwdMap))) .thenReturn(buildLoggedIn()); when(server.getAuthTicket()).thenReturn("auth"); loginDelegator.login(TEST_PASSWORD, options); verify(server).execMapCmdList(eq(LOGIN.toString()), argThat(OPT_MATCHER), eq(pwdMap)); verify(server).getAuthTicket(); }
@Test public void testLoginPwdAllHosts() throws P4JavaException { Map<String, Object> pwdMap = buildPwd(); when(server.execMapCmdList(eq(LOGIN.toString()), argThat(ALL_HOSTS_MATCHER), eq(pwdMap))) .thenReturn(buildLoggedIn()); loginDelegator.login(TEST_PASSWORD, true); verify(server).execMapCmdList(eq(LOGIN.toString()), argThat(ALL_HOSTS_MATCHER), eq(pwdMap)); }
@Test public void testLoginPwd() throws P4JavaException { Map<String, Object> pwdMap = buildPwd(); when(server.execMapCmdList(eq(LOGIN.toString()), argThat(EMPTY_MATCHER), eq(pwdMap))) .thenReturn(buildLoggedIn()); loginDelegator.login(TEST_PASSWORD); verify(server).execMapCmdList(eq(LOGIN.toString()), argThat(EMPTY_MATCHER), eq(pwdMap)); }
@Test public void testLoginTicketFromMap() throws P4JavaException { Map<String, Object> pwdMap = buildPwd(); final String ticket = "mapticket"; when(server.execMapCmdList(eq(LOGIN.toString()), argThat(EMPTY_MATCHER), eq(pwdMap))) .thenReturn(buildLoggedIn(ticket)); StringBuffer ticketBuffer = new StringBuffer(); loginDelegator.login(TEST_PASSWORD, ticketBuffer, null); verify(server).execMapCmdList(eq(LOGIN.toString()), argThat(EMPTY_MATCHER), eq(pwdMap)); assertEquals(ticket, ticketBuffer.toString()); }
@Test public void testLoginUserOptTicketFromServer() throws P4JavaException { final int nameCalls = 3; IUser user = mock(IUser.class); when(user.getLoginName()).thenReturn(TEST_USER, TEST_USER, TEST_USER); LoginOptions options = new LoginOptions().setHost(TEST_HOST).setDontWriteTicket(true) .setAllHosts(true); when(server.execMapCmdList(eq(LOGIN.toString()), argThat(USER_OPT_MATCHER), eq(null))) .thenReturn(buildLoggedIn()); when(server.getAuthTicket(TEST_USER)).thenReturn("auth"); StringBuffer ticketBuffer = new StringBuffer(); loginDelegator.login(user, ticketBuffer, options); verify(server).execMapCmdList(eq(LOGIN.toString()), argThat(USER_OPT_MATCHER), eq(null)); verify(server).getAuthTicket(TEST_USER); verify(user, times(nameCalls)).getLoginName(); assertEquals("auth", ticketBuffer.toString()); }
@Test(expected = NullPointerException.class) public void testLoginUserNull() throws P4JavaException { LoginOptions options = new LoginOptions().setHost(TEST_HOST).setDontWriteTicket(true) .setAllHosts(true); loginDelegator.login((IUser) null, null, options); }
@Test(expected = IllegalArgumentException.class) public void testLoginUserNoName() throws P4JavaException { IUser user = mock(IUser.class); when(user.getLoginName()).thenReturn(""); LoginOptions options = new LoginOptions().setHost(TEST_HOST).setDontWriteTicket(true) .setAllHosts(true); StringBuffer ticketBuffer = new StringBuffer(); loginDelegator.login(user, ticketBuffer, options); }