-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgn4.field.mapper.spec.ts
More file actions
591 lines (581 loc) · 20.3 KB
/
Copy pathgn4.field.mapper.spec.ts
File metadata and controls
591 lines (581 loc) · 20.3 KB
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
99
100
101
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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
295
296
297
298
299
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import { TestBed } from '@angular/core/testing'
import { elasticLinkFixture } from '@geonetwork-ui/common/fixtures'
import { Gn4FieldMapper } from './gn4.field.mapper'
import { MetadataUrlService } from './metadata-url.service'
import { TranslateService } from '@ngx-translate/core'
setupZoneTestEnv()
class MetadataUrlServiceMock {
translate = undefined
getUrl = () => 'url'
}
const translateServiceMock = {
currentLang: 'de',
}
describe('Gn4FieldMapper', () => {
let service: Gn4FieldMapper
let translateService: TranslateService
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: MetadataUrlService, useClass: MetadataUrlServiceMock },
{
provide: TranslateService,
useValue: translateServiceMock,
},
],
})
service = TestBed.inject(Gn4FieldMapper)
translateService = TestBed.inject(TranslateService)
})
it('should be created', () => {
expect(service).toBeTruthy()
})
describe('methods', () => {
beforeEach(() => {
service = TestBed.inject(Gn4FieldMapper)
})
describe('#getLinkType', () => {
it('correctly detects the fixtures types', () => {
const allLinks = Object.keys(elasticLinkFixture()).map(
(key) => elasticLinkFixture()[key]
)
const linkTypes = allLinks.map((fixture) =>
service.getLinkType(fixture.url, fixture.accessServiceProtocol)
)
expect(linkTypes).toStrictEqual([
'link',
'link',
'download',
'download',
'download',
'download',
'download',
'download',
'download',
'download',
'download',
'download',
'download',
'download',
'download',
'service',
'service',
'service',
'service',
'service',
'service',
'service',
'link',
'link',
'download',
'service',
'service',
'service',
'service',
])
})
})
describe('#getMappingFn', () => {
it('should return a function when given a valid field name', () => {
const fieldName = 'id'
const mappingFn = service.getMappingFn(fieldName)
expect(typeof mappingFn).toBe('function')
})
it('should return a generic field when given an invalid field name', () => {
const fieldName = 'invalidField'
const mappingFn = service.getMappingFn(fieldName)
expect(mappingFn).toBe(service.genericField)
})
it('should return a function that maps the correct field even if the source object has additional unknown properties', () => {
const fieldName = 'id'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = { id: '12345', unknownProp: 'value' }
const result = mappingFn(output, source)
expect(result).toEqual({ extras: { id: '12345' } })
})
describe('field mappings', () => {
it('id - should return a function that correctly maps the field', () => {
const fieldName = 'id'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = { id: '12345' }
const result = mappingFn(output, source)
expect(result).toEqual({ extras: { id: '12345' } })
})
it('uuid - should return a function that correctly maps the field', () => {
const fieldName = 'uuid'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = { uuid: '12345' }
const result = mappingFn(output, source)
expect(result).toEqual({
landingPage: new URL('http://localhost/url'),
uniqueIdentifier: '12345',
})
})
it('resourceTitleObject - should return a function that correctly maps the field to default lang', () => {
translateService.currentLang = 'en'
const fieldName = 'resourceTitleObject'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceTitleObject: {
default: 'Default title',
langfre: 'French title',
},
}
const result = mappingFn(output, source)
expect(result).toEqual({
title: 'Default title',
extras: {
resourceTitleObject: { default: 'Default title', langfre: 'French title' },
},
})
})
it('resourceAbstractObject - should return a function that correctly maps the field to fre lang', () => {
translateService.currentLang = 'fr'
const fieldName = 'resourceAbstractObject'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceAbstractObject: {
default: 'Default abstract',
langfre: 'French abstract',
},
}
const result = mappingFn(output, source)
expect(result).toEqual({
abstract: 'French abstract',
extras: {
resourceAbstractObject: { default: 'Default abstract', langfre: 'French abstract' },
},
})
})
it('resourceAltTitleObject - should store alt title array in extras', () => {
const fieldName = 'resourceAltTitleObject'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceAltTitleObject: [
{ langfre: 'Titre alternatif FR', langger: 'Alternativer Titel DE' },
],
}
const result = mappingFn(output, source)
expect(result).toEqual({
extras: {
resourceAltTitleObject: [
{ langfre: 'Titre alternatif FR', langger: 'Alternativer Titel DE' },
],
},
})
})
it('overview - should return a function that correctly maps the field', () => {
translateService.currentLang = 'fr'
const fieldName = 'overview'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
overview: [
{
url: 'https://mygeodata/map.png',
text: {
default: 'Default overview description',
langfre: 'French overview description',
},
},
],
}
const result = mappingFn(output, source)
expect(result).toEqual({
overviews: [
{
url: new URL('https://mygeodata/map.png'),
description: 'French overview description',
},
],
})
})
it('cl_status - should return a function that correctly maps the field', () => {
const fieldName = 'cl_status'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
cl_status: {
key: 'completed',
default: 'Finalisé',
langfre: 'Finalisé',
link: 'http://standards.iso.org/iso/19139/resources/gmxCodelists.xml#MD_ProgressCode',
},
}
const result = mappingFn(output, source)
expect(result).toEqual({ status: 'completed', extras: { cl_statusObject: { key: 'completed', default: 'Finalisé', langfre: 'Finalisé', link: 'http://standards.iso.org/iso/19139/resources/gmxCodelists.xml#MD_ProgressCode' } } })
})
it('cl_subTopicCategory - should return a function that maps subtopics', () => {
translateService.currentLang = 'de'
const fieldName = 'cl_subTopicCategory'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
cl_subTopicCategory: [
{ default: 'Subtopic 1', langger: 'Unterthema 1' },
{ default: 'Subtopic 2', langger: 'Unterthema 2' },
],
}
const result = mappingFn(output, source)
expect(result).toEqual({
subtopics: ['Unterthema 1', 'Unterthema 2'],
})
})
it('MD_LegalConstraintsOtherConstraintsObject - should store in extras and add to legalConstraints', () => {
translateService.currentLang = 'de'
const fieldName = 'MD_LegalConstraintsOtherConstraintsObject'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
MD_LegalConstraintsOtherConstraintsObject: [
{ default: 'CC-BY', langger: 'CC-BY' },
],
}
const result = mappingFn(output, source)
expect(result).toEqual({
legalConstraints: [{ text: 'CC-BY' }],
extras: {
MD_LegalConstraintsOtherConstraintsObject: [
{ default: 'CC-BY', langger: 'CC-BY' },
],
},
})
})
it('isHarvested - should return a function that correctly maps the field', () => {
const fieldName = 'isHarvested'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
isHarvested: 'true',
}
const result = mappingFn(output, source)
expect(result).toEqual({ extras: { isHarvested: true } })
})
it('edit - should return a function that correctly maps the field', () => {
const fieldName = 'edit'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
edit: true,
}
const result = mappingFn(output, source)
expect(result).toEqual({ extras: { edit: true } })
})
it('linkProtocol - should return a function that stores protocols in extras', () => {
const fieldName = 'linkProtocol'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
linkProtocol: ['MAP:Preview', 'OGC:WMS', 'WWW:DOWNLOAD-URL'],
}
const result = mappingFn(output, source)
expect(result).toEqual({
extras: {
linkProtocol: ['MAP:Preview', 'OGC:WMS', 'WWW:DOWNLOAD-URL'],
},
})
})
it('format - should return a function that stores formats in extras', () => {
const fieldName = 'format'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
format: ['ESRI Shapefile (SHP)', 'GeoJSON'],
}
const result = mappingFn(output, source)
expect(result).toEqual({
extras: {
format: ['ESRI Shapefile (SHP)', 'GeoJSON'],
},
})
})
it('featureTypes - should return a function that stores featureTypes in extras', () => {
const fieldName = 'featureTypes'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
featureTypes: [
{
typeName: 'Bornes parcellaires',
definition: '',
attributeTable: [{ name: 'ID', type: 'string' }],
},
],
}
const result = mappingFn(output, source)
expect(result).toEqual({
extras: {
featureTypes: [
{
typeName: 'Bornes parcellaires',
definition: '',
attributeTable: [{ name: 'ID', type: 'string' }],
},
],
},
})
})
it('languages - should return a list of languages even with unsupported ones and without defaultLang', () => {
const fieldName = 'otherLanguage'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = { otherLanguage: ['fre', 'ger', 'aar'] }
const result = mappingFn(output, source)
expect(result).toEqual({ otherLanguages: ['de', 'aar'] })
})
it('related - should return a function that correctly maps the field', () => {
const fieldName = 'related'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
related: {
fcats: [
{
origin: 'catalog',
_source: {
uuid: 'featurecatalog-001',
},
},
],
hassources: [
{
origin: 'catalog',
_source: {
uuid: 'hassource-001',
},
},
],
},
}
const result = mappingFn(output, source)
expect(result).toEqual({
extras: {
featureCatalogIdentifier: 'featurecatalog-001',
sourceOfIdentifiers: ['hassource-001'],
},
})
})
})
it('recordLink - should return a function that correctly maps the field', () => {
const fieldName = 'recordLink'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
recordLink: [
{
origin: 'catalog',
to: 'source-001',
type: 'sources',
title: 'Some source data',
url: 'http://www.catalog.org/record/12345',
},
{
origin: 'catalog',
to: 'source-002',
type: 'sources',
title: 'Some other source data',
url: 'http://www.catalog.org/record/67890',
},
{
origin: 'remote',
to: 'source-003',
type: 'sources',
title: 'Some remote source data',
url: 'http://www.othercatalog.org/record/12345',
},
{
origin: 'catalog',
to: 'featurecatalog-001',
type: 'fcats',
title: 'Some feature catalog',
url: 'http://www.catalog.org/record/featurecatalog-001',
},
],
}
const result = mappingFn(output, source)
expect(result).toEqual({
extras: {
sourcesIdentifiers: ['source-001', 'source-002'],
},
})
})
describe('resourceType mapper - should return a function that correctly maps the field', () => {
it('resourceType - should return reuse for resourceType map', () => {
const fieldName = 'resourceType'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceType: ['map'],
}
const result = mappingFn(output, source)
expect(result).toEqual({
kind: 'reuse',
reuseType: 'map',
})
})
it('resourceType - should return dataset for resourceType document', () => {
const fieldName = 'resourceType'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceType: ['document'],
}
const result = mappingFn(output, source)
expect(result).toEqual({
kind: 'dataset',
})
})
it('resourceType - should return reuse for resourceType document with cl_presentationForm mapDigital', () => {
const fieldName = 'resourceType'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceType: ['document'],
cl_presentationForm: [{}, { key: 'mapDigital' }],
}
const result = mappingFn(output, source)
expect(result).toEqual({
kind: 'reuse',
reuseType: 'map',
})
})
it('resourceType - should return dataset for resourceType dataset', () => {
const fieldName = 'resourceType'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceType: ['dataset'],
}
const result = mappingFn(output, source)
expect(result).toEqual({
kind: 'dataset',
})
})
it('resourceType - should return reuse for resourceType dataset with cl_presentationForm mapDigital', () => {
const fieldName = 'resourceType'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceType: ['dataset'],
cl_presentationForm: [{ key: 'mapDigital' }],
}
const result = mappingFn(output, source)
expect(result).toEqual({
kind: 'reuse',
reuseType: 'map',
})
})
it('resourceType - should return dataset for resourceType document with unknown cl_presentationForm', () => {
const fieldName = 'resourceType'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceType: ['document'],
cl_presentationForm: ['unknownType'],
}
const result = mappingFn(output, source)
expect(result).toEqual({
kind: 'dataset',
})
})
it('resourceType - should return dataset for random resourceType', () => {
const fieldName = 'resourceType'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceType: ['foo'],
}
const result = mappingFn(output, source)
expect(result).toEqual({
kind: 'dataset',
})
})
})
})
describe('resourceIdentifier mapper', () => {
it('should map all resource identifiers with code, codeSpace, and url', () => {
const fieldName = 'resourceIdentifier'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceIdentifier: [
{
code: '10.1234/example.doi',
codeSpace: 'doi.org',
link: 'https://doi.org/10.1234/example.doi',
},
{
code: 'ISBN-123-456',
codeSpace: 'ISBN',
link: 'https://isbn.org/123-456',
},
],
}
const result = mappingFn(output, source)
expect(result).toEqual({
resourceIdentifiers: [
{
code: '10.1234/example.doi',
codeSpace: 'doi.org',
url: 'https://doi.org/10.1234/example.doi',
},
{
code: 'ISBN-123-456',
codeSpace: 'ISBN',
url: 'https://isbn.org/123-456',
},
],
})
})
it('should map identifier without codeSpace', () => {
const fieldName = 'resourceIdentifier'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceIdentifier: [
{
code: 'simple-identifier',
},
],
}
const result = mappingFn(output, source)
expect(result).toEqual({
resourceIdentifiers: [
{
code: 'simple-identifier',
},
],
})
})
it('should map identifier without link', () => {
const fieldName = 'resourceIdentifier'
const mappingFn = service.getMappingFn(fieldName)
const output = {}
const source = {
resourceIdentifier: [
{
code: '10.1234/example.doi',
codeSpace: 'doi.org',
},
],
}
const result = mappingFn(output, source)
expect(result).toEqual({
resourceIdentifiers: [
{
code: '10.1234/example.doi',
codeSpace: 'doi.org',
},
],
})
})
})
})
})