-
Notifications
You must be signed in to change notification settings - Fork 14
/
AbstractIdentifiableTypeTest.java
363 lines (325 loc) · 16.4 KB
/
AbstractIdentifiableTypeTest.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Copyright (C) 2019 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.jopa.model.metamodel;
import cz.cvut.kbss.jopa.environment.OWLClassA;
import cz.cvut.kbss.jopa.environment.OWLClassB;
import cz.cvut.kbss.jopa.model.IRI;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class AbstractIdentifiableTypeTest {
private static final String ID_NAME = "uri";
private static Class<OWLClassA> cls;
private static IRI classIri;
private static String className;
private EntityTypeImpl<OWLClassA> et;
@BeforeAll
static void setUpBeforeClass() {
cls = OWLClassA.class;
classIri = IRI.create(OWLClassA.getClassIri());
className = OWLClassA.class.getName();
}
@BeforeEach
void setUp() {
this.et = new EntityTypeImpl<>(className, cls, classIri);
final Identifier<OWLClassA, URI> id = mock(Identifier.class);
when(id.getName()).thenReturn(ID_NAME);
et.setIdentifier(id);
}
@Test
void hasSingleIdAttributeReturnsAlwaysTrue() {
assertTrue(et.hasSingleIdAttribute());
}
@Test
void getAttributesReturnsDeclaredAttributesPlusInheritedAttributes() {
final AbstractIdentifiableType<? super OWLClassA> supertype = spy(new MappedSuperclassTypeImpl<>(Object.class));
et.setSupertype(supertype);
et.getAttributes();
verify(supertype).getAttributes();
}
@Test
void getAttributesReturnsDeclaredAttributesWhenThereIsNoSupertype() {
final AbstractAttribute<OWLClassA, ?> att = mock(AbstractAttribute.class);
et.addDeclaredAttribute("test", att);
final Set<Attribute<? super OWLClassA, ?>> result = et.getAttributes();
assertEquals(1, result.size());
assertTrue(result.contains(att));
}
@Test
void getAttributeReturnsInheritedAttribute() {
final AbstractIdentifiableType<? super OWLClassA> supertype = spy(new MappedSuperclassTypeImpl<>(Object.class));
et.setSupertype(supertype);
final String attName = "test";
final AbstractAttribute att = mock(AbstractAttribute.class);
doReturn(att).when(supertype).getAttribute(attName);
assertEquals(att, et.getAttribute(attName));
verify(supertype).getAttribute(attName);
}
@Test
void getAttributeThrowsIllegalArgumentWhenAttributeIsNotFoundInDeclaredAndInheritedAttributes() {
final String attName = "unknownAttribute";
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> et.getAttribute(attName));
assertEquals("Attribute " + attName + " is not present in type " + et.toString(), ex.getMessage());
}
@Test
void getCollectionThrowsIllegalArgumentWhenElementTypeDoesNotMatch() {
final AbstractPluralAttribute att = mock(AbstractPluralAttribute.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, att);
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> et.getCollection(attName, OWLClassB.class));
assertEquals("Collection attribute " + attName + " with element type " + OWLClassB.class +
" is not present in type " + et.toString(), ex.getMessage());
}
@Test
void getListReturnsListAttribute() {
final ListAttributeImpl att = mock(ListAttributeImpl.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, att);
final ListAttribute result = et.getList(attName, OWLClassA.class);
assertEquals(att, result);
}
@Test
void getSetThrowsIllegalArgumentWhenAttributeIsNotSet() {
final AbstractPluralAttribute att = mock(AbstractPluralAttribute.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, att);
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> et.getSet(attName, OWLClassA.class));
assertEquals("Set attribute " + attName + " with element type " + OWLClassA.class +
" is not present in type " + et.toString(), ex.getMessage());
}
@Test
void getPluralAttributesGetsAlsoInheritedPluralAttributes() {
final CollectionAttribute att = mock(CollectionAttribute.class);
when(att.isCollection()).thenReturn(true);
final ListAttributeImpl listAtt = mock(ListAttributeImpl.class);
when(listAtt.isCollection()).thenReturn(true);
final AbstractIdentifiableType<? super OWLClassA> supertype = spy(new MappedSuperclassTypeImpl<>(Object.class));
et.setSupertype(supertype);
et.addDeclaredAttribute("test", listAtt);
when(supertype.getPluralAttributes()).thenReturn(Collections.singleton(att));
final Set<PluralAttribute<? super OWLClassA, ?, ?>> result = et.getPluralAttributes();
assertTrue(result.contains(att));
assertTrue(result.contains(listAtt));
}
@Test
void getSingularAttributesGetsAlsoInheritedSingularAttributes() {
final SingularAttributeImpl attOne = mock(SingularAttributeImpl.class);
when(attOne.isCollection()).thenReturn(false);
final SingularAttributeImpl attTwo = mock(SingularAttributeImpl.class);
when(attTwo.isCollection()).thenReturn(false);
final AbstractIdentifiableType<? super OWLClassA> supertype = spy(new MappedSuperclassTypeImpl<>(Object.class));
et.setSupertype(supertype);
et.addDeclaredAttribute("test", attTwo);
when(supertype.getSingularAttributes()).thenReturn(Collections.singleton(attOne));
final Set<SingularAttribute<? super OWLClassA, ?>> result = et.getSingularAttributes();
assertTrue(result.contains(attOne));
assertTrue(result.contains(attTwo));
}
@Test
void getSingularAttributeReturnsAttributeWithMatchingNameAndType() {
final SingularAttributeImpl attOne = mock(SingularAttributeImpl.class);
when(attOne.isCollection()).thenReturn(false);
final String attName = "test";
when(attOne.getJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, attOne);
final SingularAttribute result = et.getSingularAttribute(attName, OWLClassA.class);
assertEquals(attOne, result);
}
@Test
void getSingularAttributeThrowsIllegalArgumentWhenAttributeIsNotSingular() {
final ListAttributeImpl att = mock(ListAttributeImpl.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
when(att.isCollection()).thenReturn(true);
et.addDeclaredAttribute(attName, att);
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> et.getSingularAttribute(attName, OWLClassB.class));
assertEquals("Singular attribute " + attName + " of type " + OWLClassB.class + " is not present in type " +
et.toString(), ex.getMessage());
}
@Test
void getDeclaredAttributeReturnsAttributeDeclaredInType() {
final AbstractAttribute attOne = mock(AbstractAttribute.class);
when(attOne.isCollection()).thenReturn(false);
final String attName = "test";
when(attOne.getJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, attOne);
final Attribute result = et.getDeclaredAttribute(attName);
assertEquals(attOne, result);
}
// Disabled this test until we have a CollectionAttribute implementation (it will probably be SetAttributeImpl, cause set semantics is
// default in ontologies and Set is compatible with Collection)
@Disabled
@Test
void getDeclaredCollectionReturnsDeclaredCollectionAttribute() {
final AbstractPluralAttribute att = mock(AbstractPluralAttribute.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, att);
final CollectionAttribute result = et.getDeclaredCollection(attName);
assertEquals(att, result);
}
@Test
void getDeclaredListThrowsIllegalArgumentWhenListElementTypeDoesNotMatch() {
final ListAttributeImpl att = mock(ListAttributeImpl.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
when(att.isCollection()).thenReturn(true);
et.addDeclaredAttribute(attName, att);
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> et.getDeclaredList(attName, OWLClassB.class));
assertEquals(
"List attribute " + attName + " with element type " + OWLClassB.class + " is not declared in type " +
et.toString(), ex.getMessage());
}
@Test
void getDeclaredSetThrowsIllegalArgumentWhenAttributeIsNotSet() {
final ListAttributeImpl att = mock(ListAttributeImpl.class);
final String attName = "test";
when(att.getBindableJavaType()).thenReturn(OWLClassA.class);
when(att.isCollection()).thenReturn(true);
et.addDeclaredAttribute(attName, att);
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> et.getDeclaredSet(attName, OWLClassB.class));
assertEquals(
"Set attribute " + attName + " with element type " + OWLClassB.class + " is not declared in type " +
et.toString(), ex.getMessage());
}
@Test
void getDeclaredSingularAttributeReturnsDeclaredSingularAttribute() {
final SingularAttributeImpl att = mock(SingularAttributeImpl.class);
final String attName = "test";
when(att.isCollection()).thenReturn(false);
when(att.getJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, att);
final SingularAttribute result = et.getDeclaredSingularAttribute(attName);
assertEquals(att, result);
}
@Test
void getDeclaredSingularAttributeThrowsIllegalArgumentForInvalidType() {
final AbstractAttribute att = mock(AbstractAttribute.class);
final String attName = "test";
when(att.isCollection()).thenReturn(false);
when(att.getJavaType()).thenReturn(OWLClassA.class);
et.addDeclaredAttribute(attName, att);
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> et.getDeclaredSingularAttribute(attName, OWLClassB.class));
assertEquals("Singular attribute " + attName + " of type " + OWLClassB.class + " is not declared in type " +
et.toString(), ex.getMessage());
}
@Test
void getFieldSpecificationsReturnsAllAttributesAndTypesAndProperties() {
final SingularAttribute supertypeAtt = mock(SingularAttribute.class);
final AbstractIdentifiableType<? super OWLClassA> supertype = spy(new MappedSuperclassTypeImpl<>(Object.class));
et.setSupertype(supertype);
when(supertype.getAttributes()).thenReturn(Collections.singleton(supertypeAtt));
final ListAttributeImpl listAtt = mock(ListAttributeImpl.class);
et.addDeclaredAttribute("list", listAtt);
final TypesSpecification types = mock(TypesSpecification.class);
et.addDirectTypes(types);
final PropertiesSpecification properties = mock(PropertiesSpecification.class);
et.addOtherProperties(properties);
final Set<FieldSpecification<? super OWLClassA, ?>> result = et.getFieldSpecifications();
assertTrue(result.contains(supertypeAtt));
assertTrue(result.contains(listAtt));
assertTrue(result.contains(types));
assertTrue(result.contains(properties));
}
@Test
void getFieldSpecificationsReturnsAlsoIdentifier() {
final Set<FieldSpecification<? super OWLClassA, ?>> result = et.getFieldSpecifications();
final Optional<FieldSpecification<? super OWLClassA, ?>> id = result.stream()
.filter(fs -> fs.equals(et.getIdentifier()))
.findAny();
assertTrue(id.isPresent());
}
@Test
void getTypesReturnsTypesAlsoFromSuperType() {
final AbstractIdentifiableType<? super OWLClassA> supertype = spy(new MappedSuperclassTypeImpl<>(Object.class));
et.setSupertype(supertype);
final TypesSpecification types = mock(TypesSpecification.class);
when(supertype.getTypes()).thenReturn(types);
final TypesSpecification result = et.getTypes();
assertEquals(types, result);
}
@Test
void getPropertiesReturnsPropertiesAlsoFromSuperType() {
final AbstractIdentifiableType<? super OWLClassA> supertype = spy(new MappedSuperclassTypeImpl<>(Object.class));
et.setSupertype(supertype);
final PropertiesSpecification properties = mock(PropertiesSpecification.class);
when(supertype.getProperties()).thenReturn(properties);
final PropertiesSpecification result = et.getProperties();
assertEquals(properties, result);
}
@Test
void getFieldSpecificationGetsFieldSpecification() {
final SingularAttribute supertypeAtt = mock(SingularAttribute.class);
final AbstractIdentifiableType<? super OWLClassA> supertype = spy(new MappedSuperclassTypeImpl<>(Object.class));
et.setSupertype(supertype);
final String attName = "test";
doReturn(supertypeAtt).when(supertype).getFieldSpecification(attName);
final FieldSpecification<? super OWLClassA, ?> result = et.getFieldSpecification(attName);
assertEquals(supertypeAtt, result);
}
@Test
void getFieldSpecificationGetsTypes() {
final TypesSpecification types = mock(TypesSpecification.class);
final String attName = "types";
when(types.getName()).thenReturn(attName);
et.addDirectTypes(types);
final FieldSpecification<? super OWLClassA, ?> result = et.getFieldSpecification(attName);
assertEquals(types, result);
}
@Test
void getFieldSpecificationGetsDeclaredAttribute() {
final AbstractPluralAttribute att = mock(AbstractPluralAttribute.class);
final String attName = "test";
et.addDeclaredAttribute(attName, att);
final FieldSpecification<? super OWLClassA, ?> result = et.getFieldSpecification(attName);
assertEquals(att, result);
}
@Test
void getFieldSpecificationThrowsIllegalArgumentWhenFieldIsNotFound() {
final String attName = "unknownAttribute";
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> et.getFieldSpecification(attName));
assertEquals("Field " + attName + " is not present in type " + et.toString(), ex.getMessage());
}
@Test
void getIdentifierReturnsIdentifierFromSuperclass() {
final Identifier id = mock(Identifier.class);
final AbstractIdentifiableType<? super OWLClassA> supertype = spy(new MappedSuperclassTypeImpl<>(Object.class));
doReturn(id).when(supertype).getIdentifier();
et.setSupertype(supertype);
et.setIdentifier(null);
assertEquals(id, et.getIdentifier());
}
@Test
void getFieldSpecificationReturnsIdentifier() {
final FieldSpecification<? super OWLClassA, ?> idSpec = et.getFieldSpecification("uri");
assertTrue(idSpec instanceof Identifier);
}
}