LocalTestRun.kt 4.52 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Normalization Service Configuration
 * Copyright (C) 2020 Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package org.memobase.test

import ch.memobase.configs.GlobalTransformsLoader
import ch.memobase.configs.LocalTransformsLoader
import ch.memobase.rdf.MemobaseModel
import ch.memobase.settings.HeaderMetadata
import java.io.File
import org.apache.jena.riot.Lang
import org.apache.jena.riot.RDFDataMgr
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Disabled
class LocalTestRun {

    private val headerMetadata = HeaderMetadata(
        "srf-001",
        "1",
        "srf",
        false,
        "record",
        "identifierMain",
        0, 0, 0, 0
    )

    /*
    @Test
    @Disabled
    fun `test a single file`() {
        val folder = "/home/jonas/memobase/data/apf-001"
        val inputPath = "/home/jonas/memobase/data/test/step-2/Fonoteca-18BD1047_A29"
        val outputPath = "/home/jonas/memobase/data/test/step-3/snp-001-18BD1047_A29"
        val mappingFile = "/config/mapping.yml"
        val klaxon = Klaxon()
        val configurationParser = MappingConfigurationParser(File(folder + mappingFile).readBytes())
        val configuration = configurationParser.get()
        val item = klaxon.parse<Map<String, Any>>(File(inputPath)).orEmpty()

        val builder = ResourceBuilder(
            item,
            configuration,
            headerMetadata.institutionId,
            headerMetadata.recordSetId,
            headerMetadata.isPublished
        )
        val result = builder.extractRecordId()
            .extractRecordTypeValue()
            .generateRecord()
            .generatePhysicalObject()
            .generateDigitalObject()
            .addDerivedConnection()

        val writtenResult = result
            .writeRecord(RDFFormat.NTRIPLES_UTF8)

        val writtenResultTurtle = result
            .writeRecord(RDFFormat.TURTLE_PRETTY)

        FileOutputStream(File(outputPath)).use {
            it.bufferedWriter().use { writer ->
                writer.write(writtenResult.second)
            }
        }
        FileOutputStream(File("output.ttl")).use {
            it.bufferedWriter().use { writer ->
                writer.write(writtenResultTurtle.second)
            }
        }

    }
       */
    @Test
    @Disabled
    fun `test local folder`() {
        val folder = "/home/jonas/memobase/data/srf-001"
        val inputFolder = "/home/jonas/memobase/data/test/step-3"
        val outputFolder = "/home/jonas/memobase/data/test/step-4"
        val mappingFile = "/config/localTransforms.yml"
        val global = GlobalTransformsLoader("src/test/resources/global/transforms.yml")
Jonas Waeber's avatar
Jonas Waeber committed
99
        global.parse()
100
        val local = LocalTransformsLoader(File(folder + mappingFile).readBytes())
101
        local.parse("test-key")
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        val transformConfigs = local.get() + global.get()
        File(inputFolder)
            .walk(FileWalkDirection.TOP_DOWN)
            .maxDepth(1)
            .filter { it.isFile }
            .map { Pair(it.name, it) }
            .map {
                val model = MemobaseModel()
                RDFDataMgr.read(model, it.second.inputStream(), Lang.NTRIPLES)
                Pair(it.first, model)
            }
            .map {
                for (transformConfig in transformConfigs) {
                    val listOfResources = it.second.listRicoResourceSubjects().toList()
                    for (resource in listOfResources) {
                        transformConfig.transform(resource, it.second)
                    }
                }
                Pair(it.first, it.second)
            }
            .forEach { result ->
                RDFDataMgr.write(
                    File(outputFolder + "/" + result.first.substringAfterLast("/")).outputStream(),
                    result.second,
                    Lang.TURTLE
                )
            }
    }
}