Add checking message ids uniqueness

This commit is contained in:
Mikołaj Pich 2021-10-03 14:06:12 +02:00
parent 04a1f5e80e
commit 2259cb3db8
6 changed files with 65 additions and 2 deletions

2
.gitignore vendored
View file

@ -72,4 +72,4 @@ gradle-app.setting
classes classes
.DS_Store .DS_Store
.idea/libraries-with-intellij-classes.xml .idea/libraries-with-intellij-classes.xml
dist/messages.json dist/v*.json

View file

@ -10,4 +10,14 @@ mainClassName = 'MainKt'
dependencies { dependencies {
implementation project(":pojos") implementation project(":pojos")
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0" implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0"
testImplementation platform("org.junit:junit-bom:5.8.1")
testImplementation "org.junit.jupiter:junit-jupiter"
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
} }

View file

@ -3,6 +3,8 @@ import kotlinx.serialization.json.Json
import java.io.File import java.io.File
fun main() { fun main() {
checkIdUniqueness(messages)
val json = Json.encodeToString(messages) val json = Json.encodeToString(messages)
File("../dist/v1.json").writeText(json) File("../dist/v1.json").writeText(json)

View file

@ -0,0 +1,9 @@
import io.github.wulkanowy.messages.pojo.Message
fun checkIdUniqueness(list: List<Message>) {
val duplicates = list.groupingBy { it.id }.eachCount().filter { it.value > 1 }
if (duplicates.isNotEmpty()) {
throw IllegalArgumentException("Found ${duplicates.size} duplicated id(s) in messages: $duplicates")
}
}

View file

@ -0,0 +1,42 @@
import io.github.wulkanowy.messages.pojo.Message
import io.github.wulkanowy.messages.pojo.MessageType
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
class UtilsTest {
@Test
fun `check message id uniqueness when really duplicates`() {
val list = listOf(
createMessage(1),
createMessage(2),
createMessage(2),
createMessage(3),
)
assertThrows<IllegalArgumentException> {
checkIdUniqueness(list)
}
}
@Test
fun `check message id uniqueness when there is no duplicates`() {
val list = listOf(
createMessage(1),
createMessage(2),
createMessage(3),
)
assertDoesNotThrow {
checkIdUniqueness(list)
}
}
private fun createMessage(id: Long) = Message(
id = id,
title = "",
content = "",
type = MessageType.DASHBOARD_MESSAGE,
)
}

View file

@ -4,7 +4,7 @@ import kotlinx.serialization.Serializable
@Serializable @Serializable
data class Message( data class Message(
val id: Int, val id: Long,
val title: String, val title: String,
val content: String, val content: String,
val versionMin: Int? = null, val versionMin: Int? = null,