@Test
 public void addUsersWithSameUsername() throws Exception {
   String origin = "testOrigin";
   String email = "*****@*****.**";
   String firstName = "FirstName";
   String lastName = "LastName";
   String password = "";
   String externalId = null;
   String userId = new RandomValueStringGenerator().generate();
   String username = new RandomValueStringGenerator().generate();
   UaaUser user =
       getUaaUser(
           new String[0],
           origin,
           email,
           firstName,
           lastName,
           password,
           externalId,
           userId,
           username);
   ScimUserBootstrap bootstrap = new ScimUserBootstrap(db, gdb, mdb, Arrays.asList(user));
   bootstrap.afterPropertiesSet();
   user = user.modifySource("newOrigin", "");
   bootstrap.addUser(user);
   assertEquals(2, db.retrieveAll().size());
 }
 @Test
 public void canAddUsers() throws Exception {
   UaaUser joe = new UaaUser("joe", "password", "*****@*****.**", "Joe", "User");
   UaaUser mabel = new UaaUser("mabel", "password", "*****@*****.**", "Mabel", "User");
   ScimUserBootstrap bootstrap = new ScimUserBootstrap(db, gdb, mdb, Arrays.asList(joe, mabel));
   bootstrap.afterPropertiesSet();
   Collection<ScimUser> users = db.retrieveAll();
   assertEquals(2, users.size());
 }
  @Test
  public void canAddNonExistentGroupThroughEvent() throws Exception {
    String[] externalAuthorities = new String[] {"extTest1", "extTest2", "extTest3"};
    String[] userAuthorities = new String[] {"usrTest1", "usrTest2", "usrTest3"};
    String origin = "testOrigin";
    String email = "*****@*****.**";
    String firstName = "FirstName";
    String lastName = "LastName";
    String password = "";
    String externalId = null;
    String userId = new RandomValueStringGenerator().generate();
    String username = new RandomValueStringGenerator().generate();
    UaaUser user =
        getUaaUser(
            userAuthorities,
            origin,
            email,
            firstName,
            lastName,
            password,
            externalId,
            userId,
            username);
    ScimUserBootstrap bootstrap = new ScimUserBootstrap(db, gdb, mdb, Arrays.asList(user));
    bootstrap.afterPropertiesSet();

    List<ScimUser> users =
        db.query("userName eq \"" + username + "\" and origin eq \"" + origin + "\"");
    assertEquals(1, users.size());
    userId = users.get(0).getId();
    user =
        getUaaUser(
            userAuthorities,
            origin,
            email,
            firstName,
            lastName,
            password,
            externalId,
            userId,
            username);
    bootstrap.onApplicationEvent(
        new ExternalGroupAuthorizationEvent(user, getAuthorities(externalAuthorities)));

    users = db.query("userName eq \"" + username + "\" and origin eq \"" + origin + "\"");
    assertEquals(1, users.size());
    ScimUser created = users.get(0);
    Set<ScimGroup> groups = mdb.getGroupsWithMember(created.getId(), true);
    String[] expected = merge(externalAuthorities, userAuthorities);
    String[] actual = getGroupNames(groups);
    assertThat(actual, IsArrayContainingInAnyOrder.arrayContainingInAnyOrder(expected));
  }
 @Test
 public void failedAttemptToUpdateUsersNotFatal() throws Exception {
   UaaUser joe = new UaaUser("joe", "password", "*****@*****.**", "Joe", "User");
   ScimUserBootstrap bootstrap = new ScimUserBootstrap(db, gdb, mdb, Arrays.asList(joe));
   bootstrap.afterPropertiesSet();
   joe = new UaaUser("joe", "new", "*****@*****.**", "Joe", "Bloggs");
   bootstrap = new ScimUserBootstrap(db, gdb, mdb, Arrays.asList(joe));
   bootstrap.setOverride(false);
   bootstrap.afterPropertiesSet();
   Collection<ScimUser> users = db.retrieveAll();
   assertEquals(1, users.size());
   assertEquals("User", users.iterator().next().getFamilyName());
 }
  @Test
  public void canAddUserWithAuthorities() throws Exception {
    UaaUser joe = new UaaUser("joe", "password", "*****@*****.**", "Joe", "User");
    joe = joe.authorities(AuthorityUtils.commaSeparatedStringToAuthorityList("openid,read"));
    ScimUserBootstrap bootstrap = new ScimUserBootstrap(db, gdb, mdb, Arrays.asList(joe));
    bootstrap.afterPropertiesSet();
    @SuppressWarnings("unchecked")
    Collection<Map<String, Object>> users =
        (Collection<Map<String, Object>>)
            userEndpoints.findUsers("id", "id pr", "id", "ascending", 1, 100).getResources();
    assertEquals(1, users.size());

    String id = (String) users.iterator().next().get("id");
    ScimUser user = userEndpoints.getUser(id, new MockHttpServletResponse());
    // uaa.user is always added
    assertEquals(3, user.getGroups().size());
  }
  @Test
  public void canRemoveAuthorities() throws Exception {
    UaaUser joe = new UaaUser("joe", "password", "*****@*****.**", "Joe", "User");
    joe = joe.authorities(AuthorityUtils.commaSeparatedStringToAuthorityList("openid,read"));
    ScimUserBootstrap bootstrap = new ScimUserBootstrap(db, gdb, mdb, Arrays.asList(joe));
    bootstrap.afterPropertiesSet();
    joe = joe.authorities(AuthorityUtils.commaSeparatedStringToAuthorityList("openid"));
    JdbcTemplate jdbcTemplate = new JdbcTemplate(database);
    System.err.println(jdbcTemplate.queryForList("SELECT * FROM group_membership"));
    bootstrap = new ScimUserBootstrap(db, gdb, mdb, Arrays.asList(joe));
    bootstrap.setOverride(true);
    bootstrap.afterPropertiesSet();
    @SuppressWarnings("unchecked")
    Collection<Map<String, Object>> users =
        (Collection<Map<String, Object>>)
            userEndpoints.findUsers("id", "id pr", "id", "ascending", 1, 100).getResources();
    assertEquals(1, users.size());

    String id = (String) users.iterator().next().get("id");
    ScimUser user = userEndpoints.getUser(id, new MockHttpServletResponse());
    // uaa.user is always added
    assertEquals(2, user.getGroups().size());
  }