TestRdfHandler.kt 5.67 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
package org.memobase

Thomas Bernhart's avatar
Thomas Bernhart committed
3
4
5
import java.io.File
import java.io.StringWriter
import java.nio.charset.Charset
Jonas Waeber's avatar
Jonas Waeber committed
6
7
import org.apache.jena.riot.Lang
import org.apache.jena.riot.RDFDataMgr
8
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
9
10
11
12
13
14
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestRdfHandler {
15
    private val log = LogManager.getLogger("TestRdfHandler")
Jonas Waeber's avatar
Jonas Waeber committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

    private val resourcePath = "src/test/resources/rdf"
    private fun readFile(fileName: String): String {
        return File("$resourcePath/$fileName").readText(Charset.defaultCharset())
    }

    private val regex = Regex("(_:B[A-Za-z0-9]+)")

    private fun sort(input: List<String>): String {
        return input.map {
            var replacedString = it
            for (matchResult in regex.findAll(it)) {
                replacedString = replacedString.replace(matchResult.groups[0]?.value.orEmpty(), "_:B")
            }
            replacedString
        }.sorted().reduce { acc, s -> acc + "\n" + s }
    }

    @Test
    fun `test get record`() {
36
        val rdfHandler = RdfHandler(readFile("input.nt"), "https://memobase.ch/")
Jonas Waeber's avatar
Jonas Waeber committed
37

38
        val pair = rdfHandler.getRecord()
Jonas Waeber's avatar
Jonas Waeber committed
39
40

        val out = StringWriter()
41
        RDFDataMgr.write(out, pair.second, Lang.NTRIPLES)
Jonas Waeber's avatar
Jonas Waeber committed
42

43
44
        log.error(out.toString())

Jonas Waeber's avatar
Jonas Waeber committed
45
46
47
48
        val sortedOut = sort(out.toString().split("\n")).trim()

        assertThat(sortedOut)
            .isEqualTo(sort(readFile("recordOutput.nt").split("\n")).trim())
49
50
51

        assertThat(pair.first)
                .isEqualTo("https://memobase.ch/record/AFZ-IB_Becker_Audiovisuals_63")
Jonas Waeber's avatar
Jonas Waeber committed
52
53
    }

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    @Test
    fun `test get record with activity and mechanism`() {
        val rdfHandler = RdfHandler(readFile("inputActivity.nt"), "https://memobase.ch/")

        val pair = rdfHandler.getRecord()

        val out = StringWriter()
        RDFDataMgr.write(out, pair.second, Lang.NTRIPLES)

        log.error(out.toString())

        val sortedOut = sort(out.toString().split("\n")).trim()

        assertThat(sortedOut)
            .isEqualTo(sort(readFile("recordActivityOutput.nt").split("\n")).trim())

        assertThat(pair.first)
            .isEqualTo("https://memobase.ch/record/AfZ-Bosshard-NL_Walter_Bosshard-504")
    }

74
75
76
77
78
    @Test
    fun `test get referenced non binary resources`() {
        val rdfHandler = RdfHandler(readFile("input.nt"), "https://memobase.ch/")
        val resources = rdfHandler.getReferencedNonBinaryResources()

79
        resources.sorted().forEachIndexed { index, uri ->
80
81
82
83
84
85
            assertThat(uri)
                    .isEqualTo(nonBinaryResourcesResult[index])
        }
    }

    private val nonBinaryResourcesResult: List<String> = listOf(
86
            "https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-1",
87
88
89
            "https://memobase.ch/instantiation/physical/AFZ-IB_Becker_Audiovisuals_63-0",
            "https://memobase.ch/institution/AFZ",
            "https://memobase.ch/institution/Memoriav",
90
91
            "https://memobase.ch/record/AFZ-IB_Becker_Audiovisuals_63",
            "https://memobase.ch/recordSet/BECKER"
92
93
    )

Jonas Waeber's avatar
Jonas Waeber committed
94
95
    @Test
    fun `test get instantiations`() {
96
        val rdfHandler = RdfHandler(readFile("input.nt"), "https://memobase.ch/")
97
        val list = rdfHandler.getInstantiations()
Jonas Waeber's avatar
Jonas Waeber committed
98

99
        list.forEachIndexed { index, pair ->
Jonas Waeber's avatar
Jonas Waeber committed
100
            val out = StringWriter()
101
            RDFDataMgr.write(out, pair.second, Lang.NTRIPLES)
Jonas Waeber's avatar
Jonas Waeber committed
102
103
104
105
            val sortedOut = sort(out.toString().split("\n")).trim()

            assertThat(sortedOut)
                .isEqualTo(sort(readFile("instantiationOutput$index.nt").split("\n")).trim())
106
107
108

            assertThat(pair.first)
                    .isEqualTo(uris[index])
Jonas Waeber's avatar
Jonas Waeber committed
109
        }
Jonas Waeber's avatar
Jonas Waeber committed
110
    }
Jonas Waeber's avatar
Jonas Waeber committed
111

112
113
114
115
116
    private val uris = listOf(
            "https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-1",
            "https://memobase.ch/instantiation/physical/AFZ-IB_Becker_Audiovisuals_63-0"
    )

Jonas Waeber's avatar
Jonas Waeber committed
117
118
    @Test
    fun `test get sftp locators`() {
119
        val rdfHandler = RdfHandler(readFile("inputSftp.nt"), "https://memobase.ch/")
Jonas Waeber's avatar
Jonas Waeber committed
120
        val pairs = rdfHandler.getSftpLocators()
Jonas Waeber's avatar
Jonas Waeber committed
121

Jonas Waeber's avatar
Jonas Waeber committed
122
123
124
125
        pairs.forEachIndexed { index, pair ->
            assertThat(pair)
                .isEqualTo(resultPairs[index])
        }
Jonas Waeber's avatar
Jonas Waeber committed
126
127
    }

Jonas Waeber's avatar
Jonas Waeber committed
128
129
130
131
    private val resultPairs: List<Pair<String, String?>> = listOf(
        Pair("https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-1", null),
        Pair("https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-2", "/path/to/file/filename.jpg")
    )
Jonas Waeber's avatar
Jonas Waeber committed
132

133
134
    @Test
    fun `test replace sftp locators`() {
135
        val rdfHandler = RdfHandler(readFile("inputSftp.nt"), "https://memobase.ch/")
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
        val list = rdfHandler.getInstantiations()

        list.forEachIndexed { index, pair ->
            val out = StringWriter()
            RDFDataMgr.write(out, pair.second, Lang.NTRIPLES)
            val sortedOut = sort(out.toString().split("\n")).trim()

            assertThat(sortedOut)
                .isEqualTo(sort(readFile("replaceSftpLocatorOutput$index.nt").split("\n")).trim())

            assertThat(pair.first)
                .isEqualTo(uris2[index])
        }
    }

151
152
    @Test
    fun `test mime type extraction for binaries`() {
153
        val rdfHandler = RdfHandler(readFile("input.nt"), "https://memobase.ch/")
154
155
        val mimeType = rdfHandler.getMimeType("https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-1")
        assertThat(mimeType).isEqualTo("video/mpeg")
156
157
    }

158
159
160
161
    private val uris2 = listOf(
        "https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-1",
        "https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-2"
    )
Thomas Bernhart's avatar
Thomas Bernhart committed
162
}