From 0b8e48f172f2a21eb6b930050684c6323d455ab3 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Tue, 8 Jun 2021 23:25:29 -0400 Subject: [PATCH 1/2] #66 Ajout de la configuration env.build.time --- .../dev/fyloz/colorrecipesexplorer/model/Configuration.kt | 3 ++- .../colorrecipesexplorer/service/ConfigurationService.kt | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Configuration.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Configuration.kt index 5284285..77ed627 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Configuration.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Configuration.kt @@ -90,7 +90,8 @@ enum class ConfigurationType( 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) ; diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/ConfigurationService.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/ConfigurationService.kt index 0dd69e0..7855597 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/ConfigurationService.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/ConfigurationService.kt @@ -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 { @@ -138,7 +140,8 @@ class ConfigurationServiceImpl( 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")} ${ From 136b9f9a36772f7c3c298e8ba869a60cb19ce834 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Tue, 8 Jun 2021 23:41:16 -0400 Subject: [PATCH 2/2] #67 Ajout de la configuration recipe.approbation.expiration --- .../model/Configuration.kt | 2 + .../colorrecipesexplorer/model/Recipe.kt | 1 + .../service/ConfigurationService.kt | 6 ++- .../service/RecipeService.kt | 11 ++++ .../service/RecipeServiceTest.kt | 50 +++++++++++++++++-- 5 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Configuration.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Configuration.kt index 77ed627..a106fa9 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Configuration.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Configuration.kt @@ -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"), diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Recipe.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Recipe.kt index fbbe87f..b0649a7 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Recipe.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Recipe.kt @@ -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, diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/ConfigurationService.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/ConfigurationService.kt index 7855597..d6f8eef 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/ConfigurationService.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/ConfigurationService.kt @@ -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() } diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeService.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeService.kt index e34b48d..55ee5f0 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeService.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeService.kt @@ -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 @@ -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) diff --git a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeServiceTest.kt b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeServiceTest.kt index 9078af9..4419e3c 100644 --- a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeServiceTest.kt @@ -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