AmbariAuthentication implements Authentication, UserIdAuthentication { @Override public Object getPrincipal() { if (principalOverride != null) { return principalOverride; } return authentication.getPrincipal(); } 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 testGetPrincipalNoOverride() throws Exception { Principal origPrincipal = new Principal() { @Override public String getName() { return "user"; } }; Authentication authentication = new TestingAuthenticationToken(origPrincipal, "password"); Authentication ambariAuthentication = new AmbariAuthentication(authentication, DEFAULT_USER_ID); Object principal = ambariAuthentication.getPrincipal(); assertSame(origPrincipal, principal); }
@Test public void testGetPrincipal() throws Exception { Authentication authentication = new TestingAuthenticationToken("user", "password"); Authentication ambariAuthentication = new AmbariAuthentication(authentication, DEFAULT_USER_ID); Object principal = ambariAuthentication.getPrincipal(); assertEquals("user", principal); }
@Test public void testGetPrincipalWithLoginAlias() 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); verifyAll(); Object principal = ambariAuthentication.getPrincipal(); assertEquals("user", principal); }
@Test public void testGetUserDetailPrincipal() 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); Object principal = ambariAuthentication.getPrincipal(); assertEquals(userDetails, principal); }
@Test public void testGetUserDetailPrincipalWithLoginAlias() 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); Object principal = ambariAuthentication.getPrincipal(); verify(); UserDetails expectedUserDetails = new User("user", "password", Collections.<GrantedAuthority>emptyList()); assertEquals(expectedUserDetails, principal); }