@PostConstruct
  public void create() {

    if (service.usersExist()) {
      return;
    }
    // Create user john
    User john = new User("john");
    john.setEmail("*****@*****.**");
    john.setFirstName("John");
    john.setLastName("Smith");

    IdentityManager identityManager = this.partitionManager.createIdentityManager();

    identityManager.add(john);
    identityManager.updateCredential(john, new Password("demo"));

    // Create user mary
    User mary = new User("mary");
    mary.setEmail("*****@*****.**");
    mary.setFirstName("Mary");
    mary.setLastName("Jones");
    identityManager.add(mary);
    identityManager.updateCredential(mary, new Password("demo"));

    // Create user jane
    User jane = new User("jane");
    jane.setEmail("*****@*****.**");
    jane.setFirstName("Jane");
    jane.setLastName("Doe");
    identityManager.add(jane);
    identityManager.updateCredential(jane, new Password("demo"));

    // Create role "manager"
    Role manager = new Role("manager");
    identityManager.add(manager);

    // Create application role "superuser"
    Role superuser = new Role("superuser");
    identityManager.add(superuser);

    // Create group "sales"
    Group sales = new Group("sales");
    identityManager.add(sales);

    RelationshipManager relationshipManager = this.partitionManager.createRelationshipManager();

    // Make john a member of the "sales" group
    addToGroup(relationshipManager, john, sales);

    // Make mary a manager of the "sales" group
    grantGroupRole(relationshipManager, mary, manager, sales);

    // Grant the "superuser" application role to jane
    grantRole(relationshipManager, jane, superuser);
  }
  @Test
  public void testRemove() throws Exception {
    IdentityManager identityManager = getIdentityManager();

    Agent someAgent = createIdentityType();
    Agent anotherAgent = createAgent("someAnotherAgent");

    identityManager.remove(someAgent);

    Agent removedUserInstance = getAgent(someAgent.getLoginName());

    assertNull(removedUserInstance);

    anotherAgent = getAgent(anotherAgent.getLoginName());

    assertNotNull(anotherAgent);

    Role role = createRole("role");
    Group group = createGroup("group", null);

    RelationshipManager relationshipManager = getPartitionManager().createRelationshipManager();

    BasicModel.grantRole(relationshipManager, anotherAgent, role);
    BasicModel.addToGroup(relationshipManager, anotherAgent, group);

    RelationshipQuery<?> relationshipQuery =
        relationshipManager.createRelationshipQuery(Grant.class);

    relationshipQuery.setParameter(Grant.ASSIGNEE, anotherAgent);

    assertFalse(relationshipQuery.getResultList().isEmpty());

    relationshipQuery = relationshipManager.createRelationshipQuery(GroupMembership.class);

    relationshipQuery.setParameter(GroupMembership.MEMBER, anotherAgent);

    assertFalse(relationshipQuery.getResultList().isEmpty());

    identityManager.remove(anotherAgent);

    relationshipQuery = relationshipManager.createRelationshipQuery(Grant.class);

    relationshipQuery.setParameter(Grant.ASSIGNEE, anotherAgent);

    assertTrue(relationshipQuery.getResultList().isEmpty());

    relationshipQuery = relationshipManager.createRelationshipQuery(GroupMembership.class);

    relationshipQuery.setParameter(GroupMembership.MEMBER, anotherAgent);

    assertTrue(relationshipQuery.getResultList().isEmpty());
  }
 private void grantRoles(User user, Role role) {
   BasicModel.grantRole(relationshipManager, user, role);
 }