12
12
*/
13
13
package cz .cvut .kbss .jsonld .serialization ;
14
14
15
- import com .github .jsonldjava .utils .JsonUtils ;
16
15
import cz .cvut .kbss .jopa .model .MultilingualString ;
17
16
import cz .cvut .kbss .jopa .vocabulary .RDFS ;
18
17
import cz .cvut .kbss .jopa .vocabulary .SKOS ;
30
29
import cz .cvut .kbss .jsonld .environment .model .StudyWithNamespaces ;
31
30
import cz .cvut .kbss .jsonld .environment .model .User ;
32
31
import jakarta .json .JsonArray ;
32
+ import jakarta .json .JsonObject ;
33
+ import jakarta .json .JsonValue ;
33
34
import org .junit .jupiter .api .BeforeEach ;
34
35
import org .junit .jupiter .api .Test ;
35
36
39
40
import java .util .HashSet ;
40
41
import java .util .LinkedHashSet ;
41
42
import java .util .List ;
42
- import java .util .Map ;
43
43
import java .util .Objects ;
44
44
import java .util .Optional ;
45
45
import java .util .Set ;
46
46
47
47
import static org .hamcrest .MatcherAssert .assertThat ;
48
48
import static org .hamcrest .Matchers .hasKey ;
49
- import static org .hamcrest .Matchers .instanceOf ;
50
49
import static org .hamcrest .Matchers .not ;
51
50
import static org .hamcrest .Matchers .startsWith ;
52
51
import static org .junit .jupiter .api .Assertions .assertEquals ;
53
- import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
54
- import static org .junit .jupiter .api .Assertions .assertNotNull ;
55
52
import static org .junit .jupiter .api .Assertions .assertTrue ;
56
53
57
54
class CompactedJsonLdSerializerTest extends JsonLdSerializerTestBase {
@@ -62,77 +59,78 @@ void setUp() {
62
59
}
63
60
64
61
@ Test
65
- void testSerializeCollectionOfObjects () throws Exception {
62
+ void testSerializeCollectionOfObjects () {
66
63
final Set <User > users = Generator .generateUsers ();
67
- sut .serialize (users );
68
- Object jsonObject = JsonUtils .fromString (jsonWriter .getResult ());
69
- assertNotNull (jsonObject );
70
- assertInstanceOf (List .class , jsonObject );
64
+ final JsonValue jsonObject = serializeAndRead (users );
65
+ assertEquals (JsonValue .ValueType .ARRAY , jsonObject .getValueType ());
71
66
}
72
67
73
68
@ Test
74
- void serializationOfCollectionOfInstancesReferencingSameInstanceUsesReferenceNode () throws Exception {
69
+ void serializationOfCollectionOfInstancesReferencingSameInstanceUsesReferenceNode () {
75
70
final Organization org = Generator .generateOrganization ();
76
71
generateEmployees (org , true );
77
72
final Set <Employee > employees = org .getEmployees ();
78
73
org .setEmployees (null );
79
- sut .serialize (employees );
80
- Object jsonObject = JsonUtils .fromString (jsonWriter .getResult ());
81
- assertNotNull (jsonObject );
82
- final List <?> jsonList = (List <?>) jsonObject ;
74
+ final JsonValue jsonObject = serializeAndRead (employees );
75
+ assertEquals (JsonValue .ValueType .ARRAY , jsonObject .getValueType ());
76
+ final JsonArray jsonList = jsonObject .asJsonArray ();
83
77
for (int i = 1 ; i < jsonList .size (); i ++) { // Start from 1, the first one contains the full object
84
- final Map <?, ?> item = ( Map <?, ?>) jsonList .get (i );
85
- assertTrue ( item .get (Vocabulary .IS_MEMBER_OF ) instanceof Map );
86
- final Map <?, ?> map = ( Map <?, ?>) item .get (Vocabulary .IS_MEMBER_OF );
78
+ final JsonObject item = jsonList .getJsonObject (i );
79
+ assertEquals ( JsonValue . ValueType . OBJECT , item .get (Vocabulary .IS_MEMBER_OF ). getValueType () );
80
+ final JsonObject map = item .getJsonObject (Vocabulary .IS_MEMBER_OF );
87
81
assertEquals (1 , map .size ());
88
- assertEquals (org .getUri ().toString (), map .get (JsonLd .ID ));
82
+ assertEquals (org .getUri ().toString (), map .getString (JsonLd .ID ));
89
83
}
90
84
}
91
85
92
86
@ Test
93
- void serializationSkipsNullDataPropertyValues () throws Exception {
87
+ void serializationSkipsNullDataPropertyValues () {
94
88
final User user = Generator .generateUser ();
95
89
user .setAdmin (null );
96
- final Map <String , ?> json = serializeAndRead (user );
97
- assertThat (json , not (hasKey (Vocabulary .IS_ADMIN )));
90
+ final JsonValue json = serializeAndRead (user );
91
+ assertEquals (JsonValue .ValueType .OBJECT , json .getValueType ());
92
+ assertThat (json .asJsonObject (), not (hasKey (Vocabulary .IS_ADMIN )));
98
93
}
99
94
100
95
@ Test
101
- void serializationSkipsNullObjectPropertyValues () throws Exception {
96
+ void serializationSkipsNullObjectPropertyValues () {
102
97
final Employee employee = Generator .generateEmployee ();
103
98
employee .setEmployer (null );
104
- final Map <String , ?> json = serializeAndRead (employee );
105
- assertThat (json , not (hasKey (Vocabulary .IS_MEMBER_OF )));
99
+ final JsonValue json = serializeAndRead (employee );
100
+ assertEquals (JsonValue .ValueType .OBJECT , json .getValueType ());
101
+ assertThat (json .asJsonObject (), not (hasKey (Vocabulary .IS_MEMBER_OF )));
106
102
}
107
103
108
104
@ Test
109
- void serializationGeneratesBlankNodeIfInstancesDoesNotHaveIdentifierValue () throws Exception {
105
+ void serializationGeneratesBlankNodeIfInstancesDoesNotHaveIdentifierValue () {
110
106
final Organization company = Generator .generateOrganization ();
111
107
company .setUri (null );
112
- final Map <String , ?> json = serializeAndRead (company );
113
- assertThat (json , hasKey (JsonLd .ID ));
114
- assertThat (json .get (JsonLd .ID ).toString (), startsWith (IdentifierUtil .B_NODE_PREFIX ));
108
+ final JsonValue json = serializeAndRead (company );
109
+ assertEquals (JsonValue .ValueType .OBJECT , json .getValueType ());
110
+ assertThat (json .asJsonObject (), hasKey (JsonLd .ID ));
111
+ assertThat (json .asJsonObject ().getString (JsonLd .ID ), startsWith (IdentifierUtil .B_NODE_PREFIX ));
115
112
}
116
113
117
114
@ Test
118
- void serializationSerializesMultilingualStringWithValues () throws Exception {
115
+ void serializationSerializesMultilingualStringWithValues () {
119
116
final ObjectWithMultilingualString instance = new ObjectWithMultilingualString (Generator .generateUri ());
120
117
final MultilingualString name = new MultilingualString ();
121
118
name .set ("en" , "Leveraging Semantic Web Technologies in Domain-specific Information Systems" );
122
119
name .set ("cs" , "Využití technologií sémantického webu v doménových informačních systémech" );
123
120
instance .setLabel (name );
124
121
125
- final Map <String , ?> json = serializeAndRead (instance );
126
- assertTrue (json .containsKey (RDFS .LABEL ));
127
- final List <?> label = (List <?>) json .get (RDFS .LABEL );
122
+ final JsonValue json = serializeAndRead (instance );
123
+ assertEquals (JsonValue .ValueType .OBJECT , json .getValueType ());
124
+ assertTrue (json .asJsonObject ().containsKey (RDFS .LABEL ));
125
+ final JsonArray label = json .asJsonObject ().getJsonArray (RDFS .LABEL );
128
126
assertEquals (name .getValue ().size (), label .size ());
129
- for (Object item : label ) {
130
- assertThat ( item , instanceOf ( Map . class ));
131
- final Map <?, ?> m = ( Map <?, ?>) item ;
127
+ for (JsonValue item : label ) {
128
+ assertEquals ( JsonValue . ValueType . OBJECT , item . getValueType ( ));
129
+ final JsonObject m = item . asJsonObject () ;
132
130
assertTrue (m .containsKey (JsonLd .LANGUAGE ));
133
131
assertTrue (m .containsKey (JsonLd .VALUE ));
134
- assertTrue (name .contains (m .get (JsonLd .LANGUAGE ). toString ( )));
135
- assertEquals (name .get (m .get (JsonLd .LANGUAGE ). toString ()) , m .get (JsonLd .VALUE ));
132
+ assertTrue (name .contains (m .getString (JsonLd .LANGUAGE )));
133
+ assertEquals (name .get (m .getString (JsonLd .LANGUAGE )) , m .getString (JsonLd .VALUE ));
136
134
}
137
135
}
138
136
@@ -163,82 +161,82 @@ void serializationSerializesPluralMultilingualString() throws Exception {
163
161
}
164
162
165
163
@ Test
166
- void serializationSerializesMultilingualStringInTypedUnmappedProperties () throws Exception {
164
+ void serializationSerializesMultilingualStringInTypedUnmappedProperties () {
167
165
final PersonWithTypedProperties instance = new PersonWithTypedProperties ();
168
166
instance .setUri (Generator .generateUri ());
169
167
final MultilingualString ms = MultilingualString .create ("en" , "Falcon" );
170
168
ms .set ("cs" , "Sokol" );
171
169
final URI property = URI .create ("http://xmlns.com/foaf/0.1/nick" );
172
170
instance .setProperties (Collections .singletonMap (property , Collections .singleton (ms )));
173
171
174
- final Map <String , ?> json = serializeAndRead (instance );
175
- assertTrue (json .containsKey (property .toString ()));
176
- final List <?> nick = (List <?>) json .get (property .toString ());
172
+ final JsonValue json = serializeAndRead (instance );
173
+ assertEquals (JsonValue .ValueType .OBJECT , json .getValueType ());
174
+ assertTrue (json .asJsonObject ().containsKey (property .toString ()));
175
+ final JsonArray nick = json .asJsonObject ().getJsonArray (property .toString ());
177
176
assertEquals (ms .getValue ().size (), nick .size ());
178
- for (Object item : nick ) {
179
- assertThat ( item , instanceOf ( Map . class ));
180
- final Map <?, ?> m = ( Map <?, ?>) item ;
177
+ for (JsonValue item : nick ) {
178
+ assertEquals ( JsonValue . ValueType . OBJECT , item . getValueType ( ));
179
+ final JsonObject m = item . asJsonObject () ;
181
180
assertTrue (m .containsKey (JsonLd .LANGUAGE ));
182
181
assertTrue (m .containsKey (JsonLd .VALUE ));
183
- assertTrue (ms .contains (m .get (JsonLd .LANGUAGE ). toString ( )));
184
- assertEquals (ms .get (m .get (JsonLd .LANGUAGE ). toString ()) , m .get (JsonLd .VALUE ));
182
+ assertTrue (ms .contains (m .getString (JsonLd .LANGUAGE )));
183
+ assertEquals (ms .get (m .getString (JsonLd .LANGUAGE )) , m .getString (JsonLd .VALUE ));
185
184
}
186
185
}
187
186
188
187
@ Test
189
- void serializationSupportsCompactedIrisBasedOnJOPANamespaces () throws Exception {
188
+ void serializationSupportsCompactedIrisBasedOnJOPANamespaces () {
190
189
final StudyWithNamespaces study = new StudyWithNamespaces ();
191
190
study .setUri (Generator .generateUri ());
192
191
study .setName ("Test study" );
193
192
study .setParticipants (Collections .singleton (Generator .generateEmployee ()));
194
193
study .setMembers (Collections .singleton (Generator .generateEmployee ()));
195
194
196
- final Map <String , ?> json = serializeAndRead (study );
197
- assertThat (json , hasKey (RDFS .LABEL ));
198
- assertThat (json , hasKey (Vocabulary .HAS_PARTICIPANT ));
199
- assertThat (json , hasKey (Vocabulary .HAS_MEMBER ));
195
+ final JsonValue json = serializeAndRead (study );
196
+ assertEquals (JsonValue .ValueType .OBJECT , json .getValueType ());
197
+ assertThat (json .asJsonObject (), hasKey (RDFS .LABEL ));
198
+ assertThat (json .asJsonObject (), hasKey (Vocabulary .HAS_PARTICIPANT ));
199
+ assertThat (json .asJsonObject (), hasKey (Vocabulary .HAS_MEMBER ));
200
200
}
201
201
202
202
/**
203
203
* Bug #36
204
204
*/
205
- @ SuppressWarnings ("unchecked" )
206
205
@ Test
207
- void serializationSerializesMultilingualStringWithLanguageLessValue () throws Exception {
206
+ void serializationSerializesMultilingualStringWithLanguageLessValue () {
208
207
final ObjectWithMultilingualString instance = new ObjectWithMultilingualString (Generator .generateUri ());
209
208
final MultilingualString name = new MultilingualString ();
210
209
name .set ("en" , "Value in English" );
211
210
name .set ("cs" , "Hodnota v češtině" );
212
211
name .set ("Default value" );
213
212
instance .setLabel (name );
214
213
215
- final Map <String , ?> json = serializeAndRead (instance );
216
- assertTrue (json .containsKey (RDFS .LABEL ));
217
- final List <?> label = (List <?>) json .get (RDFS .LABEL );
214
+ final JsonValue json = serializeAndRead (instance );
215
+ assertEquals (JsonValue .ValueType .OBJECT , json .getValueType ());
216
+ assertTrue (json .asJsonObject ().containsKey (RDFS .LABEL ));
217
+ final JsonArray label = json .asJsonObject ().getJsonArray (RDFS .LABEL );
218
218
assertEquals (name .getValue ().size (), label .size ());
219
- final Optional <Map <?, ?>> result = ( Optional < Map <?, ?>>) label .stream ().filter (item -> {
220
- assertThat ( item , instanceOf ( Map . class ));
221
- final Map <?, ?> m = ( Map <?, ?>) item ;
222
- return Objects .equals (m .get (JsonLd .LANGUAGE ), JsonLd .NONE );
219
+ final Optional <JsonValue > result = label .stream ().filter (item -> {
220
+ assertEquals ( JsonValue . ValueType . OBJECT , item . getValueType ( ));
221
+ final JsonObject m = item . asJsonObject () ;
222
+ return Objects .equals (m .getString (JsonLd .LANGUAGE ), JsonLd .NONE );
223
223
}).findAny ();
224
224
assertTrue (result .isPresent ());
225
- assertEquals (name .get (), result .get ().get (JsonLd .VALUE ));
225
+ assertEquals (name .get (), result .get ().asJsonObject (). getString (JsonLd .VALUE ));
226
226
}
227
227
228
228
@ Test
229
- void serializationSerializesRootCollectionOfEnumConstantsMappedToIndividualsAsArrayOfIndividuals () throws Exception {
229
+ void serializationSerializesRootCollectionOfEnumConstantsMappedToIndividualsAsArrayOfIndividuals () {
230
230
final List <OwlPropertyType > value = Arrays .asList (OwlPropertyType .values ());
231
231
232
- sut .serialize (new LinkedHashSet <>(value ));
233
- Object jsonObject = JsonUtils .fromString (jsonWriter .getResult ());
234
- assertInstanceOf (List .class , jsonObject );
235
- final List <?> lst = (List <?>) jsonObject ;
232
+ final JsonValue jsonObject = serializeAndRead (new LinkedHashSet <>(value ));
233
+ assertEquals (JsonValue .ValueType .ARRAY , jsonObject .getValueType ());
234
+ final JsonArray lst = jsonObject .asJsonArray ();
236
235
assertEquals (value .size (), lst .size ());
237
236
for (int i = 0 ; i < value .size (); i ++) {
238
- assertInstanceOf (Map .class , lst .get (i ));
239
- final Map <?, ?> element = (Map <?, ?>) lst .get (i );
237
+ final JsonObject element = lst .getJsonObject (i );
240
238
assertThat (element , hasKey (JsonLd .ID ));
241
- assertEquals (OwlPropertyType .getMappedIndividual (value .get (i )), element .get (JsonLd .ID ));
239
+ assertEquals (OwlPropertyType .getMappedIndividual (value .get (i )), element .getString (JsonLd .ID ));
242
240
}
243
241
}
244
242
}
0 commit comments