#67 Ajout de la configuration recipe.approbation.expiration

This commit is contained in:
FyloZ 2021-06-08 23:41:16 -04:00
parent 0b8e48f172
commit 136b9f9a36
5 changed files with 64 additions and 6 deletions

View File

@ -86,6 +86,8 @@ enum class ConfigurationType(
DATABASE_PASSWORD("database.password", file = true, requireRestart = true),
DATABASE_SUPPORTED_VERSION("database.version.supported", computed = true),
RECIPE_APPROBATION_EXPIRATION("recipe.approbation.expiration"),
TOUCH_UP_KIT_CACHE_PDF("touchupkit.pdf.cache"),
TOUCH_UP_KIT_EXPIRATION("touchupkit.expiration"),

View File

@ -145,6 +145,7 @@ data class RecipeOutputDto(
val gloss: Byte,
val sample: Int?,
val approbationDate: LocalDate?,
val approbationExpired: Boolean?,
val remark: String?,
val company: Company,
val mixes: Set<MixOutputDto>,

View File

@ -132,8 +132,9 @@ class ConfigurationServiceImpl(
ConfigurationType.DATABASE_URL -> databaseProperties.url
ConfigurationType.DATABASE_USER -> databaseProperties.username
ConfigurationType.DATABASE_PASSWORD -> databaseProperties.password
ConfigurationType.RECIPE_APPROBATION_EXPIRATION -> period(months = 4)
ConfigurationType.TOUCH_UP_KIT_CACHE_PDF -> "true"
ConfigurationType.TOUCH_UP_KIT_EXPIRATION -> Period.ofMonths(1).toString()
ConfigurationType.TOUCH_UP_KIT_EXPIRATION -> period(months = 1)
else -> ""
}
@ -152,4 +153,7 @@ class ConfigurationServiceImpl(
else -> throw IllegalArgumentException("Cannot get the value of the configuration with the key ${key.key} because it is not a computed configuration")
}.toString()
)
private fun period(days: Int = 0, months: Int = 0, years: Int = 0) =
Period.of(days, months, years).toString()
}

View File

@ -10,6 +10,8 @@ import org.springframework.context.annotation.Profile
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import java.io.File
import java.time.LocalDate
import java.time.Period
import javax.transaction.Transactional
interface RecipeService :
@ -20,6 +22,9 @@ interface RecipeService :
/** Checks if a recipe exists with the given [name] and [company]. */
fun existsByNameAndCompany(name: String, company: Company): Boolean
/** Checks if the approbation date of the given [recipe] is expired. */
fun isApprobationExpired(recipe: Recipe): Boolean?
/** Gets all recipes with the given [name]. */
fun getAllByName(name: String): Collection<Recipe>
@ -62,6 +67,7 @@ class RecipeServiceImpl(
this.gloss,
this.sample,
this.approbationDate,
isApprobationExpired(this),
this.remark,
this.company,
this.mixes.map {
@ -79,6 +85,11 @@ class RecipeServiceImpl(
override fun existsByNameAndCompany(name: String, company: Company) =
repository.existsByNameAndCompany(name, company)
override fun isApprobationExpired(recipe: Recipe): Boolean? =
with(Period.parse(configService.get(ConfigurationType.RECIPE_APPROBATION_EXPIRATION).content)) {
recipe.approbationDate?.plus(this)?.isBefore(LocalDate.now())
}
override fun getAllByName(name: String) = repository.findAllByName(name)
override fun getAllByCompany(company: Company) = repository.findAllByCompany(company)

View File

@ -5,7 +5,6 @@ import dev.fyloz.colorrecipesexplorer.exception.AlreadyExistsException
import dev.fyloz.colorrecipesexplorer.model.*
import dev.fyloz.colorrecipesexplorer.model.account.group
import dev.fyloz.colorrecipesexplorer.repository.RecipeRepository
import dev.fyloz.colorrecipesexplorer.service.FileService
import io.mockk.*
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
@ -14,9 +13,9 @@ import org.junit.jupiter.api.assertThrows
import org.springframework.mock.web.MockMultipartFile
import org.springframework.web.multipart.MultipartFile
import java.io.File
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import java.time.LocalDate
import java.time.Period
import kotlin.test.*
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RecipeServiceTest :
@ -26,8 +25,9 @@ class RecipeServiceTest :
private val mixService: MixService = mock()
private val groupService: GroupService = mock()
private val recipeStepService: RecipeStepService = mock()
private val configService: ConfigurationService = mock()
override val service: RecipeService =
spy(RecipeServiceImpl(repository, companyService, mixService, recipeStepService, groupService, mock(), mock()))
spy(RecipeServiceImpl(repository, companyService, mixService, recipeStepService, groupService, mock(), configService))
private val company: Company = company(id = 0L)
override val entity: Recipe = recipe(id = 0L, name = "recipe", company = company)
@ -74,6 +74,46 @@ class RecipeServiceTest :
}
}
// isApprobationExpired()
@Test
fun `isApprobationExpired() returns false when the approbation date of the given recipe is within the configured period`() {
val period = Period.ofMonths(4)
val config = configuration(type = ConfigurationType.RECIPE_APPROBATION_EXPIRATION, content = period.toString())
val recipe = recipe(approbationDate = LocalDate.now())
whenever(configService.get(ConfigurationType.RECIPE_APPROBATION_EXPIRATION)).doReturn(config)
val approbationExpired = service.isApprobationExpired(recipe)
assertNotNull(approbationExpired)
assertFalse(approbationExpired)
}
@Test
fun `isApprobationExpired() returns true when the approbation date of the given recipe is outside the configured period`() {
val period = Period.ofMonths(4)
val config = configuration(type = ConfigurationType.RECIPE_APPROBATION_EXPIRATION, content = period.toString())
val recipe = recipe(approbationDate = LocalDate.now().minus(period).minusMonths(1))
whenever(configService.get(ConfigurationType.RECIPE_APPROBATION_EXPIRATION)).doReturn(config)
val approbationExpired = service.isApprobationExpired(recipe)
assertNotNull(approbationExpired)
assertTrue(approbationExpired)
}
@Test
fun `isApprobationExpired() returns null when the given recipe as no approbation date`() {
val period = Period.ofMonths(4)
val config = configuration(type = ConfigurationType.RECIPE_APPROBATION_EXPIRATION, content = period.toString())
val recipe = recipe(approbationDate = null)
whenever(configService.get(ConfigurationType.RECIPE_APPROBATION_EXPIRATION)).doReturn(config)
val approbationExpired = service.isApprobationExpired(recipe)
assertNull(approbationExpired)
}
// getAllByName()
@Test