@Test
  public void remove() throws Exception {
    try (DbContext dbContext = contextProvider.getDbContext()) {
      MessageRatingDAO messageRatingDAO = dbContext.getMessageRatingDAO();
      PersonDAO personDAO = dbContext.getPersonDAO();
      MessageDAO messageDAO = dbContext.getMessageDAO();

      Person person = personDAO.getById(118);
      Message message = messageDAO.getById(2);
      MessageRating rating =
          dbContext.getMessageRatingDAO().findRatingByPersonAndMessage(message, person);
      message.removeMesasgeRating(rating);
      person.removeMesasgeRating(rating);
      messageRatingDAO.remove(rating);
      dbContext.commit();
    }

    try (DbContext dbContext = contextProvider.getDbContext()) {
      MessageRatingDAO messageRatingDAO = dbContext.getMessageRatingDAO();
      MessageDAO messageDAO = dbContext.getMessageDAO();
      Message message = messageDAO.getById(2);
      List<Person> raters = messageRatingDAO.loadAllRatersByMessage(message);
      assertEquals(0, raters.size());
    }
  }
  @Test
  public void insert() throws Exception, ConstraintViolationException {

    Person p = new Person("etester", "Tester", "Ehrenfried", "1234567");

    try (DbContext dbContext = contextProvider.getDbContext()) {

      PersonDAO personDAO = dbContext.getPersonDAO();

      personDAO.insert(p);
      dbContext.commit();

      assertNotEquals(0, p.getPersonId());

    } catch (Exception e) {

      e.printStackTrace();
      fail("Exception " + e.getMessage());

    } finally {

      if (p.getPersonId() != 0) {
        try (DbContext dbContext = contextProvider.getDbContext()) {

          PersonDAO personDAO = dbContext.getPersonDAO();

          personDAO.remove(p.getPersonId());

          dbContext.commit();
        }
      }
    }
  }
  @Test(expected = EntityNotFoundException.class)
  public void getUserByInvalidEMailAddress() throws Exception {
    DbContext dbContext = contextProvider.getDbContext();

    PersonDAO personDAO = dbContext.getPersonDAO();
    Person p = personDAO.getByEmailAddress("*****@*****.**");
    assertEquals(p, new Person("", "", "", ""));
  }
  @Test(expected = ParameterException.class)
  public void removeOnNull() throws Exception {
    try (DbContext dbContext = contextProvider.getDbContext()) {

      PersonDAO personDAO = dbContext.getPersonDAO();

      personDAO.remove(null);
    }
  }
  @Test(expected = EntityNotFoundException.class)
  public void getByIdNoneFound() throws Exception {

    try (DbContext dbContext = contextProvider.getDbContext()) {

      PersonDAO personDAO = dbContext.getPersonDAO();

      personDAO.getById(-1);
    }
  }
  @Test(expected = ParameterException.class)
  public void removeOnIdZero() throws Exception {
    try (DbContext dbContext = contextProvider.getDbContext()) {

      PersonDAO personDAO = dbContext.getPersonDAO();

      Person p = new Person();
      p.setPersonId(0);

      personDAO.remove(p);
    }
  }
  @Test
  public void loadAllRatersByMessage() throws Exception {
    try (DbContext dbContext = contextProvider.getDbContext()) {
      PersonDAO personDAO = dbContext.getPersonDAO();
      MessageDAO messageDAO = dbContext.getMessageDAO();
      MessageRatingDAO messageRatingDAO = dbContext.getMessageRatingDAO();

      Person person = personDAO.getById(108);
      Message message = messageDAO.getById(1);

      List<Person> raters = messageRatingDAO.loadAllRatersByMessage(message);
      assertEquals(person, raters.get(0));
    }
  }
  @Test
  public void findRatingByPersonAndMessage() throws Exception {
    try (DbContext dbContext = contextProvider.getDbContext()) {
      PersonDAO personDAO = dbContext.getPersonDAO();
      MessageDAO messageDAO = dbContext.getMessageDAO();
      MessageRatingDAO messageRatingDAO = dbContext.getMessageRatingDAO();

      Person person = personDAO.getById(108);
      Message message = messageDAO.getById(1);

      MessageRating rating = messageRatingDAO.findRatingByPersonAndMessage(message, person);
      assertEquals(message, rating.getMessage());
      assertEquals(person, rating.getPerson());
    }
  }
  @Test
  public void getByUsernameXaseMismatch() {

    try (DbContext dbContext = contextProvider.getDbContext()) {

      PersonDAO personDAO = dbContext.getPersonDAO();

      Person p = personDAO.getByUsername("Pse_system");

      assertNull(p);

    } catch (Exception e) {
      e.printStackTrace();
      fail("Exception " + e.getMessage());
    }
  }
  @Test
  public void getByEmailAddress() {

    try (DbContext dbContext = contextProvider.getDbContext()) {

      PersonDAO personDAO = dbContext.getPersonDAO();

      Person p = personDAO.getByEmailAddress("*****@*****.**");

      assertEquals(p.getPersonId(), 2);

    } catch (Exception e) {
      e.printStackTrace();
      fail("Exception " + e.getMessage());
    }
  }
  @Test
  public void getByUsernameNoneFound() {

    try (DbContext dbContext = contextProvider.getDbContext()) {

      PersonDAO personDAO = dbContext.getPersonDAO();

      Person p = personDAO.getByUsername("gnurmifgjp�sh l hlfdyghlfd glkdfghv y");

      assertEquals(p, null);

    } catch (Exception e) {
      e.printStackTrace();
      fail("Exception " + e.getMessage());
    }
  }
  @Test
  public void getById() {

    try (DbContext dbContext = contextProvider.getDbContext()) {

      PersonDAO personDAO = dbContext.getPersonDAO();

      Person p = personDAO.getById(1); // 1 is the system internal user
      // that should always exist

      assertEquals(p.getUserName(), "pse_system");

    } catch (Exception e) {
      e.printStackTrace();
      fail("Exception " + e.getMessage());
    }
  }
  @Test(expected = EntityNotFoundException.class)
  public void insertRemovCycle() throws Exception {

    Person p = new Person("etester", "Tester", "Ehrenfried", "12345678");

    try (DbContext dbContext = contextProvider.getDbContext()) {

      PersonDAO personDAO = dbContext.getPersonDAO();

      personDAO.insert(p);
      dbContext.commit();

      assertNotEquals(0, p.getPersonId());
    }

    try (DbContext dbContext = contextProvider.getDbContext()) {

      PersonDAO personDAO = dbContext.getPersonDAO();

      Person x = personDAO.getById(p.getPersonId());

      personDAO.remove(x);

      dbContext.commit();
    }

    try (DbContext dbContext = contextProvider.getDbContext()) {

      PersonDAO personDAO = dbContext.getPersonDAO();

      Person x = personDAO.getById(p.getPersonId());
      assertEquals(x, new Person());
    }
  }
  @Test
  public void insert() throws Exception {
    try (DbContext dbContext = contextProvider.getDbContext()) {
      MessageRatingDAO messageRatingDAO = dbContext.getMessageRatingDAO();
      PersonDAO personDAO = dbContext.getPersonDAO();
      MessageDAO messageDAO = dbContext.getMessageDAO();

      Person person = personDAO.getById(104);
      Message message = messageDAO.getById(4);

      MessageRating rating = new MessageRating(new Date(), message, person);

      messageRatingDAO.insert(rating);

      List<Person> raters = messageRatingDAO.loadAllRatersByMessage(message);
      assertEquals(person, raters.get(0));
    }
  }
  @After
  public void teardown() throws Exception {

    try (DbContext context = contextProvider.getDbContext()) {

      CommunityDAO communityDAO = context.getCommunityDAO();

      for (Community c : toDelete) {
        communityDAO.remove(c.getCommunityId());
      }

      PersonDAO personDao = context.getPersonDAO();

      personDao.remove(plainPerson.getPersonId());
      personDao.remove(adminPerson.getPersonId());
      personDao.remove(inActivePerson.getPersonId());

      context.commit();
    }
  }
  @Test(expected = ConstraintViolationException.class)
  public void insertDuplicate() throws Exception {

    try (DbContext dbContext = contextProvider.getDbContext()) {

      PersonDAO personDAO = dbContext.getPersonDAO();

      Person p1 = new Person("etester", "Tester", "Ehrenfried", "12345678");

      Person p2 = new Person("etester", "Tester", "Ehrenfried", "12345678");

      personDAO.insert(p1);
      personDAO.insert(p2);
      dbContext.commit();

    } catch (Exception e) {

      throw e;
    }
  }
  @BeforeClass
  public static void setup() throws Exception {
    DbTestBase.prepare();

    try (DbContext dbContext = contextProvider.getDbContext()) {
      MessageRatingDAO messageRatingDAO = dbContext.getMessageRatingDAO();
      PersonDAO personDAO = dbContext.getPersonDAO();
      MessageDAO messageDAO = dbContext.getMessageDAO();

      Person person = personDAO.getById(118);
      Message message = messageDAO.getById(2);
      MessageRating rating = new MessageRating(new Date(), message, person);
      messageRatingDAO.insert(rating);

      person = personDAO.getById(114);
      message = messageDAO.getById(3);
      rating = new MessageRating(new Date(), message, person);
      messageRatingDAO.insert(rating);

      dbContext.commit();
    }
  }