RunDao extends BaseHibernateEntityDao<RunHibernateEntity> { @Override public void create(RunHibernateEntity entity) { persist(entity); } @Inject  RunDao(SessionFactory sessionFactory); @Override void create(RunHibernateEntity entity); @Override List<RunHibernateEntity> findAll(); @Override RunHibernateEntity findById(String id); RunHibernateEntity findLastInSequenceFor(EventHibernateEntity event); RunHibernateEntity findFirstInSequenceWithoutRawTime(EventHibernateEntity event); List<RunHibernateEntity> getAllWith(EventHibernateEntity event); List<RunHibernateEntity> getAllWith(RegistrationHibernateEntity registration);  }
@Test public void itShouldThrowWhenEventIsNull() { RunHibernateEntity entity = buildUnsavedRun(); entity.setEvent(null); try { daoTestRule.inTransaction(() -> dao.create(entity)); failBecauseExceptionWasNotThrown(PersistenceException.class); } catch (Exception e) { assertThat(e) .isInstanceOf(PersistenceException.class) .hasCauseInstanceOf(ConstraintViolationException.class); } }
@Test public void itShouldThrowWhenRawTimeOverflows() { RunHibernateEntity entity = buildUnsavedRun(); entity.setRawTime(BigDecimal.valueOf(1000d)); try { daoTestRule.inTransaction(() -> dao.create(entity)); failBecauseExceptionWasNotThrown(PersistenceException.class); } catch (Exception e) { assertThat(e) .isInstanceOf(PersistenceException.class) .hasCauseInstanceOf(DataException.class); } }
@Test public void itShouldThrowWhenSequenceLessThanOne() { RunHibernateEntity entity = buildUnsavedRun(); entity.setSequence(0); try { daoTestRule.inTransaction(() -> dao.create(entity)); failBecauseExceptionWasNotThrown(javax.validation.ConstraintViolationException.class); } catch (Exception e) { assertThat(e).isInstanceOf(javax.validation.ConstraintViolationException.class); } }
@Test @Ignore("Deferring work on this, see https: public void itShouldThrowWhenEventAndSequenceNotUnique() throws InterruptedException { RunHibernateEntity entity1 = buildUnsavedRun(); entity1.setSequence(1); RunHibernateEntity entity2 = buildUnsavedRun(); entity2.setSequence(1); assertThat(entity1.getEvent()).isSameAs(entity2.getEvent()); assertThat(entity1.getSequence()).isEqualTo(entity2.getSequence()); try { daoTestRule.inTransaction(() -> { dao.create(entity1); dao.create(entity2); failBecauseExceptionWasNotThrown(PersistenceException.class); }); } catch (Exception e) { assertThat(e) .isInstanceOf(PersistenceException.class) .hasCauseInstanceOf(ConstraintViolationException.class); } }
@Test public void itShouldInsertWithoutRegistration() { RunHibernateEntity entity = buildUnsavedRun(); entity.setRegistration(null); daoTestRule.inTransaction(() -> dao.create(entity)); }
@Test public void itShouldInsertWithoutRawTime() { RunHibernateEntity entity = buildUnsavedRun(); entity.setRawTime(null); daoTestRule.inTransaction(() -> dao.create(entity)); }
@Test public void itShouldInsertWithoutRegistrationAndRawTime() { RunHibernateEntity entity = buildUnsavedRun(); entity.setRegistration(null); entity.setRawTime(null); daoTestRule.inTransaction(() -> dao.create(entity)); }