@Test
  public void getByEmailTest() {
    User user = getUser(UserRole.ROLE_ORG_USER, UserRole.ROLE_GROUP_USER);

    user = userService.getByEmail(user.getEmail());
    assertNotNull(user);
  }
  @Test
  public void getApplicationVersionsByUserTest() {
    User user = getUser(UserRole.ROLE_ORG_ADMIN, UserRole.ROLE_GROUP_USER);
    user = userService.getByEmail(user.getEmail());

    List<ApplicationVersion> applicationVersions = userService.getApplicationVersions(user);
    assertTrue(applicationVersions.size() == 2);
  }
  @Test
  public void getOrganizationsTest() {
    User user = getUser(UserRole.ROLE_ORG_ADMIN, UserRole.ROLE_GROUP_USER);
    user = userService.getByEmail(user.getEmail());

    List<Organization> organizations = userService.getOrganizations(user);
    assertTrue(organizations.size() == 1);
  }
  @Test
  public void getGroupsForOrgAdminTest() {
    User user = getUser(UserRole.ROLE_ORG_ADMIN, UserRole.ROLE_GROUP_USER);
    user = userService.getByEmail(user.getEmail());

    List<Group> groups = userService.getGroups(user);
    assertTrue(groups.size() == 1);
  }
 @Test
 public void getApplicationsByUserApplicationTypeAppStates() {
   User user = getUser(UserRole.ROLE_ORG_USER, UserRole.ROLE_GROUP_USER);
   user = userService.getByEmail(user.getEmail());
   List<Application> applications =
       userService.getApplicationsForUser(user, ApplicationType.ANDROID, AppState.GROUP_PUBLISH);
   assertTrue(applications.size() == 1);
 }
  @Test
  public void updateTest() {
    User user = getUser(UserRole.ROLE_ORG_USER, UserRole.ROLE_GROUP_USER);
    user.setLastName("Tester");

    user = userService.getByEmail(user.getEmail());
    userService.update(user);
    assertEquals(user.getLastName(), "Tester");
  }
 @Test
 public void activationTest() {
   User user = getUser(UserRole.ROLE_ORG_USER, UserRole.ROLE_GROUP_USER);
   user = userService.getByEmail(user.getEmail());
   assertFalse(user.isActivated());
   boolean activated = userService.activate(user.getId(), user.getActivationCode());
   assertTrue(activated);
   assertTrue(user.isActivated());
 }
 @Test
 public void getApplicationsByUserApplicationTypeCategoryAppStatesTest() {
   User user = getUser(UserRole.ROLE_ORG_USER, UserRole.ROLE_GROUP_USER);
   user = userService.getByEmail(user.getEmail());
   Organization organization = organizationService.getAll().get(0);
   Category category = organization.getCategories().get(0);
   List<Application> applications =
       userService.getApplicationsForUser(
           user, ApplicationType.ANDROID, category.getId(), AppState.GROUP_PUBLISH);
   assertTrue(applications.size() == 1);
 }
  @Test
  public void getApplicationVersionByUserApplicationAppState() {
    User user = getUser(UserRole.ROLE_ORG_ADMIN, UserRole.ROLE_GROUP_USER);
    user = userService.getByEmail(user.getEmail());

    List<ApplicationVersion> applicationVersions = userService.getApplicationVersions(user);
    for (ApplicationVersion applicationVersion : applicationVersions) {
      applicationVersions =
          userService.getApplicationVersions(
              user,
              applicationVersion.getApplication().getId(),
              SortOrder.ASCENDING,
              AppState.ORGANIZATION_PUBLISH,
              AppState.GROUP_PUBLISH);
      assertTrue(applicationVersions.size() == 1);
    }
  }
  protected User getUser() {
    if (user == null) {
      MessageDigestPasswordEncoder passwordEncoder =
          new MessageDigestPasswordEncoder("SHA-256", true);
      user = new User();
      user.setEmail("*****@*****.**");
      user.setFirstName("John");
      user.setLastName("Doe");
      user.setUsername("*****@*****.**");
      user.setPassword(passwordEncoder.encodePassword("1234", user.getEmail()));
      user.setActivationCode(UUID.randomUUID().toString());
      user.getUuid();

      userService.add(user);
    }

    return user;
  }
  private void addUserToInvitedDomains(User user) {

    String email = user.getEmail();

    List<Invitation> invitations = invitationService.getByEmail(email);
    for (Invitation invitation : invitations) {
      if (DomainType.GROUP.equals(invitation.getDomain().getDomainType())) {
        userService.addUserToGroup(
            user,
            invitation.getDomain().getId(),
            UserRole.valueOf(invitation.getRole().getAuthority()));
      } else if (DomainType.ORGANIZATION.equals(invitation.getDomain().getDomainType())) {
        userService.addUserToOrganization(
            user,
            invitation.getDomain().getId(),
            UserRole.valueOf(invitation.getRole().getAuthority()));
      }
    }

    for (Invitation invitation : invitations) {
      invitationService.delete(invitation.getId());
    }
  }