-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRoCrateMetadataContextTest.java
More file actions
306 lines (246 loc) · 10.6 KB
/
Copy pathRoCrateMetadataContextTest.java
File metadata and controls
306 lines (246 loc) · 10.6 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
package edu.kit.datamanager.ro_crate.context;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.kit.datamanager.ro_crate.HelpFunctions;
import edu.kit.datamanager.ro_crate.RoCrate;
import edu.kit.datamanager.ro_crate.entities.AbstractEntity;
import edu.kit.datamanager.ro_crate.entities.contextual.ContextualEntity;
import edu.kit.datamanager.ro_crate.entities.data.DataEntity;
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
public class RoCrateMetadataContextTest {
RoCrateMetadataContext context;
RoCrateMetadataContext complexContext;
@BeforeEach
void initContext() throws IOException {
// this will load the default context
this.context = new RoCrateMetadataContext();
final String crateManifestPath = "/crates/extendedContextExample/ro-crate-metadata.json";
ObjectMapper objectMapper = MyObjectMapper.getMapper();
JsonNode jsonNode = objectMapper.readTree(RoCrateMetadataContextTest.class.getResourceAsStream(crateManifestPath));
this.complexContext = new RoCrateMetadataContext(jsonNode.get("@context"));
}
@Test
void testContext() {
var entity = this.context.getContextJsonEntity();
ObjectMapper objectMapper = MyObjectMapper.getMapper();
// we expect the default RO-crate context;
var expected = objectMapper.createObjectNode();
expected.put("@context", "https://w3id.org/ro/crate/1.1/context");
HelpFunctions.compare(entity, expected, true);
// this entity is not correct
// there is no type blabla in schema.org
var contextualEntity = new ContextualEntity.ContextualEntityBuilder()
.setId("dkfaj")
.addType("blabla")
.build();
assertFalse(this.context.checkEntity(contextualEntity));
// this entity is correct the type Accommodation exists in schema.org,
// so it should be valid
var contextualEntityCorrect = new ContextualEntity.ContextualEntityBuilder()
.setId("dkfaj")
.addType("Accommodation")
.addProperty("name", "ffjkjk")
.build();
assertTrue(this.context.checkEntity(contextualEntityCorrect));
// this entity has correct types but wrong field
var contextualEntityWrongField = new ContextualEntity.ContextualEntityBuilder()
.setId("dkfaj")
.addType("Accommodation")
.addProperty("notExisting", "kfdjfk")
.build();
assertFalse(this.context.checkEntity(contextualEntityWrongField));
}
@Test
void creationUrlTest() {
RoCrateMetadataContext newContext = new RoCrateMetadataContext(
List.of("www.example.com", "www.example.com/context"));
var contextualEntityWrongField = new ContextualEntity.ContextualEntityBuilder()
.setId("dkfaj")
.addType("Accommodation")
.build();
// this will be false since here there is no default context, and the example
// context could not be retrieved
assertFalse(newContext.checkEntity(contextualEntityWrongField));
// the two example context should be nevertheless added to the final result
var jsonNode = newContext.getContextJsonEntity();
assertEquals(2, jsonNode.get("@context").size());
}
@Test
void creationFromPairsJsonTest() {
var objectMapper = MyObjectMapper.getMapper();
ObjectNode rawContext = objectMapper.createObjectNode();
rawContext.put("house", "www.example.com/house");
rawContext.put("road", "www.example.com/road");
ObjectNode rawCrate = objectMapper.createObjectNode();
rawCrate.set("@context", rawContext);
RoCrateMetadataContext newContext = new RoCrateMetadataContext(rawContext);
assertNotNull(newContext);
HelpFunctions.compare(newContext.getContextJsonEntity(), rawCrate, true);
var entityWithTerms = new ContextualEntity.ContextualEntityBuilder()
.setId("dkfaj")
.addType("house")
.addType("road")
.build();
assertTrue(newContext.checkEntity(entityWithTerms));
}
@Test
void deletePairTest() {
RoCrateMetadataContext context = new RoCrateMetadataContext();
RoCrateMetadataContext emptyContext = new RoCrateMetadataContext();
context.addToContext("key", "value");
context.deleteValuePairFromContext("key");
HelpFunctions.compare(context.getContextJsonEntity(), emptyContext.getContextJsonEntity(), true);
}
@Test
void deleteNonExistingPairTest() {
RoCrateMetadataContext context = new RoCrateMetadataContext();
RoCrateMetadataContext emptyContext = new RoCrateMetadataContext();
context.deleteValuePairFromContext("key");
HelpFunctions.compare(context.getContextJsonEntity(), emptyContext.getContextJsonEntity(), true);
}
@Test
void deleteUrlTest() {
RoCrateMetadataContext context = new RoCrateMetadataContext();
RoCrateMetadataContext emptyContext = new RoCrateMetadataContext();
context.addToContextFromUrl("www.example.com");
context.deleteUrlFromContext("www.example.com");
HelpFunctions.compare(context.getContextJsonEntity(), emptyContext.getContextJsonEntity(), true);
}
@Test
void doubledContextUrlsTest() {
String url = "www.example.com";
RoCrateMetadataContext context = new RoCrateMetadataContext();
assertFalse(context.urls.contains(url));
context.addToContextFromUrl(url);
assertTrue(context.urls.contains(url));
RoCrateMetadataContext contextDoubled = new RoCrateMetadataContext();
contextDoubled.addToContextFromUrl(url);
contextDoubled.addToContextFromUrl(url);
HelpFunctions.compare(context.getContextJsonEntity(), contextDoubled.getContextJsonEntity(), true);
RoCrateMetadataContext emptyContext = new RoCrateMetadataContext();
contextDoubled.deleteUrlFromContext(url);
HelpFunctions.compare(emptyContext.getContextJsonEntity(), contextDoubled.getContextJsonEntity(), true);
}
@Test
void deleteNonExistentUrlTest() {
RoCrateMetadataContext context = new RoCrateMetadataContext();
RoCrateMetadataContext emptyContext = new RoCrateMetadataContext();
context.deleteUrlFromContext("www.example.com");
HelpFunctions.compare(context.getContextJsonEntity(), emptyContext.getContextJsonEntity(), true);
}
@Test
void creationFromUrlAndPairsTest() {
var objectMapper = MyObjectMapper.getMapper();
ObjectNode res = objectMapper.createObjectNode();
ArrayNode arr = objectMapper.createArrayNode();
ObjectNode node = objectMapper.createObjectNode();
node.put("house", "www.example.con/house");
node.put("road", "www.example.con/road");
arr.add(node);
arr.add("www.example.com/context");
res.set("@context", arr);
RoCrateMetadataContext newContext = new RoCrateMetadataContext(arr);
HelpFunctions.compare(newContext.getContextJsonEntity(), res, true);
var data = new DataEntity.DataEntityBuilder()
.addType("house")
.setId("https://www.example.com/entity")
.build();
// house is in the context
assertTrue(newContext.checkEntity(data));
}
@Test
void testIdType() {
AbstractEntity validEntity = new DataEntity.DataEntityBuilder()
.setId("Airline") // this is defined in the context!
.addType("@id") // this is a JSON-LD feature to refer to the ID ("Airline") as a type
.addType("@json") // this is a JSON-LD built-in type
.build();
assertTrue(this.context.checkEntity(validEntity));
AbstractEntity invalidEntity = new DataEntity.DataEntityBuilder()
.setId("Something which is definitely not in the context")
.addType("@id")
.build();
assertFalse(this.context.checkEntity(invalidEntity));
}
@Test
void testJsonType() {
AbstractEntity validEntity = new DataEntity.DataEntityBuilder()
.addType("@json") // this is a JSON-LD built-in type
.build();
assertTrue(this.context.checkEntity(validEntity));
}
@Test
void testAbsoluteUrlType() {
AbstractEntity validEntity = new DataEntity.DataEntityBuilder()
.addType("http://example.org/Person") // this is not in the context!
.addProperty("http://example.org/Thing", "Some thing")
.build();
assertTrue(this.context.checkEntity(validEntity));
}
@Test
void testSetDeleteGetPair() {
String key = "key";
String value = "value";
context.addToContext(key, value);
assertEquals(value, context.getValueOf(key));
context.deleteValuePairFromContext(key);
assertNull(context.getValueOf(key));
}
@Test
void testReadDeleteGetPair() {
String key = "custom";
String value = "_:";
assertEquals(value, this.complexContext.getValueOf(key));
this.complexContext.deleteValuePairFromContext(key);
assertNull(this.complexContext.getValueOf(key));
this.complexContext.addToContext(key, value);
assertEquals(value, this.complexContext.getValueOf(key));
}
@Test
void testReadKeys() {
var expected = Set.of("custom", "owl", "datacite", "xsd", "rdfs");
var given = this.complexContext.getKeys();
for (String key : expected) {
assertTrue(given.contains(key), "Key " + key + " not found in the context");
}
// prove immutability
assertThrows(UnsupportedOperationException.class, () -> given.add("newKey"));
}
@Test
void testReadPairs() {
var expected = Set.of("custom", "owl", "datacite", "xsd", "rdfs");
var given = this.complexContext.getPairs();
var keys = given.keySet();
var values = given.values();
for (String key : expected) {
assertTrue(keys.contains(key), "Key " + key + " not found in the context");
values.forEach(s -> assertFalse(s.isEmpty(), "Value for key " + key + " is empty"));
}
// prove immutability
assertThrows(UnsupportedOperationException.class, () -> given.put("newKey", "newValue"));
}
@Test
void checkEntity_withDefinedPrefixedType_succeeds() throws IOException {
// assume we read a crate just for demonstration
RoCrate crate = new RoCrate();
// and we extend the context
RoCrateMetadataContext context = new RoCrateMetadataContext();
context.addToContext("rdfs", "https://www.w3.org/2000/01/rdf-schema#");
crate.setMetadataContext(context);
// then we use the new context
DataEntity entity = new DataEntity.DataEntityBuilder()
.addType("rdfs:Property")
.build();
crate.addDataEntity(entity);
// Then we expect this to work
assertTrue(context.checkEntity(entity));
}
}