public static <T> T runAs(String username, Callable<T> callable) {
        final Subject subject = new Subject.Builder()
                .principals(new SimplePrincipalCollection(username, "runAs-context"))
                .authenticated(true)
                .sessionCreationEnabled(false)
                .buildSubject();

        return subject.execute(callable);
    }
@Test
    void runAs() {
        // Simulate what we do in the DefaultSecurityManagerProvider
        DefaultSecurityManager sm = new DefaultSecurityManager();
        SecurityUtils.setSecurityManager(sm);
        final DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        final DefaultSessionStorageEvaluator sessionStorageEvaluator = new DefaultSessionStorageEvaluator() {
            @Override
            public boolean isSessionStorageEnabled(Subject subject) {
                // save to session if we already have a session. do not create on just for saving the subject
                return subject.getSession(false) != null;
            }
        };
        sessionStorageEvaluator.setSessionStorageEnabled(false);
        subjectDAO.setSessionStorageEvaluator(sessionStorageEvaluator);
        sm.setSubjectDAO(subjectDAO);

        final User user = new UserImpl(mock(PasswordAlgorithmFactory.class), mock(Permissions.class), mock(ClusterConfigService.class), ImmutableMap.of());
        when(userService.load(anyString())).thenReturn(user);
        when(userService.loadById(anyString())).thenReturn(user);

        final String USERID = "123456";
        UserContext.<Void>runAs(USERID, () -> {

            final UserContext userContext = new UserContext.Factory(userService).create();
            assertThat(userContext.getUserId()).isEqualTo(USERID);
            assertThat(userContext.getUser()).isEqualTo(user);

            return null;
        });
    }