### Question:
SecurityContextServiceImpl implements SecurityContextService { @Override public Optional<UserProfile> getLoggedInUserProfile() { return getLoggedInUser().filter(this::isUserFullyAuthenticated) .map(authentication -> (Principal) authentication.getPrincipal()) .map(profileService::getUserProfile); } SecurityContextServiceImpl(final SecurityContextHolderSpringDelegate securityContextHolder, final ProfileService profileService); @Override Optional<Authentication> getLoggedInUser(); @Override boolean isUserFullyAuthenticated(); @Override Optional<UserProfile> getLoggedInUserProfile();  }### Answer:
@Test public void getLoggedInUserProfile() { final Authentication authentication = mock(Authentication.class); final String userId = "fdsgzdg"; final Principal principal = mock(Principal.class); final UserProfile userProfile = mock(UserProfile.class); when(authentication.isAuthenticated()).thenReturn(true); when(authentication.getPrincipal()).thenReturn(principal); when(principal.getName()).thenReturn(userId); when(profileService.getUserProfile(principal)).thenReturn(userProfile); when(securityContextHolder.getContext().getAuthentication()).thenReturn(authentication); final Optional<UserProfile> result = securityContextService.getLoggedInUserProfile(); assertThat(result).containsSame(userProfile); }
@Test public void getLoggedInUserProfile_noPrincipal() { final Authentication authentication = mock(Authentication.class); final String userId = "fdsgzdg"; when(authentication.isAuthenticated()).thenReturn(true); when(authentication.getPrincipal()).thenReturn(null); when(securityContextHolder.getContext().getAuthentication()).thenReturn(authentication); final Optional<UserProfile> result = securityContextService.getLoggedInUserProfile(); assertThat(result).isEmpty(); }
@Test public void getLoggedInUserProfile_noUserProfile() { final Authentication authentication = mock(Authentication.class); final String userId = "fdsgzdg"; final Principal principal = mock(Principal.class); when(authentication.isAuthenticated()).thenReturn(true); when(authentication.getPrincipal()).thenReturn(principal); when(principal.getName()).thenReturn(userId); when(profileService.getUserProfile(principal)).thenReturn(null); when(securityContextHolder.getContext().getAuthentication()).thenReturn(authentication); final Optional<UserProfile> result = securityContextService.getLoggedInUserProfile(); assertThat(result).isEmpty(); }
@Test public void getLoggedInUserProfile_noAuthentication() { when(securityContextHolder.getContext().getAuthentication()).thenReturn(null); final Optional<UserProfile> result = securityContextService.getLoggedInUserProfile(); assertThat(result).isEmpty(); }
@Test public void getLoggedInUserProfile_notAuthentciated() { final Authentication authentication = mock(Authentication.class); when(authentication.isAuthenticated()).thenReturn(false); when(securityContextHolder.getContext().getAuthentication()).thenReturn(authentication); final Optional<UserProfile> result = securityContextService.getLoggedInUserProfile(); assertThat(result).isEmpty(); }
@Test public void getLoggedInUserProfile_AnonymousAuthenticationToken() { final Authentication authentication = spy(new AnonymousAuthenticationToken("dssg", "htesn", Arrays.asList((GrantedAuthority) () -> "dhfgj", (GrantedAuthority) () -> "dhfc"))); when(authentication.isAuthenticated()).thenReturn(true); when(securityContextHolder.getContext().getAuthentication()).thenReturn(authentication); final Optional<UserProfile> result = securityContextService.getLoggedInUserProfile(); assertThat(result).isEmpty(); }