diff --git a/build.gradle.kts b/build.gradle.kts index ea5352c..5ee9261 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,6 @@ plugins { kotlin("multiplatform") version "2.2.21" + kotlin("plugin.serialization") version "2.2.21" id("maven-publish") } @@ -24,14 +25,6 @@ kotlin { else -> throw GradleException("Host OS is not supported in Kotlin/Native.") } - nativeTarget.apply { - binaries { - executable { - entryPoint = "dn42.m724.registry.main" - } - } - } - sourceSets { nativeMain.dependencies { implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.8.0") diff --git a/src/nativeMain/kotlin/dn42/m724/registry/FileSystemRegistryLoader.kt b/src/nativeMain/kotlin/dn42/m724/registry/FileSystemRegistryLoader.kt deleted file mode 100644 index 2c58c7b..0000000 --- a/src/nativeMain/kotlin/dn42/m724/registry/FileSystemRegistryLoader.kt +++ /dev/null @@ -1,12 +0,0 @@ -package dn42.m724.registry - -import dn42.m724.registry.model.Maintainer - -class FileSystemRegistryLoader( - -) : RegistryLoader { - override fun getMaintainer(maintainerName: String): Maintainer { - - TODO("Not yet implemented") - } -} \ No newline at end of file diff --git a/src/nativeMain/kotlin/dn42/m724/registry/Main.kt b/src/nativeMain/kotlin/dn42/m724/registry/Main.kt deleted file mode 100644 index 395ea5f..0000000 --- a/src/nativeMain/kotlin/dn42/m724/registry/Main.kt +++ /dev/null @@ -1,8 +0,0 @@ -package eu.m724.dn42.m724.registry - -import dn42.m724.registry.FileSystemRegistryLoader - -fun main() { - val loader = FileSystemRegistryLoader() - -} \ No newline at end of file diff --git a/src/nativeMain/kotlin/dn42/m724/registry/RegistryLoader.kt b/src/nativeMain/kotlin/dn42/m724/registry/RegistryLoader.kt deleted file mode 100644 index fa9a7d8..0000000 --- a/src/nativeMain/kotlin/dn42/m724/registry/RegistryLoader.kt +++ /dev/null @@ -1,7 +0,0 @@ -package dn42.m724.registry - -import dn42.m724.registry.model.Maintainer - -interface RegistryLoader { - fun getMaintainer(maintainerName: String): Maintainer -} \ No newline at end of file diff --git a/src/nativeMain/kotlin/dn42/m724/registry/loader/FileSystemRegistryLoader.kt b/src/nativeMain/kotlin/dn42/m724/registry/loader/FileSystemRegistryLoader.kt new file mode 100644 index 0000000..62025d4 --- /dev/null +++ b/src/nativeMain/kotlin/dn42/m724/registry/loader/FileSystemRegistryLoader.kt @@ -0,0 +1,20 @@ +package dn42.m724.registry.loader + +import dn42.m724.registry.serializer.RegistryObjectSerializer +import kotlinx.io.buffered +import kotlinx.io.files.Path +import kotlinx.io.files.SystemFileSystem + +class FileSystemRegistryLoader( + val root: Path +) : RegistryLoader { + override fun getData(path: String): Map> { + check(!path.contains("..")) { "Path may not contain ..s"} + + return SystemFileSystem.source( + path = Path(root, "data", path) + ).buffered().use { source -> + RegistryObjectSerializer.readToMap(source) + } + } +} \ No newline at end of file diff --git a/src/nativeMain/kotlin/dn42/m724/registry/loader/RegistryLoader.kt b/src/nativeMain/kotlin/dn42/m724/registry/loader/RegistryLoader.kt new file mode 100644 index 0000000..c3e366e --- /dev/null +++ b/src/nativeMain/kotlin/dn42/m724/registry/loader/RegistryLoader.kt @@ -0,0 +1,21 @@ +package dn42.m724.registry.loader + +interface RegistryLoader { + /** + * Fetches and processes data from the specified path. + * + * @param path The relative path within the registry. + * @return A map where each key is associated with a list of corresponding values + * as defined by the data file at the specified path. + */ + fun getData(path: String): Map> + + /** + * Fetches and processes data from the specified path. + * + * @param path The segments of a relative path within the registry, passed as a variable-length argument. + * @return A map where each key is associated with a list of corresponding values + * as defined by the data file at the specified path. + */ + fun getData(vararg path: String): Map> = getData(path.joinToString("/")) +} \ No newline at end of file diff --git a/src/nativeMain/kotlin/dn42/m724/registry/model/Maintainer.kt b/src/nativeMain/kotlin/dn42/m724/registry/model/Maintainer.kt deleted file mode 100644 index fddd77e..0000000 --- a/src/nativeMain/kotlin/dn42/m724/registry/model/Maintainer.kt +++ /dev/null @@ -1,4 +0,0 @@ -package dn42.m724.registry.model - -class Maintainer { -} \ No newline at end of file diff --git a/src/nativeTest/kotlin/dn42/m724/registry/loader/FileSystemRegistryLoaderTest.kt b/src/nativeTest/kotlin/dn42/m724/registry/loader/FileSystemRegistryLoaderTest.kt new file mode 100644 index 0000000..90f8d3f --- /dev/null +++ b/src/nativeTest/kotlin/dn42/m724/registry/loader/FileSystemRegistryLoaderTest.kt @@ -0,0 +1,45 @@ +package dn42.m724.registry.loader + +import dn42.m724.registry.test.getResourcePath +import dn42.m724.registry.test.shouldContainExactly +import kotlinx.io.files.FileNotFoundException +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertFailsWith + +class FileSystemRegistryLoaderTest { + lateinit var loader: FileSystemRegistryLoader + + @BeforeTest + fun initializeLoader() { + loader = FileSystemRegistryLoader(getResourcePath("registry")) + } + + @Test + fun `getData should return a map for successful parse`() { + val map = loader.getData("mntner", "EXAMPLE-MNT") + + map shouldContainExactly mapOf( + "mntner" to listOf("EXAMPLE-MNT"), + "mnt-by" to listOf("EXAMPLE-MNT"), + "auth" to listOf( + "pgp-fingerprint 2DE81640E41DD84A9ED9B8F8FD4D7C6652E72E7B", + "pgp-fingerprint 139F1460BC66A19A2F880D8D47BA020D8EBCC05E" + ), + "remarks" to listOf( + "This has spaces too yay!", + "As well as emojis \uD83D\uDE18!", + "Also: commas", + "" // blank value + ), + "source" to listOf("DN42") + ) + } + + @Test + fun `getData should throw FileNotFoundException for not existing file`() { + assertFailsWith { + loader.getData("mntner", "NOTEXIST-MNT") + } + } +} \ No newline at end of file diff --git a/src/nativeTest/kotlin/dn42/m724/registry/serializer/RegistryObjectSerializerTest.kt b/src/nativeTest/kotlin/dn42/m724/registry/serializer/RegistryObjectSerializerTest.kt index eac0686..33bfd62 100644 --- a/src/nativeTest/kotlin/dn42/m724/registry/serializer/RegistryObjectSerializerTest.kt +++ b/src/nativeTest/kotlin/dn42/m724/registry/serializer/RegistryObjectSerializerTest.kt @@ -43,7 +43,7 @@ class RegistryObjectSerializerTest { @Test fun `readToMap should throw InvalidKeyException when the key contains illegal numbers`() { withMockSource("li6ne: this has number! not possible have number!") { source -> - assertFailsWith(InvalidKeyException::class) { + assertFailsWith { RegistryObjectSerializer.readToMap(source) } } @@ -52,7 +52,7 @@ class RegistryObjectSerializerTest { @Test fun `readToMap should throw InvalidKeyException when a key contains illegal greater than characters`() { withMockSource("li>ne: this has >! cannot have !") { source -> - assertFailsWith(InvalidKeyException::class) { + assertFailsWith { RegistryObjectSerializer.readToMap(source) } } @@ -61,7 +61,7 @@ class RegistryObjectSerializerTest { @Test fun `readToMap should throw InvalidKeyException when a key contains illegal spaces`() { withMockSource(" line: leading ! bad!") { source -> - assertFailsWith(InvalidKeyException::class) { + assertFailsWith { RegistryObjectSerializer.readToMap(source) } } @@ -70,7 +70,7 @@ class RegistryObjectSerializerTest { @Test fun `readToMap should throw InvalidKeyException when a key contains uppercase characters`() { withMockSource("Line: Content") { source -> - assertFailsWith(InvalidKeyException::class) { + assertFailsWith { RegistryObjectSerializer.readToMap(source) } } @@ -79,7 +79,7 @@ class RegistryObjectSerializerTest { @Test fun `readToMap should throw InvalidKeyException when a key is empty`() { withMockSource(": what key?") { source -> - assertFailsWith(InvalidKeyException::class) { + assertFailsWith { RegistryObjectSerializer.readToMap(source) } } @@ -88,7 +88,7 @@ class RegistryObjectSerializerTest { @Test fun `readToMap should throw MissingKeyException when there's an invalid pair`() { withMockSource("line: content\nanother line") { source -> - assertFailsWith(MissingKeyException::class) { + assertFailsWith { RegistryObjectSerializer.readToMap(source) } } diff --git a/src/nativeTest/kotlin/dn42/m724/registry/test/TestUtils.kt b/src/nativeTest/kotlin/dn42/m724/registry/test/TestUtils.kt index 1958b23..e1e3e25 100644 --- a/src/nativeTest/kotlin/dn42/m724/registry/test/TestUtils.kt +++ b/src/nativeTest/kotlin/dn42/m724/registry/test/TestUtils.kt @@ -10,12 +10,11 @@ import kotlinx.io.readString import kotlinx.io.writeString import kotlin.test.Test import kotlin.test.assertEquals -import kotlin.test.fail -private const val RESOURCES_PATH_PREFIX = "src/nativeTest/resources/" +private val RESOURCE_ROOT_PATH = Path("src/nativeTest/resources/") fun getResourcePath(resource: String): Path = - Path(RESOURCES_PATH_PREFIX + resource) + Path(RESOURCE_ROOT_PATH, resource) /** * Retrieves a raw source for the specified resource.