CHOSpec.scala 12.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * rico2edm
 * Copyright (C) 2021  UB Basel
 *
 * 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/>.
 *
 */

20
package ch.memobase.rico2edm.edm
21
22

import ch.memobase.Utils.loadFile
23
24
import ch.memobase.rico2edm.edm.subjects.ProvidedCHO
import ch.memobase.rico2edm.rdf.writer.RdfXmlWriter
25
import org.eclipse.rdf4j.rio.Rio
26
27
28
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers

29
30
import scala.util.{Failure, Success}

31
32
33
class CHOSpec extends AnyFunSuite with Matchers{

  private lazy val rawRicoIndent = loadFile("src/test/resources/rawIndent.json")
34
  private lazy val rawRicoIndentNoContributor = loadFile("src/test/resources/rawIndent.no.contributor.json")
35

36
  private lazy val ricoWithContributors = loadFile("src/test/resources/raw.contributor.json")
37
  private lazy val ricoContributorP60441 = loadFile("src/test/resources/contributor.p60441.json")
38
39
40
  private lazy val ricoRegulatedBy = loadFile("src/test/resources/rico.regulatedBy.json")

  private lazy val ricoNoSourceInRecord = loadFile("src/test/resources/raw2.indent.json")
41
  private lazy val ricoPlaceCapture = loadFile("src/test/resources/fuer.silvia.json")
42
43
  private lazy val ricoCertainty = loadFile("src/test/resources/rico.certainty.json")

44
45
46
47
48
49
50
  private lazy val ricoDuration = loadFile("src/test/resources/rico.duration.json")

  private lazy val ricoNoPhysicalObjectAvailable = loadFile("src/test/resources/rico.NoPhysicalObject.json")

  private val ricoExpressedDateForIssued = loadFile("src/test/resources/ricoFakedExpressedDateForIssued.json")

  private val ricoNoCorrectIssuedProperty = loadFile("src/test/resources/rico.durationNoCorrectIssuedDate.json")
51

52

53
  test("get creator") {
54
55
56

    val graph = Extractors.jsonGraph(rawRicoIndent).get.arr
    val record = Extractors.record(graph).get.obj
57
    val contributors = Extractors
58
59
60
      .resourceCreator(graph)(
        record.value
      )
61
62
63
64
    assert(contributors.length == 1)
    assert(contributors.head._2 == "Valentino Monotti")

  }
65
66


67
  test("missing creators - wrong blank node") {
68

69
70
71
72
73
74
75
    val graph = Extractors.jsonGraph(rawRicoIndentNoContributor).get.arr
    val record = Extractors.record(graph).get.obj
    val contributors = Extractors
      .resourceCreator(graph)(
        record.value
      )
    assert(contributors.isEmpty)
76
77
78

  }

79
80
81
  test ("get contributors") {
    val graph = Extractors.jsonGraph(ricoWithContributors).get.arr
    val record = Extractors.record(graph).get.obj
82

83
    val contributors = Extractors
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
      .resourceSourceOfRelationCreation(graph)(record.value)("contributor")

    assert(contributors.length == 4)

    val creators = Extractors
      .resourceSourceOfRelationCreation(graph)(record.value)("creator")

    assert(creators.length == 1)

    val graph1 = Extractors.jsonGraph(ricoContributorP60441).get.arr
    val record1 = Extractors.record(graph1).get.obj
    val creators1 = Extractors
      .resourceSourceOfRelationCreation(graph1)(record1.value)("creator")
    val contributors1 = Extractors
      .resourceSourceOfRelationCreation(graph1)(record1.value)("contributor")

    assert(creators1.length == 1)
    assert(contributors1.length == 2)
    val p60441 = Extractors.resourceContributorRDauP60441(graph1)(record1.value)
    assert(p60441.get == "Studio Radio Lausanne")
104
105
106

  }

107
108
109
110
111
112
113
114
115
116

  test ("get short record ID") {
    val graph = Extractors.jsonGraph(ricoRegulatedBy)
    val record = Extractors.record(graph.get.arr).get

    val shortRecordId = Extractors.recordIdShort(graph.get.arr)(record)
    assert(shortRecordId.isDefined && shortRecordId.get == "bbb-002-387250")

  }

117
118
119
120
121
122
123
124
125
  test ("get identifier") {
    val graph = Extractors.jsonGraph(ricoWithContributors).get.arr
    val record = Extractors.record(graph).get.obj

    val identifier = Extractors.identifiedBy(graph)(record.value)
    assert(identifier.length == 2)
    assert(identifier(0) == "FNB023_1639.023")
    assert(identifier(1) == "rts-002-FNB023_1639_023")

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  }

  test ("has language") {
    val graph = Extractors.jsonGraph(ricoWithContributors).get.arr
    val record = Extractors.record(graph).get.obj

    //todo: in der bestehenden Implementierung wird _:b43 (resultsFrom)
    // ausgefiltert - s. hier auch Kommentar von Silvia
    // nehmen wir property @language oder Mapping nach Wikidata???
    val languages = Extractors.resourceAllLanguages(graph)(record.value)

    assert(languages.length == 4)
    assert(languages.head == "Français")

  }

  test ("test publisher") {
    val graph = Extractors.jsonGraph(ricoContributorP60441).get.arr
    val record = Extractors.record(graph).get.obj

    //todo: in der bestehenden Implementierung wird _:b43 (resultsFrom)
    // ausgefiltert - s. hier auch Kommentar von Silvia
    // nehmen wir property @language oder Mapping nach Wikidata???
    val publisher = Extractors.publishedByGH(graph)(record.value)

    assert(publisher.length == 1 && publisher.head == "[Aucune information]")

  }

  test ("collect dc:rights") {
    val graph = Extractors.jsonGraph(ricoRegulatedBy).get.arr
    val record = Extractors.record(graph).get.obj

    val regulated = Extractors.regulatedMultipleByHolder(graph)(record)
    assert(regulated.length == 1 && regulated.head == "Erben von Eugen Thierstein")

  }

  test ("collect dc:source") {
    val graph = Extractors.jsonGraph(rawRicoIndentNoContributor).get.arr
    val record = Extractors.record(graph).get.obj
    val source = Extractors.recordSource(record.value)
    assert(source.isDefined && source.get == "Monotti_L_44.13.jpg")

    val graph1 = Extractors.jsonGraph(ricoNoSourceInRecord).get.arr
    val record1 = Extractors.record(graph1).get.obj

    //no source property in this graph
    assert(Extractors.recordSource(record1.value).isEmpty)

  }

  test ("collect subjects dc:subject") {
    val graph = Extractors.jsonGraph(ricoWithContributors).get.arr
    val record = Extractors.record(graph).get.obj

    val subjects = Extractors.resourceSubjects(graph)(record.value)

    assert(subjects.length == 4 && subjects.head == "Coup d'état en Algérie")

  }

  test ("collect dc:title") {
    val graph = Extractors.jsonGraph(ricoWithContributors).get.arr
    val record = Extractors.record(graph).get.obj

    val titles = Extractors.resourceTitles(graph)(record.value)

    //we have both hasTitle and title in record
    assert(titles.length == 1)
    val title = Extractors.title(record)
    assert(title.isDefined)

  }

  test ("collect genre dc:type") {
    val graph = Extractors.jsonGraph(ricoRegulatedBy).get.arr
    val record = Extractors.record(graph).get.obj

    val genres = Extractors.resourceGenres(graph)(record.value)
    assert(genres.length == 1 && genres.head == "Fotos")
207
208

  }
209

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

  test ("collect alternatives dcterms:created") {
    val graph = Extractors.jsonGraph(ricoRegulatedBy).get.arr
    val record = Extractors.record(graph).get.obj

    val normalizedDateValue = Extractors.dcTermsCreated(graph)(record)

    assert(normalizedDateValue.length == 1 && normalizedDateValue.head == "1942-08-07")

    val graph1 = Extractors.jsonGraph(ricoCertainty).get.arr
    val record1 = Extractors.record(graph1).get.obj
    val certainty = Extractors.dcTermsCreated(graph1)(record1)
    certainty.foreach(println)

  }

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
  test ("collect dcterms:extent") {
    val graph = Extractors.jsonGraph(ricoDuration).get.arr
    val physicalObjects = Extractors.physicalObject(graph).get
    val phyObjWithDuration = Extractors.resourceDCTermsExtent(physicalObjects)
    assert(phyObjWithDuration.length == 1 && phyObjWithDuration.head == "00:30:00")


    //no duration on physical object available
    val graph1 = Extractors.jsonGraph(ricoCertainty).get.arr
    val physicalObjects1 = Extractors.physicalObject(graph1).get
    val noDuration = Extractors.resourceDCTermsExtent(physicalObjects1)
    assert(noDuration.isEmpty)


    //no physical objects available (haven't found an object so far in dataset - test-graph was edited by GH)
    val graph2 = Extractors.jsonGraph(ricoNoPhysicalObjectAvailable).get.arr
    val physicalObjectsEmpty = Extractors.physicalObject(graph2).get
    assert(physicalObjectsEmpty.isEmpty)
    val responseForEmptyPhysicalObjectsList = Extractors.resourceDCTermsExtent(physicalObjectsEmpty)
    assert(responseForEmptyPhysicalObjectsList.isEmpty)


  }

  test ("collect dcterms:issued") {
    val graph = Extractors.jsonGraph(ricoDuration).get.arr
    val record = Extractors.record(graph).get


    val firstCollectedNormalizedIssuedDate = Extractors.dcTermsIssuedTemporal(graph)(record)("issued")
    assert(firstCollectedNormalizedIssuedDate.nonEmpty && firstCollectedNormalizedIssuedDate.get == "2012-12-07")

    val graph1 = Extractors.jsonGraph(ricoExpressedDateForIssued).get.arr
    val record1 = Extractors.record(graph1).get
    val firstCollectedExpressedDate = Extractors.dcTermsIssuedTemporal(graph1)(record1)("issued")
    assert(firstCollectedExpressedDate.nonEmpty && firstCollectedExpressedDate.get == "1953-12-07-1953-12-07")

    val graph2 = Extractors.jsonGraph(ricoCertainty).get.arr
    val record2 = Extractors.record(graph2).get
    val noIssuedDate = Extractors.dcTermsIssuedTemporal(graph2)(record2)("issued")
    assert(noIssuedDate.isEmpty)

    //partial collectFirst function doesn't match even there is a blank node for the issued property
    //ricoNoCorrectIssuedProperty

    val graph3 = Extractors.jsonGraph(ricoNoCorrectIssuedProperty).get.arr
    val record3 = Extractors.record(graph3).get
    val noCorrectIssuedDate = Extractors.dcTermsIssuedTemporal(graph3)(record3)("issued")
    assert(noCorrectIssuedDate.isEmpty)

  }

  test ("collect dcterms:medium") {
    val graph = Extractors.jsonGraph(rawRicoIndent).get.arr

    //s. zum Umgang mit dem Extractor auch den ausführlichen Kommentar bei der Funktion
    val mediums = Extractors.resourceDCTermsMedium(graph)
    assert(mediums.nonEmpty && mediums.head == "Nitrato")

  }

  test ("collect dcterms:spatial") {
    val graph = Extractors.jsonGraph(ricoNoSourceInRecord).get.arr
    val record = Extractors.record(graph).get

    //s. zum Umgang mit dem Extractor auch den ausführlichen Kommentar bei der Funktion
    val spatials = Extractors.dcTermsSpatial(graph)(record)
    assert(spatials.length == 2 && spatials.head == "Madrid" && spatials(1) == "Spagna" )

295
296
297
298
299
    val graph2 = Extractors.jsonGraph(ricoPlaceCapture).get.arr
    val record2 = Extractors.record(graph2).get
    val spatialsCapture = Extractors.dcTermsSpatialCapture(graph2)(record2)
    assert(spatialsCapture.length == 1 && spatialsCapture.head == "Genève" )

300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  }

  test ("collect dcterms:temporal") {
    val graph = Extractors.jsonGraph(ricoContributorP60441).get.arr
    val record = Extractors.record(graph).get

    val temporal: Option[String] = Extractors.dcTermsIssuedTemporal(graph)(record)("temporal")
    assert(temporal.isDefined && temporal.get == "1949-07-22-1949-07-22; 1944")

  }



  test ("get institution from graph") {
    val graph = Extractors.jsonGraph(ricoContributorP60441).get.arr
    val record = Extractors.record(graph)

    //extract institutions
    val identsInstitution = Extractors.recordSetOrInstitution(record)("heldBy")
    assert(identsInstitution.length == 2 && identsInstitution.head == "https://memobase.ch/institution/rts" &&
      identsInstitution(1) == "https://memobase.ch/institution/nurEinTest")

    val kuerzelInstitution = identsInstitution.map( ident => EDM.getInstitutionOrRecordsetIdent(ident))
    assert(kuerzelInstitution.length == 2 && kuerzelInstitution.head == "rts" &&
      kuerzelInstitution(1) == "nurEinTest")

    //extract recordsets
    //isPartOf
    val identsRecordsets = Extractors.recordSetOrInstitution(record)("isPartOf")
    assert(identsRecordsets.length == 1 && identsRecordsets.head == "https://memobase.ch/recordSet/rts-002")
    val kuerzelRecordsets = identsRecordsets.map( ident => EDM.getInstitutionOrRecordsetIdent(ident))
    assert(kuerzelRecordsets.length == 1 && kuerzelRecordsets.head == "rts-002")

  }

335
336
337
338
339
340
341
  test ("edm type") {
    val graph = Extractors.jsonGraph(ricoContributorP60441).get.arr
    val record = Extractors.record(graph)
    val edmType = Extractors.edmType(record.get)
    assert(edmType.isDefined && edmType.get == "SOUND")
  }

342

343

344
}