diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/MixService.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/MixService.kt index 65edbe3..8416417 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/MixService.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/MixService.kt @@ -1,8 +1,8 @@ package dev.fyloz.colorrecipesexplorer.service -import dev.fyloz.colorrecipesexplorer.exception.AlreadyExistsException import dev.fyloz.colorrecipesexplorer.model.* import dev.fyloz.colorrecipesexplorer.repository.MixRepository +import dev.fyloz.colorrecipesexplorer.service.utils.setAll import org.springframework.context.annotation.Lazy import org.springframework.stereotype.Service import javax.transaction.Transactional @@ -43,6 +43,8 @@ class MixServiceImpl( val mixType = mixTypeService.getOrCreateForNameAndMaterialType(entity.name, materialType) val mixMaterials = if (entity.mixMaterials != null) mixMaterialService.create(entity.mixMaterials) else setOf() + mixMaterialService.validateMixMaterials(mixMaterials) + var mix = mix(recipe = recipe, mixType = mixType, mixMaterials = mixMaterials.toMutableSet()) mix = save(mix) @@ -68,8 +70,7 @@ class MixServiceImpl( } } if (entity.mixMaterials != null) { - mix.mixMaterials.clear() - mix.mixMaterials.addAll(mixMaterialService.create(entity.mixMaterials!!).toMutableSet()) + mix.mixMaterials.setAll(mixMaterialService.create(entity.mixMaterials!!).toMutableSet()) } return update(mix) } diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeStepService.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeStepService.kt index 8fc0121..44ba176 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeStepService.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeStepService.kt @@ -31,7 +31,7 @@ class RecipeStepServiceImpl(recipeStepRepository: RecipeStepRepository) : if (groupInformation.steps == null) return try { - validateSteps(groupInformation.steps!!.toSet()) + validateSteps(groupInformation.steps!!) } catch (validationException: InvalidStepsPositionsException) { throw InvalidGroupStepsPositionsException(groupInformation.group, validationException) } diff --git a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MixMaterialServiceTest.kt b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MixMaterialServiceTest.kt index 35259e4..bffc6a4 100644 --- a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MixMaterialServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/MixMaterialServiceTest.kt @@ -4,6 +4,7 @@ import com.nhaarman.mockitokotlin2.* import dev.fyloz.colorrecipesexplorer.model.* import dev.fyloz.colorrecipesexplorer.repository.MixMaterialRepository import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNotEquals @@ -99,4 +100,70 @@ class MixMaterialServiceTest : AbstractModelServiceTest { + service.validateMixMaterials(mixMaterials) + } + } + + private fun assertInvalidMixMaterialsPositionsException(mixMaterials: Set, errorType: String) { + val exception = assertThrows { + service.validateMixMaterials(mixMaterials) + } + + assertTrue { exception.errors.size == 1 } + assertTrue { exception.errors.first().type == errorType } + } } diff --git a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeStepServiceTest.kt b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeStepServiceTest.kt index ef8d213..48f476f 100644 --- a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeStepServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeStepServiceTest.kt @@ -1,7 +1,6 @@ package dev.fyloz.colorrecipesexplorer.service -import com.nhaarman.mockitokotlin2.mock -import com.nhaarman.mockitokotlin2.spy +import com.nhaarman.mockitokotlin2.* import dev.fyloz.colorrecipesexplorer.model.* import dev.fyloz.colorrecipesexplorer.repository.RecipeStepRepository import org.junit.jupiter.api.Test @@ -19,68 +18,86 @@ class RecipeStepServiceTest : // validateGroupInformationSteps() @Test - fun `validateGroupInformationSteps() throws an InvalidGroupStepsPositionsException when the position of the first step of the given groupInformation is not 1`() { - withSteps( + fun `validateGroupInformationSteps() calls validateSteps() with the given RecipeGroupInformation steps`() { + withGroupInformation { + service.validateGroupInformationSteps(this) + + verify(service).validateSteps(this.steps!!) + } + } + + @Test + fun `validateGroupInformationSteps() throws InvalidGroupStepsPositionsException when validateSteps() throws an InvalidStepsPositionsException`() { + withGroupInformation { + doAnswer { throw InvalidStepsPositionsException(setOf()) }.whenever(service).validateSteps(this.steps!!) + + assertThrows { + service.validateGroupInformationSteps(this) + } + } + } + + // validateSteps() + + @Test + fun `validateSteps() throws an InvalidStepsPositionsException when the position of the first step of the given groupInformation is not 1`() { + assertInvalidStepsPositionsException( mutableSetOf( recipeStep(id = 0L, position = 0), recipeStep(id = 1L, position = 1), recipeStep(id = 2L, position = 2), recipeStep(id = 3L, position = 3) - ) - ) { - val exception = assertThrows { - service.validateGroupInformationSteps(this) - } - - assertTrue { exception.errors.count() == 1 } - assertTrue { exception.errors.first().type == INVALID_FIRST_STEP_POSITION_ERROR_CODE } - } + ), + INVALID_FIRST_STEP_POSITION_ERROR_CODE + ) } @Test - fun `validateGroupInformationSteps() throws an InvalidGroupStepsPositionsException when steps positions are duplicated in the given groupInformation`() { - withSteps( + fun `validateSteps() throws an InvalidStepsPositionsException when steps positions are duplicated in the given groupInformation`() { + assertInvalidStepsPositionsException( mutableSetOf( recipeStep(id = 0L, position = 1), recipeStep(id = 1L, position = 2), recipeStep(id = 2L, position = 2), recipeStep(id = 3L, position = 3) - ) - ) { - val exception = assertThrows { - service.validateGroupInformationSteps(this) - } - - assertTrue { exception.errors.count() == 1 } - assertTrue { exception.errors.first().type == DUPLICATED_STEPS_POSITIONS_ERROR_CODE } - } + ), + DUPLICATED_STEPS_POSITIONS_ERROR_CODE + ) } @Test - fun `validateGroupInformationSteps() throws an InvalidGroupStepsPositionsException when there is a gap between steps positions in the given groupInformation`() { - withSteps( + fun `validateSteps() throws an InvalidStepsPositionsException when there is a gap between steps positions in the given groupInformation`() { + assertInvalidStepsPositionsException( mutableSetOf( recipeStep(id = 0L, position = 1), recipeStep(id = 1L, position = 2), recipeStep(id = 2L, position = 4), recipeStep(id = 3L, position = 5) - ) - ) { - val exception = assertThrows { - service.validateGroupInformationSteps(this) - } - - assertTrue { exception.errors.count() == 1 } - assertTrue { exception.errors.first().type == GAP_BETWEEN_STEPS_POSITIONS_ERROR_CODE } - } + ), + GAP_BETWEEN_STEPS_POSITIONS_ERROR_CODE + ) } - private fun withSteps(steps: MutableSet, test: RecipeGroupInformation.() -> Unit) { + private fun withGroupInformation(steps: MutableSet? = null, test: RecipeGroupInformation.() -> Unit) { recipeGroupInformation( group = employeeGroup(id = 0L), - steps = steps + steps = steps ?: mutableSetOf( + recipeStep(id = 0L, position = 1), + recipeStep(id = 1L, position = 2), + recipeStep(id = 2L, position = 3), + recipeStep(id = 3L, position = 4) + ) ) { test() } } + + private fun assertInvalidStepsPositionsException(steps: MutableSet, errorType: String) { + val exception = assertThrows { + service.validateSteps(steps) + } + + assertTrue { exception.errors.size == 1 } + assertTrue { exception.errors.first().type == errorType } + } }