package org.baeldung.test.common.service; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.util.List; import org.baeldung.common.persistence.model.IEntity; import org.baeldung.common.persistence.service.IRawService; import org.baeldung.common.util.SearchField; import org.baeldung.common.util.order.OrderById; import org.baeldung.test.common.util.IDUtil; import org.hamcrest.Matchers; import org.junit.Ignore; import org.junit.Test; import org.springframework.dao.DataAccessException; import org.springframework.data.domain.Sort; public abstract class AbstractRawServiceIntegrationTest<T extends IEntity> { // tests // find - one @Test public final void givenResourceDoesNotExist_whenResourceIsRetrieved_thenNoResourceIsReceived() { // When final T createdResource = getApi().findOne(IDUtil.randomPositiveLong()); // Then assertNull(createdResource); } @Test public void givenResourceExists_whenResourceIsRetrieved_thenNoExceptions() { final T existingResource = persistNewEntity(); getApi().findOne(existingResource.getId()); } @Test public void givenResourceDoesNotExist_whenResourceIsRetrieved_thenNoExceptions() { getApi().findOne(IDUtil.randomPositiveLong()); } @Test public void givenResourceExists_whenResourceIsRetrieved_thenTheResultIsNotNull() { final T existingResource = persistNewEntity(); final T retrievedResource = getApi().findOne(existingResource.getId()); assertNotNull(retrievedResource); } @Test public void givenResourceExists_whenResourceIsRetrieved_thenResourceIsRetrievedCorrectly() { final T existingResource = persistNewEntity(); final T retrievedResource = getApi().findOne(existingResource.getId()); assertEquals(existingResource, retrievedResource); } // find - one - by name // find - all @Test public void whenAllResourcesAreRetrieved_thenNoExceptions() { getApi().findAll(); } @Test public void whenAllResourcesAreRetrieved_thenTheResultIsNotNull() { final List<T> resources = getApi().findAll(); assertNotNull(resources); } @Test public void givenAtLeastOneResourceExists_whenAllResourcesAreRetrieved_thenRetrievedResourcesAreNotEmpty() { persistNewEntity(); // When final List<T> allResources = getApi().findAll(); // Then assertThat(allResources, not(Matchers.<T> empty())); } @Test public void givenAnResourceExists_whenAllResourcesAreRetrieved_thenTheExistingResourceIsIndeedAmongThem() { final T existingResource = persistNewEntity(); final List<T> resources = getApi().findAll(); assertThat(resources, hasItem(existingResource)); } @Test public void whenAllResourcesAreRetrieved_thenResourcesHaveIds() { persistNewEntity(); // When final List<T> allResources = getApi().findAll(); // Then for (final T resource : allResources) { assertNotNull(resource.getId()); } } // find - all - pagination @Test public final void whenResourcesAreRetrievedPaginated_thenNoExceptions() { getApi().findAllPaginated(1, 1); } @Test public final void whenFirstPageOfResourcesAreRetrieved_thenResourcesPageIsReturned() { persistNewEntity(); // When final List<T> allPaginated = getApi().findAllPaginated(0, 1); // Then assertFalse(allPaginated.isEmpty()); } // find - all - sorting @Test public final void whenResourcesAreRetrievedSortedDescById_thenNoExceptions() { getApi().findAllSorted(SearchField.id.toString(), Sort.Direction.DESC.name()); } @Test public final void whenResourcesAreRetrievedSortedAscById_thenResultsAreOrderedCorrectly() { final List<T> resourcesOrderedById = getApi().findAllSorted(SearchField.id.toString(), Sort.Direction.ASC.name()); assertTrue(new OrderById<T>().isOrdered(resourcesOrderedById)); } @Test public final void whenResourcesAreRetrievedSortedDescById_thenResultsAreOrderedCorrectly() { final List<T> resourcesOrderedById = getApi().findAllSorted(SearchField.id.toString(), Sort.Direction.DESC.name()); assertTrue(new OrderById<T>().reverse().isOrdered(resourcesOrderedById)); } // create @Test(expected = RuntimeException.class) public void whenNullResourceIsCreated_thenException() { getApi().create(null); } @Test public void whenResourceIsCreated_thenNoExceptions() { persistNewEntity(); } @Test public void whenResourceIsCreated_thenResourceIsRetrievable() { final T existingResource = persistNewEntity(); assertNotNull(getApi().findOne(existingResource.getId())); } @Test public void whenResourceIsCreated_thenSavedResourceIsEqualToOriginalResource() { final T originalResource = createNewEntity(); final T savedResource = getApi().create(originalResource); assertEquals(originalResource, savedResource); } @Test(expected = RuntimeException.class) public void whenResourceWithFailedConstraintsIsCreated_thenException() { final T invalidResource = createNewEntity(); invalidate(invalidResource); getApi().create(invalidResource); } /** * -- specific to the persistence engine */ @Test(expected = DataAccessException.class) @Ignore("Hibernate simply ignores the id silently and still saved (tracking this)") public void whenResourceWithIdIsCreated_thenDataAccessException() { final T resourceWithId = createNewEntity(); resourceWithId.setId(IDUtil.randomPositiveLong()); getApi().create(resourceWithId); } // update @Test(expected = RuntimeException.class) public void whenNullResourceIsUpdated_thenException() { getApi().update(null); } @Test public void givenResourceExists_whenResourceIsUpdated_thenNoExceptions() { // Given final T existingResource = persistNewEntity(); // When getApi().update(existingResource); } /** * - can also be the ConstraintViolationException which now occurs on the update operation will not be translated; as a consequence, it will be a TransactionSystemException */ @Test(expected = RuntimeException.class) public void whenResourceIsUpdatedWithFailedConstraints_thenException() { final T existingResource = persistNewEntity(); invalidate(existingResource); getApi().update(existingResource); } @Test public void givenResourceExists_whenResourceIsUpdated_thenUpdatesArePersisted() { // Given final T existingResource = persistNewEntity(); // When change(existingResource); getApi().update(existingResource); final T updatedResource = getApi().findOne(existingResource.getId()); // Then assertEquals(existingResource, updatedResource); } // delete @Test(expected = RuntimeException.class) public void givenResourceDoesNotExists_whenResourceIsDeleted_thenException() { // When getApi().delete(IDUtil.randomPositiveLong()); } @Test(expected = RuntimeException.class) public void whenResourceIsDeletedByNegativeId_thenException() { // When getApi().delete(IDUtil.randomNegativeLong()); } @Test public void givenResourceExists_whenResourceIsDeleted_thenNoExceptions() { // Given final T existingResource = persistNewEntity(); // When getApi().delete(existingResource.getId()); } @Test public final void givenResourceExists_whenResourceIsDeleted_thenResourceNoLongerExists() { // Given final T existingResource = persistNewEntity(); // When getApi().delete(existingResource.getId()); // Then assertNull(getApi().findOne(existingResource.getId())); } // template method protected abstract void invalidate(T invalidResource); protected abstract void change(T invalidResource); protected abstract T createNewEntity(); protected abstract IRawService<T> getApi(); protected T persistNewEntity() { return getApi().create(createNewEntity()); } }