Program: 105215
Project: 105215
Buggy source:
@Test public void testListAndSwitchSAMLAccountCmd() throws Exception {
  final Map<String,Object[]> params=new HashMap<String,Object[]>();
  final String sessionKeyValue="String_Node_Str";
  Mockito.when(session.getAttribute(ApiConstants.SESSIONKEY)).thenReturn(sessionKeyValue);
  Mockito.when(session.getAttribute("String_Node_Str")).thenReturn(2L);
  params.put(ApiConstants.USER_ID,new String[]{"String_Node_Str"});
  params.put(ApiConstants.DOMAIN_ID,new String[]{"String_Node_Str"});
  Mockito.when(userDao.findByUuid(Mockito.anyString())).thenReturn(new UserVO(2L));
  Mockito.when(domainDao.findByUuid(Mockito.anyString())).thenReturn(new DomainVO());
  ListAndSwitchSAMLAccountCmd cmd=new ListAndSwitchSAMLAccountCmd();
  Field apiServerField=ListAndSwitchSAMLAccountCmd.class.getDeclaredField("String_Node_Str");
  apiServerField.setAccessible(true);
  apiServerField.set(cmd,apiServer);
  Field managerField=ListAndSwitchSAMLAccountCmd.class.getDeclaredField("String_Node_Str");
  managerField.setAccessible(true);
  managerField.set(cmd,samlAuthManager);
  Field accountServiceField=BaseCmd.class.getDeclaredField("String_Node_Str");
  accountServiceField.setAccessible(true);
  accountServiceField.set(cmd,accountService);
  Field userAccountDaoField=ListAndSwitchSAMLAccountCmd.class.getDeclaredField("String_Node_Str");
  userAccountDaoField.setAccessible(true);
  userAccountDaoField.set(cmd,userAccountDao);
  Field userDaoField=ListAndSwitchSAMLAccountCmd.class.getDeclaredField("String_Node_Str");
  userDaoField.setAccessible(true);
  userDaoField.set(cmd,userDao);
  Field domainDaoField=ListAndSwitchSAMLAccountCmd.class.getDeclaredField("String_Node_Str");
  domainDaoField.setAccessible(true);
  domainDaoField.set(cmd,domainDao);
  try {
    cmd.authenticate("String_Node_Str",params,null,"String_Node_Str",HttpUtils.RESPONSE_TYPE_JSON,new StringBuilder(),req,resp);
  }
 catch (  ServerApiException exception) {
    assertEquals(exception.getErrorCode(),ApiErrorCode.UNAUTHORIZED);
  }
 finally {
    Mockito.verify(accountService,Mockito.times(0)).getUserAccountById(Mockito.anyLong());
  }
  params.put(ApiConstants.SESSIONKEY,new String[]{"String_Node_Str"});
  try {
    Mockito.when(session.isNew()).thenReturn(false);
    cmd.authenticate("String_Node_Str",params,session,"String_Node_Str",HttpUtils.RESPONSE_TYPE_JSON,new StringBuilder(),req,resp);
  }
 catch (  ServerApiException exception) {
    assertEquals(exception.getErrorCode(),ApiErrorCode.UNAUTHORIZED);
  }
 finally {
    Mockito.verify(accountService,Mockito.times(0)).getUserAccountById(Mockito.anyLong());
  }
  params.put(ApiConstants.SESSIONKEY,new String[]{sessionKeyValue});
  try {
    cmd.authenticate("String_Node_Str",params,session,"String_Node_Str",HttpUtils.RESPONSE_TYPE_JSON,new StringBuilder(),req,resp);
  }
 catch (  ServerApiException exception) {
    assertEquals(exception.getErrorCode(),ApiErrorCode.ACCOUNT_ERROR);
  }
 finally {
    Mockito.verify(accountService,Mockito.times(1)).getUserAccountById(Mockito.anyLong());
  }
  UserAccountVO mockedUserAccount=new UserAccountVO();
  mockedUserAccount.setId(2L);
  mockedUserAccount.setAccountState(Account.State.enabled.toString());
  mockedUserAccount.setUsername("String_Node_Str");
  mockedUserAccount.setExternalEntity("String_Node_Str");
  mockedUserAccount.setDomainId(0L);
  mockedUserAccount.setSource(User.Source.UNKNOWN);
  Mockito.when(accountService.getUserAccountById(Mockito.anyLong())).thenReturn(mockedUserAccount);
  try {
    cmd.authenticate("String_Node_Str",params,session,"String_Node_Str",HttpUtils.RESPONSE_TYPE_JSON,new StringBuilder(),req,resp);
  }
 catch (  ServerApiException exception) {
    assertEquals(exception.getErrorCode(),ApiErrorCode.ACCOUNT_ERROR);
  }
 finally {
    Mockito.verify(accountService,Mockito.times(2)).getUserAccountById(Mockito.anyLong());
  }
  mockedUserAccount.setSource(User.Source.SAML2);
  Mockito.when(accountService.getUserAccountById(Mockito.anyLong())).thenReturn(mockedUserAccount);
  Mockito.when(apiServer.verifyUser(Mockito.anyLong())).thenReturn(true);
  LoginCmdResponse loginCmdResponse=new LoginCmdResponse();
  loginCmdResponse.setUserId("String_Node_Str");
  loginCmdResponse.setDomainId("String_Node_Str");
  loginCmdResponse.setType("String_Node_Str");
  loginCmdResponse.setUsername("String_Node_Str");
  loginCmdResponse.setAccount("String_Node_Str");
  loginCmdResponse.setFirstName("String_Node_Str");
  loginCmdResponse.setLastName("String_Node_Str");
  loginCmdResponse.setSessionKey("String_Node_Str");
  Mockito.when(apiServer.loginUser(Mockito.any(HttpSession.class),Mockito.anyString(),Mockito.anyString(),Mockito.anyLong(),Mockito.anyString(),Mockito.anyString(),Mockito.anyMap())).thenReturn(loginCmdResponse);
  try {
    cmd.authenticate("String_Node_Str",params,session,"String_Node_Str",HttpUtils.RESPONSE_TYPE_JSON,new StringBuilder(),req,resp);
  }
 catch (  ServerApiException exception) {
    fail("String_Node_Str" + exception.getMessage());
  }
 finally {
    Mockito.verify(accountService,Mockito.times(4)).getUserAccountById(Mockito.anyLong());
    Mockito.verify(resp,Mockito.times(1)).sendRedirect(Mockito.anyString());
  }
}

Fixed version (ground truth):
@Test public void testListAndSwitchSAMLAccountCmd() throws Exception {
  final Map<String,Object[]> params=new HashMap<String,Object[]>();
  final String sessionKeyValue="String_Node_Str";
  Mockito.when(session.getAttribute(ApiConstants.SESSIONKEY)).thenReturn(sessionKeyValue);
  Mockito.when(session.getAttribute("String_Node_Str")).thenReturn(2L);
  params.put(ApiConstants.USER_ID,new String[]{"String_Node_Str"});
  params.put(ApiConstants.DOMAIN_ID,new String[]{"String_Node_Str"});
  Mockito.when(userDao.findByUuid(Mockito.anyString())).thenReturn(new UserVO(2L));
  Mockito.when(domainDao.findByUuid(Mockito.anyString())).thenReturn(new DomainVO());
  ListAndSwitchSAMLAccountCmd cmd=new ListAndSwitchSAMLAccountCmd();
  Field apiServerField=ListAndSwitchSAMLAccountCmd.class.getDeclaredField("String_Node_Str");
  apiServerField.setAccessible(true);
  apiServerField.set(cmd,apiServer);
  Field managerField=ListAndSwitchSAMLAccountCmd.class.getDeclaredField("String_Node_Str");
  managerField.setAccessible(true);
  managerField.set(cmd,samlAuthManager);
  Field accountServiceField=BaseCmd.class.getDeclaredField("String_Node_Str");
  accountServiceField.setAccessible(true);
  accountServiceField.set(cmd,accountService);
  Field userAccountDaoField=ListAndSwitchSAMLAccountCmd.class.getDeclaredField("String_Node_Str");
  userAccountDaoField.setAccessible(true);
  userAccountDaoField.set(cmd,userAccountDao);
  Field userDaoField=ListAndSwitchSAMLAccountCmd.class.getDeclaredField("String_Node_Str");
  userDaoField.setAccessible(true);
  userDaoField.set(cmd,userDao);
  Field domainDaoField=ListAndSwitchSAMLAccountCmd.class.getDeclaredField("String_Node_Str");
  domainDaoField.setAccessible(true);
  domainDaoField.set(cmd,domainDao);
  try {
    cmd.authenticate("String_Node_Str",params,null,null,HttpUtils.RESPONSE_TYPE_JSON,new StringBuilder(),req,resp);
  }
 catch (  ServerApiException exception) {
    assertEquals(exception.getErrorCode(),ApiErrorCode.UNAUTHORIZED);
  }
 finally {
    Mockito.verify(accountService,Mockito.times(0)).getUserAccountById(Mockito.anyLong());
  }
  params.put(ApiConstants.SESSIONKEY,new String[]{"String_Node_Str"});
  try {
    Mockito.when(session.isNew()).thenReturn(false);
    cmd.authenticate("String_Node_Str",params,session,null,HttpUtils.RESPONSE_TYPE_JSON,new StringBuilder(),req,resp);
  }
 catch (  ServerApiException exception) {
    assertEquals(exception.getErrorCode(),ApiErrorCode.UNAUTHORIZED);
  }
 finally {
    Mockito.verify(accountService,Mockito.times(0)).getUserAccountById(Mockito.anyLong());
  }
  params.put(ApiConstants.SESSIONKEY,new String[]{sessionKeyValue});
  try {
    cmd.authenticate("String_Node_Str",params,session,null,HttpUtils.RESPONSE_TYPE_JSON,new StringBuilder(),req,resp);
  }
 catch (  ServerApiException exception) {
    assertEquals(exception.getErrorCode(),ApiErrorCode.ACCOUNT_ERROR);
  }
 finally {
    Mockito.verify(accountService,Mockito.times(1)).getUserAccountById(Mockito.anyLong());
  }
  UserAccountVO mockedUserAccount=new UserAccountVO();
  mockedUserAccount.setId(2L);
  mockedUserAccount.setAccountState(Account.State.enabled.toString());
  mockedUserAccount.setUsername("String_Node_Str");
  mockedUserAccount.setExternalEntity("String_Node_Str");
  mockedUserAccount.setDomainId(0L);
  mockedUserAccount.setSource(User.Source.UNKNOWN);
  Mockito.when(accountService.getUserAccountById(Mockito.anyLong())).thenReturn(mockedUserAccount);
  try {
    cmd.authenticate("String_Node_Str",params,session,null,HttpUtils.RESPONSE_TYPE_JSON,new StringBuilder(),req,resp);
  }
 catch (  ServerApiException exception) {
    assertEquals(exception.getErrorCode(),ApiErrorCode.ACCOUNT_ERROR);
  }
 finally {
    Mockito.verify(accountService,Mockito.times(2)).getUserAccountById(Mockito.anyLong());
  }
  mockedUserAccount.setSource(User.Source.SAML2);
  Mockito.when(accountService.getUserAccountById(Mockito.anyLong())).thenReturn(mockedUserAccount);
  Mockito.when(apiServer.verifyUser(Mockito.anyLong())).thenReturn(true);
  LoginCmdResponse loginCmdResponse=new LoginCmdResponse();
  loginCmdResponse.setUserId("String_Node_Str");
  loginCmdResponse.setDomainId("String_Node_Str");
  loginCmdResponse.setType("String_Node_Str");
  loginCmdResponse.setUsername("String_Node_Str");
  loginCmdResponse.setAccount("String_Node_Str");
  loginCmdResponse.setFirstName("String_Node_Str");
  loginCmdResponse.setLastName("String_Node_Str");
  loginCmdResponse.setSessionKey("String_Node_Str");
  Mockito.when(apiServer.loginUser(Mockito.any(HttpSession.class),Mockito.anyString(),Mockito.anyString(),Mockito.anyLong(),Mockito.anyString(),Mockito.any(InetAddress.class),Mockito.anyMap())).thenReturn(loginCmdResponse);
  try {
    cmd.authenticate("String_Node_Str",params,session,null,HttpUtils.RESPONSE_TYPE_JSON,new StringBuilder(),req,resp);
  }
 catch (  ServerApiException exception) {
    fail("String_Node_Str" + exception.getMessage());
  }
 finally {
    Mockito.verify(accountService,Mockito.times(4)).getUserAccountById(Mockito.anyLong());
    Mockito.verify(resp,Mockito.times(1)).sendRedirect(Mockito.anyString());
  }
}