-
Notifications
You must be signed in to change notification settings - Fork 2
/
MappingJsonLdContextTest.java
123 lines (110 loc) · 5.42 KB
/
MappingJsonLdContextTest.java
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
/*
* JB4JSON-LD
* Copyright (C) 2023 Czech Technical University in Prague
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package cz.cvut.kbss.jsonld.serialization.context;
import cz.cvut.kbss.jopa.vocabulary.DC;
import cz.cvut.kbss.jopa.vocabulary.RDFS;
import cz.cvut.kbss.jopa.vocabulary.XSD;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.environment.Generator;
import cz.cvut.kbss.jsonld.serialization.JsonNodeFactory;
import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
import cz.cvut.kbss.jsonld.serialization.serializer.SerializerUtils;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
class MappingJsonLdContextTest {
private final MappingJsonLdContext sut = new MappingJsonLdContext();
@Test
void getMappedTermReturnsMappedTermWhenSimpleIriBasedMappingIsRegistered() {
final String term = "types";
sut.registerTermMapping(term, JsonLd.TYPE);
final Optional<String> result = sut.getMappedTerm(JsonLd.TYPE);
assertTrue(result.isPresent());
assertEquals(term, result.get());
}
@Test
void getMappedTermReturnsMappedTermWhenObjectNodeIsRegisteredForIt() {
final String term = "created";
sut.registerTermMapping("id", JsonLd.ID);
sut.registerTermMapping(term, SerializerUtils.createTypedTermDefinition(term, DC.Terms.CREATED, XSD.DATE));
final Optional<String> result = sut.getMappedTerm(DC.Terms.CREATED);
assertTrue(result.isPresent());
assertEquals(term, result.get());
}
@Test
void getMappedTermReturnsEmptyOptionalWhenNoMatchingMappingIsRegistered() {
sut.registerTermMapping("id", JsonLd.ID);
sut.registerTermMapping("created",
SerializerUtils.createTypedTermDefinition("created", DC.Terms.CREATED, XSD.DATE));
final Optional<String> result = sut.getMappedTerm(Generator.generateUri().toString());
assertNotNull(result);
assertFalse(result.isPresent());
}
@Test
void getTermMappingRetrievesMappingFromParentContextWhenAvailable() {
sut.registerTermMapping("id", JsonLd.ID);
final MappingJsonLdContext childSut = new MappingJsonLdContext(sut);
final Optional<JsonNode> result = childSut.getTermMapping("id");
assertTrue(result.isPresent());
assertEquals(JsonNodeFactory.createStringLiteralNode("id", JsonLd.ID), result.get());
}
@Test
void getTermMappingReturnsMappingFromCurrentContextThatOverridesParentMapping() {
final String term = "name";
sut.registerTermMapping(term, RDFS.LABEL);
final MappingJsonLdContext childSut = new MappingJsonLdContext(sut);
childSut.registerTermMapping(term, DC.Terms.TITLE);
final Optional<JsonNode> result = childSut.getTermMapping(term);
assertTrue(result.isPresent());
assertEquals(JsonNodeFactory.createStringLiteralNode(term, DC.Terms.TITLE), result.get());
}
@Test
void hasTermMappingReturnsTrueWhenTermIsMappedInParentContext() {
sut.registerTermMapping("id", JsonLd.ID);
final MappingJsonLdContext childSut = new MappingJsonLdContext(sut);
assertTrue(childSut.hasTermMapping("id"));
}
@Test
void registerTermMappingRegistersTermsIntoParentContextAsLongAsNoMappingConflictsAppear() {
final MappingJsonLdContext childSut = new MappingJsonLdContext(sut);
final String term = "name";
childSut.registerTermMapping(term, RDFS.LABEL);
childSut.registerTermMapping("uri", JsonLd.ID);
assertTrue(sut.hasTermMapping(term));
assertTrue(sut.hasTermMapping("uri"));
assertTrue(childSut.isCurrentEmpty());
childSut.registerTermMapping(term, DC.Terms.TITLE);
assertTrue(childSut.hasTermMapping(term));
final Optional<JsonNode> parentResult = sut.getTermMapping(term);
assertTrue(parentResult.isPresent());
assertEquals(JsonNodeFactory.createStringLiteralNode(term, RDFS.LABEL), parentResult.get());
final Optional<JsonNode> childResult = childSut.getTermMapping(term);
assertTrue(childResult.isPresent());
assertEquals(JsonNodeFactory.createStringLiteralNode(term, DC.Terms.TITLE), childResult.get());
}
@Test
void registerTermMappingRegistersTermsIntoParentContextAfterMappingConflictWhenRegisteredTermIsNotInConflict() {
final MappingJsonLdContext childSut = new MappingJsonLdContext(sut);
final String term = "name";
childSut.registerTermMapping(term, RDFS.LABEL);
childSut.registerTermMapping(term, DC.Terms.TITLE);
childSut.registerTermMapping("created", SerializerUtils.createTypedTermDefinition("created", DC.Terms.CREATED,
XSD.DATETIME));
assertTrue(sut.hasTermMapping("created"));
}
}