SecurityContextUtil implements UserUtil { public DuracloudUserDetails getCurrentUserDetails() throws NoUserLoggedInException { SecurityContext context = SecurityContextHolder.getContext(); Authentication auth = context.getAuthentication(); if (null == auth) { log.debug("no user-auth found."); throw new NoUserLoggedInException(); } Object obj = auth.getPrincipal(); if (obj instanceof DuracloudUserDetails) { return (DuracloudUserDetails) obj; } else { log.debug("no user logged in: {}", obj.getClass().getName()); throw new NoUserLoggedInException(); } }  @Override String getCurrentUsername(); Credential getCurrentUser(); DuracloudUserDetails getCurrentUserDetails();  }
@Test public void testGetCurrentUserDetails() throws NoUserLoggedInException { createCurrentUserMock(); DuracloudUserDetails userDetails = util.getCurrentUserDetails(); Assert.assertNotNull(userDetails); Collection<GrantedAuthority> auths = userDetails.getAuthorities(); Assert.assertNotNull(auths); List<GrantedAuthority> authsList = new ArrayList<>(auths); Assert.assertEquals(authorities.size(), authsList.size()); for (GrantedAuthority authority : authorities) { Assert.assertTrue(authsList.contains(authority)); } Assert.assertEquals(groups, userDetails.getGroups()); Assert.assertEquals(username, userDetails.getUsername()); Assert.assertEquals(email, userDetails.getEmail()); Assert.assertEquals(ipLimits, userDetails.getIpLimits()); }
@Test public void testGetCurrentUserDetailsError() throws Exception { replayMocks(); SecurityContextHolder.clearContext(); try { util.getCurrentUserDetails(); Assert.fail("exception expected."); } catch (NoUserLoggedInException e) { Assert.assertNotNull(e); } }