From 689bd2a602bc27df82e2915a2a1f7b270f6daf2b Mon Sep 17 00:00:00 2001 From: FyloZ Date: Tue, 13 Apr 2021 11:11:14 -0400 Subject: [PATCH] Correction des tests --- .../config/WebSecurityConfig.kt | 3 ++- .../model/EmployeePermission.kt | 1 + .../colorrecipesexplorer/service/Service.kt | 4 ---- .../service/AbstractServiceTest.kt | 10 ---------- .../service/CompanyServiceTest.kt | 18 +++++++++++++++++- .../service/MaterialServiceTest.kt | 18 +++++++++++++++++- .../service/MaterialTypeServiceTest.kt | 19 ++++++++++++++----- .../service/MixServiceTest.kt | 19 +++++++++++++++++-- .../service/MixTypeServiceTest.kt | 19 +++++++++++++++++-- .../service/RecipeServiceTest.kt | 2 +- 10 files changed, 86 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/config/WebSecurityConfig.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/config/WebSecurityConfig.kt index 681bf0e..0d4834c 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/config/WebSecurityConfig.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/config/WebSecurityConfig.kt @@ -7,6 +7,7 @@ import dev.fyloz.colorrecipesexplorer.model.EmployeeLoginRequest import dev.fyloz.colorrecipesexplorer.model.EmployeePermission import dev.fyloz.colorrecipesexplorer.service.EmployeeService import dev.fyloz.colorrecipesexplorer.service.EmployeeServiceImpl +import dev.fyloz.colorrecipesexplorer.service.EmployeeUserDetailsService import dev.fyloz.colorrecipesexplorer.service.EmployeeUserDetailsServiceImpl import io.jsonwebtoken.ExpiredJwtException import io.jsonwebtoken.Jwts @@ -219,7 +220,7 @@ class JwtAuthenticationFilter( } class JwtAuthorizationFilter( - private val userDetailsService: EmployeeUserDetailsServiceImpl, + private val userDetailsService: EmployeeUserDetailsService, private val securityConfigurationProperties: SecurityConfigurationProperties, authenticationManager: AuthenticationManager ) : BasicAuthenticationFilter(authenticationManager) { diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/EmployeePermission.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/EmployeePermission.kt index a12ae7b..e6ef1b7 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/EmployeePermission.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/EmployeePermission.kt @@ -2,6 +2,7 @@ package dev.fyloz.colorrecipesexplorer.model import org.springframework.security.core.GrantedAuthority import org.springframework.security.core.authority.SimpleGrantedAuthority +import java.util.* enum class EmployeePermission( val impliedPermissions: List = listOf(), diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/Service.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/Service.kt index 918247c..ef45f8e 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/Service.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/Service.kt @@ -51,9 +51,6 @@ interface NamedModelService> : ModelServ /** Gets the entity with the given [name]. */ fun getByName(name: String): E - - /** Deletes the entity with the given [name]. */ - fun deleteByName(name: String) } @@ -100,7 +97,6 @@ abstract class AbstractNamedModelService { service.update(entity) } - .assertErrorCode("name") } @Test @@ -261,15 +260,6 @@ abstract class AbstractNamedModelServiceTest { service.update(entity) } .assertErrorCode("name") } - - // deleteByName() - - @Test - open fun `deleteByName() deletes the entity with the given name in the repository`() { - service.deleteByName(entity.name) - - verify(repository).deleteByName(entity.name) - } } interface ExternalModelServiceTest { diff --git a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/CompanyServiceTest.kt b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/CompanyServiceTest.kt index a2ebbc7..75dfba1 100644 --- a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/CompanyServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/CompanyServiceTest.kt @@ -61,7 +61,23 @@ class CompanyServiceTest : // delete() + override fun `delete() deletes in the repository`() { + whenCanBeDeleted { + super.`delete() deletes in the repository`() + } + } + + // deleteById() + override fun `deleteById() deletes the entity with the given id in the repository`() { - super.`deleteById() deletes the entity with the given id in the repository`() + whenCanBeDeleted { + super.`deleteById() deletes the entity with the given id in the repository`() + } + } + + private fun whenCanBeDeleted(id: Long = any(), test: () -> Unit) { + whenever(repository.canBeDeleted(id)).doReturn(true) + + test() } } diff --git a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MaterialServiceTest.kt b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MaterialServiceTest.kt index 6da874e..9def652 100644 --- a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MaterialServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MaterialServiceTest.kt @@ -239,11 +239,27 @@ class MaterialServiceTest : // delete() + override fun `delete() deletes in the repository`() { + whenCanBeDeleted { + super.`delete() deletes in the repository`() + } + } + + // deleteById() + override fun `deleteById() deletes the entity with the given id in the repository`() { - super.`deleteById() deletes the entity with the given id in the repository`() + whenCanBeDeleted { + super.`deleteById() deletes the entity with the given id in the repository`() + } } /** Utility property to check if the identifier of the given [Material] is even. */ private val Material.evenId: Boolean get() = this.id!! % 2 == 0L + + private fun whenCanBeDeleted(id: Long = any(), test: () -> Unit) { + whenever(repository.canBeDeleted(id)).doReturn(true) + + test() + } } diff --git a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MaterialTypeServiceTest.kt b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MaterialTypeServiceTest.kt index 6846394..797c1d0 100644 --- a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MaterialTypeServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MaterialTypeServiceTest.kt @@ -164,12 +164,21 @@ class MaterialTypeServiceTest : // delete() - @Test - fun `delete() calls delete() in the repository`() { - doReturn(false).whenever(service).isUsedByMaterial(entity) + override fun `delete() deletes in the repository`() { + whenCanBeDeleted { + super.`delete() deletes in the repository`() + } + } - service.delete(entity) + override fun `deleteById() deletes the entity with the given id in the repository`() { + whenCanBeDeleted { + super.`deleteById() deletes the entity with the given id in the repository`() + } + } - verify(repository).delete(entity) + private fun whenCanBeDeleted(id: Long = any(), test: () -> Unit) { + whenever(repository.canBeDeleted(id)).doReturn(true) + + test() } } diff --git a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MixServiceTest.kt b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MixServiceTest.kt index e4bdca0..22a41b0 100644 --- a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MixServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MixServiceTest.kt @@ -215,10 +215,25 @@ class MixServiceTest : AbstractExternalModelServiceTest Unit) { + whenever(repository.canBeDeleted(id)).doReturn(true) + + test() } } diff --git a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MixTypeServiceTest.kt b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MixTypeServiceTest.kt index 8dac13d..b57d4ce 100644 --- a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MixTypeServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MixTypeServiceTest.kt @@ -149,8 +149,23 @@ class MixTypeServiceTest : AbstractNamedModelServiceTest Unit) { + whenever(repository.canBeDeleted(id)).doReturn(true) + + test() } } diff --git a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeServiceTest.kt b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeServiceTest.kt index 3bb751e..72f60d2 100644 --- a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeServiceTest.kt @@ -196,12 +196,12 @@ class RecipeImageServiceTest { @Test fun `getByIdForRecipe() throws RecipeImageNotFoundException when no image with the given recipe and image id exists`() { doReturn(imagePath).whenever(service).getPath(imageId, recipeId) + whenever(recipeService.getById(recipeId)).doReturn(recipe) whenever(fileService.readAsBytes(imagePath)).doAnswer { throw NoSuchFileException(imagePath) } assertThrows { service.getByIdForRecipe(imageId, recipeId) } } - // getAllIdsForRecipe() @Test