Les étapes inutilisées sont maintenant supprimées lors de la mise à jour d'une recette.
Corrige #52
This commit is contained in:
parent
26ab3ebb10
commit
099534c9b2
|
@ -52,10 +52,10 @@ data class Recipe(
|
|||
val company: Company,
|
||||
|
||||
@OneToMany(cascade = [CascadeType.ALL], mappedBy = "recipe")
|
||||
val mixes: MutableCollection<Mix>,
|
||||
val mixes: MutableList<Mix>,
|
||||
|
||||
@OneToMany(cascade = [CascadeType.ALL], mappedBy = "recipe")
|
||||
var steps: MutableCollection<RecipeStep>
|
||||
var steps: List<RecipeStep>
|
||||
) : Model {
|
||||
constructor() : this(
|
||||
null,
|
||||
|
@ -199,8 +199,8 @@ fun recipe(
|
|||
remark: String = "remark",
|
||||
note: String = "",
|
||||
company: Company = company(),
|
||||
mixes: MutableCollection<Mix> = mutableListOf(),
|
||||
steps: MutableCollection<RecipeStep> = mutableListOf(),
|
||||
mixes: MutableList<Mix> = mutableListOf(),
|
||||
steps: List<RecipeStep> = listOf(),
|
||||
op: Recipe.() -> Unit = {}
|
||||
) = Recipe(id, name, description, color, gloss, sample, approbationDate, remark, note, company, mixes, steps).apply(op)
|
||||
|
||||
|
|
|
@ -33,7 +33,8 @@ interface RecipeService : ExternalModelService<Recipe, RecipeSaveDto, RecipeUpda
|
|||
class RecipeServiceImpl(
|
||||
recipeRepository: RecipeRepository,
|
||||
val companyService: CompanyService,
|
||||
val mixService: MixService
|
||||
val mixService: MixService,
|
||||
val stepService: RecipeStepService
|
||||
) :
|
||||
AbstractExternalModelService<Recipe, RecipeSaveDto, RecipeUpdateDto, RecipeRepository>(recipeRepository),
|
||||
RecipeService {
|
||||
|
@ -73,11 +74,24 @@ class RecipeServiceImpl(
|
|||
note = persistedRecipe.note,
|
||||
company = persistedRecipe.company,
|
||||
mixes = persistedRecipe.mixes,
|
||||
steps = steps?.map { recipeStep(recipe = persistedRecipe, message = it.message) }?.toMutableList() ?: persistedRecipe.steps
|
||||
steps = updateSteps(persistedRecipe, steps)
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
private fun updateSteps(recipe: Recipe, steps: List<RecipeStep>?): List<RecipeStep> =
|
||||
if (steps != null) {
|
||||
val toDelete = recipe.steps.filter { it !in steps }
|
||||
toDelete.forEach(stepService::delete)
|
||||
recipe.steps
|
||||
.filter { it !in toDelete } + steps
|
||||
.map { recipeStep(recipe = recipe, message = it.message) }
|
||||
.filter { it !in recipe.steps }
|
||||
.toMutableList()
|
||||
} else {
|
||||
recipe.steps
|
||||
}
|
||||
|
||||
@ExperimentalContracts
|
||||
override fun updatePublicData(publicDataDto: RecipePublicDataDto) {
|
||||
val recipe = getById(publicDataDto.id)
|
||||
|
|
|
@ -334,18 +334,18 @@ fun <E : Model, U : EntityDto<E>> withBaseUpdateDtoTest(
|
|||
entity: E,
|
||||
entityUpdateDto: U,
|
||||
service: ExternalModelService<E, *, U, *>,
|
||||
updateMockMatcher: E,
|
||||
op: () -> Unit = {}
|
||||
updateMockMatcher: () -> E,
|
||||
op: E.() -> Unit = {}
|
||||
) {
|
||||
doAnswer { it.arguments[0] }.whenever(service).update(updateMockMatcher)
|
||||
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)
|
||||
verify(service).update(updateMockMatcher())
|
||||
assertEquals(entity, found)
|
||||
|
||||
op()
|
||||
found.op()
|
||||
}
|
||||
|
|
|
@ -168,8 +168,9 @@ class EmployeeServiceTest :
|
|||
|
||||
// update()
|
||||
|
||||
@Test
|
||||
override fun `update(dto) calls and returns update() with the created entity`() =
|
||||
withBaseUpdateDtoTest(entity, entityUpdateDto, service, any())
|
||||
withBaseUpdateDtoTest(entity, entityUpdateDto, service, { any() })
|
||||
|
||||
@Test
|
||||
fun `update() throws EntityAlreadyExistsRestException when a different employee with the given first name and last name exists`() {
|
||||
|
@ -199,7 +200,7 @@ class EmployeeGroupServiceTest :
|
|||
override val entity: EmployeeGroup = employeeGroup(id = 0L, name = "group")
|
||||
override val anotherEntity: EmployeeGroup = employeeGroup(id = 1L, name = "another group")
|
||||
override val entitySaveDto: EmployeeGroupSaveDto = spy(employeeGroupSaveDto(name = "group"))
|
||||
override val entityUpdateDto: EmployeeGroupUpdateDto = spy(employeeGroupUpdateDto(id = 0L, name = "groupL"))
|
||||
override val entityUpdateDto: EmployeeGroupUpdateDto = spy(employeeGroupUpdateDto(id = 0L, name = "group"))
|
||||
override val entityWithEntityName: EmployeeGroup = employeeGroup(id = 2L, name = entity.name)
|
||||
|
||||
private val groupEmployeeId = 1000000L + entity.id!!
|
||||
|
@ -397,8 +398,9 @@ class EmployeeGroupServiceTest :
|
|||
|
||||
// update()
|
||||
|
||||
@Test
|
||||
override fun `update(dto) calls and returns update() with the created entity`() =
|
||||
withBaseUpdateDtoTest(entity, entityUpdateDto, service, any())
|
||||
withBaseUpdateDtoTest(entity, entityUpdateDto, service, { any() })
|
||||
}
|
||||
|
||||
class EmployeeUserDetailsServiceTest {
|
||||
|
|
|
@ -20,7 +20,7 @@ class CompanyServiceTest :
|
|||
override val anotherEntity: Company = company(id = 1L, name = "another company")
|
||||
override val entityWithEntityName: Company = company(id = 2L, name = entity.name)
|
||||
override val entitySaveDto: CompanySaveDto = spy(companySaveDto())
|
||||
override val entityUpdateDto: CompanyUpdateDto = spy(companyUpdateDto(id = entity.id!!))
|
||||
override val entityUpdateDto: CompanyUpdateDto = spy(companyUpdateDto(id = entity.id!!, name = null))
|
||||
|
||||
@AfterEach
|
||||
override fun afterEach() {
|
||||
|
@ -50,8 +50,9 @@ class CompanyServiceTest :
|
|||
|
||||
// update()
|
||||
|
||||
@Test
|
||||
override fun `update(dto) calls and returns update() with the created entity`() =
|
||||
withBaseUpdateDtoTest(entity, entityUpdateDto, service, any())
|
||||
withBaseUpdateDtoTest(entity, entityUpdateDto, service, { any() })
|
||||
|
||||
// delete()
|
||||
|
||||
|
|
|
@ -108,8 +108,9 @@ class MaterialTypeServiceTest :
|
|||
|
||||
// update()
|
||||
|
||||
@Test
|
||||
override fun `update(dto) calls and returns update() with the created entity`() =
|
||||
withBaseUpdateDtoTest(entity, entityUpdateDto, service, any())
|
||||
withBaseUpdateDtoTest(entity, entityUpdateDto, service, { any() })
|
||||
|
||||
override fun `update() saves in the repository and returns the updated value`() {
|
||||
whenever(repository.save(entity)).doReturn(entity)
|
||||
|
|
|
@ -8,6 +8,7 @@ import dev.fyloz.trial.colorrecipesexplorer.service.files.FilesService
|
|||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import org.mockito.Mockito
|
||||
import org.springframework.mock.web.MockMultipartFile
|
||||
import java.io.File
|
||||
import java.nio.file.NoSuchFileException
|
||||
|
@ -20,7 +21,8 @@ class RecipeServiceTest :
|
|||
override val repository: RecipeRepository = mock()
|
||||
private val companyService: CompanyService = mock()
|
||||
private val mixService: MixService = mock()
|
||||
override val service: RecipeService = spy(RecipeServiceImpl(repository, companyService, mixService))
|
||||
private val stepService: RecipeStepService = mock()
|
||||
override val service: RecipeService = spy(RecipeServiceImpl(repository, companyService, mixService, stepService))
|
||||
|
||||
private val company: Company = company(id = 0L)
|
||||
override val entity: Recipe = recipe(id = 0L, name = "recipe", company = company)
|
||||
|
@ -28,6 +30,13 @@ class RecipeServiceTest :
|
|||
override val entitySaveDto: RecipeSaveDto = spy(recipeSaveDto(name = entity.name, companyId = entity.company.id!!))
|
||||
override val entityUpdateDto: RecipeUpdateDto = spy(recipeUpdateDto(id = entity.id!!, name = entity.name))
|
||||
|
||||
@AfterEach
|
||||
override fun afterEach() {
|
||||
reset(companyService, mixService, stepService)
|
||||
super.afterEach()
|
||||
}
|
||||
|
||||
|
||||
// existsByCompany()
|
||||
|
||||
@Test
|
||||
|
@ -69,9 +78,36 @@ class RecipeServiceTest :
|
|||
}
|
||||
|
||||
// update()
|
||||
|
||||
@Test
|
||||
override fun `update(dto) calls and returns update() with the created entity`() =
|
||||
withBaseUpdateDtoTest(entity, entityUpdateDto, service, any())
|
||||
withBaseUpdateDtoTest(entity, entityUpdateDto, service, { any() })
|
||||
|
||||
@Test
|
||||
fun `update(dto) remove unused steps`() {
|
||||
val steps = listOf(
|
||||
recipeStep(message = "step 1"),
|
||||
recipeStep(message = "step 2"),
|
||||
recipeStep(message = "step 3"),
|
||||
recipeStep(message = "step 4"),
|
||||
recipeStep(message = "step 5"),
|
||||
recipeStep(message = "step 6"),
|
||||
recipeStep(message = "step 7"),
|
||||
recipeStep(message = "step 8")
|
||||
)
|
||||
val originalSteps = steps.subList(0, 5)
|
||||
val updatedSteps = steps.subList(2, 7)
|
||||
val deletedSteps = steps.subList(0, 1)
|
||||
val recipe = recipe(id = 0L, steps = originalSteps)
|
||||
val dto = spy(recipeUpdateDto(id = recipe.id!!, steps = updatedSteps))
|
||||
|
||||
withBaseUpdateDtoTest(recipe, dto, service, { any() }) {
|
||||
verify(stepService).delete(argThat { this in deletedSteps })
|
||||
|
||||
assertTrue {
|
||||
this.steps.containsAll(updatedSteps)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// updatePublicData()
|
||||
|
||||
|
|
Loading…
Reference in New Issue