Skip to content

Commit 85ab018

Browse files
committed
1466-instantiate-csv-data-as-rdf: refactor to read files for testing of expected output, and moved current rules into a nested folder
1 parent a982d1c commit 85ab018

File tree

9 files changed

+191
-101
lines changed

9 files changed

+191
-101
lines changed

stack-clients/src/test/java/com/cmclinnovations/stack/clients/utils/YarrrmlFileTest.java

Lines changed: 46 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,75 @@
22

33
import java.io.IOException;
44
import java.net.URISyntaxException;
5+
import java.net.URL;
6+
import java.nio.file.Files;
57
import java.nio.file.Path;
68
import java.nio.file.Paths;
79
import java.util.HashMap;
8-
import java.util.List;
910
import java.util.Map;
1011

1112
import org.junit.jupiter.api.Assertions;
1213
import org.junit.jupiter.api.Test;
14+
import org.yaml.snakeyaml.Yaml;
1315

1416
class YarrrmlFileTest {
1517
private static final String TEST_ENDPOINT = "https://example.org/kg/sparql";
16-
private static final String TEST_ONE_FILE_NAME = "rules.yml";
17-
private static final String TEST_TWO_FILE_NAME = "rules2.yml";
18-
private static final String TEST_THREE_FILE_NAME = "rules_functions.yml";
19-
private static final String TEST_FOUR_FILE_NAME = "rules2_functions.yml";
18+
private static final String TEST_ONE_FILE_NAME = "yml/test/rules.yml";
19+
private static final String EXPECTED_ONE_FILE_NAME = "yml/expected/rules.yml";
20+
private static final String TEST_TWO_FILE_NAME = "yml/test/rules2.yml";
21+
private static final String EXPECTED_TWO_FILE_NAME = "yml/expected/rules2.yml";
22+
private static final String TEST_THREE_FILE_NAME = "yml/test/rules_functions.yml";
23+
private static final String EXPECTED_THREE_FILE_NAME = "yml/expected/rules_functions.yml";
24+
private static final String TEST_FOUR_FILE_NAME = "yml/test/rules2_functions.yml";
25+
private static final String EXPECTED_FOUR_FILE_NAME = "yml/expected/rules2_functions.yml";
2026

2127
@Test
2228
void testDefaultConstructor() {
2329
YarrrmlFile yarrrmlFile = new YarrrmlFile();
24-
Assertions.assertEquals(new HashMap<>(), yarrrmlFile.getRules());
30+
Assertions.assertEquals(new HashMap<>(), yarrrmlFile.getRules());
2531
}
2632

2733
@Test
2834
void testFileConstructor() throws IOException, URISyntaxException {
2935
Path rulesFilePath = Paths.get(YarrrmlFileTest.class.getResource(TEST_ONE_FILE_NAME).toURI());
3036
YarrrmlFile yarrrmlFile = new YarrrmlFile(rulesFilePath, TEST_ENDPOINT);
31-
Assertions.assertEquals(this.genExpectedYarrrmlContents(rulesFilePath.toString()), yarrrmlFile.getRules());
37+
Assertions.assertEquals(
38+
this.genExpectedYarrrmlContents(
39+
YarrrmlFileTest.class.getResource(EXPECTED_ONE_FILE_NAME),
40+
rulesFilePath, TEST_ENDPOINT),
41+
yarrrmlFile.getRules());
3242
}
3343

3444
@Test
3545
void testAddRules_SuccessOriginalFormat() throws IOException, URISyntaxException {
3646
Path rulesFilePath = Paths.get(YarrrmlFileTest.class.getResource(TEST_ONE_FILE_NAME).toURI());
3747
YarrrmlFile yarrrmlFile = new YarrrmlFile();
3848
yarrrmlFile.addRules(rulesFilePath, TEST_ENDPOINT);
39-
Assertions.assertEquals(this.genExpectedYarrrmlContents(rulesFilePath.toString()), yarrrmlFile.getRules());
49+
Assertions.assertEquals(this.genExpectedYarrrmlContents(
50+
YarrrmlFileTest.class.getResource(EXPECTED_ONE_FILE_NAME),
51+
rulesFilePath, TEST_ENDPOINT),
52+
yarrrmlFile.getRules());
4053
}
4154

4255
@Test
4356
void testAddRules_SuccessShortcutFormat() throws IOException, URISyntaxException {
4457
Path rulesFilePath = Paths.get(YarrrmlFileTest.class.getResource(TEST_TWO_FILE_NAME).toURI());
4558
YarrrmlFile yarrrmlFile = new YarrrmlFile();
4659
yarrrmlFile.addRules(rulesFilePath, TEST_ENDPOINT);
47-
Assertions.assertEquals(this.genExpectedYarrrmlContents(rulesFilePath.toString()), yarrrmlFile.getRules());
60+
Assertions.assertEquals(this.genExpectedYarrrmlContents(
61+
YarrrmlFileTest.class.getResource(EXPECTED_TWO_FILE_NAME),
62+
rulesFilePath, TEST_ENDPOINT),
63+
yarrrmlFile.getRules());
4864
}
4965

5066
@Test
5167
void testAddRules_SuccessFunctionFormat() throws IOException, URISyntaxException {
5268
Path rulesFilePath = Paths.get(YarrrmlFileTest.class.getResource(TEST_THREE_FILE_NAME).toURI());
5369
YarrrmlFile yarrrmlFile = new YarrrmlFile();
5470
yarrrmlFile.addRules(rulesFilePath, TEST_ENDPOINT);
55-
Assertions.assertEquals(this.genExpectedYarrrmlFunctionContents(rulesFilePath.toString()),
71+
Assertions.assertEquals(this.genExpectedYarrrmlContents(
72+
YarrrmlFileTest.class.getResource(EXPECTED_THREE_FILE_NAME),
73+
rulesFilePath, TEST_ENDPOINT),
5674
yarrrmlFile.getRules());
5775
}
5876

@@ -61,94 +79,26 @@ void testAddRules_SuccessFunctionShorcutFormat() throws IOException, URISyntaxEx
6179
Path rulesFilePath = Paths.get(YarrrmlFileTest.class.getResource(TEST_FOUR_FILE_NAME).toURI());
6280
YarrrmlFile yarrrmlFile = new YarrrmlFile();
6381
yarrrmlFile.addRules(rulesFilePath, TEST_ENDPOINT);
64-
Assertions.assertEquals(this.genExpectedYarrrmlFunctionContents(rulesFilePath.toString()),
82+
Assertions.assertEquals(this.genExpectedYarrrmlContents(
83+
YarrrmlFileTest.class.getResource(EXPECTED_FOUR_FILE_NAME),
84+
rulesFilePath, TEST_ENDPOINT),
6585
yarrrmlFile.getRules());
6686
}
6787

68-
private Map<String, Object> genExpectedYarrrmlContents(String fileName) {
69-
Map<String, Object> yamlData = this.genExpectedYarrrmlTemplate(fileName);
70-
71-
Map<String, Map<String, Object>> mappings = new HashMap<>();
72-
73-
mappings.put("person", Map.of(
74-
"sources", "source-ref",
75-
"s", Map.of(
76-
"value", "base:person/$(id)",
77-
"targets", "target-ref"),
78-
"po", List.of(this.genExpectedYarrrmlPredObj("a", "base:Person"),
79-
this.genExpectedYarrrmlPredObj("base:hasName",
80-
"base:person/name/$(id)~iri"))));
81-
82-
mappings.put("person-name", Map.of(
83-
"sources", "source-ref",
84-
"s", Map.of(
85-
"value", "base:person/name/$(id)",
86-
"targets", "target-ref"),
87-
"po", List.of(this.genExpectedYarrrmlPredObj("a", "base:PersonName"),
88-
this.genExpectedYarrrmlPredObj("rdfs:label", "$(name)"))));
89-
90-
yamlData.put("mappings", mappings);
91-
return yamlData;
92-
}
93-
94-
private Map<String, Object> genExpectedYarrrmlFunctionContents(String fileName) {
95-
Map<String, Object> yamlData = this.genExpectedYarrrmlTemplate(fileName);
96-
Map<String, Map<String, Object>> mappings = new HashMap<>();
97-
98-
mappings.put("person", Map.of(
99-
"sources", "source-ref",
100-
"s", this.genExpectedYarrrmlFunction(
101-
"grel:string_trim",
102-
"base:person/$(id)~iri",
103-
Map.of("targets", "target-ref")),
104-
"po", List.of(this.genExpectedYarrrmlPredObj("a", "base:Person"),
105-
this.genExpectedYarrrmlPredObj("base:hasName",
106-
this.genExpectedYarrrmlFunction(
107-
"grel:toLowerCase",
108-
this.genExpectedYarrrmlFunction(
109-
"grel:string_trim",
110-
"base:department/name/$(name)~iri"))))));
111-
yamlData.put("mappings", mappings);
112-
return yamlData;
113-
}
114-
115-
private Map<String, Object> genExpectedYarrrmlTemplate(String fileName) {
116-
Map<String, Object> yamlData = new AliasMap<>();
117-
yamlData.put("prefixes", Map.of(
118-
"rdfs", "http://www.w3.org/2000/01/rdf-schema#",
119-
"base", "https://theworldavatar.io/kg/",
120-
"grel", "http://users.ugent.be/~bjdmeest/function/grel.ttl#"));
121-
yamlData.put("sources", Map.of(
122-
"source-ref", Map.of(
123-
"referenceFormulation", "csv",
124-
"access", FileUtils.replaceExtension(fileName, "csv"))));
125-
yamlData.put("targets", Map.of(
126-
"target-ref", Map.of(
127-
"serialization", "turtle",
128-
"access", TEST_ENDPOINT,
129-
"type", "sd")));
130-
return yamlData;
131-
}
132-
133-
private Map<String, Object> genExpectedYarrrmlPredObj(String predVal, Object objVal) {
134-
return Map.of("p", predVal,
135-
"o", objVal);
136-
}
137-
138-
private Map<String, Object> genExpectedYarrrmlFunction(String function, Object paramVal) {
139-
return this.genExpectedYarrrmlFunction(function, paramVal, null);
140-
}
141-
142-
private Map<String, Object> genExpectedYarrrmlFunction(String function, Object paramVal,
143-
Map<String, Object> additionalFields) {
144-
Map<String, Object> result = new HashMap<>();
145-
result.put("function", function);
146-
result.put("parameters", List.of(
147-
Map.of("parameter", "grel:valueParameter",
148-
"value", paramVal)));
149-
if (additionalFields != null && !additionalFields.isEmpty()) {
150-
result.putAll(additionalFields);
151-
}
152-
return result;
88+
private AliasMap<Object> genExpectedYarrrmlContents(URL expectedFilePath, Path expectedSourceLocation,
89+
String endpoint)
90+
throws IOException, URISyntaxException {
91+
Yaml yaml = new Yaml();
92+
String content = new String(Files.readAllBytes(Paths.get(expectedFilePath.toURI())));
93+
String modifiedContent = content
94+
.replace("[source]",
95+
FileUtils.replaceExtension(expectedSourceLocation.toString(), "csv"))
96+
.replace("[target]", endpoint);
97+
98+
Map<String, Object> loadedMap = yaml.load(modifiedContent);
99+
100+
AliasMap<Object> rules = new AliasMap<>();
101+
rules.putAll(loadedMap);
102+
return rules;
153103
}
154104
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
prefixes:
2+
rdfs: http://www.w3.org/2000/01/rdf-schema#
3+
base: https://theworldavatar.io/kg/
4+
5+
mappings:
6+
person-name:
7+
sources: source-ref
8+
s:
9+
value: base:person/name/$(id)
10+
targets: target-ref
11+
po:
12+
- p: a
13+
o: base:PersonName
14+
- p: rdfs:label
15+
o: $(name)
16+
person:
17+
sources: source-ref
18+
s:
19+
value: base:person/$(id)
20+
targets: target-ref
21+
po:
22+
- p: a
23+
o: base:Person
24+
- p: base:hasName
25+
o: base:person/name/$(id)~iri
26+
sources:
27+
source-ref:
28+
referenceFormulation: csv
29+
access: [source]
30+
targets:
31+
target-ref:
32+
serialization: turtle
33+
type: sd
34+
access: [target]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
prefixes:
2+
rdfs: http://www.w3.org/2000/01/rdf-schema#
3+
base: https://theworldavatar.io/kg/
4+
5+
mappings:
6+
person-name:
7+
sources: source-ref
8+
po:
9+
- p: a
10+
o: base:PersonName
11+
- p: rdfs:label
12+
o: $(name)
13+
s:
14+
value: base:person/name/$(id)
15+
targets: target-ref
16+
person:
17+
sources: source-ref
18+
s:
19+
value: base:person/$(id)
20+
targets: target-ref
21+
po:
22+
- p: a
23+
o: base:Person
24+
- p: base:hasName
25+
o: base:person/name/$(id)~iri
26+
sources:
27+
source-ref:
28+
referenceFormulation: csv
29+
access: [source]
30+
targets:
31+
target-ref:
32+
serialization: turtle
33+
type: sd
34+
access: [target]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
prefixes:
2+
rdfs: "http://www.w3.org/2000/01/rdf-schema#"
3+
base: "https://theworldavatar.io/kg/"
4+
grel: "http://users.ugent.be/~bjdmeest/function/grel.ttl#"
5+
6+
mappings:
7+
person:
8+
sources: source-ref
9+
s:
10+
function: grel:string_trim
11+
parameters:
12+
- parameter: grel:valueParameter
13+
value: base:person/$(id)
14+
targets: target-ref
15+
po:
16+
- p: a
17+
o: base:Person
18+
- p: base:hasName
19+
o:
20+
function: grel:toLowerCase
21+
parameters:
22+
- parameter: grel:valueParameter
23+
value:
24+
function: grel:string_trim
25+
parameters:
26+
- parameter: grel:valueParameter
27+
value: base:department/name/$(name)
28+
type: iri
29+
sources:
30+
source-ref:
31+
referenceFormulation: csv
32+
access: [source]
33+
targets:
34+
target-ref:
35+
serialization: turtle
36+
type: sd
37+
access: [target]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
prefixes:
2+
rdfs: "http://www.w3.org/2000/01/rdf-schema#"
3+
base: "https://theworldavatar.io/kg/"
4+
grel: "http://users.ugent.be/~bjdmeest/function/grel.ttl#"
5+
6+
mappings:
7+
person:
8+
sources: source-ref
9+
s:
10+
function: grel:string_trim
11+
parameters:
12+
- parameter: grel:valueParameter
13+
value: base:person/$(id)
14+
targets: target-ref
15+
po:
16+
- p: a
17+
o: base:Person
18+
- p: base:hasName
19+
o:
20+
function: grel:toLowerCase
21+
parameters:
22+
- parameter: grel:valueParameter
23+
value:
24+
function: grel:string_trim
25+
parameters:
26+
- parameter: grel:valueParameter
27+
value: base:department/name/$(name)~iri
28+
sources:
29+
source-ref:
30+
referenceFormulation: csv
31+
access: [source]
32+
targets:
33+
target-ref:
34+
serialization: turtle
35+
type: sd
36+
access: [target]

stack-clients/src/test/resources/com/cmclinnovations/stack/clients/utils/rules.yml renamed to stack-clients/src/test/resources/com/cmclinnovations/stack/clients/utils/yml/test/rules.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
prefixes:
22
rdfs: "http://www.w3.org/2000/01/rdf-schema#"
33
base: "https://theworldavatar.io/kg/"
4-
grel: "http://users.ugent.be/~bjdmeest/function/grel.ttl#"
54

65
mappings:
76
person:

stack-clients/src/test/resources/com/cmclinnovations/stack/clients/utils/rules2.yml renamed to stack-clients/src/test/resources/com/cmclinnovations/stack/clients/utils/yml/test/rules2.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
prefixes:
22
rdfs: "http://www.w3.org/2000/01/rdf-schema#"
33
base: "https://theworldavatar.io/kg/"
4-
grel: "http://users.ugent.be/~bjdmeest/function/grel.ttl#"
54

65
mapping:
76
person:

stack-clients/src/test/resources/com/cmclinnovations/stack/clients/utils/rules2_functions.yml renamed to stack-clients/src/test/resources/com/cmclinnovations/stack/clients/utils/yml/test/rules2_functions.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ m:
88
s:
99
function: grel:string_trim
1010
parameters:
11-
- [grel:valueParameter, base:person/$(id)~iri]
11+
- [grel:valueParameter, base:person/$(id)]
1212
po:
1313
- [a, base:Person]
1414
- p: base:hasName
1515
o:
16+
type: iri
1617
function: grel:toLowerCase
1718
parameters:
1819
- parameter: grel:valueParameter
1920
value:
2021
function: grel:string_trim
2122
parameters:
22-
- [grel:valueParameter, base:department/name/$(name)~iri]
23+
- [grel:valueParameter, base:department/name/$(name)]

stack-clients/src/test/resources/com/cmclinnovations/stack/clients/utils/rules_functions.yml renamed to stack-clients/src/test/resources/com/cmclinnovations/stack/clients/utils/yml/test/rules_functions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ m:
99
function: grel:string_trim
1010
parameters:
1111
- parameter: grel:valueParameter
12-
value: base:person/$(id)~iri
12+
value: base:person/$(id)
1313
po:
1414
- p: a
1515
o: base:Person

0 commit comments

Comments
 (0)