22
33import java .io .IOException ;
44import java .net .URISyntaxException ;
5+ import java .net .URL ;
6+ import java .nio .file .Files ;
57import java .nio .file .Path ;
68import java .nio .file .Paths ;
79import java .util .HashMap ;
8- import java .util .List ;
910import java .util .Map ;
1011
1112import org .junit .jupiter .api .Assertions ;
1213import org .junit .jupiter .api .Test ;
14+ import org .yaml .snakeyaml .Yaml ;
1315
1416class 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}
0 commit comments