Skip to content

Commit 5681afb

Browse files
authored
Merge pull request #188 from kit-data-manager/fix-#6-reserved-keywords-warnings
Fix invalid context check warnings
2 parents 07ae1f4 + c8d89de commit 5681afb

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
1111

1212
import java.io.IOException;
13-
import java.util.HashMap;
14-
import java.util.HashSet;
15-
import java.util.Collection;
16-
import java.util.Map;
17-
import java.util.Set;
13+
import java.util.*;
1814
import java.util.function.Consumer;
15+
16+
import edu.kit.datamanager.ro_crate.special.IdentifierUtils;
1917
import org.apache.http.client.methods.CloseableHttpResponse;
2018
import org.apache.http.client.methods.HttpGet;
2119
import org.apache.http.impl.client.CloseableHttpClient;
@@ -119,7 +117,22 @@ public boolean checkEntity(AbstractEntity entity) {
119117
new TypeReference<>() {
120118
});
121119
// check if the items in the array of types are present in the context
122-
for (var s : types) {
120+
for (String s : types) {
121+
// special cases:
122+
if (s.equals("@id")) {
123+
// @id will refer to the value of the id of the node
124+
// so we need to extract this value
125+
s = entity.getId();
126+
}
127+
if (s.equals("@json")) {
128+
// A linked data builtin type, which is fine.
129+
continue;
130+
}
131+
if (IdentifierUtils.isUrl(s)) {
132+
// full URLs are considered fine
133+
continue;
134+
}
135+
123136
if (this.contextMap.get(s) == null) {
124137
System.err.println("type " + s + " is missing from the context!");
125138
return false;
@@ -129,8 +142,12 @@ public boolean checkEntity(AbstractEntity entity) {
129142
// check if the fields of the entity are present in the context
130143
for (var names = node.fieldNames(); names.hasNext();) {
131144
String s = names.next();
145+
if (IdentifierUtils.isUrl(s)) {
146+
// full URLs are considered fine
147+
continue;
148+
}
132149
if (this.contextMap.get(s) == null) {
133-
System.err.println("entity " + s + " is missing from context;");
150+
System.err.println("attribute name " + s + " is missing from context;");
134151
return false;
135152
}
136153
}

src/main/java/edu/kit/datamanager/ro_crate/special/IdentifierUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static boolean isValidUri(String uri) {
5151
public static boolean isUrl(String uri) {
5252
try {
5353
return encode(uri)
54-
.map(decodedUri -> asUrl(decodedUri).isPresent())
54+
.map(encodedUri -> asUrl(encodedUri).isPresent())
5555
.orElse(false);
5656
} catch (Exception e) {
5757
return false;

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.databind.node.ObjectNode;
77

88
import edu.kit.datamanager.ro_crate.HelpFunctions;
9+
import edu.kit.datamanager.ro_crate.entities.AbstractEntity;
910
import edu.kit.datamanager.ro_crate.entities.contextual.ContextualEntity;
1011
import edu.kit.datamanager.ro_crate.entities.data.DataEntity;
1112
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
@@ -190,4 +191,37 @@ void creationFromUrlAndPairsTest() {
190191
// house is in the context
191192
assertTrue(newContext.checkEntity(data));
192193
}
194+
195+
@Test
196+
void testIdType() {
197+
AbstractEntity validEntity = new DataEntity.DataEntityBuilder()
198+
.setId("Airline") // this is defined in the context!
199+
.addType("@id") // this is a JSON-LD feature to refer to the ID ("Airline") as a type
200+
.addType("@json") // this is a JSON-LD built-in type
201+
.build();
202+
assertTrue(this.context.checkEntity(validEntity));
203+
204+
AbstractEntity invalidEntity = new DataEntity.DataEntityBuilder()
205+
.setId("Something which is definitely not in the context")
206+
.addType("@id")
207+
.build();
208+
assertFalse(this.context.checkEntity(invalidEntity));
209+
}
210+
211+
@Test
212+
void testJsonType() {
213+
AbstractEntity validEntity = new DataEntity.DataEntityBuilder()
214+
.addType("@json") // this is a JSON-LD built-in type
215+
.build();
216+
assertTrue(this.context.checkEntity(validEntity));
217+
}
218+
219+
@Test
220+
void testAbsoluteUrlType() {
221+
AbstractEntity validEntity = new DataEntity.DataEntityBuilder()
222+
.addType("http://example.org/Person") // this is not in the context!
223+
.addProperty("http://example.org/Thing", "Some thing")
224+
.build();
225+
assertTrue(this.context.checkEntity(validEntity));
226+
}
193227
}

0 commit comments

Comments
 (0)