/** Test personal item persistence */
  @Test
  public void rootPersonalItemDAOTest() {

    TransactionStatus ts = tm.getTransaction(td);
    UserEmail userEmail = new UserEmail("user@email");

    // create a user who has their own folder
    IrUser user = new IrUser("user", "password");
    user.setPasswordEncoding("none");
    user.addUserEmail(userEmail, true);

    // create the user
    userDAO.makePersistent(user);
    tm.commit(ts);

    ts = tm.getTransaction(td);
    GenericItem genericItem = new GenericItem("aItem");
    VersionedItem versionedItem = new VersionedItem(user, genericItem);
    PersonalItem personalItem = new PersonalItem(user, versionedItem);
    personalItemDAO.makePersistent(personalItem);
    tm.commit(ts);

    ts = tm.getTransaction(td);
    PersonalItem other = personalItemDAO.getById(personalItem.getId(), false);
    VersionedItem otherVersionedItem = other.getVersionedItem();
    assert otherVersionedItem != null : "Versioned Item should be found";
    assert otherVersionedItem.getOwner() != null : "Owner should not be null";
    assert other.equals(personalItem) : "The personal item " + personalItem + " should be found";

    personalItemDAO.makeTransient(other);
    versionedItemDAO.makeTransient(otherVersionedItem);
    itemDAO.makeTransient(itemDAO.getById(genericItem.getId(), false));
    userDAO.makeTransient(userDAO.getById(user.getId(), false));
    tm.commit(ts);
  }
  /** Test adding onwer to group spaces */
  @Test
  public void addGroupWorkspaceOwnerDAOTest() throws Exception {

    TransactionStatus ts = tm.getTransaction(td);
    GroupWorkspace groupSpace = new GroupWorkspace("grouName", "groupDescription");

    // create a user who has their own folder
    UserManager userManager = new UserManager();
    IrUser user = userManager.createUser("passowrd", "userName");
    UserEmail userEmail = new UserEmail("user@email");
    user.addUserEmail(userEmail, true);
    user.setAccountExpired(true);
    user.setAccountLocked(true);
    user.setCredentialsExpired(true);

    // create the user and their folder.
    userDAO.makePersistent(user);

    groupSpace.add(user, true);

    groupWorkspaceDAO.makePersistent(groupSpace);
    tm.commit(ts);

    ts = tm.getTransaction(td);
    GroupWorkspace other = groupWorkspaceDAO.getById(groupSpace.getId(), false);
    GroupWorkspaceUser workspaceUser = other.getUser(user);
    assert workspaceUser != null : "Workspace user for " + user + " should not be null";
    assert workspaceUser.isOwner()
        : "Workspace user  " + workspaceUser + " should be owner of project but is not";
    List<GroupWorkspace> workspaces =
        groupWorkspaceDAO.getGroupWorkspacesForUser(user.getId(), 0, 20, OrderType.ASCENDING_ORDER);
    assert workspaces.size() == 1 : "Should find one workspace but found " + workspaces.size();
    assert workspaces.contains(other) : "List should contain " + other + " but does not";
    tm.commit(ts);

    ts = tm.getTransaction(td);
    other = groupWorkspaceDAO.getById(groupSpace.getId(), false);
    groupWorkspaceDAO.makeTransient(other);
    assert groupWorkspaceDAO.getById(other.getId(), false) == null
        : "Should no longer be able to find groupSpace";
    userDAO.makeTransient(userDAO.getById(user.getId(), false));
    tm.commit(ts);
  }
  /** Test getting the set of root personal items */
  @Test
  public void getRootPersonalItemsDAOTest() throws Exception {

    UserEmail userEmail = new UserEmail("user@email");

    UserManager userManager = new UserManager();
    IrUser user = userManager.createUser("passowrd", "userName");
    user.addUserEmail(userEmail, true);
    user.setAccountExpired(true);
    user.setAccountLocked(true);
    user.setCredentialsExpired(true);

    userDAO.makePersistent(user);

    TransactionStatus ts = tm.getTransaction(td);

    IrUser other = userDAO.getById(user.getId(), false);
    GenericItem genericItem = new GenericItem("aItem");
    VersionedItem versionedItem = new VersionedItem(user, genericItem);
    PersonalItem personalItem = other.createRootPersonalItem(versionedItem);

    // complete the transaction
    tm.commit(ts);

    ts = tm.getTransaction(td);
    other = userDAO.getById(user.getId(), false);
    assert other != null : "Other should not be null";

    Set<PersonalItem> personalItems = other.getRootPersonalItems();

    assert personalItems != null : "Should be able to find the personal items";
    assert personalItems.size() == 1
        : "Root personal items should be 1 but is " + personalItems.size();

    tm.commit(ts);

    ts = tm.getTransaction(td);
    personalItemDAO.makeTransient(personalItemDAO.getById(personalItem.getId(), false));
    versionedItemDAO.makeTransient(versionedItemDAO.getById(versionedItem.getId(), false));
    itemDAO.makeTransient(itemDAO.getById(genericItem.getId(), false));
    userDAO.makeTransient(userDAO.getById(other.getId(), false));
    tm.commit(ts);

    assert userDAO.getById(other.getId(), false) == null : "Should not be able to find other";
  }
  /** Test User Access Control Entry DAO */
  @Test
  public void baseUserControlEntryDAOTest() throws Exception {

    TransactionStatus ts = tm.getTransaction(td);
    LanguageType lt = new LanguageType();
    lt.setName("languageName");
    lt.setDescription("languageDescription");
    languageTypeDAO.makePersistent(lt);

    IrClassType languageClassType = new IrClassType(LanguageType.class);
    irClassTypeDAO.makePersistent(languageClassType);

    IrAcl irAcl = new IrAcl(lt, languageClassType);

    irAclDAO.makePersistent(irAcl);
    // complete the transaction
    tm.commit(ts);

    ts = tm.getTransaction(td);
    UserEmail userEmail = new UserEmail("user@email");

    UserManager userManager = new UserManager();
    IrUser user = userManager.createUser("passowrd", "userName");
    user.setLastName("familyName");
    user.setFirstName("forename");
    user.addUserEmail(userEmail, true);

    // save the user
    userDAO.makePersistent(user);

    // create the user access control entry
    IrUserAccessControlEntry uace = irAcl.createUserAccessControlEntry(user);

    IrClassTypePermission classTypePermission = new IrClassTypePermission(languageClassType);
    classTypePermission.setName("permissionName");
    classTypePermission.setDescription("permissionDescription");

    IrClassTypePermission classTypePermission1 = new IrClassTypePermission(languageClassType);
    classTypePermission1.setName("permissionName1");
    classTypePermission1.setDescription("permissionDescription1");

    // save the class type permission
    classTypePermissionDAO.makePersistent(classTypePermission);
    classTypePermissionDAO.makePersistent(classTypePermission1);

    uace.addPermission(classTypePermission1);
    uace.addPermission(classTypePermission);
    uaceDAO.makePersistent(uace);
    tm.commit(ts);

    // start a new transaction
    ts = tm.getTransaction(td);

    IrUserAccessControlEntry other = uaceDAO.getById(uace.getId(), false);
    assert other.equals(uace) : "User access control entries should be equal";
    assert uace.getAcl().equals(irAcl) : "Acl's should be equal";
    assert irAcl.getUserAccessControlEntry(uace.getId()).equals(uace)
        : "Access control should bin in the irAcl";
    assert uace.getPermissions().size() == 2 : "Should have at least one permission";
    assert uace.getIrClassTypePermissions().contains(classTypePermission)
        : "Should equal the class type permission";
    assert uace.getIrUser().equals(user) : "Users should be equal";
    assert uaceDAO.getCount() == 1 : "Should have one uace";

    // make sure we can get the acl for the role
    List<IrAcl> acls = irAclDAO.getAllAclsForSid(user);
    assert acls.size() == 1
        : "Should be able to find 1 acl for user " + user + " but found " + acls.size();

    // commit the transaction
    tm.commit(ts);

    ts = tm.getTransaction(td);
    // clean up the database
    irAclDAO.makeTransient(irAclDAO.getById(irAcl.getId(), false));
    assert uaceDAO.getById(uace.getId(), false) == null
        : "Should not be able to find the access control entry";

    classTypePermissionDAO.makeTransient(
        classTypePermissionDAO.getById(classTypePermission.getId(), false));
    classTypePermissionDAO.makeTransient(
        classTypePermissionDAO.getById(classTypePermission1.getId(), false));

    irClassTypeDAO.makeTransient(irClassTypeDAO.getById(languageClassType.getId(), false));
    // assert irClassTypeDAO.getById(languageClassType.getId(), false) == null : "Should not be able
    // to find class type " + languageClassType;

    // start a new transaction

    userDAO.makeTransient(userDAO.getById(user.getId(), false));
    languageTypeDAO.makeTransient(languageTypeDAO.getById(lt.getId(), false));
    // commit the transaction
    tm.commit(ts);
  }
  /** Bulk entry creation */
  @Test
  public void createEntriesForUsersMultipleClassTypesTest() throws ClassNotFoundException {
    TransactionStatus ts = tm.getTransaction(td);
    LanguageType lt = new LanguageType();
    lt.setName("languageName");
    languageTypeDAO.makePersistent(lt);

    ContributorType ct = new ContributorType("type");
    contributorTypeDAO.makePersistent(ct);

    IrClassType languageClassType = new IrClassType(LanguageType.class);
    irClassTypeDAO.makePersistent(languageClassType);

    IrClassType contributorClassType = new IrClassType(ContributorType.class);
    irClassTypeDAO.makePersistent(contributorClassType);

    IrAcl irAcl = new IrAcl(lt, languageClassType);
    IrAcl irAcl2 = new IrAcl(ct, contributorClassType);

    irAclDAO.makePersistent(irAcl);
    irAclDAO.makePersistent(irAcl2);

    UserEmail userEmail = new UserEmail("user@email");

    UserManager userManager = new UserManager();
    IrUser user = userManager.createUser("passowrd", "userName");
    user.setLastName("familyName");
    user.setFirstName("forename");
    user.addUserEmail(userEmail, true);

    // save the user
    userDAO.makePersistent(user);

    UserEmail userEmail2 = new UserEmail("user@email2");
    IrUser user2 = userManager.createUser("passowrd2", "userName2");
    user.setLastName("familyName2");
    user.setFirstName("forename2");
    user.addUserEmail(userEmail2, true);
    userDAO.makePersistent(user2);

    // create some permissions
    IrClassTypePermission classTypePermission = new IrClassTypePermission(languageClassType);
    classTypePermission.setName("permissionName");
    classTypePermission.setDescription("permissionDescription");

    IrClassTypePermission classTypePermission1 = new IrClassTypePermission(contributorClassType);
    classTypePermission1.setName("permissionName1");
    classTypePermission1.setDescription("permissionDescription1");

    // save the class type permission
    classTypePermissionDAO.makePersistent(classTypePermission);
    classTypePermissionDAO.makePersistent(classTypePermission1);

    // complete the transaction
    tm.commit(ts);

    // start a new transaction
    ts = tm.getTransaction(td);
    List<IrUser> users = new LinkedList<IrUser>();
    users.add(user);
    users.add(user2);

    List<IrAcl> acls = new LinkedList<IrAcl>();
    acls.add(irAcl);
    acls.add(irAcl2);

    // create user entries for all given acls
    int count = uaceDAO.createUserControlEntriesForUsers(users, acls);
    assert count == 4 : "Should have 4 entries but have " + count;

    List<IrUserAccessControlEntry> entries = new LinkedList<IrUserAccessControlEntry>();

    entries.addAll(uaceDAO.getUserControlEntriesForUsers(irAcl, users));
    entries.addAll(uaceDAO.getUserControlEntriesForUsers(irAcl2, users));

    assert entries.size() == 4 : "Should find 4 entries but found " + entries.size();

    // create permissions for all given user control entries
    List<IrClassTypePermission> permissions = new LinkedList<IrClassTypePermission>();
    permissions.add(classTypePermission);
    permissions.add(classTypePermission1);

    int permissionEntriesCount =
        uaceDAO.createPermissionsForUserControlEntries(entries, permissions);
    assert permissionEntriesCount == 4 : "Should have 4 entries but have " + permissionEntriesCount;

    // commit the transaction
    tm.commit(ts);

    // start a new transaction
    ts = tm.getTransaction(td);
    // clean up the database
    irAclDAO.makeTransient(irAclDAO.getById(irAcl.getId(), false));
    irAclDAO.makeTransient(irAclDAO.getById(irAcl2.getId(), false));
    userDAO.makeTransient(userDAO.getById(user.getId(), false));
    userDAO.makeTransient(userDAO.getById(user2.getId(), false));
    languageTypeDAO.makeTransient(languageTypeDAO.getById(lt.getId(), false));
    contributorTypeDAO.makeTransient(contributorTypeDAO.getById(ct.getId(), false));

    classTypePermissionDAO.makeTransient(
        classTypePermissionDAO.getById(classTypePermission.getId(), false));
    classTypePermissionDAO.makeTransient(
        classTypePermissionDAO.getById(classTypePermission1.getId(), false));

    irClassTypeDAO.makeTransient(irClassTypeDAO.getById(languageClassType.getId(), false));
    irClassTypeDAO.makeTransient(irClassTypeDAO.getById(contributorClassType.getId(), false));

    // commit the transaction
    tm.commit(ts);
  }