/** 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);
  }