Skip to content

Commit 9d639bd

Browse files
committed
test: add Context tests for new context implementation
1 parent 355b856 commit 9d639bd

File tree

1 file changed

+254
-0
lines changed

1 file changed

+254
-0
lines changed
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
package edu.kit.datamanager.ro_crate.context;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.node.ObjectNode;
8+
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.Nested;
12+
import org.junit.jupiter.api.io.TempDir;
13+
14+
import java.io.IOException;
15+
import java.net.URI;
16+
import java.nio.file.Files;
17+
import java.nio.file.Path;
18+
import java.util.Map;
19+
20+
class ContextTest {
21+
private Context context;
22+
private static final String SCHEMA_URL = "https://schema.org/";
23+
private static final String EXAMPLE_URL = "http://example.org/terms/";
24+
25+
@BeforeEach
26+
void setUp() {
27+
context = new Context();
28+
}
29+
30+
@Nested
31+
class CreationAndBasics {
32+
@Test
33+
void defaultConstructor_shouldCreateEmptyContext() {
34+
ObjectNode json = context.toJsonLd();
35+
assertNotNull(json.get("@context"));
36+
assertTrue(json.get("@context").isEmpty());
37+
}
38+
39+
@Test
40+
void fromJson_withValidContext_shouldCreateEquivalentContext() {
41+
ObjectMapper mapper = MyObjectMapper.getMapper();
42+
ObjectNode input = mapper.createObjectNode();
43+
input.put("schema", SCHEMA_URL);
44+
45+
Context newContext = Context.fromJson(input);
46+
assertEquals(SCHEMA_URL, newContext.getDefinition("schema"));
47+
}
48+
49+
@Test
50+
void fromJson_withInvalidJson_shouldThrowException() {
51+
assertThrows(IllegalArgumentException.class, () ->
52+
Context.fromJson(null)
53+
);
54+
}
55+
}
56+
57+
@Nested
58+
class TermDefinitions {
59+
@Test
60+
void defineTerm_withValidTuple_shouldAddDefinition() {
61+
context.define("name", "https://schema.org/name");
62+
assertTrue(context.isValidTerm("name"));
63+
assertEquals("https://schema.org/name", context.getDefinition("name"));
64+
}
65+
66+
@Test
67+
void defineTerm_withNull_shouldThrowException() {
68+
assertThrows(IllegalArgumentException.class, () ->
69+
context.define(null, "https://example.org")
70+
);
71+
assertThrows(IllegalArgumentException.class, () ->
72+
context.define("term", null)
73+
);
74+
}
75+
76+
@Test
77+
void defineTerm_withInvalidIRI_shouldThrowException() {
78+
assertThrows(IllegalArgumentException.class, () ->
79+
context.define("term", "not a valid IRI")
80+
);
81+
}
82+
}
83+
84+
@Nested
85+
class PrefixHandling {
86+
@Test
87+
void definePrefix_withValidIRI_shouldAllowPrefixedTerms() {
88+
context.define("schema", SCHEMA_URL);
89+
assertTrue(context.isValidTerm("schema:name"));
90+
}
91+
92+
@Test
93+
void validateTerm_withUndefinedPrefix_shouldReturnFalse() {
94+
assertFalse(context.isValidTerm("undefined:term"));
95+
}
96+
97+
@Test
98+
void validateTerm_withDefinedPrefixButInvalidTerm_shouldReturnFalse() {
99+
context.define("ex", EXAMPLE_URL);
100+
assertFalse(context.isValidTerm("ex:nonexistentTerm"));
101+
}
102+
}
103+
104+
@Nested
105+
class ContextResolution {
106+
@Test
107+
void addContext_withResolvableUrl_shouldExpandContext() {
108+
context.addContext(URI.create(SCHEMA_URL));
109+
assertTrue(context.isValidTerm("Person"));
110+
assertTrue(context.isValidTerm("name"));
111+
}
112+
113+
@Test
114+
void addContext_withUnresolvableUrl_shouldThrow() {
115+
URI unreachableUri = URI.create("http://nonexistent.example.org/");
116+
assertThrows(ContextLoadException.class, () ->
117+
context.addContext(unreachableUri)
118+
);
119+
}
120+
121+
@Test
122+
void addContext_withLocalFile_shouldLoadFromFileSystem(@TempDir Path tempDir) throws IOException {
123+
Path contextFile = tempDir.resolve("test-context.jsonld");
124+
Files.writeString(contextFile, """
125+
{
126+
"@context": {
127+
"test": "http://example.org/test#",
128+
"LocalTerm": "test:LocalTerm"
129+
}
130+
}
131+
""");
132+
133+
context.addContext(contextFile.toUri());
134+
assertTrue(context.isValidTerm("LocalTerm"));
135+
}
136+
137+
@Test
138+
void addContext_withNonexistentLocalFile_shouldThrow(@TempDir Path tempDir) {
139+
Path nonExistentFile = tempDir.resolve("nonexistent.jsonld");
140+
URI fileUri = nonExistentFile.toUri();
141+
142+
assertThrows(ContextLoadException.class, () ->
143+
context.addContext(fileUri)
144+
);
145+
}
146+
147+
@Test
148+
void addContext_withInvalidContent_shouldThrow(@TempDir Path tempDir) throws IOException {
149+
Path invalidFile = tempDir.resolve("invalid.jsonld");
150+
Files.writeString(invalidFile, "{ invalid json");
151+
152+
assertThrows(ContextLoadException.class, () ->
153+
context.addContext(invalidFile.toUri())
154+
);
155+
}
156+
}
157+
158+
@Nested
159+
class TermValidation {
160+
@Test
161+
void isValidTerm_withAbsoluteIRI_shouldReturnTrue() {
162+
assertTrue(context.isValidTerm("https://schema.org/Person"));
163+
}
164+
165+
@Test
166+
void isValidTerm_withRelativeIRI_shouldReturnFalse() {
167+
assertFalse(context.isValidTerm("Person"));
168+
}
169+
170+
@Test
171+
void isValidTerm_withDefinedTerm_shouldReturnTrue() {
172+
context.define("person", "https://schema.org/Person");
173+
assertTrue(context.isValidTerm("person"));
174+
}
175+
176+
@Test
177+
void isValidTerm_withPrefixedTerm_shouldValidateAgainstPrefix() {
178+
context.define("schema", SCHEMA_URL);
179+
assertTrue(context.isValidTerm("schema:Person"));
180+
assertFalse(context.isValidTerm("schema:NonexistentType"));
181+
}
182+
}
183+
184+
@Nested
185+
class JsonSerialization {
186+
@Test
187+
void toJsonLd_withEmptyContext_shouldReturnMinimalStructure() {
188+
ObjectNode json = context.toJsonLd();
189+
assertNotNull(json.get("@context"));
190+
assertTrue(json.get("@context").isEmpty());
191+
}
192+
193+
@Test
194+
void toJsonLd_withDefinitions_shouldIncludeAllDefinitions() {
195+
context.define("schema", SCHEMA_URL);
196+
context.define("name", "https://schema.org/name");
197+
198+
ObjectNode json = context.toJsonLd();
199+
JsonNode contextNode = json.get("@context");
200+
assertEquals(SCHEMA_URL, contextNode.get("schema").asText());
201+
assertEquals("https://schema.org/name", contextNode.get("name").asText());
202+
}
203+
}
204+
205+
@Nested
206+
class ContextModification {
207+
@Test
208+
void remove_existingDefinition_shouldRemoveDefinition() {
209+
context.define("term", "https://example.org/term");
210+
assertTrue(context.isValidTerm("term"));
211+
212+
context.remove("term");
213+
assertFalse(context.isValidTerm("term"));
214+
}
215+
216+
@Test
217+
void remove_nonexistentDefinition_shouldNotThrowException() {
218+
assertDoesNotThrow(() -> context.remove("nonexistent"));
219+
}
220+
221+
@Test
222+
void getDefinitions_shouldReturnUnmodifiableMap() {
223+
context.define("term", "https://example.org/term");
224+
Map<String, String> definitions = context.getDefinitions();
225+
226+
assertTrue(definitions.containsKey("term"));
227+
assertTrue(definitions.containsValue("https://example.org/term"));
228+
229+
assertThrows(UnsupportedOperationException.class, () ->
230+
definitions.put("new", "value")
231+
);
232+
}
233+
234+
@Test
235+
void removeContext_shouldRemoveAllAssociatedTerms() throws IOException {
236+
URI contextUri = URI.create(SCHEMA_URL);
237+
context.addContext(contextUri);
238+
assertTrue(context.isValidTerm("Person"));
239+
240+
context.removeContext(contextUri);
241+
assertFalse(context.isValidTerm("Person"));
242+
}
243+
244+
@Test
245+
void getDefinitions_shouldIncludeContextTerms() throws IOException {
246+
URI contextUri = URI.create(SCHEMA_URL);
247+
context.addContext(contextUri);
248+
Map<String, String> definitions = context.getDefinitions();
249+
250+
assertTrue(definitions.containsKey("Person"));
251+
assertTrue(definitions.containsValue(SCHEMA_URL + "Person"));
252+
}
253+
}
254+
}

0 commit comments

Comments
 (0)