@Test public void getPrincipal_UnknownAuthenticationType_ShouldReturnNull() { SecurityContext securityContextMock = mock(SecurityContext.class); Authentication authenticationMock = mock(Authentication.class); PowerMockito.mockStatic(SecurityContextHolder.class); when(SecurityContextHolder.getContext()).thenReturn(securityContextMock); when(securityContextMock.getAuthentication()).thenReturn(authenticationMock); when(authenticationMock.getPrincipal()).thenReturn(new String("")); UserDetails principal = securityContextUtil.getPrincipal(); PowerMockito.verifyStatic(times(1)); SecurityContextHolder.getContext(); verify(securityContextMock, times(1)).getAuthentication(); verifyNoMoreInteractions(securityContextMock); verify(authenticationMock, times(1)).getPrincipal(); verifyNoMoreInteractions(authenticationMock); assertNull(principal); }
public UserDetails getPrincipal() { LOGGER.debug("Getting principal from the security context"); UserDetails principal = null; Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { Object currentPrincipal = authentication.getPrincipal(); if (currentPrincipal instanceof UserDetails) { principal = (UserDetails) currentPrincipal; } } return principal; }
SecurityContextUtil { public UserDetails getPrincipal() { LOGGER.debug("Getting principal from the security context"); UserDetails principal = null; Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { Object currentPrincipal = authentication.getPrincipal(); if (currentPrincipal instanceof UserDetails) { principal = (UserDetails) currentPrincipal; } } return principal; } }
SecurityContextUtil { public UserDetails getPrincipal() { LOGGER.debug("Getting principal from the security context"); UserDetails principal = null; Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { Object currentPrincipal = authentication.getPrincipal(); if (currentPrincipal instanceof UserDetails) { principal = (UserDetails) currentPrincipal; } } return principal; }  }
SecurityContextUtil { public UserDetails getPrincipal() { LOGGER.debug("Getting principal from the security context"); UserDetails principal = null; Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { Object currentPrincipal = authentication.getPrincipal(); if (currentPrincipal instanceof UserDetails) { principal = (UserDetails) currentPrincipal; } } return principal; }  UserDetails getPrincipal(); }
SecurityContextUtil { public UserDetails getPrincipal() { LOGGER.debug("Getting principal from the security context"); UserDetails principal = null; Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { Object currentPrincipal = authentication.getPrincipal(); if (currentPrincipal instanceof UserDetails) { principal = (UserDetails) currentPrincipal; } } return principal; }  UserDetails getPrincipal();  }