Correction du test update(dto)

This commit is contained in:
FyloZ 2021-03-08 13:56:59 -05:00
parent 1d4bd01ed5
commit 08c6a9db49
8 changed files with 139 additions and 62 deletions

View File

@ -81,7 +81,7 @@ tasks.test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
events("skipped", "failed")
}
}

View File

@ -1,9 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.reset
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import com.nhaarman.mockitokotlin2.*
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.model.EntityDto
@ -11,7 +8,6 @@ import dev.fyloz.trial.colorrecipesexplorer.model.Model
import dev.fyloz.trial.colorrecipesexplorer.model.NamedModel
import dev.fyloz.trial.colorrecipesexplorer.repository.NamedJpaRepository
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.data.jpa.repository.JpaRepository
@ -19,6 +15,7 @@ import java.util.*
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import dev.fyloz.trial.colorrecipesexplorer.service.AbstractServiceTest as AbstractServiceTest1
abstract class AbstractServiceTest<E, S : Service<E, *>, R : JpaRepository<E, *>> {
protected abstract val repository: R
@ -93,7 +90,7 @@ abstract class AbstractServiceTest<E, S : Service<E, *>, R : JpaRepository<E, *>
}
abstract class AbstractModelServiceTest<E : Model, S : ModelService<E, *>, R : JpaRepository<E, Long>> :
AbstractServiceTest<E, S, R>() {
AbstractServiceTest1<E, S, R>() {
// existsById()
@ -279,10 +276,15 @@ abstract class AbstractNamedModelServiceTest<E : NamedModel, S : NamedModelServi
}
}
interface ExternalModelServiceTest {
fun `save(dto) calls and returns save() with the created entity`()
fun `update(dto) calls and returns update() with the created entity`()
}
// ==== IMPLEMENTATIONS FOR EXTERNAL SERVICES ====
// Lots of code duplication but I don't have a better solution for now
abstract class AbstractExternalModelServiceTest<E : Model, N : EntityDto<E>, U : EntityDto<E>, S : ExternalModelService<E, N, U, *>, R : JpaRepository<E, Long>> :
AbstractModelServiceTest<E, S, R>() {
AbstractModelServiceTest<E, S, R>(), ExternalModelServiceTest {
protected abstract val entitySaveDto: N
protected abstract val entityUpdateDto: U
@ -292,21 +294,12 @@ abstract class AbstractExternalModelServiceTest<E : Model, N : EntityDto<E>, U :
super.afterEach()
}
// save()
@Test
open fun `save(dto) calls and returns save() with the created entity`() =
saveDtoTest(entity, entitySaveDto, service)
// update()
@Test
open fun `update(dto) calls and returns update() with the created entity`() =
updateDtoTest(entity, entityUpdateDto, service)
override fun `save(dto) calls and returns save() with the created entity`() =
withBaseSaveDtoTest(entity, entitySaveDto, service)
}
abstract class AbstractExternalNamedModelServiceTest<E : NamedModel, N : EntityDto<E>, U : EntityDto<E>, S : ExternalNamedModelService<E, N, U, *>, R : NamedJpaRepository<E>> :
AbstractNamedModelServiceTest<E, S, R>() {
AbstractNamedModelServiceTest<E, S, R>(), ExternalModelServiceTest {
protected abstract val entitySaveDto: N
protected abstract val entityUpdateDto: U
@ -316,20 +309,16 @@ abstract class AbstractExternalNamedModelServiceTest<E : NamedModel, N : EntityD
super.afterEach()
}
// save()
@Test
open fun `save(dto) calls and returns save() with the created entity`() =
saveDtoTest(entity, entitySaveDto, service)
// update()
@Test
open fun `update(dto) calls and returns update() with the created entity`() =
updateDtoTest(entity, entityUpdateDto, service)
override fun `save(dto) calls and returns save() with the created entity`() =
withBaseSaveDtoTest(entity, entitySaveDto, service)
}
fun <E, N : EntityDto<E>> saveDtoTest(entity: E, entitySaveDto: N, service: ExternalService<E, N, *, *>) {
fun <E, N : EntityDto<E>> withBaseSaveDtoTest(
entity: E,
entitySaveDto: N,
service: ExternalService<E, N, *, *>,
op: () -> Unit = {}
) {
doReturn(entity).whenever(service).save(entity)
doReturn(entity).whenever(entitySaveDto).toEntity()
@ -337,21 +326,26 @@ fun <E, N : EntityDto<E>> saveDtoTest(entity: E, entitySaveDto: N, service: Exte
verify(service).save(entity)
assertEquals(entity, found)
op()
}
fun <E : Model, U : EntityDto<E>> updateDtoTest(
fun <E : Model, U : EntityDto<E>> withBaseUpdateDtoTest(
entity: E,
entityUpdateDto: U,
service: ExternalModelService<E, *, U, *>
service: ExternalModelService<E, *, U, *>,
updateMockMatcher: E,
op: () -> Unit = {}
) {
// doReturn(entity).whenever(service).update(entity)
// doReturn(entity).whenever(entityUpdateDto).toEntity()
// doReturn(entity).whenever(service).getById(entity.id!!)
// doReturn(true).whenever(service).existsById(entity.id!!)
//
// val found = service.update(entityUpdateDto)
//
// verify(service).update(entity)
// assertEquals(entity, found)
assertTrue(true, "Disabled because the wrong methods are mocked for some reason")
doAnswer { it.arguments[0] }.whenever(service).update(updateMockMatcher)
doReturn(entity).whenever(entityUpdateDto).toEntity()
doReturn(entity).whenever(service).getById(entity.id!!)
doReturn(true).whenever(service).existsById(entity.id!!)
val found = service.update(entityUpdateDto)
verify(service).update(updateMockMatcher)
assertEquals(entity, found)
op()
}

View File

@ -1,7 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import com.nhaarman.mockitokotlin2.*
import dev.fyloz.trial.colorrecipesexplorer.config.SecurityConfigurationProperties
import dev.fyloz.trial.colorrecipesexplorer.config.defaultGroupCookieName
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException
@ -169,6 +168,9 @@ class EmployeeServiceTest :
// update()
override fun `update(dto) calls and returns update() with the created entity`() =
withBaseUpdateDtoTest(entity, entityUpdateDto, service, any())
@Test
fun `update() throws EntityAlreadyExistsRestException when a different employee with the given first name and last name exists`() {
whenever(repository.findByFirstNameAndLastName(entity.firstName, entity.lastName)).doReturn(
@ -392,6 +394,11 @@ class EmployeeGroupServiceTest :
verify(service, times(0)).update(group)
verify(employeeService, times(0)).update(employee)
}
// update()
override fun `update(dto) calls and returns update() with the created entity`() =
withBaseUpdateDtoTest(entity, entityUpdateDto, service, any())
}
class EmployeeUserDetailsServiceTest {

View File

@ -6,6 +6,7 @@ import dev.fyloz.trial.colorrecipesexplorer.repository.CompanyRepository
import dev.fyloz.trial.colorrecipesexplorer.service.model.RecipeJavaService
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
@ -29,6 +30,7 @@ class CompanyServiceTest :
// isLinkedToRecipes
@Test
fun `isLinkedToRecipes() returns true when a given company is linked to one or more recipes`() {
whenever(recipeService.existsByCompany(entity)).doReturn(true)
@ -37,6 +39,7 @@ class CompanyServiceTest :
assertTrue(found)
}
@Test
fun `isLinkedToRecipes() returns false when a given company is not linked to any recipe`() {
whenever(recipeService.existsByCompany(entity)).doReturn(false)
@ -44,4 +47,9 @@ class CompanyServiceTest :
assertFalse(found)
}
// update()
override fun `update(dto) calls and returns update() with the created entity`() =
withBaseUpdateDtoTest(entity, entityUpdateDto, service, any())
}

View File

@ -175,21 +175,21 @@ class MaterialServiceTest :
assertFalse(found contains anotherMixTypeMaterial)
}
// @Nested
// inner class UpdateDto {
// @Test
// fun `calls simdutService_update() with the updated entity`() {
// val mockSimdutFile = MockMultipartFile("simdut", byteArrayOf(0))
// val materialUpdateDto = spy(materialUpdateDto(id = 0L, simdutFile = mockSimdutFile))
//
// doReturn(entity).whenever(service).update(entity)
// doReturn(entity).whenever(materialUpdateDto).toEntity()
//
// service.update(materialUpdateDto)
//
// verify(simdutService).update(mockSimdutFile, entity)
// }
// }
// update()
@Test
override fun `update(dto) calls and returns update() with the created entity`() {
val mockSimdutFile = MockMultipartFile("simdut", byteArrayOf(1, 2, 3, 4, 5))
val materialUpdateDto = spy(materialUpdateDto(id = 0L, simdutFile = mockSimdutFile))
doReturn(entity).whenever(service).getById(materialUpdateDto.id)
doReturn(entity).whenever(service).update(any<Material>())
doReturn(entity).whenever(materialUpdateDto).toEntity()
service.update(materialUpdateDto)
verify(simdutService).update(eq(mockSimdutFile), any())
}
/** Helper function to replace collections.in because the id is not considered in the equals function of Material while Thymeleaf is supported. */
private infix fun Collection<Material>.contains(material: Material): Boolean =

View File

@ -109,6 +109,9 @@ class MaterialTypeServiceTest :
// update()
override fun `update(dto) calls and returns update() with the created entity`() =
withBaseUpdateDtoTest(entity, entityUpdateDto, service, any())
override fun `update() saves in the repository and returns the updated value`() {
whenever(repository.save(entity)).doReturn(entity)
whenever(repository.findByName(entity.name)).doReturn(null)

View File

@ -73,6 +73,67 @@ class MixServiceTest : AbstractExternalModelServiceTest<Mix, MixSaveDto, MixUpda
assertEquals(mixWithMaterials, found)
}
// update()
@Test
override fun `update(dto) calls and returns update() with the created entity`() {
val mixUpdateDto = spy(mixUpdateDto(id = 0L, name = null, materialTypeId = null))
doReturn(entity).whenever(service).getById(mixUpdateDto.id)
doReturn(entity).whenever(service).update(entity)
val found = service.update(mixUpdateDto)
verify(service).update(entity)
assertEquals(entity, found)
}
@Test
fun `update(dto) calls MixTypeService createForNameAndMaterialType() when mix type is shared`() {
val newMixType = mixType(name = "another mix type")
val mix = mix(id = 0L, mixType = mixType(name = "mix type"))
val materialType = materialType(id = 0L)
val mixUpdateDto = spy(mixUpdateDto(id = 0L, name = "mix", materialTypeId = 0L, mixMaterials = null))
doReturn(true).whenever(service).existsById(mix.id!!)
doReturn(mix).whenever(service).getById(mix.id!!)
doReturn(true).whenever(service).mixTypeIsShared(mix.mixType)
doAnswer { it.arguments[0] }.whenever(service).update(any<Mix>())
whenever(materialTypeService.getById(materialType.id!!)).doReturn(materialType)
whenever(mixTypeService.createForNameAndMaterialType(mixUpdateDto.name!!, materialType)).doReturn(newMixType)
val found = service.update(mixUpdateDto)
verify(mixTypeService).createForNameAndMaterialType(mixUpdateDto.name!!, materialType)
assertEquals(newMixType, found.mixType)
}
@Test
fun `update(dto) calls MixTypeService updateForNameAndMaterialType() when mix type is not shared`() {
val mixType = mixType(name = "mix type")
val newMixType = mixType(name = "another mix type")
val mix = mix(id = 0L, mixType = mixType)
val materialType = materialType(id = 0L)
val mixUpdateDto = spy(mixUpdateDto(id = 0L, name = "mix", materialTypeId = 0L, mixMaterials = null))
doReturn(true).whenever(service).existsById(mix.id!!)
doReturn(mix).whenever(service).getById(mix.id!!)
doReturn(false).whenever(service).mixTypeIsShared(mix.mixType)
doAnswer { it.arguments[0] }.whenever(service).update(any<Mix>())
whenever(materialTypeService.getById(materialType.id!!)).doReturn(materialType)
whenever(mixTypeService.updateForNameAndMaterialType(mixType, mixUpdateDto.name!!, materialType)).doReturn(newMixType)
val found = service.update(mixUpdateDto)
verify(mixTypeService).updateForNameAndMaterialType(mixType, mixUpdateDto.name!!, materialType)
assertEquals(newMixType, found.mixType)
}
// updateLocation()
@Test

View File

@ -6,7 +6,6 @@ import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.repository.RecipeRepository
import dev.fyloz.trial.colorrecipesexplorer.service.files.FilesService
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.mock.web.MockMultipartFile
@ -66,9 +65,14 @@ class RecipeServiceTest :
@Test
override fun `save(dto) calls and returns save() with the created entity`() {
whenever(companyService.getById(company.id!!)).doReturn(company)
saveDtoTest(entity, entitySaveDto, service)
withBaseSaveDtoTest(entity, entitySaveDto, service)
}
// update()
override fun `update(dto) calls and returns update() with the created entity`() =
withBaseUpdateDtoTest(entity, entityUpdateDto, service, any())
// updatePublicData()
@Test