From 08c6a9db4999bd79e8e133bd15351c92f40b0c61 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Mon, 8 Mar 2021 13:56:59 -0500 Subject: [PATCH] Correction du test update(dto) --- build.gradle.kts | 2 +- .../service/AbstractServiceTest.kt | 80 +++++++++---------- .../service/AccountsServiceTest.kt | 9 ++- .../service/CompanyServiceTest.kt | 8 ++ .../service/MaterialServiceTest.kt | 30 +++---- .../service/MaterialTypeServiceTest.kt | 3 + .../service/MixServiceTest.kt | 61 ++++++++++++++ .../service/RecipeServiceTest.kt | 8 +- 8 files changed, 139 insertions(+), 62 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 46876d0..9b2fb99 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -81,7 +81,7 @@ tasks.test { useJUnitPlatform() testLogging { - events("passed", "skipped", "failed") + events("skipped", "failed") } } diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AbstractServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AbstractServiceTest.kt index 097aaec..ff0baf2 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AbstractServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AbstractServiceTest.kt @@ -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, R : JpaRepository> { protected abstract val repository: R @@ -93,7 +90,7 @@ abstract class AbstractServiceTest, R : JpaRepository } abstract class AbstractModelServiceTest, R : JpaRepository> : - AbstractServiceTest() { + AbstractServiceTest1() { // existsById() @@ -279,10 +276,15 @@ abstract class AbstractNamedModelServiceTest, U : EntityDto, S : ExternalModelService, R : JpaRepository> : - AbstractModelServiceTest() { + AbstractModelServiceTest(), ExternalModelServiceTest { protected abstract val entitySaveDto: N protected abstract val entityUpdateDto: U @@ -292,21 +294,12 @@ abstract class AbstractExternalModelServiceTest, 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, U : EntityDto, S : ExternalNamedModelService, R : NamedJpaRepository> : - AbstractNamedModelServiceTest() { + AbstractNamedModelServiceTest(), ExternalModelServiceTest { protected abstract val entitySaveDto: N protected abstract val entityUpdateDto: U @@ -316,20 +309,16 @@ abstract class AbstractExternalNamedModelServiceTest> saveDtoTest(entity: E, entitySaveDto: N, service: ExternalService) { +fun > withBaseSaveDtoTest( + entity: E, + entitySaveDto: N, + service: ExternalService, + op: () -> Unit = {} +) { doReturn(entity).whenever(service).save(entity) doReturn(entity).whenever(entitySaveDto).toEntity() @@ -337,21 +326,26 @@ fun > saveDtoTest(entity: E, entitySaveDto: N, service: Exte verify(service).save(entity) assertEquals(entity, found) + + op() } -fun > updateDtoTest( +fun > withBaseUpdateDtoTest( entity: E, entityUpdateDto: U, - service: ExternalModelService + service: ExternalModelService, + 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() } diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt index da872a2..b862d96 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt @@ -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 { diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/CompanyServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/CompanyServiceTest.kt index 450336a..458f688 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/CompanyServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/CompanyServiceTest.kt @@ -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()) } diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt index d8b0303..24ed8a4 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt @@ -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()) + 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.contains(material: Material): Boolean = diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialTypeServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialTypeServiceTest.kt index 04e85a3..802cdd3 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialTypeServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialTypeServiceTest.kt @@ -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) diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixServiceTest.kt index 71dc035..30e38e0 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixServiceTest.kt @@ -73,6 +73,67 @@ class MixServiceTest : AbstractExternalModelServiceTest()) + + 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()) + + 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 diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeServiceTest.kt index 6886983..b03d4af 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeServiceTest.kt @@ -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