1
1
package edu .kit .datamanager .ro_crate .entities .data ;
2
2
3
+ import com .fasterxml .jackson .databind .JsonNode ;
3
4
import com .fasterxml .jackson .databind .ObjectMapper ;
5
+ import com .fasterxml .jackson .databind .node .ArrayNode ;
4
6
import com .fasterxml .jackson .databind .node .ObjectNode ;
5
7
6
8
import java .io .IOException ;
9
11
import edu .kit .datamanager .ro_crate .HelpFunctions ;
10
12
import edu .kit .datamanager .ro_crate .entities .data .DataEntity .DataEntityBuilder ;
11
13
import edu .kit .datamanager .ro_crate .objectmapper .MyObjectMapper ;
12
- import java .net .MalformedURLException ;
13
14
import java .net .URI ;
14
15
15
16
import java .net .URISyntaxException ;
17
+ import java .nio .file .Path ;
16
18
import java .nio .file .Paths ;
17
19
import java .util .Arrays ;
18
20
import java .util .List ;
21
+ import java .util .stream .Stream ;
22
+
19
23
import org .junit .jupiter .api .Test ;
24
+ import org .junit .jupiter .params .ParameterizedTest ;
25
+ import org .junit .jupiter .params .provider .MethodSource ;
20
26
21
27
import static org .junit .jupiter .api .Assertions .assertEquals ;
22
28
import static org .junit .jupiter .api .Assertions .assertNotEquals ;
@@ -93,12 +99,14 @@ void testUriCheck() {
93
99
94
100
assertNotNull (dataEntity );
95
101
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
+ );
102
110
103
111
assertEquals ("This Data Entity remote ID does not resolve to a valid URL." , exception .getMessage ());
104
112
@@ -114,22 +122,25 @@ void testPathCheck() throws IOException {
114
122
.addProperty ("name" , "RO-Crate specification" )
115
123
.setEncodingFormat ("application/json" )
116
124
.build ();
117
- assertEquals (dataEntity . getId (), "example.json" );
125
+ assertEquals ("example.json" , dataEntity . getId () );
118
126
HelpFunctions .compareEntityWithFile (dataEntity , "/json/entities/data/localFile.json" );
119
127
128
+ String fileName = "cp7glop.ai" ;
129
+ Path filePath = Paths .get (fileName );
130
+
120
131
dataEntity = new FileEntity .FileEntityBuilder ()
121
- .setLocationWithExceptions (Paths . get ( "cp7glop.ai" ) )
122
- .setId ("cp7glop.ai" )
132
+ .setLocationWithExceptions (filePath )
133
+ .setId (fileName )
123
134
.addProperty ("name" , "Diagram showing trend to increase" )
124
135
.setEncodingFormat ("application/pdf" )
125
136
.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 ());
129
140
}
130
141
131
142
@ Test
132
- void testEncodedUrl () throws MalformedURLException , URISyntaxException {
143
+ void testEncodedUrl () throws URISyntaxException {
133
144
//this entity id is a valid URL so there should not be any console information
134
145
DataEntity dataEntity = new DataEntity .DataEntityBuilder ()
135
146
.addType ("File" )
@@ -156,15 +167,15 @@ void testEncodedUrl() throws MalformedURLException, URISyntaxException {
156
167
.build ();
157
168
158
169
assertTrue (dataEntity .getTypes ().contains ("File" ));
159
- assertEquals (dataEntity . getId (), "almost-50%25.json" );
170
+ assertEquals ("almost-50%25.json" , dataEntity . getId () );
160
171
161
172
// even if the Path is not correctly encoded, the data entity will be added with an encoded Path.
162
173
dataEntity = new DataEntity .DataEntityBuilder ()
163
174
.addType ("File" )
164
175
.setLocationWithExceptions (Paths .get ("/json/crate/Results and Diagrams/almost-50%.json" ))
165
176
.setId ("almost-50%.json" )
166
177
.build ();
167
- assertEquals (dataEntity . getId (), "almost-50%25.json" );
178
+ assertEquals ("almost-50%25.json" , dataEntity . getId () );
168
179
169
180
}
170
181
@@ -179,10 +190,50 @@ void testRemoveProperty() {
179
190
180
191
dataEntity .removeProperty ("url" );
181
192
assertNull (dataEntity .getProperty ("url" ));
182
- assertEquals (dataEntity .getProperties ().size (), 4 );
193
+ assertEquals (4 , dataEntity .getProperties ().size ());
183
194
184
195
List <String > keyList = Arrays .asList ("encodingFormat" , "name" );
185
196
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 );
187
238
}
188
239
}
0 commit comments