Skip to content

Commit 3274fa3

Browse files
authored
Merge pull request #245 from kit-data-manager/241-getting-key-value-pairs-from-the-context
Fix #241: Getting key value pairs from the context
2 parents 67480bd + 597568d commit 3274fa3

7 files changed

Lines changed: 222 additions & 12 deletions

File tree

src/main/java/edu/kit/datamanager/ro_crate/Crate.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.util.Collection;
5+
import java.util.Map;
56
import java.util.Optional;
67
import java.util.Set;
78

@@ -51,6 +52,25 @@ public interface Crate {
5152

5253
void setMetadataContext(CrateMetadataContext metadataContext);
5354

55+
/**
56+
* Get the value of a key from the metadata context.
57+
* @param key the key to be searched
58+
* @return the value of the key, null if not found
59+
*/
60+
String getMetadataContextValueOf(String key);
61+
62+
/**
63+
* Get an immutable collection of the keys in the metadata context.
64+
* @return the keys in the metadata context
65+
*/
66+
Set<String> getMetadataContextKeys();
67+
68+
/**
69+
* Get an immutable map of the context.
70+
* @return an immutable map containing the context key-value pairs
71+
*/
72+
Map<String, String> getMetadataContextPairs();
73+
5474
RootDataEntity getRootDataEntity();
5575

5676
void setRootDataEntity(RootDataEntity rootDataEntity);

src/main/java/edu/kit/datamanager/ro_crate/RoCrate.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ public void setMetadataContext(CrateMetadataContext metadataContext) {
6262
this.metadataContext = metadataContext;
6363
}
6464

65+
public String getMetadataContextValueOf(String key) {
66+
return this.metadataContext.getValueOf(key);
67+
}
68+
69+
@Override
70+
public Set<String> getMetadataContextKeys() {
71+
return this.metadataContext.getKeys();
72+
}
73+
74+
@Override
75+
public Map<String, String> getMetadataContextPairs() {
76+
return this.metadataContext.getPairs();
77+
}
78+
6579
public ContextualEntity getJsonDescriptor() {
6680
return jsonDescriptor;
6781
}

src/main/java/edu/kit/datamanager/ro_crate/context/CrateMetadataContext.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import edu.kit.datamanager.ro_crate.entities.AbstractEntity;
66

7+
import java.util.Map;
8+
import java.util.Set;
9+
710
/**
811
* Interface for the metadata context.
912
* Most often in an ROCrate this is the default context,
@@ -22,6 +25,25 @@ public interface CrateMetadataContext {
2225

2326
void addToContext(String key, String value);
2427

28+
/**
29+
* Get the value of a key from the context.
30+
* @param key the key to be searched
31+
* @return the value of the key, null if not found
32+
*/
33+
String getValueOf(String key);
34+
35+
/**
36+
* Get an immutable collection of the keys in the metadata context.
37+
* @return the keys in the metadata context
38+
*/
39+
Set<String> getKeys();
40+
41+
/**
42+
* Get an immutable map of the context.
43+
* @return an immutable map containing the context key-value pairs
44+
*/
45+
Map<String, String> getPairs();
46+
2547
void deleteValuePairFromContext(String key);
2648

2749
void deleteUrlFromContext(String url);

src/main/java/edu/kit/datamanager/ro_crate/context/RoCrateMetadataContext.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class RoCrateMetadataContext implements CrateMetadataContext {
3131
protected static final String DEFAULT_CONTEXT_LOCATION = "default_context/version1.1.json";
3232
protected static JsonNode defaultContext = null;
3333

34-
protected final Set<String> url = new HashSet<>();
34+
protected final Set<String> urls = new HashSet<>();
3535
protected final HashMap<String, String> contextMap = new HashMap<>();
3636
// we need to keep the ones that are no coming from url
3737
// for the final representation
@@ -89,7 +89,7 @@ public ObjectNode getContextJsonEntity() {
8989
ArrayNode array = objectMapper.createArrayNode();
9090
ObjectNode jsonNode = objectMapper.createObjectNode();
9191
ObjectNode finalNode = objectMapper.createObjectNode();
92-
for (String e : url) {
92+
for (String e : urls) {
9393
array.add(e);
9494
}
9595
for (Map.Entry<String, String> s : other.entrySet()) {
@@ -156,7 +156,7 @@ public boolean checkEntity(AbstractEntity entity) {
156156

157157
@Override
158158
public void addToContextFromUrl(String url) {
159-
this.url.add(url);
159+
this.urls.add(url);
160160

161161
ObjectMapper objectMapper = MyObjectMapper.getMapper();
162162

@@ -200,6 +200,29 @@ public void addToContext(String key, String value) {
200200
this.other.put(key, value);
201201
}
202202

203+
@Override
204+
public String getValueOf(String key) {
205+
return Optional.ofNullable(this.contextMap.get(key))
206+
.orElseGet(() -> this.other.get(key));
207+
}
208+
209+
@Override
210+
public Set<String> getKeys() {
211+
List<String> merged = new ArrayList<>();
212+
merged.addAll(this.contextMap.keySet());
213+
merged.addAll(this.other.keySet());
214+
return Set.copyOf(merged);
215+
}
216+
217+
@Override
218+
public Map<String, String> getPairs() {
219+
Map<String, String> merged = new HashMap<>();
220+
merged.putAll(this.contextMap);
221+
merged.putAll(this.other);
222+
return Map.copyOf(merged);
223+
}
224+
225+
203226
@Override
204227
public void deleteValuePairFromContext(String key) {
205228
this.contextMap.remove(key);
@@ -208,7 +231,7 @@ public void deleteValuePairFromContext(String key) {
208231

209232
@Override
210233
public void deleteUrlFromContext(String url) {
211-
this.url.remove(url);
234+
this.urls.remove(url);
212235
}
213236

214237
}

src/test/java/edu/kit/datamanager/ro_crate/context/ContextTest.java

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package edu.kit.datamanager.ro_crate.context;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
3+
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.fasterxml.jackson.databind.node.ArrayNode;
66
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -14,18 +14,26 @@
1414
import org.junit.jupiter.api.BeforeEach;
1515
import org.junit.jupiter.api.Test;
1616

17+
import java.io.IOException;
1718
import java.util.List;
19+
import java.util.Set;
1820

1921
import static org.junit.jupiter.api.Assertions.*;
2022

2123
public class ContextTest {
2224

2325
RoCrateMetadataContext context;
26+
RoCrateMetadataContext complexContext;
2427

2528
@BeforeEach
26-
void initContext() {
29+
void initContext() throws IOException {
2730
// this will load the default context
2831
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"));
2937
}
3038

3139
@Test
@@ -137,12 +145,12 @@ void deleteUrlTest() {
137145
}
138146

139147
@Test
140-
void doubledContextUrlsTest() throws JsonProcessingException {
148+
void doubledContextUrlsTest() {
141149
String url = "www.example.com";
142150
RoCrateMetadataContext context = new RoCrateMetadataContext();
143-
assertFalse(context.url.contains(url));
151+
assertFalse(context.urls.contains(url));
144152
context.addToContextFromUrl(url);
145-
assertTrue(context.url.contains(url));
153+
assertTrue(context.urls.contains(url));
146154

147155
RoCrateMetadataContext contextDoubled = new RoCrateMetadataContext();
148156
contextDoubled.addToContextFromUrl(url);
@@ -224,4 +232,50 @@ void testAbsoluteUrlType() {
224232
.build();
225233
assertTrue(this.context.checkEntity(validEntity));
226234
}
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+
}
227281
}

src/test/java/edu/kit/datamanager/ro_crate/crate/TestRemoveAddContext.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
package edu.kit.datamanager.ro_crate.crate;
22

3-
import org.junit.jupiter.api.Test;
3+
import edu.kit.datamanager.ro_crate.reader.FolderReader;
4+
import edu.kit.datamanager.ro_crate.reader.RoCrateReader;
5+
import edu.kit.datamanager.ro_crate.HelpFunctions;
6+
import edu.kit.datamanager.ro_crate.RoCrate;
47

58
import com.fasterxml.jackson.core.JsonProcessingException;
69

7-
import edu.kit.datamanager.ro_crate.HelpFunctions;
8-
import edu.kit.datamanager.ro_crate.RoCrate;
10+
import java.util.Objects;
11+
import java.util.Set;
12+
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
15+
import static org.junit.jupiter.api.Assertions.*;
916

1017
public class TestRemoveAddContext {
18+
private RoCrate crateWithComplexContext;
19+
20+
@BeforeEach
21+
void setup() {
22+
String crateManifestPath = "/crates/extendedContextExample/";
23+
crateManifestPath = Objects.requireNonNull(TestRemoveAddContext.class.getResource(crateManifestPath)).getPath();
24+
this.crateWithComplexContext = new RoCrateReader(new FolderReader()).readCrate(crateManifestPath);
25+
}
26+
1127
@Test
1228
void testAddRemoveValuePair() throws JsonProcessingException {
1329
RoCrate crate = new RoCrate.RoCrateBuilder().addValuePairToContext("key", "value").build();
@@ -30,4 +46,37 @@ void testAddRemoveUrl() throws JsonProcessingException {
3046

3147
}
3248

49+
@Test
50+
void testReadDeleteGetContextPair() {
51+
String key = "custom";
52+
String value = "_:";
53+
assertEquals(value, this.crateWithComplexContext.getMetadataContextValueOf(key));
54+
this.crateWithComplexContext.deleteValuePairFromContext(key);
55+
assertNull(this.crateWithComplexContext.getMetadataContextValueOf(key));
56+
}
57+
58+
@Test
59+
void testReadContextKeys() {
60+
var expected = Set.of("custom", "owl", "datacite", "xsd", "rdfs");
61+
var given = this.crateWithComplexContext.getMetadataContextKeys();
62+
for (String key : expected) {
63+
assertTrue(given.contains(key), "Key " + key + " not found in the context");
64+
}
65+
// prove immutability
66+
assertThrows(UnsupportedOperationException.class, () -> given.add("newKey"));
67+
}
68+
69+
@Test
70+
void testReadContextPairs() {
71+
var expected = Set.of("custom", "owl", "datacite", "xsd", "rdfs");
72+
var given = this.crateWithComplexContext.getMetadataContextPairs();
73+
var keys = given.keySet();
74+
var values = given.values();
75+
for (String key : expected) {
76+
assertTrue(keys.contains(key), "Key " + key + " not found in the context");
77+
values.forEach(s -> assertFalse(s.isEmpty(), "Value for key " + key + " is empty"));
78+
}
79+
// prove immutability
80+
assertThrows(UnsupportedOperationException.class, () -> given.put("newKey", "newValue"));
81+
}
3382
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"@context": [
3+
"https://w3id.org/ro/crate/1.1/context",
4+
{
5+
"owl": "https://www.w3.org/TR/owl-ref/#",
6+
"datacite": "http://datacite.org/schema/kernel-4#",
7+
"xsd": "http://www.w3.org/2001/XMLSchema#",
8+
"rdfs": "https://www.w3.org/2000/01/rdf-schema#",
9+
"custom": "_:"
10+
}
11+
],
12+
"@graph": [
13+
{
14+
"@id": "./",
15+
"@type": "Dataset",
16+
"name" : "minimal",
17+
"description" : "minimal RO_crate",
18+
"datePublished": "2024",
19+
"license": {"@id": "https://creativecommons.org/licenses/by-nc-sa/3.0/au/"}
20+
},
21+
{
22+
"@type": "CreativeWork",
23+
"@id": "ro-crate-metadata.json",
24+
"conformsTo": {"@id": "https://w3id.org/ro/crate/1.1"},
25+
"about": {"@id": "./"}
26+
}
27+
]
28+
}

0 commit comments

Comments
 (0)