@Override
    public UserDetails loadUserByUsername(String userId)
            throws UsernameNotFoundException {
        User user = null;
        try {
            user = this.identityService.createUserQuery()
                    .userId(userId)
                    .singleResult();
        } catch (FlowableException ex) {
            // don't care
        }

        if (null == user) {
            throw new UsernameNotFoundException(
                    String.format("user (%s) could not be found", userId));
        }

        return createFlowableUser(user);
    }
@Test
    public void testLoadingKnownUserWithAllPrivileges() {
        UserDetails kermit = userDetailsService.loadUserByUsername("kermit");

        assertThat(kermit).isNotNull();
        assertThat(kermit.isCredentialsNonExpired()).as("credentialsNonExpired").isTrue();
        assertThat(kermit.isAccountNonLocked()).as("accountNonLocked").isTrue();
        assertThat(kermit.isAccountNonExpired()).as("accountNonExpired").isTrue();
        assertThat(kermit.isEnabled()).as("enabled").isTrue();
        assertThat(kermit.getUsername()).as("username").isEqualTo("kermit");
        assertThat(kermit.getPassword()).as("password").isEqualTo("kermit");
        assertThat(kermit.getAuthorities())
            .extracting(GrantedAuthority::getAuthority)
            .as("granted authorities")
            .containsExactly(
                "access admin application",
                "access modeler application",
                "start processes"
            );

        assertThat(kermit).isInstanceOf(FlowableUserDetails.class);

        FlowableUserDetails kermitFlowable = (FlowableUserDetails) kermit;

        User user = kermitFlowable.getUser();
        assertThat(user.getId()).isEqualTo("kermit");
        assertThat(user.getFirstName()).isEqualTo("Kermit");
        assertThat(user.getLastName()).isEqualTo("the Frog");
        assertThat(user.getDisplayName()).isEqualTo("Kermit the Frog");
        assertThat(user.getEmail()).isEqualTo("kermit@muppetshow.com");
        assertThat(user.getPassword()).isEqualTo("kermit");

        user.setId("test");
        user.setFirstName("test");
        user.setLastName("test");
        user.setDisplayName("test");
        user.setEmail("test");

        assertThat(user.getId()).isEqualTo("kermit");
        assertThat(user.getFirstName()).isEqualTo("Kermit");
        assertThat(user.getLastName()).isEqualTo("the Frog");
        assertThat(user.getDisplayName()).isEqualTo("Kermit the Frog");
        assertThat(user.getEmail()).isEqualTo("kermit@muppetshow.com");

        assertThat(kermitFlowable.getGroups())
            .extracting(Group::getId, Group::getName, Group::getType)
            .as("Groups")
            .containsExactlyInAnyOrder(
                tuple("admins", "Admins", "user"),
                tuple("sales", "Sales", "user"),
                tuple("engineering", "Engineering", "tech")
            );

        kermitFlowable.getGroups().forEach(group -> {
            group.setId("test");
            group.setType("test");
            group.setName("test");
        });

        assertThat(kermitFlowable.getGroups())
            .extracting(Group::getId, Group::getName, Group::getType)
            .as("Groups")
            .containsExactlyInAnyOrder(
                tuple("admins", "Admins", "user"),
                tuple("sales", "Sales", "user"),
                tuple("engineering", "Engineering", "tech")
            );

        assertThat(kermit).isInstanceOf(CredentialsContainer.class);
        CredentialsContainer container = (CredentialsContainer) kermit;
        container.eraseCredentials();
        assertThat(kermit.getPassword()).as("Password after erase").isNull();
        assertThat(kermitFlowable.getUser().getPassword()).as("User password after erase").isNull();
    }