-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1039 JSON serialization of the settings
- Loading branch information
1 parent
1092219
commit 82318dd
Showing
3 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...ng/src/test/java/net/nemerosa/ontrack/extension/av/settings/AutoVersioningSettingsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package net.nemerosa.ontrack.extension.av.settings | ||
|
||
import net.nemerosa.ontrack.json.asJson | ||
import net.nemerosa.ontrack.json.parse | ||
import org.junit.jupiter.api.Test | ||
import java.time.Duration | ||
import kotlin.test.assertEquals | ||
|
||
internal class AutoVersioningSettingsTest { | ||
|
||
@Test | ||
fun `Json serialization and deserialization`() { | ||
val settings = AutoVersioningSettings( | ||
enabled = true, | ||
auditRetentionDuration = Duration.ofDays(14L), | ||
auditCleanupDuration = Duration.ofDays(90L), | ||
buildLinks = true, | ||
) | ||
val json = settings.asJson() | ||
val loaded = json.parse<AutoVersioningSettings>() | ||
assertEquals(settings, loaded) | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
ontrack-json/src/main/java/net/nemerosa/ontrack/json/SimpleDurationSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package net.nemerosa.ontrack.json | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator | ||
import com.fasterxml.jackson.databind.JsonSerializer | ||
import com.fasterxml.jackson.databind.SerializerProvider | ||
import java.time.Duration | ||
|
||
class SimpleDurationSerializer : JsonSerializer<Duration>() { | ||
|
||
override fun serialize(value: Duration, gen: JsonGenerator, serializers: SerializerProvider) { | ||
val hoursPart = value.toHoursPart() | ||
if (hoursPart == 0) { | ||
val days = value.toDays() | ||
if (days % 7L == 0L) { | ||
val weeks = days / 7L | ||
gen.writeString("${weeks}w") | ||
} else { | ||
gen.writeString("${days}d") | ||
} | ||
} else { | ||
gen.writeString("${value.toHours()}h") | ||
} | ||
} | ||
|
||
} |