|
1 | 1 | package edu.kit.datamanager.ro_crate.context; |
2 | 2 |
|
3 | | -import com.fasterxml.jackson.core.JsonProcessingException; |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
4 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
5 | 5 | import com.fasterxml.jackson.databind.node.ArrayNode; |
6 | 6 | import com.fasterxml.jackson.databind.node.ObjectNode; |
|
14 | 14 | import org.junit.jupiter.api.BeforeEach; |
15 | 15 | import org.junit.jupiter.api.Test; |
16 | 16 |
|
| 17 | +import java.io.IOException; |
17 | 18 | import java.util.List; |
| 19 | +import java.util.Set; |
18 | 20 |
|
19 | 21 | import static org.junit.jupiter.api.Assertions.*; |
20 | 22 |
|
21 | 23 | public class ContextTest { |
22 | 24 |
|
23 | 25 | RoCrateMetadataContext context; |
| 26 | + RoCrateMetadataContext complexContext; |
24 | 27 |
|
25 | 28 | @BeforeEach |
26 | | - void initContext() { |
| 29 | + void initContext() throws IOException { |
27 | 30 | // this will load the default context |
28 | 31 | this.context = new RoCrateMetadataContext(); |
| 32 | + |
| 33 | + final String crateManifestPath = "/crates/extendedContextExample/ro-crate-metadata.json"; |
| 34 | + ObjectMapper objectMapper = MyObjectMapper.getMapper(); |
| 35 | + JsonNode jsonNode = objectMapper.readTree(ContextTest.class.getResourceAsStream(crateManifestPath)); |
| 36 | + this.complexContext = new RoCrateMetadataContext(jsonNode.get("@context")); |
29 | 37 | } |
30 | 38 |
|
31 | 39 | @Test |
@@ -137,12 +145,12 @@ void deleteUrlTest() { |
137 | 145 | } |
138 | 146 |
|
139 | 147 | @Test |
140 | | - void doubledContextUrlsTest() throws JsonProcessingException { |
| 148 | + void doubledContextUrlsTest() { |
141 | 149 | String url = "www.example.com"; |
142 | 150 | RoCrateMetadataContext context = new RoCrateMetadataContext(); |
143 | | - assertFalse(context.url.contains(url)); |
| 151 | + assertFalse(context.urls.contains(url)); |
144 | 152 | context.addToContextFromUrl(url); |
145 | | - assertTrue(context.url.contains(url)); |
| 153 | + assertTrue(context.urls.contains(url)); |
146 | 154 |
|
147 | 155 | RoCrateMetadataContext contextDoubled = new RoCrateMetadataContext(); |
148 | 156 | contextDoubled.addToContextFromUrl(url); |
@@ -224,4 +232,50 @@ void testAbsoluteUrlType() { |
224 | 232 | .build(); |
225 | 233 | assertTrue(this.context.checkEntity(validEntity)); |
226 | 234 | } |
| 235 | + |
| 236 | + @Test |
| 237 | + void testSetDeleteGetPair() { |
| 238 | + String key = "key"; |
| 239 | + String value = "value"; |
| 240 | + context.addToContext(key, value); |
| 241 | + assertEquals(value, context.getValueOf(key)); |
| 242 | + context.deleteValuePairFromContext(key); |
| 243 | + assertNull(context.getValueOf(key)); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + void testReadDeleteGetPair() { |
| 248 | + String key = "custom"; |
| 249 | + String value = "_:"; |
| 250 | + assertEquals(value, this.complexContext.getValueOf(key)); |
| 251 | + this.complexContext.deleteValuePairFromContext(key); |
| 252 | + assertNull(this.complexContext.getValueOf(key)); |
| 253 | + this.complexContext.addToContext(key, value); |
| 254 | + assertEquals(value, this.complexContext.getValueOf(key)); |
| 255 | + } |
| 256 | + |
| 257 | + @Test |
| 258 | + void testReadKeys() { |
| 259 | + var expected = Set.of("custom", "owl", "datacite", "xsd", "rdfs"); |
| 260 | + var given = this.complexContext.getKeys(); |
| 261 | + for (String key : expected) { |
| 262 | + assertTrue(given.contains(key), "Key " + key + " not found in the context"); |
| 263 | + } |
| 264 | + // prove immutability |
| 265 | + assertThrows(UnsupportedOperationException.class, () -> given.add("newKey")); |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + void testReadPairs() { |
| 270 | + var expected = Set.of("custom", "owl", "datacite", "xsd", "rdfs"); |
| 271 | + var given = this.complexContext.getPairs(); |
| 272 | + var keys = given.keySet(); |
| 273 | + var values = given.values(); |
| 274 | + for (String key : expected) { |
| 275 | + assertTrue(keys.contains(key), "Key " + key + " not found in the context"); |
| 276 | + values.forEach(s -> assertFalse(s.isEmpty(), "Value for key " + key + " is empty")); |
| 277 | + } |
| 278 | + // prove immutability |
| 279 | + assertThrows(UnsupportedOperationException.class, () -> given.put("newKey", "newValue")); |
| 280 | + } |
227 | 281 | } |
0 commit comments