AmbariAuthentication implements Authentication, UserIdAuthentication { @Override public String getName() { if (principalOverride != null) { if (principalOverride instanceof UserDetails) { return ((UserDetails) principalOverride).getUsername(); } return principalOverride.toString(); } return authentication.getName(); } AmbariAuthentication(Authentication authentication, Integer userId); @Override Collection<? extends GrantedAuthority> getAuthorities(); @Override Object getCredentials(); @Override Object getDetails(); @Override Object getPrincipal(); @Override boolean isAuthenticated(); @Override void setAuthenticated(boolean isAuthenticated); @Override String getName(); @Override boolean equals(Object o); @Override int hashCode(); @Override Integer getUserId();  }
@Test public void testGetNameNoOverride () throws Exception { Principal origPrincipal = new Principal() { @Override public String getName() { return "user1"; } }; Authentication authentication = new TestingAuthenticationToken(origPrincipal, "password"); Authentication ambariAuthentication = new AmbariAuthentication(authentication, DEFAULT_USER_ID); String name = ambariAuthentication.getName(); assertEquals("user1", name); }
@Test public void testGetName() throws Exception { Authentication authentication = new TestingAuthenticationToken("user", "password"); Authentication ambariAuthentication = new AmbariAuthentication(authentication, DEFAULT_USER_ID); String name = ambariAuthentication.getName(); assertEquals("user", name); }
@Test public void testGetNameWithLoginAlias() throws Exception { Authentication authentication = new TestingAuthenticationToken("loginAlias", "password"); expect(servletRequestAttributes.getAttribute(eq("loginAlias"), eq(RequestAttributes.SCOPE_SESSION))) .andReturn("user").atLeastOnce(); replayAll(); Authentication ambariAuthentication = new AmbariAuthentication(authentication, DEFAULT_USER_ID); String name = ambariAuthentication.getName(); verifyAll(); assertEquals("user", name); }
@Test public void testGetNameWithUserDetailsPrincipal() throws Exception { UserDetails userDetails = new User("user", "password", Collections.<GrantedAuthority>emptyList()); Authentication authentication = new TestingAuthenticationToken(userDetails, userDetails.getPassword()); Authentication ambariAuthentication = new AmbariAuthentication(authentication, DEFAULT_USER_ID); String name = ambariAuthentication.getName(); assertEquals("user", name); }
@Test public void testGetNameWithUserDetailsPrincipalWithLoginAlias() throws Exception { UserDetails userDetails = new User("loginAlias", "password", Collections.<GrantedAuthority>emptyList()); Authentication authentication = new TestingAuthenticationToken(userDetails, userDetails.getPassword()); expect(servletRequestAttributes.getAttribute(eq("loginAlias"), eq(RequestAttributes.SCOPE_SESSION))) .andReturn("user").atLeastOnce(); replayAll(); Authentication ambariAuthentication = new AmbariAuthentication(authentication, DEFAULT_USER_ID); String name = ambariAuthentication.getName(); verifyAll(); assertEquals("user", name); }