### Question:
ContextsStorageSecurityAspect { protected String getCurrentUserQualifiedName(){ final User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); final String tenantId = user.getTenantId(); return user.getUsername() + (tenantId != null ? "|" + tenantId : ""); }  @Around("execution(* ContextDataStorage.save(com.jaspersoft.jasperserver.remote.connection.storage.ContextDataPair))") Object saveOwnedContext(ProceedingJoinPoint joinPoint); @Around("execution(* ContextDataStorage.update(java.util.UUID, com.jaspersoft.jasperserver.remote.connection.storage.ContextDataPair))") void updateOwnedContext(ProceedingJoinPoint joinPoint); @Before("execution(* ContextDataStorage.update(java.util.UUID, com.jaspersoft.jasperserver.remote.connection.storage.ContextDataPair)) ||" + "execution(* ContextDataStorage.get(java.util.UUID)) ||" + "execution(* ContextDataStorage.delete(java.util.UUID))" ) void checkOwner(JoinPoint joinPoint);  }### Answer:
@Test public void getCurrentUserQualifiedName_withTenant_usernamePipeTenantId(){ final SecurityContext initialContext = SecurityContextHolder.getContext(); final SecurityContextImpl actualContext = new SecurityContextImpl(); final Authentication authenticationMock = mock(Authentication.class); final UserImpl currentUser = new UserImpl(); currentUser.setUsername("someUser"); currentUser.setTenantId("someTenant"); when(authenticationMock.getPrincipal()).thenReturn(currentUser); actualContext.setAuthentication(authenticationMock); SecurityContextHolder.setContext(actualContext); when(aspect.getCurrentUserQualifiedName()).thenCallRealMethod(); final String result = aspect.getCurrentUserQualifiedName(); assertEquals(result, "someUser|someTenant"); SecurityContextHolder.setContext(initialContext); }
@Test public void getCurrentUserQualifiedName_rootOrgUser_usernameOnly(){ final SecurityContext initialContext = SecurityContextHolder.getContext(); final SecurityContextImpl actualContext = new SecurityContextImpl(); final Authentication authenticationMock = mock(Authentication.class); final UserImpl currentUser = new UserImpl(); currentUser.setUsername("someRootOrgUser"); when(authenticationMock.getPrincipal()).thenReturn(currentUser); actualContext.setAuthentication(authenticationMock); SecurityContextHolder.setContext(actualContext); when(aspect.getCurrentUserQualifiedName()).thenCallRealMethod(); final String result = aspect.getCurrentUserQualifiedName(); assertEquals(result, "someRootOrgUser"); SecurityContextHolder.setContext(initialContext); }