AccountService { public Map<String, Object> login(String username, String password) { logger.debug("login in user: " + username); Map<String, Object> loginResponse = null; Account account = accounts.findByUseridAndPasswd(username, password); if (account != null) { logger.debug("Found Account for user: " + username); account.setAuthtoken(UUID.randomUUID().toString()); account.setLogincount(account.getLogincount() + 1); account.setLastlogin(new Date()); account = accounts.save(account); loginResponse = new HashMap<String, Object>(); loginResponse.put("authToken", account.getAuthtoken()); loginResponse.put("accountid", account.getId()); logger.info("AccountService.login success for " + username + " username::token=" + loginResponse.get("authToken")); } else { logger.warn("AccountService.login failed to find username=" + username + " password=" + password); throw new AuthenticationException("Login failed for user: " + username); } return loginResponse; }  Account findAccount(Integer id); Account findAccount(String id); @Cacheable(value = "authorizationCache") Account findAccountprofileByAuthtoken(String token); Integer saveAccount(Account accountRequest); Map<String, Object> login(String username, String password); Account logout(String userId);  }
@Test public void testLogin() { Account acc = ServiceTestConfiguration.account(); when(repo.findByUseridAndPasswd(ServiceTestConfiguration.USER_ID, ServiceTestConfiguration.PASSWORD)).thenReturn(acc); when(repo.save(isA(Account.class))).thenReturn(acc); Map<String,Object> result = service.login(ServiceTestConfiguration.USER_ID, ServiceTestConfiguration.PASSWORD); assertEquals(result.get("accountid"),ServiceTestConfiguration.PROFILE_ID); assertNotNull(result.get("authToken")); }
@Test(expected=AuthenticationException.class) public void testLoginNull() { Account acc = ServiceTestConfiguration.account(); when(repo.findByUseridAndPasswd(ServiceTestConfiguration.USER_ID, ServiceTestConfiguration.PASSWORD)).thenReturn(null); service.login(ServiceTestConfiguration.USER_ID, ServiceTestConfiguration.PASSWORD); }