-
Notifications
You must be signed in to change notification settings - Fork 2
/
BeanAnnotationProcessorTest.java
267 lines (226 loc) · 11.3 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
/**
* 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.OWLAnnotationProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
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.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.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.*;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;
public class BeanAnnotationProcessorTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void getOwlClassesExtractsClassIriFromObject() {
final Person p = new Person();
final Set<String> result = BeanAnnotationProcessor.getOwlClasses(p);
assertEquals(1, result.size());
assertTrue(result.contains(Vocabulary.PERSON));
}
@Test
public void getOwlClassesReturnsNullForNonOwlClass() {
final URI uri = URI.create("http://test");
assertTrue(BeanAnnotationProcessor.getOwlClasses(uri).isEmpty());
}
@Test
public 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
public 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
public 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
public void getSerializableFieldsSkipsNonAnnotatedFields() throws Exception {
final Organization org = new Organization();
final List<Field> fields = BeanAnnotationProcessor.getSerializableFields(org);
assertFalse(fields.contains(Organization.class.getDeclaredField("age")));
}
@Test
public 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
public 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
public void getAttributeIdentifierReturnsIdForIdAttribute() throws Exception {
final String id = BeanAnnotationProcessor.getAttributeIdentifier(Person.class.getDeclaredField("uri"));
assertEquals(JsonLd.ID, id);
}
@Test
public void getAttributeIdentifierReturnsIriOfOWLDataProperty() throws Exception {
final String id = BeanAnnotationProcessor.getAttributeIdentifier(Person.class.getDeclaredField("firstName"));
assertEquals(Vocabulary.FIRST_NAME, id);
}
@Test
public void getAttributeIdentifierReturnsIriOfOWLObjectProperty() throws Exception {
final String id = BeanAnnotationProcessor.getAttributeIdentifier(Employee.class.getDeclaredField("employer"));
assertEquals(Vocabulary.IS_MEMBER_OF, id);
}
@Test
public void getAttributeIdentifierReturnsIriOfOWLAnnotationProperty() throws Exception {
final String id = BeanAnnotationProcessor.getAttributeIdentifier(Organization.class.getDeclaredField("name"));
assertEquals(RDFS.LABEL, id);
}
@Test
public void getAttributeIdentifierThrowsIllegalArgumentWhenFieldIsNotJsonLdSerializable() throws Exception {
thrown.expect(JsonLdSerializationException.class);
thrown.expectMessage(
"Field " + Organization.class.getDeclaredField("DEFAULT_COUNTRY") + " is not JSON-LD serializable.");
BeanAnnotationProcessor.getAttributeIdentifier(Organization.class.getDeclaredField("DEFAULT_COUNTRY"));
}
@Test
public void getInstanceIdentifierExtractsIdFieldValue() {
final Organization org = Generator.generateOrganization();
assertEquals(org.getUri(), BeanAnnotationProcessor.getInstanceIdentifier(org).get());
}
@Test
public void getInstanceIdentifierExtractsIdFieldFromAncestorClass() {
final Employee e = Generator.generateEmployee();
assertEquals(e.getUri(), BeanAnnotationProcessor.getInstanceIdentifier(e).get());
}
@Test
public 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
public void isOwlClassEntityReturnsTrueForClassAnnotatedWithOWLClass() {
assertTrue(BeanAnnotationProcessor.isOwlClassEntity(User.class));
}
@Test
public void isOwlClassEntityReturnsFalseForClassWithoutOWLClassAnnotation() {
assertFalse(BeanAnnotationProcessor.isOwlClassEntity(String.class));
}
@Test
public void isOwlClassEntityReturnsFalseForNullArgument() {
assertFalse(BeanAnnotationProcessor.isOwlClassEntity(null));
}
@Test
public void isInstanceIdentifierReturnsTrueForIdField() throws Exception {
assertTrue(BeanAnnotationProcessor.isInstanceIdentifier(Person.class.getDeclaredField("uri")));
}
@Test
public void isInstanceIdentifierReturnsFalseForNonIdField() throws Exception {
assertFalse(BeanAnnotationProcessor.isInstanceIdentifier(Person.class.getDeclaredField("firstName")));
}
@Test
public void getOwlClassExtractsClassIriFromJavaType() {
assertEquals(Vocabulary.EMPLOYEE, BeanAnnotationProcessor.getOwlClass(Employee.class));
}
@Test
public void getOwlClassThrowsIllegalArgumentWhenNonOwlClassJavaTypeIsPassedAsArgument() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(Integer.class + " is not an OWL class entity.");
BeanAnnotationProcessor.getOwlClass(Integer.class);
}
@Test
public void getSerializableFieldsReturnsPropertiesFieldAsWell() throws Exception {
final List<Field> fields = BeanAnnotationProcessor.getSerializableFields(new Person());
assertTrue(fields.contains(Person.class.getDeclaredField("properties")));
}
@Test
public void mapSerializableFieldsSkipsPropertiesField() throws Exception {
final Map<String, Field> fields = BeanAnnotationProcessor.mapSerializableFields(Person.class);
assertFalse(fields.containsValue(Person.class.getDeclaredField("properties")));
}
@Test
public void hasPropertiesFieldReturnsFalseForClassWithoutProperties() {
assertFalse(BeanAnnotationProcessor.hasPropertiesField(Organization.class));
}
@Test
public void hasPropertiesFieldReturnsTrueForClassWithProperties() {
assertTrue(BeanAnnotationProcessor.hasPropertiesField(Person.class));
assertTrue(BeanAnnotationProcessor.hasPropertiesField(User.class)); // Check also subclasses
}
@Test
public void getPropertiesFieldReturnsPropertiesFieldOfClass() throws Exception {
final Field result = BeanAnnotationProcessor.getPropertiesField(Person.class);
assertEquals(Person.class.getDeclaredField("properties"), result);
}
@Test
public void getPropertiesFieldThrowsIllegalArgumentForClassWithoutPropertiesField() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(containsString(Organization.class + " does not have a @Properties field."));
BeanAnnotationProcessor.getPropertiesField(Organization.class);
}
@Test
public void getAttributeOrderReturnsValueOfJsonLdAttributeOrderAnnotation() {
final String[] result = BeanAnnotationProcessor.getAttributeOrder(Study.class);
assertTrue(Arrays.equals(Study.class.getDeclaredAnnotation(JsonLdAttributeOrder.class).value(), result));
}
@Test
public void getAttributeOrderReturnsEmptyArrayWhenOrderIsNotSpecifiedOnClass() {
final String[] result = BeanAnnotationProcessor.getAttributeOrder(Person.class);
assertEquals(0, result.length);
}
@Test
public void getTypesFieldReturnsTypesAttribute() {
final Optional<Field> result = BeanAnnotationProcessor.getTypesField(User.class);
assertTrue(result.isPresent());
}
@Test
public void getTypesFieldReturnsEmptyOptionalIfTypesAttributeIsMissing() {
final Optional<Field> result = BeanAnnotationProcessor.getTypesField(Person.class);
assertFalse(result.isPresent());
}
@Test
public void hasTypesFieldReturnsTrueWhenClassHasTypesAttribute() {
assertTrue(BeanAnnotationProcessor.hasTypesField(Employee.class));
}
@Test
public void hasTypesFieldReturnsFalseWhenClassDoesNotHaveTypesAttribute() {
assertFalse(BeanAnnotationProcessor.hasTypesField(Person.class));
}
}