11package com .cmclinnovations .stack .clients .utils ;
22
3- import java .io .ByteArrayOutputStream ;
43import java .io .IOException ;
5- import java .io .OutputStreamWriter ;
6- import java .io .Writer ;
7- import java .net .URI ;
84import java .net .URISyntaxException ;
9- import java .nio .charset .StandardCharsets ;
105import java .nio .file .Path ;
116import java .nio .file .Paths ;
127import java .util .ArrayList ;
13- import java .util .LinkedHashMap ;
8+ import java .util .HashMap ;
149import java .util .List ;
1510import java .util .Map ;
1611
1712import org .junit .jupiter .api .Assertions ;
1813import org .junit .jupiter .api .Test ;
19- import org . yaml . snakeyaml . DumperOptions ;
20- import org . yaml . snakeyaml . Yaml ;
14+
15+ import com . google . common . base . Objects ;
2116
2217class YarrrmlFileTest {
2318 private static final String TEST_ENDPOINT = "https://example.org/kg/sparql" ;
2419 private static final String TEST_ONE_FILE_NAME = "rules.yml" ;
2520 private static final String TEST_TWO_FILE_NAME = "rules2.yml" ;
2621
2722 @ Test
28- void testDefaultConstructor () throws IOException {
23+ void testDefaultConstructor () {
2924 YarrrmlFile yarrrmlFile = new YarrrmlFile ();
3025 String expected = "" ;
31- checkRules (yarrrmlFile , expected , expected );
26+ checkRules (yarrrmlFile , expected , new HashMap <>() );
3227 }
3328
3429 @ Test
@@ -54,82 +49,53 @@ void testAddRules_SuccessShortcutFormat() throws IOException, URISyntaxException
5449 checkRules (yarrrmlFile , TEST_TWO_FILE_NAME , this .genExpectedYarrrmlContents (TEST_TWO_FILE_NAME ));
5550 }
5651
57- private void checkRules (YarrrmlFile yarrrmlFile , String expectedFileName , String expectedContents )
58- throws IOException {
52+ private void checkRules (YarrrmlFile yarrrmlFile , String expectedFileName , Map <String , Object > expectedContents ) {
5953 Assertions .assertEquals (expectedFileName , yarrrmlFile .getFileName ());
60- Assertions .assertEquals ( expectedContents , new String ( yarrrmlFile .write (), StandardCharsets . UTF_8 ));
54+ Assertions .assertTrue ( Objects . equal ( expectedContents , yarrrmlFile .getRules () ));
6155 }
6256
63- private String genExpectedYarrrmlContents (String fileName ) throws IOException {
64- DumperOptions options = new DumperOptions ();
65- options .setDefaultFlowStyle (DumperOptions .FlowStyle .BLOCK );
66- options .setPrettyFlow (true );
67- Yaml yaml = new Yaml (options );
68- Map <String , Object > yamlData = new LinkedHashMap <>();
69-
70- Map <String , String > prefixes = new LinkedHashMap <>();
71- prefixes .put ("rdfs" , "http://www.w3.org/2000/01/rdf-schema#" );
72- prefixes .put ("base" , "https://theworldavatar.io/kg/" );
73- yamlData .put ("prefixes" , prefixes );
74-
75- Map <String , Map <String , Object >> mappings = new LinkedHashMap <>();
76-
77- Map <String , Object > personMapping = new LinkedHashMap <>();
78- Map <String , Object > personS = new LinkedHashMap <>();
79- personS .put ("value" , "base:person/$(id)" );
80- personS .put ("targets" , "target-ref" );
81- personMapping .put ("s" , personS );
82-
83- List <Map <String , Object >> personPO = new ArrayList <>();
84- this .genExpectedYarrrmlPredObj (personPO , "a" , "base:Person" );
85- this .genExpectedYarrrmlPredObj (personPO , "base:hasName" , "base:person/name/$(id)~iri" );
86- personMapping .put ("po" , personPO );
57+ private Map <String , Object > genExpectedYarrrmlContents (String fileName ) {
58+ Map <String , Object > yamlData = new HashMap <>();
8759
88- personMapping .put ("sources" , "source-ref" );
89- mappings .put ("person" , personMapping );
60+ yamlData .put ("prefixes" , Map .of (
61+ "rdfs" , "http://www.w3.org/2000/01/rdf-schema#" ,
62+ "base" , "https://theworldavatar.io/kg/" ));
9063
91- Map <String , Object > personNameMapping = new LinkedHashMap <>();
92- Map <String , Object > personNameS = new LinkedHashMap <>();
93- personNameS .put ("value" , "base:person/name/$(id)" );
94- personNameS .put ("targets" , "target-ref" );
95- personNameMapping .put ("s" , personNameS );
64+ Map <String , Map <String , Object >> mappings = new HashMap <>();
9665
97- List <Map <String , Object >> personNamePO = new ArrayList <>();
98- this .genExpectedYarrrmlPredObj (personNamePO , "a" , "base:PersonName" );
99- this .genExpectedYarrrmlPredObj (personNamePO , "rdfs:label" , "$(name)" );
100- personNameMapping .put ("po" , personNamePO );
66+ mappings .put ("person" , Map .of (
67+ "sources" , "source-ref" ,
68+ "s" , Map .of (
69+ "value" , "base:person/$(id)" ,
70+ "targets" , "target-ref" ),
71+ "po" , List .of (this .genExpectedYarrrmlPredObj ("a" , "base:Person" ),
72+ this .genExpectedYarrrmlPredObj ("base:hasName" , "base:person/name/$(id)~iri" ))));
10173
102- personNameMapping .put ("sources" , "source-ref" );
103- mappings .put ("person-name" , personNameMapping );
74+ mappings .put ("person-name" , Map .of (
75+ "sources" , "source-ref" ,
76+ "s" , Map .of (
77+ "value" , "base:person/name/$(id)" ,
78+ "targets" , "target-ref" ),
79+ "po" , List .of (this .genExpectedYarrrmlPredObj ("a" , "base:PersonName" ),
80+ this .genExpectedYarrrmlPredObj ("rdfs:label" , "$(name)" ))));
10481
10582 yamlData .put ("mappings" , mappings );
10683
107- Map <String , Map <String , Object >> sources = new LinkedHashMap <>();
108- Map <String , Object > sourceRef = new LinkedHashMap <>();
109- sourceRef .put ("referenceFormulation" , "csv" );
110- sourceRef .put ("access" , FileUtils .replaceExtension (fileName , "csv" ));
111- sources .put ("source-ref" , sourceRef );
112- yamlData .put ("sources" , sources );
113-
114- Map <String , Map <String , Object >> targets = new LinkedHashMap <>();
115- Map <String , Object > targetRef = new LinkedHashMap <>();
116- targetRef .put ("serialization" , "turtle" );
117- targetRef .put ("access" , "https://example.org/kg/sparql" );
118- targetRef .put ("type" , "sd" );
119- targets .put ("target-ref" , targetRef );
120- yamlData .put ("targets" , targets );
121-
122- try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
123- Writer writer = new OutputStreamWriter (outputStream )) {
124- yaml .dump (yamlData , writer );
125- return outputStream .toString ();
126- }
84+ yamlData .put ("sources" , Map .of (
85+ "source-ref" , Map .of (
86+ "referenceFormulation" , "csv" ,
87+ "access" , FileUtils .replaceExtension (fileName , "csv" ))));
88+ yamlData .put ("targets" , Map .of (
89+ "target-ref" , Map .of (
90+ "serialization" , "turtle" ,
91+ "access" , "https://example.org/kg/sparql" ,
92+ "type" , "sd" )));
93+ return yamlData ;
12794 }
12895
129- private void genExpectedYarrrmlPredObj (List <Map <String , Object >> output , String predVal , String objVal ) {
130- Map <String , Object > predObjMap = new LinkedHashMap <>();
131- predObjMap .put ("p" , predVal );
132- predObjMap .put ("o" , objVal );
133- output .add (predObjMap );
96+ private Map <String , Object > genExpectedYarrrmlPredObj (String predVal ,
97+ String objVal ) {
98+ return Map .of ("p" , predVal ,
99+ "o" , objVal );
134100 }
135101}
0 commit comments