private ScimUser getScimUser() {
   String email = "joe@" + generator.generate().toLowerCase() + ".com";
   ScimUser user = new ScimUser();
   user.setUserName(email);
   user.setName(new ScimUser.Name("Joe", "User"));
   user.addEmail(email);
   return user;
 }
 private ResponseEntity<ScimUser> createUser(
     String username, String firstName, String lastName, String email) {
   ScimUser user = new ScimUser();
   user.setUserName(username);
   user.setName(new ScimUser.Name(firstName, lastName));
   user.addEmail(email);
   user.setPassword("pas5Word");
   user.setVerified(true);
   return client.postForEntity(serverRunning.getUrl(userEndpoint), user, ScimUser.class);
 }
  @Test
  public void testUserSelfAccess_Get_and_Post() throws Exception {
    ScimUser user = getScimUser();
    user.setPassword("secret");
    user = createUser(user, scimReadWriteToken, IdentityZone.getUaa().getSubdomain());

    String selfToken =
        testClient.getUserOAuthAccessToken("cf", "", user.getUserName(), "secret", "");

    user.setName(new ScimUser.Name("Given1", "Family1"));
    user = updateUser(selfToken, HttpStatus.OK.value(), user);

    user = getAndReturnUser(HttpStatus.OK.value(), user, selfToken);
  }
  protected ScimUser updateUser(String token, int status) throws Exception {
    ScimUserProvisioning usersRepository =
        getWebApplicationContext().getBean(ScimUserProvisioning.class);
    String email = "otheruser@" + generator.generate().toLowerCase() + ".com";
    ScimUser user = new ScimUser(null, email, "Other", "User");
    user.addEmail(email);
    user = usersRepository.createUser(user, "pas5Word");
    if (status == HttpStatus.BAD_REQUEST.value()) {
      user.setUserName(null);
    } else {
      String username2 = "ou" + generator.generate().toLowerCase();
      user.setUserName(username2);
    }

    user.setName(new ScimUser.Name("Joe", "Smith"));

    return updateUser(token, status, user);
  }
 @Override
 public ScimUser mapRow(ResultSet rs, int rowNum) throws SQLException {
   String id = rs.getString(1);
   int version = rs.getInt(2);
   Date created = rs.getTimestamp(3);
   Date lastModified = rs.getTimestamp(4);
   String userName = rs.getString(5);
   String email = rs.getString(6);
   String givenName = rs.getString(7);
   String familyName = rs.getString(8);
   boolean active = rs.getBoolean(9);
   String phoneNumber = rs.getString(10);
   boolean verified = rs.getBoolean(11);
   String origin = rs.getString(12);
   String externalId = rs.getString(13);
   String zoneId = rs.getString(14);
   String salt = rs.getString(15);
   Date passwordLastModified = rs.getTimestamp(16);
   ScimUser user = new ScimUser();
   user.setId(id);
   ScimMeta meta = new ScimMeta();
   meta.setVersion(version);
   meta.setCreated(created);
   meta.setLastModified(lastModified);
   user.setMeta(meta);
   user.setUserName(userName);
   user.addEmail(email);
   if (phoneNumber != null) {
     user.addPhoneNumber(phoneNumber);
   }
   Name name = new Name();
   name.setGivenName(givenName);
   name.setFamilyName(familyName);
   user.setName(name);
   user.setActive(active);
   user.setVerified(verified);
   user.setOrigin(origin);
   user.setExternalId(externalId);
   user.setZoneId(zoneId);
   user.setSalt(salt);
   user.setPasswordLastModified(passwordLastModified);
   return user;
 }
  @Test
  public void testUpdateUserInOtherZoneWithUaaAdminToken() throws Exception {
    IdentityZone identityZone = getIdentityZone();
    ScimUser user = setUpScimUser(identityZone);
    user.setName(new ScimUser.Name("changed", "name"));

    getMockMvc()
        .perform(
            put("/Users/" + user.getId())
                .header("Authorization", "Bearer " + uaaAdminToken)
                .header(IdentityZoneSwitchingFilter.HEADER, identityZone.getId())
                .header("If-Match", "\"" + user.getVersion() + "\"")
                .contentType(APPLICATION_JSON)
                .content(JsonUtils.writeValueAsBytes(user)))
        .andExpect(status().isOk())
        .andExpect(header().string("ETag", "\"1\""))
        .andExpect(jsonPath("$.userName").value(user.getUserName()))
        .andExpect(jsonPath("$.emails[0].value").value(user.getPrimaryEmail()))
        .andExpect(jsonPath("$.name.givenName").value(user.getGivenName()))
        .andExpect(jsonPath("$.name.familyName").value(user.getFamilyName()));
  }