diff --git a/persistence/src/test/kotlin/com/github/jactor/persistence/AbstractSpringBootNoDirtyContextTest.kt b/persistence/src/test/kotlin/com/github/jactor/persistence/AbstractSpringBootNoDirtyContextTest.kt index 1a80ea80..0b7ce301 100644 --- a/persistence/src/test/kotlin/com/github/jactor/persistence/AbstractSpringBootNoDirtyContextTest.kt +++ b/persistence/src/test/kotlin/com/github/jactor/persistence/AbstractSpringBootNoDirtyContextTest.kt @@ -40,9 +40,6 @@ abstract class AbstractSpringBootNoDirtyContextTest { @Autowired protected lateinit var blogEntryRepository: BlogEntryRepository - @Autowired - protected lateinit var entityManager: EntityManager - @Autowired protected lateinit var guestBookRepository: GuestBookRepository @@ -75,4 +72,17 @@ abstract class AbstractSpringBootNoDirtyContextTest { @Value("\${server.servlet.context-path}") protected lateinit var contextPath: String + + // database operations + + @Autowired + private lateinit var entityManager: EntityManager + + protected fun flush(databaseOperation: () -> T): T { + val entity = databaseOperation.invoke() + entityManager.flush() + entityManager.clear() + + return entity + } } diff --git a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/AddressRepositoryTest.kt b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/AddressRepositoryTest.kt index 941d6474..f189eb3f 100644 --- a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/AddressRepositoryTest.kt +++ b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/AddressRepositoryTest.kt @@ -15,33 +15,31 @@ import assertk.assertions.isPresent internal class AddressRepositoryTest : AbstractSpringBootNoDirtyContextTest() { @Test fun `should fetch address entities`() { - addressRepository.save( - AddressBuilder - .new( - addressInternalDto = AddressInternalDto( - zipCode = "1234", - addressLine1 = "somewhere out there", - city = "Rud" + flush { + addressRepository.save( + AddressBuilder + .new( + addressInternalDto = AddressInternalDto( + zipCode = "1234", + addressLine1 = "somewhere out there", + city = "Rud" + ) ) - ) - .build() - ) + .build() + ) - addressRepository.save( - AddressBuilder - .new( - addressInternalDto = AddressInternalDto( - zipCode = "1234", - addressLine1 = "somewhere in there", - city = "Rud" + addressRepository.save( + AddressBuilder + .new( + addressInternalDto = AddressInternalDto( + zipCode = "1234", + addressLine1 = "somewhere in there", + city = "Rud" + ) ) - ) - .build() - ) - - entityManager.flush() - entityManager.clear() - + .build() + ) + } val addressEntities = addressRepository.findByZipCode(zipCode = "1234") assertAll { @@ -66,9 +64,7 @@ internal class AddressRepositoryTest : AbstractSpringBootNoDirtyContextTest() { ) ).build() - addressRepository.save(addressEntityToPersist) - entityManager.flush() - entityManager.clear() + flush { addressRepository.save(addressEntityToPersist) } val possibleAddressEntityById = addressRepository.findById(addressEntityToPersist.id!!) @@ -99,9 +95,7 @@ internal class AddressRepositoryTest : AbstractSpringBootNoDirtyContextTest() { ) .build() - addressRepository.save(addressEntityToPersist) - entityManager.flush() - entityManager.clear() + flush { addressRepository.save(addressEntityToPersist) } val addressEntitySaved = addressRepository.findById(addressEntityToPersist.id!!).orElseThrow { addressNotFound() }!! @@ -112,9 +106,7 @@ internal class AddressRepositoryTest : AbstractSpringBootNoDirtyContextTest() { addressEntitySaved.city = "Cloud city" addressEntitySaved.country = "XX" - addressRepository.save(addressEntitySaved) - entityManager.flush() - entityManager.clear() + flush { addressRepository.save(addressEntitySaved) } val possibleAddressEntityById = addressRepository.findById(addressEntityToPersist.id!!) diff --git a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/BlogEntryRepositoryTest.kt b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/BlogEntryRepositoryTest.kt index d86813b5..3c46ab70 100644 --- a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/BlogEntryRepositoryTest.kt +++ b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/BlogEntryRepositoryTest.kt @@ -19,7 +19,7 @@ import assertk.assertions.hasSize import assertk.assertions.isEqualTo import assertk.assertions.isStrictlyBetween -internal class BlogEntryRepositoryTest: AbstractSpringBootNoDirtyContextTest() { +internal class BlogEntryRepositoryTest : AbstractSpringBootNoDirtyContextTest() { @Test fun `should save then read blog entry`() { val addressDto = AddressInternalDto( @@ -47,9 +47,7 @@ internal class BlogEntryRepositoryTest: AbstractSpringBootNoDirtyContextTest() { blogEntryDto = BlogEntryDto(blog = blogDto, creatorName = "smith", entry = "once upon a time") ) - blogEntryRepository.save(blogData.buildBlogEntryEntity()) - entityManager.flush() - entityManager.clear() + flush { blogEntryRepository.save(blogData.buildBlogEntryEntity()) } val blogEntries = blogEntryRepository.findAll().toList() @@ -91,9 +89,7 @@ internal class BlogEntryRepositoryTest: AbstractSpringBootNoDirtyContextTest() { .withEntry(BlogEntryDto(creatorName = "smith", entry = "once upon a time")) .buildBlogEntryEntity() - blogEntryRepository.save(blogEntryToSave) - entityManager.flush() - entityManager.clear() + flush { blogEntryRepository.save(blogEntryToSave) } val blogEntries = blogEntryRepository.findAll().toList() @@ -102,9 +98,7 @@ internal class BlogEntryRepositoryTest: AbstractSpringBootNoDirtyContextTest() { val blogEntry = blogEntries.iterator().next() blogEntry.modify("happily ever after", "luke") - blogEntryRepository.save(blogEntry) - entityManager.flush() - entityManager.clear() + flush { blogEntryRepository.save(blogEntry) } val modifiedEntries = blogEntryRepository.findAll().toList() diff --git a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/BlogRepositoryTest.kt b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/BlogRepositoryTest.kt index 98335e4d..d61257d0 100644 --- a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/BlogRepositoryTest.kt +++ b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/BlogRepositoryTest.kt @@ -48,8 +48,6 @@ internal class BlogRepositoryTest : AbstractSpringBootNoDirtyContextTest(){ .buildBlogEntity() blogRepository.save(blogEntityToSave) - entityManager.flush() - entityManager.clear() val blogs = blogRepository.findAll().toList() assertThat(blogs).hasSize(1) @@ -89,8 +87,6 @@ internal class BlogRepositoryTest : AbstractSpringBootNoDirtyContextTest(){ .buildBlogEntity() blogRepository.save(blogEntityToSave) - entityManager.flush() - entityManager.clear() val blogs = blogRepository.findBlogsByTitle("Blah") assertThat(blogs).hasSize(1) @@ -99,8 +95,6 @@ internal class BlogRepositoryTest : AbstractSpringBootNoDirtyContextTest(){ blogEntitySaved.title = "Duh" blogRepository.save(blogEntitySaved) - entityManager.flush() - entityManager.clear() val modifiedBlogs = blogRepository.findBlogsByTitle("Duh") assertThat(modifiedBlogs).hasSize(1) @@ -139,8 +133,6 @@ internal class BlogRepositoryTest : AbstractSpringBootNoDirtyContextTest(){ .buildBlogEntity() blogRepository.save(blogEntityToSave) - entityManager.flush() - entityManager.clear() val blogs = blogRepository.findBlogsByTitle("Blah") @@ -194,8 +186,6 @@ internal class BlogRepositoryTest : AbstractSpringBootNoDirtyContextTest(){ blogEntityToSave.add(blogEntryToSave) blogRepository.save(blogEntityToSave) - entityManager.flush() - entityManager.clear() val blogs = blogRepository.findBlogsByTitle("Blah") assertThat(blogs).hasSize(1) diff --git a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/GuestBookEntryRepositoryTest.kt b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/GuestBookEntryRepositoryTest.kt index 1a2a7408..94e3e845 100644 --- a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/GuestBookEntryRepositoryTest.kt +++ b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/GuestBookEntryRepositoryTest.kt @@ -19,7 +19,7 @@ import assertk.assertions.hasSize import assertk.assertions.isEqualTo import assertk.assertions.isNotNull -internal class GuestBookEntryRepositoryTest: AbstractSpringBootNoDirtyContextTest() { +internal class GuestBookEntryRepositoryTest : AbstractSpringBootNoDirtyContextTest() { @Test fun `should save then read guest book entry entity`() { val addressDto = AddressBuilder.new( @@ -59,9 +59,7 @@ internal class GuestBookEntryRepositoryTest: AbstractSpringBootNoDirtyContextTes ) ) - guestBookEntryRepository.save(guestBookData.buildGuestBookEntryEntity()) - entityManager.flush() - entityManager.clear() + flush { guestBookEntryRepository.save(guestBookData.buildGuestBookEntryEntity()) } val entriesByGuestBook = guestBookEntryRepository.findByGuestBook(savedGuestBook) assertThat(entriesByGuestBook).hasSize(1) @@ -102,27 +100,24 @@ internal class GuestBookEntryRepositoryTest: AbstractSpringBootNoDirtyContextTes ) val savedGuestBook = guestBookRepository.save(guestBookData.buildGuestBookEntity()) - guestBookEntryRepository.save( - guestBookData.withEntry( - GuestBookEntryDto( - guestBook = savedGuestBook.asDto(), - creatorName = "Harry", - entry = "Draco Dormiens Nunquam Tittilandus" - ) - ).buildGuestBookEntryEntity() - ) - entityManager.flush() - entityManager.clear() + flush { + guestBookEntryRepository.save( + guestBookData.withEntry( + GuestBookEntryDto( + guestBook = savedGuestBook.asDto(), + creatorName = "Harry", + entry = "Draco Dormiens Nunquam Tittilandus" + ) + ).buildGuestBookEntryEntity() + ) + } val entriesByGuestBook = guestBookEntryRepository.findByGuestBook(savedGuestBook) assertThat(entriesByGuestBook).hasSize(1) entriesByGuestBook.iterator().next().modify("Willie", "On the road again") - guestBookEntryRepository.save(entriesByGuestBook.iterator().next()) - - entityManager.flush() - entityManager.clear() + flush { guestBookEntryRepository.save(entriesByGuestBook.iterator().next()) } val modifiedEntriesByGuestBook = guestBookEntryRepository.findByGuestBook(savedGuestBook) assertThat(modifiedEntriesByGuestBook).hasSize(1) @@ -200,9 +195,7 @@ internal class GuestBookEntryRepositoryTest: AbstractSpringBootNoDirtyContextTes ) ).buildGuestBookEntryEntity() - guestBookEntryRepository.save(anotherEntry) - entityManager.flush() - entityManager.clear() + flush { guestBookEntryRepository.save(anotherEntry) } val lastEntry = guestBookRepository.findAll().toList() .flatMap { it.getEntries() } diff --git a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/GuestBookRepositoryTest.kt b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/GuestBookRepositoryTest.kt index 854a386d..54ab53fd 100644 --- a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/GuestBookRepositoryTest.kt +++ b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/GuestBookRepositoryTest.kt @@ -2,6 +2,7 @@ package com.github.jactor.persistence.repository import java.util.UUID import org.junit.jupiter.api.Test +import org.junit.jupiter.api.fail import com.github.jactor.persistence.AbstractSpringBootNoDirtyContextTest import com.github.jactor.persistence.dto.AddressInternalDto import com.github.jactor.persistence.dto.GuestBookDto @@ -12,12 +13,13 @@ import com.github.jactor.persistence.entity.AddressBuilder import com.github.jactor.persistence.entity.GuestBookBuilder import com.github.jactor.persistence.entity.PersonBuilder import com.github.jactor.persistence.entity.UserBuilder +import com.github.jactor.persistence.entity.UserEntity import assertk.assertAll import assertk.assertThat import assertk.assertions.isEqualTo import assertk.assertions.isNotNull -internal class GuestBookRepositoryTest: AbstractSpringBootNoDirtyContextTest() { +internal class GuestBookRepositoryTest : AbstractSpringBootNoDirtyContextTest() { @Test fun `should write then read guest book`() { val addressDto = AddressBuilder @@ -55,8 +57,7 @@ internal class GuestBookRepositoryTest: AbstractSpringBootNoDirtyContextTest() { ).buildGuestBookEntity() ) - entityManager.flush() - entityManager.clear() + flush { } val guestBookEntity = guestBookRepository.findByUser(userEntity) @@ -96,17 +97,13 @@ internal class GuestBookRepositoryTest: AbstractSpringBootNoDirtyContextTest() { ).buildGuestBookEntity() ) - guestBookRepository.save(userEntity.guestBook!!) - entityManager.flush() - entityManager.clear() + flush { guestBookRepository.save(userEntity.guestBook ?: fail(message = "User missing guest book")) } val guestBookEntityToUpdate = guestBookRepository.findByUser(userEntity) guestBookEntityToUpdate!!.title = "5000 thousands miles away from home" - guestBookRepository.save(guestBookEntityToUpdate) - entityManager.flush() - entityManager.clear() + flush { guestBookRepository.save(guestBookEntityToUpdate) } val guestBookEntity = guestBookRepository.findByUser(userEntity) diff --git a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/PersonRepositoryTest.kt b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/PersonRepositoryTest.kt index 875e8969..99159209 100644 --- a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/PersonRepositoryTest.kt +++ b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/PersonRepositoryTest.kt @@ -48,9 +48,7 @@ internal class PersonRepositoryTest: AbstractSpringBootNoDirtyContextTest() { ) ).build() - personRepository.save(personToPersist) - entityManager.flush() - entityManager.clear() + flush { personRepository.save(personToPersist) } val people = personRepository.findAll().toList() assertThat(people).hasSize(allreadyPresentPeople + 1) @@ -82,9 +80,7 @@ internal class PersonRepositoryTest: AbstractSpringBootNoDirtyContextTest() { ) ).build() - personRepository.save(personToPersist) - entityManager.flush() - entityManager.clear() + flush { personRepository.save(personToPersist) } val mine = personRepository.findBySurname("Mine") val person = mine.iterator().next() @@ -94,9 +90,7 @@ internal class PersonRepositoryTest: AbstractSpringBootNoDirtyContextTest() { person.firstName = "Dr. A." person.surname = "Cula" - personRepository.save(person) - entityManager.flush() - entityManager.clear() + flush { personRepository.save(person) } val foundCula = personRepository.findBySurname("Cula") val personEntity = foundCula.iterator().next() @@ -131,9 +125,7 @@ internal class PersonRepositoryTest: AbstractSpringBootNoDirtyContextTest() { val userEntity = UserBuilder.new(userDto = userInternalDto).build() val personToPersist = userEntity.fetchPerson() - personRepository.save(personToPersist) - entityManager.flush() - entityManager.clear() + flush { personRepository.save(personToPersist) } assertThat(personRepository.findAll().toList()).hasSize(alreadyPresentPeople + 1) val personEntity = personRepository.findBySurname("Adder").iterator().next() diff --git a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/RepositoriesTest.kt b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/RepositoriesTest.kt index 398b6708..5436417a 100644 --- a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/RepositoriesTest.kt +++ b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/RepositoriesTest.kt @@ -38,25 +38,22 @@ internal class RepositoriesTest : AbstractSpringBootNoDirtyContextTest() { ) ).build() - userRepository.save(userToPersist) - entityManager.flush() - entityManager.clear() + flush { userRepository.save(userToPersist) } var userByUsername = userRepository.findByUsername("r2d2") .orElseThrow { AssertionError("User not found!") } - blogRepository.save( - BlogBuilder.new( - blogDto = BlogDto( - created = LocalDate.now(), - title = "Far, far, away...", - userInternal = userByUsername.asDto() - ) - ).buildBlogEntity() - ) - - entityManager.flush() - entityManager.clear() + flush { + blogRepository.save( + BlogBuilder.new( + blogDto = BlogDto( + created = LocalDate.now(), + title = "Far, far, away...", + userInternal = userByUsername.asDto() + ) + ).buildBlogEntity() + ) + } userByUsername = userRepository.findByUsername("r2d2") .orElseThrow { AssertionError("User not found!") } diff --git a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/UserRepositoryTest.kt b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/UserRepositoryTest.kt index 2cc3f15c..9199b059 100644 --- a/persistence/src/test/kotlin/com/github/jactor/persistence/repository/UserRepositoryTest.kt +++ b/persistence/src/test/kotlin/com/github/jactor/persistence/repository/UserRepositoryTest.kt @@ -16,7 +16,7 @@ import assertk.assertThat import assertk.assertions.containsAtLeast import assertk.assertions.isEqualTo -internal class UserRepositoryTest: AbstractSpringBootNoDirtyContextTest() { +internal class UserRepositoryTest : AbstractSpringBootNoDirtyContextTest() { @Test fun `should find user with username jactor`() { val userByName = userRepository.findByUsername("jactor") @@ -48,9 +48,7 @@ internal class UserRepositoryTest: AbstractSpringBootNoDirtyContextTest() { ) ).build() - userRepository.save(userToPersist) - entityManager.flush() - entityManager.clear() + flush { userRepository.save(userToPersist) } val userById = userRepository.findByUsername("smuggler") val userEntity = userById.orElseThrow { userNotFound() } @@ -83,17 +81,13 @@ internal class UserRepositoryTest: AbstractSpringBootNoDirtyContextTest() { ) ).build() - userRepository.save(userToPersist) - entityManager.flush() - entityManager.clear() + flush { userRepository.save(userToPersist) } val lukewarm = "lukewarm" userToPersist.username = lukewarm userToPersist.emailAddress = "luke@force.com" - userRepository.save(userToPersist) - entityManager.flush() - entityManager.clear() + flush { userRepository.save(userToPersist) } val userByName = userRepository.findByUsername(lukewarm) val userEntity = userByName.orElseThrow { userNotFound() } @@ -128,19 +122,19 @@ internal class UserRepositoryTest: AbstractSpringBootNoDirtyContextTest() { UserInternalDto(PersistentDto(), spidyPersonInternalDto, null, "spiderman") ).build() - userRepository.save(userEntity) - userRepository.save( - UserBuilder.new( - userDto = UserInternalDto( - person = superPersonInternalDto, - emailAddress = null, - username = "superman", - usertype = Usertype.INACTIVE - ) - ).build() - ) - entityManager.flush() - entityManager.clear() + flush { + userRepository.save(userEntity) + userRepository.save( + UserBuilder.new( + userDto = UserInternalDto( + person = superPersonInternalDto, + emailAddress = null, + username = "superman", + usertype = Usertype.INACTIVE + ) + ).build() + ) + } val usernames = userRepository.findByUserTypeIn(listOf(UserEntity.UserType.ACTIVE, UserEntity.UserType.ADMIN)) .map(UserEntity::username) diff --git a/persistence/src/test/kotlin/com/github/jactor/persistence/test/Database.kt b/persistence/src/test/kotlin/com/github/jactor/persistence/test/Database.kt deleted file mode 100644 index 57f0c401..00000000 --- a/persistence/src/test/kotlin/com/github/jactor/persistence/test/Database.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.github.jactor.persistence.test - -object Database { - const val ID_ADDRESS = "1130f2b7-7b51-49b2-b982-d636e610ee5f" - const val ID_JACTOR = "3c613503-f55d-4e60-8986-a37b122bb6fe" - const val ID_SUTHATIP = "3b227477-cf90-47fa-b5dc-27e9ee87851c" - const val ID_TIP = "4d54ae40-08e4-4804-969f-c4feac8ea8cd" - const val ID_TOREGIL = "a3378f9d-902b-436e-9626-535907df520e" -}