@Test public void shouldPersistEntity() throws Exception { // given Color black = new Color("Black"); // when repository.persist(black); entityManager.flush(); // then assertThat(black.getId(), is(notNullValue())); assertThat(entityManager.find(Color.class, black.getId()), equalTo(black)); }
@Test public void shouldRemoveEntityByObject() throws Exception { // when repository.remove(blue); entityManager.flush(); // then assertThat(entityManager.find(Color.class, blue.getId()), is(nullValue())); }
@Test public void shouldReturnNullIfResultListIsEmpty() throws Exception { // given entityManager.remove(blue); entityManager.remove(red); // when Color color = repository.singleResult(entityManager.createQuery("select c from Color c", Color.class)); // then assertThat(color, is(nullValue())); }
@Test public void shouldMergeEntity() throws Exception { // given entityManager.flush(); entityManager.detach(blue); // when blue.setName("Dark Blue"); repository.merge(blue); entityManager.flush(); // then String query = "select s from Color s where s.name = 'Dark Blue'"; Color merged = entityManager.createQuery(query, Color.class).getSingleResult(); assertThat(merged.getId(), equalTo(blue.getId())); }
@Test public void shouldIncrementVersionValueOnMerge() throws Exception { // given entityManager.flush(); entityManager.detach(blue); // when blue.setName("Light Blue"); Color merged = repository.merge(blue); entityManager.flush(); // then assertThat(merged.getVersion(), is(greaterThan(blue.getVersion()))); }
@Test public void shouldRemoveEntityByIdAndReturnTrue() throws Exception { // when boolean found = repository.remove(blue.getId()); entityManager.flush(); // then assertThat(found, is(true)); assertThat(entityManager.find(Color.class, blue.getId()), is(nullValue())); }
@Before public void setup() throws Exception { repository = new ColorRepository(); repository.setEntityManager(entityManager); blue = new Color("Blue"); entityManager.persist(blue); red = new Color("Red"); entityManager.persist(red); }
@Test public void shouldFindEntity() throws Exception { // when Color result = repository.find(red.getId()); // then assertThat(result, equalTo(red)); }
@Test public void shouldReturnFalseIfEntityToRemoveWasNotFound() throws Exception { // when boolean found = repository.remove(Long.MAX_VALUE); entityManager.flush(); // then assertThat(found, is(false)); }
@Test public void shouldFindAll() throws Exception { // when List<Color> result = repository.findAll(); // then assertEquals(2, result.size()); }