@Test
  public void deleting_and_add_of_same_mail_address_works() {
    createFullUser(IRRELEVANT);
    createUpdateUserWhereTheSameEmailIsSetToDeleteAndAdd();
    updateUser();

    assertThat(testUser.getEmails(), is(databaseUser.getEmails()));
  }
  @Test
  public void username_is_set_no_empty_string_is_thrown_probably() {
    createFullUser("ietiuuitp");
    createUpdateUserWithEmptyUserName();

    updateUser();

    assertThat(returnUser.getUserName(), is(equalTo(testUser.getUserName())));
  }
 private void assertThatOnlyNewEmailAddressIsPrimary() {
   for (Email email : returnUser.getEmails()) {
     if (email.getValue().equals("*****@*****.**")) {
       assertThat(email.isPrimary(), is(true));
     } else {
       assertThat(email.isPrimary(), is(false));
     }
   }
 }
  @Test
  public void delete_all_single_values() {
    createFullUser("desv");
    createUpdateUserWithDeleteFields();

    updateUser();

    assertNull(returnUser.getNickName());
    assertNull(returnUser.getDisplayName());
    assertNull(returnUser.getLocale());
    assertNull(returnUser.getPreferredLanguage());
    assertNull(returnUser.getProfileUrl());
    assertNull(returnUser.getTimezone());
    assertNull(returnUser.getTitle());
    assertNull(returnUser.getUserType());
    assertNull(returnUser.getName());
    assertNull(returnUser.getExternalId());
  }
  @Test
  @Ignore("write a private equals method to compare both users for value-equality")
  public void compare_returned_user_with_database_user() {
    createFullUser("dma");
    createUpdateUserWithMultiDeleteFields();

    updateUser();

    assertTrue(returnUser.equals(databaseUser));
  }
  @Test
  public void delete_all_multi_value_attributes() {
    createFullUser("dama");
    createUpdateUserWithMultiAllDeleteFields();

    updateUser();

    assertThat(returnUser.getEmails(), is(empty()));
    assertThat(returnUser.getAddresses(), is(empty()));
    assertThat(returnUser.getEntitlements(), is(empty()));
    assertThat(returnUser.getIms(), is(empty()));
    assertThat(returnUser.getPhoneNumbers(), is(empty()));
    assertThat(returnUser.getPhotos(), is(empty()));
    assertThat(returnUser.getRoles(), is(empty()));
    assertThat(returnUser.getX509Certificates(), is(empty()));
  }
  @Test
  @Ignore("Fails because of a bug in resource-server")
  public void deleting_and_add_of_same_mail_address_works_independent_of_order() {
    User user = createFullUser(IRRELEVANT);
    Email email = user.getEmails().get(0);
    UpdateUser updateUser = new UpdateUser.Builder().addEmail(email).deleteEmail(email).build();

    User updatedUser = OSIAM_CONNECTOR.updateUser(user.getId(), updateUser, accessToken);

    assertThat(updatedUser.getEmails(), is(equalTo(user.getEmails())));
  }
  @Test
  public void delete_multi_value_attributes() {
    createFullUser("dma");
    createUpdateUserWithMultiDeleteFields();

    updateUser();

    assertFalse(isValuePartOfEmailList(returnUser.getEmails(), "*****@*****.**"));
    assertFalse(isValuePartOfPhoneNumberList(returnUser.getPhoneNumbers(), "0245817964"));
    assertFalse(isValuePartOfImList(returnUser.getIms(), "ims01"));
    URI uri = null;
    try {
      uri = new URI("photo01.jpg");
    } catch (Exception e) {
    }

    assertFalse(isValuePartOfPhotoList(returnUser.getPhotos(), uri));
    assertFalse(isValuePartOfRoleList(returnUser.getRoles(), "role01"));
    assertFalse(isValuePartOfAddressList(returnUser.getAddresses(), "formated address 01"));
    assertFalse(isValuePartOfEntitlementList(returnUser.getEntitlements(), "right2"));
    assertFalse(
        isValuePartOfX509CertificateList(returnUser.getX509Certificates(), "certificate01"));
  }
  @Test
  public void REGT_015_delete_multi_value_attributes_twice() {
    createFullUser("dma");
    createUpdateUserWithMultiDeleteFields();

    updateUser();
    updateUser();

    assertTrue(isValuePartOfEntitlementList(testUser.getEntitlements(), "right2"));
    assertFalse(isValuePartOfEntitlementList(returnUser.getEntitlements(), "right2"));
    assertFalse(isValuePartOfEntitlementList(databaseUser.getEntitlements(), "right2"));
    assertTrue(isValuePartOfX509CertificateList(testUser.getX509Certificates(), "certificate01"));
    assertFalse(
        isValuePartOfX509CertificateList(returnUser.getX509Certificates(), "certificate01"));
    assertFalse(
        isValuePartOfX509CertificateList(databaseUser.getX509Certificates(), "certificate01"));
  }
 private void updateUser() {
   returnUser = OSIAM_CONNECTOR.updateUser(idExistingUser, updateUser, accessToken);
   // get user again from database to be able to compare with return object
   databaseUser = OSIAM_CONNECTOR.getUser(returnUser.getId(), accessToken);
 }
 private void createUpdateUserWhereTheSameEmailIsSetToDeleteAndAdd() {
   Email email = testUser.getEmails().get(0);
   updateUser = new UpdateUser.Builder().deleteEmail(email).addEmail(email).build();
 }
  private User createFullUser(String userName) {
    User.Builder userBuilder = new User.Builder(userName);

    Email email01 =
        new Email.Builder()
            .setValue("*****@*****.**")
            .setType(Email.Type.WORK)
            .setPrimary(true)
            .build();
    Email email02 =
        new Email.Builder().setValue("*****@*****.**").setType(Email.Type.WORK).build();
    List<Email> emails = new ArrayList<>();
    emails.add(email01);
    emails.add(email02);

    PhoneNumber phoneNumber01 =
        new PhoneNumber.Builder()
            .setValue("+497845/1157")
            .setType(PhoneNumber.Type.WORK)
            .setPrimary(true)
            .build();
    PhoneNumber phoneNumber02 =
        new PhoneNumber.Builder().setValue("0245817964").setType(PhoneNumber.Type.WORK).build();
    List<PhoneNumber> phoneNumbers = new ArrayList<>();
    phoneNumbers.add(phoneNumber01);
    phoneNumbers.add(phoneNumber02);

    Address simpleAddress01 =
        new Address.Builder()
            .setCountry("de")
            .setFormatted("formated address 01")
            .setLocality("Berlin")
            .setPostalCode("111111")
            .build();
    Address simpleAddress02 =
        new Address.Builder()
            .setCountry("en")
            .setFormatted("address formated 02")
            .setLocality("New York")
            .setPostalCode("123456")
            .build();
    List<Address> addresses = new ArrayList<>();
    addresses.add(simpleAddress01);
    addresses.add(simpleAddress02);

    Entitlement entitlement01 = new Entitlement.Builder().setValue("right1").build();
    Entitlement entitlement02 = new Entitlement.Builder().setValue("right2").build();
    List<Entitlement> entitlements = new ArrayList<>();
    entitlements.add(entitlement01);
    entitlements.add(entitlement02);

    Im ims01 = new Im.Builder().setValue("ims01").setType(Im.Type.SKYPE).build();
    Im ims02 = new Im.Builder().setValue("ims02").build();
    List<Im> ims = new ArrayList<>();
    ims.add(ims01);
    ims.add(ims02);

    URI uri1 = null;
    URI uri2 = null;
    try {
      uri1 = new URI("photo01.jpg");
      uri2 = new URI("photo02.jpg");
    } catch (Exception e) {
    }

    Photo photo01 = new Photo.Builder().setValue(uri1).setType(Photo.Type.THUMBNAIL).build();

    Photo photo02 = new Photo.Builder().setValue(uri2).build();
    List<Photo> photos = new ArrayList<>();
    photos.add(photo01);
    photos.add(photo02);

    Role role01 = new Role.Builder().setValue("role01").build();
    Role role02 = new Role.Builder().setValue("role02").build();
    List<Role> roles = new ArrayList<>();
    roles.add(role01);
    roles.add(role02);

    X509Certificate certificate01 = new X509Certificate.Builder().setValue("certificate01").build();
    X509Certificate certificate02 = new X509Certificate.Builder().setValue("certificate02").build();
    List<X509Certificate> certificates = new ArrayList<>();
    certificates.add(certificate01);
    certificates.add(certificate02);

    Name name =
        new Name.Builder()
            .setFamilyName("familiyName")
            .setFormatted("formatted Name")
            .setGivenName("givenName")
            .build();

    userBuilder
        .setNickName("irgendwas")
        .addEmails(emails)
        .addPhoneNumbers(phoneNumbers)
        .setActive(false)
        .setDisplayName("irgendwas")
        .setLocale("de")
        .setPassword("geheim")
        .setPreferredLanguage("de")
        .setProfileUrl("irgendwas")
        .setTimezone("irgendwas")
        .setTitle("irgendwas")
        .setUserType("irgendwas")
        .addAddresses(addresses)
        .addIms(ims)
        .addPhotos(photos)
        .addRoles(roles)
        .setName(name)
        .addX509Certificates(certificates)
        .addEntitlements(entitlements)
        .setExternalId("irgendwas");
    User newUser = userBuilder.build();

    testUser = OSIAM_CONNECTOR.createUser(newUser, accessToken);
    idExistingUser = testUser.getId();

    return testUser;
  }
  @Test
  public void change_one_field_and_other_attributes_are_the_same() {
    createFullUser("cnaoaats");
    createUpdateUserWithJustOtherNickname();

    updateUser();

    assertNotEquals(testUser.getNickName(), returnUser.getNickName());
    assertEquals(testUser.isActive(), returnUser.isActive());
    assertEquals(testUser.getDisplayName(), returnUser.getDisplayName());
    assertEquals(testUser.getExternalId(), returnUser.getExternalId());
    assertEquals(testUser.getLocale(), returnUser.getLocale());
    assertEquals(testUser.getPreferredLanguage(), returnUser.getPreferredLanguage());
    assertEquals(testUser.getProfileUrl(), returnUser.getProfileUrl());
    assertEquals(testUser.getTimezone(), returnUser.getTimezone());
    assertEquals(testUser.getTitle(), returnUser.getTitle());
    assertEquals(testUser.getUserType(), returnUser.getUserType());
    assertEquals(testUser.getName().getFamilyName(), returnUser.getName().getFamilyName());
  }
  @Test
  public void update_all_single_values() {
    createFullUser("uasv");
    createUpdateUserWithUpdateFields();

    updateUser();

    assertEquals("UserName", returnUser.getUserName());
    assertEquals("NickName", returnUser.getNickName());
    assertNotEquals(testUser.isActive(), returnUser.isActive());
    assertEquals("DisplayName", returnUser.getDisplayName());
    assertEquals("ExternalId", returnUser.getExternalId());
    assertEquals("Locale", returnUser.getLocale());
    assertEquals("PreferredLanguage", returnUser.getPreferredLanguage());
    assertEquals("ProfileUrl", returnUser.getProfileUrl());
    assertEquals("Timezone", returnUser.getTimezone());
    assertEquals("Title", returnUser.getTitle());
    assertEquals("UserType", returnUser.getUserType());
    assertEquals("FamilyName", returnUser.getName().getFamilyName());
    assertEquals("ExternalId", returnUser.getExternalId());
  }
  @Test
  public void add_multi_value_attributes() {
    createFullUser("ama");
    createUpdateUserWithMultiAddFields();

    updateUser();

    assertEquals(testUser.getPhoneNumbers().size() + 1, returnUser.getPhoneNumbers().size());
    assertTrue(isValuePartOfPhoneNumberList(returnUser.getPhoneNumbers(), "99999999991"));
    assertEquals(testUser.getEmails().size() + 1, returnUser.getEmails().size());
    assertTrue(isValuePartOfEmailList(returnUser.getEmails(), "*****@*****.**"));
    assertEquals(testUser.getAddresses().size() + 1, returnUser.getAddresses().size());
    getAddress(returnUser.getAddresses(), "new Address");
    assertEquals(testUser.getEntitlements().size() + 1, returnUser.getEntitlements().size());
    assertTrue(isValuePartOfEntitlementList(returnUser.getEntitlements(), "right3"));
    assertEquals(testUser.getIms().size() + 1, returnUser.getIms().size());
    assertTrue(isValuePartOfImList(returnUser.getIms(), "ims03"));
    assertEquals(testUser.getPhotos().size() + 1, returnUser.getPhotos().size());
    URI uri = null;
    try {
      uri = new URI("photo03.jpg");
    } catch (Exception e) {
    }

    assertTrue(isValuePartOfPhotoList(returnUser.getPhotos(), uri));
    assertEquals(testUser.getRoles().size() + 1, returnUser.getRoles().size());
    assertTrue(isValuePartOfRoleList(returnUser.getRoles(), "role03"));
    assertEquals(
        testUser.getX509Certificates().size() + 1, returnUser.getX509Certificates().size());
    assertTrue(isValuePartOfX509CertificateList(returnUser.getX509Certificates(), "certificate03"));
  }