UserAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String userName = (String) authentication.getPrincipal(); String passwd = (String) authentication.getCredentials(); try { IAuthenticationService userAuthenticationService = new UserAuthenticationService(new VCAuthenticationAdapter( userName, passwd)); userAuthenticationService.validate(); UserDetails user = userService.loadUserByUsername(authentication.getName()); UserAuthenticationToken accountAuthenticationToken = new UserAuthenticationToken(user.getAuthorities()); return accountAuthenticationToken; } catch (AuthenticationServiceException serviceException) { throw serviceException; } catch (UsernameNotFoundException userNotfoundException) { throw userNotfoundException; } catch (Exception e) { if (e instanceof ConnectionException) { String errorMsg = "vCenter connect failed: " + e.getMessage(); logger.error(errorMsg); throw new VCConnectException(errorMsg, e); } else { logger.error("Authentication error: " + e.getMessage()); throw new BadCredentialsException(e.getMessage()); } } }  @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication); UserService getUserService(); void setUserService(UserService userService);  }
@Test public void testAuthenticate() throws Exception { Authentication authentication = new MockUp<Authentication>() { @Mock Object getPrincipal() { return "serengeti"; } @Mock Object getCredentials() { return "password"; } @Mock public String getName() { return "serengeti"; } }.getMockInstance(); new NonStrictExpectations(){ @SuppressWarnings("unused") Configuration configuration; { Configuration.getString("vim.host"); returns("192.168.0.1"); }; { Configuration.getString("vim.thumbprint"); returns("28:37:dc:69:96:43:77:a8:33:0c:5c:64:73:ae:50:2c:62:fb:c0:09"); }; { Configuration.getInt("vim.port", 110); returns(110); } }; new MockUp<AuthenticateVcUser>() { @Mock public void authenticateUser(String name, String password) throws Exception { return; } }; Users users = new Users(); User user = new User(); user.setName("serengeti"); users.setUsers(Arrays.asList(user)); String confPath = System.getProperties().get("serengeti.home.dir") + File.separator + "conf"; new File(confPath).mkdir(); String userXmlPath = confPath + File.separator + UsersFile; File usrXmlFile = new File(userXmlPath); TestFileUtils.createXMLFile(users, usrXmlFile); UserAuthenticationProvider provider = new UserAuthenticationProvider(); provider.setUserService(new UserService()); provider.authenticate(authentication); TestFileUtils.deleteXMLFile(usrXmlFile); }