/** Test deleting user. */
  @Test
  public void testDeleteUser() throws Exception {
    System.out.println(">>> Deleting user");
    UserTransaction tx = getUserTransaction();
    try {
      AdminEJBLocal instance = (AdminEJBLocal) getEJBInstance(AdminEJB.class.getSimpleName());
      tx.begin();
      // Get user
      User user = instance.getUser(USER_NAME);
      tx.commit();

      assertNotNull("Failed to get user", user);

      user.setEntityAction(EntityAction.DELETE);

      tx.begin();
      User deltedUser = instance.saveUser(user);
      tx.commit();

      assertNull("Failed to delete user", deltedUser);

      System.out.println(">>> User has been deleted!");

    } catch (Exception e) {
      tx.rollback();
      fail(e.getMessage());
    }
  }
  /** Test creating user. */
  @Test
  public void testCreateUser() throws Exception {
    System.out.println(">>> Create new user.");
    UserTransaction tx = getUserTransaction();
    try {
      AdminEJBLocal instance = (AdminEJBLocal) getEJBInstance(AdminEJB.class.getSimpleName());
      tx.begin();

      // Get group
      Group group = instance.getGroup(GROUP_ID);
      tx.commit();

      assertNotNull("Failed to get group", group);

      tx.begin();

      UserGroup userGroup = new UserGroup();
      userGroup.setGroupId(GROUP_ID);
      userGroup.setEntityAction(EntityAction.INSERT);

      List<UserGroup> listUserGroups = new ArrayList<UserGroup>();
      listUserGroups.add(userGroup);

      User user = new User();
      user.setId(USER_ID);
      user.setActive(true);
      user.setDescription("Test user");
      user.setEntityAction(EntityAction.INSERT);
      user.setFirstName("First name test user");
      user.setLastName("Last name for user");
      user.setLoaded(false);
      user.setUserName(USER_NAME);
      user.setUserGroups(listUserGroups);

      User savedUser = instance.saveUser(user);
      tx.commit();

      assertNotNull("Failed to create user", savedUser);
      System.out.println(">>> User has been created!");

      // Re-read user
      tx.begin();
      savedUser = instance.getUser(USER_NAME);
      savedUser.getUserGroups();
      tx.commit();

      // Check user
      assertEquals("User id is wrong", savedUser.getId(), user.getId());
      assertEquals("User active flag is wrong", savedUser.isActive(), user.isActive());
      assertEquals("User description is wrong", savedUser.getDescription(), user.getDescription());
      assertEquals("User first name is wrong", savedUser.getFirstName(), user.getFirstName());
      assertEquals("User name is wrong", savedUser.getUserName(), user.getUserName());
      assertNotNull("User groups is null", savedUser.getUserGroups());
      assertEquals(
          "User number of groups is wrong",
          savedUser.getUserGroups().size(),
          user.getUserGroups().size());

      System.out.println(">>> User was saved with correct values.");
    } catch (Exception e) {
      tx.rollback();
      fail(e.getMessage());
    }
  }