11package edu .kit .datamanager .ro_crate .entities .data ;
22
3+ import com .fasterxml .jackson .databind .JsonNode ;
34import com .fasterxml .jackson .databind .ObjectMapper ;
5+ import com .fasterxml .jackson .databind .node .ArrayNode ;
46import com .fasterxml .jackson .databind .node .ObjectNode ;
57
68import java .io .IOException ;
911import edu .kit .datamanager .ro_crate .HelpFunctions ;
1012import edu .kit .datamanager .ro_crate .entities .data .DataEntity .DataEntityBuilder ;
1113import edu .kit .datamanager .ro_crate .objectmapper .MyObjectMapper ;
12- import java .net .MalformedURLException ;
1314import java .net .URI ;
1415
1516import java .net .URISyntaxException ;
17+ import java .nio .file .Path ;
1618import java .nio .file .Paths ;
1719import java .util .Arrays ;
1820import java .util .List ;
21+ import java .util .stream .Stream ;
22+
1923import org .junit .jupiter .api .Test ;
24+ import org .junit .jupiter .params .ParameterizedTest ;
25+ import org .junit .jupiter .params .provider .MethodSource ;
2026
2127import static org .junit .jupiter .api .Assertions .assertEquals ;
2228import static org .junit .jupiter .api .Assertions .assertNotEquals ;
@@ -93,12 +99,14 @@ void testUriCheck() {
9399
94100 assertNotNull (dataEntity );
95101
96- Exception exception = assertThrows (IllegalArgumentException .class , () -> {
97- new DataEntity .DataEntityBuilder ()
98- .addType ("File" )
99- .setLocationWithExceptions (URI .create ("zzz://wrong/url" ))
100- .build ();
101- });
102+ Exception exception = assertThrows (
103+ IllegalArgumentException .class ,
104+ () ->
105+ new DataEntity .DataEntityBuilder ()
106+ .addType ("File" )
107+ .setLocationWithExceptions (URI .create ("zzz://wrong/url" ))
108+ .build ()
109+ );
102110
103111 assertEquals ("This Data Entity remote ID does not resolve to a valid URL." , exception .getMessage ());
104112
@@ -114,22 +122,25 @@ void testPathCheck() throws IOException {
114122 .addProperty ("name" , "RO-Crate specification" )
115123 .setEncodingFormat ("application/json" )
116124 .build ();
117- assertEquals (dataEntity . getId (), "example.json" );
125+ assertEquals ("example.json" , dataEntity . getId () );
118126 HelpFunctions .compareEntityWithFile (dataEntity , "/json/entities/data/localFile.json" );
119127
128+ String fileName = "cp7glop.ai" ;
129+ Path filePath = Paths .get (fileName );
130+
120131 dataEntity = new FileEntity .FileEntityBuilder ()
121- .setLocationWithExceptions (Paths . get ( "cp7glop.ai" ) )
122- .setId ("cp7glop.ai" )
132+ .setLocationWithExceptions (filePath )
133+ .setId (fileName )
123134 .addProperty ("name" , "Diagram showing trend to increase" )
124135 .setEncodingFormat ("application/pdf" )
125136 .build ();
126- assertEquals (dataEntity .getId (), "cp7glop.ai" );
127- assertEquals (dataEntity .getPath (), Paths . get ( "cp7glop.ai" ) );
128- assertEquals (dataEntity .getProperty ("encodingFormat" ).asText (), "application/pdf" );
137+ assertEquals (fileName , dataEntity .getId ());
138+ assertEquals (dataEntity .getPath (), filePath );
139+ assertEquals ("application/pdf" , dataEntity .getProperty ("encodingFormat" ).asText ());
129140 }
130141
131142 @ Test
132- void testEncodedUrl () throws MalformedURLException , URISyntaxException {
143+ void testEncodedUrl () throws URISyntaxException {
133144 //this entity id is a valid URL so there should not be any console information
134145 DataEntity dataEntity = new DataEntity .DataEntityBuilder ()
135146 .addType ("File" )
@@ -156,15 +167,15 @@ void testEncodedUrl() throws MalformedURLException, URISyntaxException {
156167 .build ();
157168
158169 assertTrue (dataEntity .getTypes ().contains ("File" ));
159- assertEquals (dataEntity . getId (), "almost-50%25.json" );
170+ assertEquals ("almost-50%25.json" , dataEntity . getId () );
160171
161172 // even if the Path is not correctly encoded, the data entity will be added with an encoded Path.
162173 dataEntity = new DataEntity .DataEntityBuilder ()
163174 .addType ("File" )
164175 .setLocationWithExceptions (Paths .get ("/json/crate/Results and Diagrams/almost-50%.json" ))
165176 .setId ("almost-50%.json" )
166177 .build ();
167- assertEquals (dataEntity . getId (), "almost-50%25.json" );
178+ assertEquals ("almost-50%25.json" , dataEntity . getId () );
168179
169180 }
170181
@@ -179,10 +190,50 @@ void testRemoveProperty() {
179190
180191 dataEntity .removeProperty ("url" );
181192 assertNull (dataEntity .getProperty ("url" ));
182- assertEquals (dataEntity .getProperties ().size (), 4 );
193+ assertEquals (4 , dataEntity .getProperties ().size ());
183194
184195 List <String > keyList = Arrays .asList ("encodingFormat" , "name" );
185196 dataEntity .removeProperties (keyList );
186- assertEquals (dataEntity .getProperties ().size (), 2 );
197+ assertEquals (2 , dataEntity .getProperties ().size ());
198+ }
199+
200+ @ Test
201+ void testMixedArrayProperties () {
202+ ObjectMapper mapper = new ObjectMapper ();
203+
204+ ArrayNode propertyValue = mapper .createArrayNode ();
205+ propertyValue
206+ .add (1 )
207+ .add ("2" )
208+ .add (3.14 )
209+ .add (true )
210+ .add ((JsonNode ) null )
211+ .add (mapper .nullNode ());
212+ String propertyName = "mixedArray" ;
213+
214+ DataEntity entity = new DataEntityBuilder ()
215+ .addProperty (propertyName , propertyValue )
216+ .build ();
217+ assertEquals (propertyValue , entity .getProperty (propertyName ));
218+ }
219+
220+ @ ParameterizedTest
221+ @ MethodSource ("provideInvalidPropertyValues" )
222+ void testNestedArrayProperties (ArrayNode propertyValue ) {
223+ String propertyName = "invalidArray" ;
224+ DataEntity entity = new DataEntityBuilder ()
225+ .addProperty (propertyName , propertyValue )
226+ .build ();
227+ assertNull (entity .getProperty (propertyName ));
228+ }
229+
230+ private static Stream <ArrayNode > provideInvalidPropertyValues () {
231+ ObjectMapper mapper = new ObjectMapper ();
232+ ArrayNode withSubArray = mapper .createArrayNode ().add (1 ).add ("2" );
233+ withSubArray .addArray ();
234+ ArrayNode withSubObject = mapper .createArrayNode ().add (1 ).add ("2" );
235+ withSubObject .addObject ();
236+
237+ return Stream .of (withSubArray , withSubObject );
187238 }
188239}
0 commit comments