Ajout des tests de la vérification des ingrédients des mélanges

This commit is contained in:
FyloZ 2021-04-18 22:52:47 -04:00
parent 931494c1f9
commit 97c6e17b80
4 changed files with 126 additions and 41 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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<MixMaterial, MixMaterial
assertEquals(found.quantity, quantity)
}
// validateMixMaterials()
@Test
fun `validateMixMaterials() throws InvalidMixMaterialsPositionsException when the position of the first mix material is not 1`() {
assertInvalidMixMaterialsPositionsException(
setOf(
mixMaterial(id = 0L, position = 0),
mixMaterial(id = 1L, position = 1),
mixMaterial(id = 2L, position = 2),
mixMaterial(id = 3L, position = 3)
),
INVALID_FIRST_MIX_MATERIAL_POSITION_ERROR_CODE
)
}
@Test
fun `validateMixMaterials() throws InvalidMixMaterialsPositionsException when positions are duplicated`() {
assertInvalidMixMaterialsPositionsException(
setOf(
mixMaterial(id = 0L, position = 1),
mixMaterial(id = 1L, position = 2),
mixMaterial(id = 2L, position = 2),
mixMaterial(id = 3L, position = 3)
),
DUPLICATED_MIX_MATERIALS_POSITIONS_ERROR_CODE
)
}
@Test
fun `validateMixMaterials() throws InvalidMixMaterialsPositionsException when there is a gap between positions`() {
assertInvalidMixMaterialsPositionsException(
setOf(
mixMaterial(id = 0L, position = 1),
mixMaterial(id = 1L, position = 2),
mixMaterial(id = 2L, position = 4),
mixMaterial(id = 3L, position = 5)
),
GAP_BETWEEN_MIX_MATERIALS_POSITIONS_ERROR_CODE
)
}
@Test
fun `validateMixMaterials() throws InvalidFirstMixMaterial when the first mix material's quantity is expressed in percents`() {
val normalMaterial = material(materialType = materialType(usePercentages = false))
val percentsMaterial = material(materialType = materialType(usePercentages = true))
val mixMaterials = setOf(
mixMaterial(id = 0L, position = 1, material = percentsMaterial),
mixMaterial(id = 1L, position = 2, material = normalMaterial),
mixMaterial(id = 2L, position = 3, material = normalMaterial),
mixMaterial(id = 3L, position = 4, material = normalMaterial)
)
assertThrows<InvalidFirstMixMaterial> {
service.validateMixMaterials(mixMaterials)
}
}
private fun assertInvalidMixMaterialsPositionsException(mixMaterials: Set<MixMaterial>, errorType: String) {
val exception = assertThrows<InvalidMixMaterialsPositionsException> {
service.validateMixMaterials(mixMaterials)
}
assertTrue { exception.errors.size == 1 }
assertTrue { exception.errors.first().type == errorType }
}
}

View File

@ -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<InvalidGroupStepsPositionsException> {
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<InvalidGroupStepsPositionsException> {
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<InvalidGroupStepsPositionsException> {
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<InvalidGroupStepsPositionsException> {
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<RecipeStep>, test: RecipeGroupInformation.() -> Unit) {
private fun withGroupInformation(steps: MutableSet<RecipeStep>? = 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<RecipeStep>, errorType: String) {
val exception = assertThrows<InvalidStepsPositionsException> {
service.validateSteps(steps)
}
assertTrue { exception.errors.size == 1 }
assertTrue { exception.errors.first().type == errorType }
}
}