From 7c8a977817574dcc8771c2bdcf3baf378a2ee2cd Mon Sep 17 00:00:00 2001
From: "stu126940@mail.uni-kiel.de" <stu126940@mail.uni-kiel.de>
Date: Sat, 1 May 2021 18:46:53 +0200
Subject: [PATCH] Add test for IOHandler
---
.../main/kotlin/theodolite/util/IOHandler.kt | 1 -
.../kotlin/theodolite/util/IOHandlerTest.kt | 76 +++++++++++++++++++
2 files changed, 76 insertions(+), 1 deletion(-)
create mode 100644 theodolite-quarkus/src/test/kotlin/theodolite/util/IOHandlerTest.kt
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/IOHandler.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/IOHandler.kt
index e2679f048..8d379fcf0 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/util/IOHandler.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/IOHandler.kt
@@ -4,7 +4,6 @@ import com.google.gson.GsonBuilder
import mu.KotlinLogging
import java.io.File
import java.io.PrintWriter
-import java.lang.IllegalArgumentException
private val logger = KotlinLogging.logger {}
diff --git a/theodolite-quarkus/src/test/kotlin/theodolite/util/IOHandlerTest.kt b/theodolite-quarkus/src/test/kotlin/theodolite/util/IOHandlerTest.kt
new file mode 100644
index 000000000..76e3d506b
--- /dev/null
+++ b/theodolite-quarkus/src/test/kotlin/theodolite/util/IOHandlerTest.kt
@@ -0,0 +1,76 @@
+package theodolite.util
+
+import com.google.gson.GsonBuilder
+import io.quarkus.test.junit.QuarkusTest
+import org.junit.Rule
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.Test
+import org.junit.rules.TemporaryFolder
+
+
+const val FOLDER_URL = "Test-Folder"
+@QuarkusTest
+internal class IOHandlerTest {
+
+ @Rule
+ var temporaryFolder = TemporaryFolder()
+
+ @Test
+ fun testWriteStringToText() {
+ temporaryFolder.create()
+ val testContent = "Test-File-Content"
+ val folder = temporaryFolder.newFolder(FOLDER_URL)
+
+ IOHandler().writeStringToTextFile(
+ fileURL = "${folder.absolutePath}/test-file.txt",
+ data = testContent)
+
+ assertEquals(
+ testContent,
+ IOHandler().readFileAsString("${folder.absolutePath}/test-file.txt")
+ )
+ }
+
+ @Test
+ fun testWriteToCSVFile() {
+ temporaryFolder.create()
+ val folder = temporaryFolder.newFolder(FOLDER_URL)
+
+ val testContent = listOf(
+ listOf("apples","red"),
+ listOf("bananas","yellow"),
+ listOf("avocado","brown"))
+ val columns = listOf("Fruit", "Color")
+
+ IOHandler().writeToCSVFile(
+ fileURL = "${folder.absolutePath}/test-file",
+ data = testContent,
+ columns = columns)
+
+ var expected = "Fruit,Color\n"
+ testContent.forEach { expected += it[0] + "," + it[1] + "\n" }
+
+ assertEquals(
+ expected.trim(),
+ IOHandler().readFileAsString("${folder.absolutePath}/test-file.csv")
+ )
+ }
+
+ @Test
+ fun testWriteToJSONFile() {
+ temporaryFolder.create()
+ val folder = temporaryFolder.newFolder(FOLDER_URL)
+ val testContent = Resource(0, emptyList())
+
+ IOHandler().writeToJSONFile(
+ fileURL = "${folder.absolutePath}/test-file.json",
+ objectToSave = testContent)
+
+ val expected = GsonBuilder().enableComplexMapKeySerialization().setPrettyPrinting().create().toJson(testContent)
+
+ assertEquals(
+ expected,
+ IOHandler().readFileAsString("${folder.absolutePath}/test-file.json")
+ )
+ }
+}
--
GitLab