Merge branch 'features' into 'master'

#66, #67 Ajout de configurations

See merge request color-recipes-explorer/backend!33
This commit is contained in:
William Nolin 2021-06-09 03:49:42 +00:00
commit 049e6992fa
5 changed files with 70 additions and 8 deletions

View File

@ -86,11 +86,14 @@ 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"),
EMERGENCY_MODE_ENABLED("env.emergency", computed = true, public = true),
VERSION("env.version", computed = true),
BUILD_VERSION("env.build.version", computed = true),
BUILD_TIME("env.build.time", computed = true),
JAVA_VERSION("env.java.version", computed = true),
OPERATING_SYSTEM("env.os", computed = true)
;

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

@ -11,7 +11,9 @@ import org.springframework.boot.info.BuildProperties
import org.springframework.context.annotation.Lazy
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import java.time.LocalDate
import java.time.Period
import java.time.ZoneId
import javax.annotation.PostConstruct
interface ConfigurationService {
@ -130,15 +132,17 @@ 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 -> ""
}
private fun getComputedConfiguration(key: ConfigurationType) = configuration(
key, when (key) {
ConfigurationType.EMERGENCY_MODE_ENABLED -> emergencyMode
ConfigurationType.VERSION -> buildInfo.version
ConfigurationType.BUILD_VERSION -> buildInfo.version
ConfigurationType.BUILD_TIME -> LocalDate.ofInstant(buildInfo.time, ZoneId.systemDefault()).toString()
ConfigurationType.DATABASE_SUPPORTED_VERSION -> SUPPORTED_DATABASE_VERSION
ConfigurationType.JAVA_VERSION -> Runtime.version()
ConfigurationType.OPERATING_SYSTEM -> "${System.getProperty("os.name")} ${System.getProperty("os.version")} ${
@ -149,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