-
Notifications
You must be signed in to change notification settings - Fork 2
/
BeanAnnotationProcessorTest.java
288 lines (244 loc) · 12.1 KB
/
BeanAnnotationProcessorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* Copyright (C) 2017 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jsonld.common;
import cz.cvut.kbss.jopa.model.annotations.Id;
import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
import cz.cvut.kbss.jopa.vocabulary.RDFS;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.annotation.JsonLdAttributeOrder;
import cz.cvut.kbss.jsonld.annotation.JsonLdProperty;
import cz.cvut.kbss.jsonld.environment.Generator;
import cz.cvut.kbss.jsonld.environment.Vocabulary;
import cz.cvut.kbss.jsonld.environment.model.*;
import cz.cvut.kbss.jsonld.exception.JsonLdSerializationException;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.*;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
@SuppressWarnings("unused")
class BeanAnnotationProcessorTest {
@Test
void getOwlClassesExtractsClassIriFromObject() {
final Person p = new Person();
final Set<String> result = BeanAnnotationProcessor.getOwlClasses(p);
assertEquals(1, result.size());
assertTrue(result.contains(Vocabulary.PERSON));
}
@Test
void getOwlClassesReturnsNullForNonOwlClass() {
final URI uri = URI.create("http://test");
assertTrue(BeanAnnotationProcessor.getOwlClasses(uri).isEmpty());
}
@Test
void getOwlClassesExtractsClassesFromAncestors() {
final User u = new User();
final Set<String> result = BeanAnnotationProcessor.getOwlClasses(u);
assertTrue(result.contains(Vocabulary.USER));
assertTrue(result.contains(Vocabulary.PERSON));
}
@Test
void getSerializableFieldsExtractsAllFieldsOfAnInstanceIncludingSuperclassOnes() throws Exception {
final User u = new User();
final List<Field> fields = BeanAnnotationProcessor.getSerializableFields(u);
assertTrue(fields.contains(User.class.getDeclaredField("username")));
assertTrue(fields.containsAll(
Arrays.asList(Person.class.getDeclaredField("uri"), Person.class.getDeclaredField("firstName"),
Person.class.getDeclaredField("lastName"))));
}
@Test
void getSerializableFieldsSkipsStaticFields() throws Exception {
final Organization org = new Organization();
final List<Field> fields = BeanAnnotationProcessor.getSerializableFields(org);
assertFalse(fields.contains(Organization.class.getDeclaredField("DEFAULT_COUNTRY")));
}
@Test
void getSerializableFieldsSkipsNonAnnotatedFields() throws Exception {
final Organization org = new Organization();
final List<Field> fields = BeanAnnotationProcessor.getSerializableFields(org);
assertFalse(fields.contains(Organization.class.getDeclaredField("age")));
}
@Test
void getSerializableFieldsSkipsFieldsWithWriteOnlyAccess() throws Exception {
final SerializableWithWriteOnly instance = new SerializableWithWriteOnly();
final List<Field> result = BeanAnnotationProcessor.getSerializableFields(instance);
assertFalse(result.contains(SerializableWithWriteOnly.class.getDeclaredField("writeOnly")));
}
private static class SerializableWithWriteOnly {
@Id
private URI uri;
@OWLDataProperty(iri = "http://serializable")
private String serializableField;
@JsonLdProperty(access = JsonLdProperty.Access.WRITE_ONLY)
@OWLDataProperty(iri = "http://writeOnly")
private String writeOnly;
}
@Test
void mapSerializableFieldsReturnsMapOfSerializableFieldsWithPropertyIriKeys() throws Exception {
final Map<String, Field> result = BeanAnnotationProcessor.mapSerializableFields(Employee.class);
assertEquals(Person.class.getDeclaredField("firstName"), result.get(Vocabulary.FIRST_NAME));
assertEquals(Person.class.getDeclaredField("lastName"), result.get(Vocabulary.LAST_NAME));
assertEquals(User.class.getDeclaredField("username"), result.get(Vocabulary.USERNAME));
assertEquals(User.class.getDeclaredField("admin"), result.get(Vocabulary.IS_ADMIN));
assertEquals(Employee.class.getDeclaredField("employer"), result.get(Vocabulary.IS_MEMBER_OF));
}
@Test
void mapSerializableFieldsResultContainsTypesFieldMappedToRdfType() throws Exception {
final Map<String, Field> result = BeanAnnotationProcessor.mapSerializableFields(User.class);
assertTrue(result.containsKey(JsonLd.TYPE));
assertEquals(User.class.getDeclaredField("types"), result.get(JsonLd.TYPE));
}
@Test
void getAttributeIdentifierReturnsIdForIdAttribute() throws Exception {
final String id = BeanAnnotationProcessor.getAttributeIdentifier(Person.class.getDeclaredField("uri"));
assertEquals(JsonLd.ID, id);
}
@Test
void getAttributeIdentifierReturnsIriOfOWLDataProperty() throws Exception {
final String id = BeanAnnotationProcessor.getAttributeIdentifier(Person.class.getDeclaredField("firstName"));
assertEquals(Vocabulary.FIRST_NAME, id);
}
@Test
void getAttributeIdentifierReturnsIriOfOWLObjectProperty() throws Exception {
final String id = BeanAnnotationProcessor.getAttributeIdentifier(Employee.class.getDeclaredField("employer"));
assertEquals(Vocabulary.IS_MEMBER_OF, id);
}
@Test
void getAttributeIdentifierReturnsIriOfOWLAnnotationProperty() throws Exception {
final String id = BeanAnnotationProcessor.getAttributeIdentifier(Organization.class.getDeclaredField("name"));
assertEquals(RDFS.LABEL, id);
}
@Test
void getAttributeIdentifierThrowsIllegalArgumentWhenFieldIsNotJsonLdSerializable() throws Exception {
final JsonLdSerializationException result = assertThrows(JsonLdSerializationException.class,
() -> BeanAnnotationProcessor
.getAttributeIdentifier(Organization.class.getDeclaredField("DEFAULT_COUNTRY")));
assertEquals(
"Field " + Organization.class.getDeclaredField("DEFAULT_COUNTRY") + " is not JSON-LD serializable.",
result.getMessage());
}
@Test
void getInstanceIdentifierExtractsIdFieldValue() {
final Organization org = Generator.generateOrganization();
assertEquals(org.getUri(), BeanAnnotationProcessor.getInstanceIdentifier(org).get());
}
@Test
void getInstanceIdentifierExtractsIdFieldFromAncestorClass() {
final Employee e = Generator.generateEmployee();
assertEquals(e.getUri(), BeanAnnotationProcessor.getInstanceIdentifier(e).get());
}
@Test
void getInstanceIdentifierReturnsEmptyOptionalWhenInstanceHasNoIdentifierField() {
final ClassWithoutIdentifier instance = new ClassWithoutIdentifier();
final Optional<Object> result = BeanAnnotationProcessor.getInstanceIdentifier(instance);
assertNotNull(result);
assertFalse(result.isPresent());
}
@OWLClass(iri = "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/ClassWithoutIdentifier")
private static class ClassWithoutIdentifier {
@OWLAnnotationProperty(iri = RDFS.LABEL)
private String label;
}
@Test
void isOwlClassEntityReturnsTrueForClassAnnotatedWithOWLClass() {
assertTrue(BeanAnnotationProcessor.isOwlClassEntity(User.class));
}
@Test
void isOwlClassEntityReturnsFalseForClassWithoutOWLClassAnnotation() {
assertFalse(BeanAnnotationProcessor.isOwlClassEntity(String.class));
}
@Test
void isOwlClassEntityReturnsFalseForNullArgument() {
assertFalse(BeanAnnotationProcessor.isOwlClassEntity(null));
}
@Test
void isInstanceIdentifierReturnsTrueForIdField() throws Exception {
assertTrue(BeanAnnotationProcessor.isInstanceIdentifier(Person.class.getDeclaredField("uri")));
}
@Test
void isInstanceIdentifierReturnsFalseForNonIdField() throws Exception {
assertFalse(BeanAnnotationProcessor.isInstanceIdentifier(Person.class.getDeclaredField("firstName")));
}
@Test
void getOwlClassExtractsClassIriFromJavaType() {
assertEquals(Vocabulary.EMPLOYEE, BeanAnnotationProcessor.getOwlClass(Employee.class));
}
@Test
void getOwlClassThrowsIllegalArgumentWhenNonOwlClassJavaTypeIsPassedAsArgument() {
final IllegalArgumentException result = assertThrows(IllegalArgumentException.class,
() -> BeanAnnotationProcessor.getOwlClass(Integer.class));
assertEquals(Integer.class + " is not an OWL class entity.", result.getMessage());
}
@Test
void getSerializableFieldsReturnsPropertiesFieldAsWell() throws Exception {
final List<Field> fields = BeanAnnotationProcessor.getSerializableFields(new Person());
assertTrue(fields.contains(Person.class.getDeclaredField("properties")));
}
@Test
void mapSerializableFieldsSkipsPropertiesField() throws Exception {
final Map<String, Field> fields = BeanAnnotationProcessor.mapSerializableFields(Person.class);
assertFalse(fields.containsValue(Person.class.getDeclaredField("properties")));
}
@Test
void hasPropertiesFieldReturnsFalseForClassWithoutProperties() {
assertFalse(BeanAnnotationProcessor.hasPropertiesField(Organization.class));
}
@Test
void hasPropertiesFieldReturnsTrueForClassWithProperties() {
assertTrue(BeanAnnotationProcessor.hasPropertiesField(Person.class));
assertTrue(BeanAnnotationProcessor.hasPropertiesField(User.class)); // Check also subclasses
}
@Test
void getPropertiesFieldReturnsPropertiesFieldOfClass() throws Exception {
final Field result = BeanAnnotationProcessor.getPropertiesField(Person.class);
assertEquals(Person.class.getDeclaredField("properties"), result);
}
@Test
void getPropertiesFieldThrowsIllegalArgumentForClassWithoutPropertiesField() {
final IllegalArgumentException result = assertThrows(IllegalArgumentException.class,
() -> BeanAnnotationProcessor.getPropertiesField(Organization.class));
assertThat(result.getMessage(), containsString(Organization.class + " does not have a @Properties field."));
}
@Test
void getAttributeOrderReturnsValueOfJsonLdAttributeOrderAnnotation() {
final String[] result = BeanAnnotationProcessor.getAttributeOrder(Study.class);
assertArrayEquals(Study.class.getDeclaredAnnotation(JsonLdAttributeOrder.class).value(), result);
}
@Test
void getAttributeOrderReturnsEmptyArrayWhenOrderIsNotSpecifiedOnClass() {
final String[] result = BeanAnnotationProcessor.getAttributeOrder(Person.class);
assertEquals(0, result.length);
}
@Test
void getTypesFieldReturnsTypesAttribute() {
final Optional<Field> result = BeanAnnotationProcessor.getTypesField(User.class);
assertTrue(result.isPresent());
}
@Test
void getTypesFieldReturnsEmptyOptionalIfTypesAttributeIsMissing() {
final Optional<Field> result = BeanAnnotationProcessor.getTypesField(Person.class);
assertFalse(result.isPresent());
}
@Test
void hasTypesFieldReturnsTrueWhenClassHasTypesAttribute() {
assertTrue(BeanAnnotationProcessor.hasTypesField(Employee.class));
}
@Test
void hasTypesFieldReturnsFalseWhenClassDoesNotHaveTypesAttribute() {
assertFalse(BeanAnnotationProcessor.hasTypesField(Person.class));
}
}