1
+ package cz .cvut .kbss .jsonld .deserialization .reference ;
2
+
3
+ import cz .cvut .kbss .jsonld .environment .Generator ;
4
+ import cz .cvut .kbss .jsonld .environment .model .Employee ;
5
+ import cz .cvut .kbss .jsonld .environment .model .User ;
6
+ import cz .cvut .kbss .jsonld .exception .UnknownPropertyException ;
7
+ import org .junit .jupiter .api .Test ;
8
+
9
+ import java .net .URI ;
10
+ import java .util .ArrayList ;
11
+
12
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
13
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
14
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
15
+ import static org .junit .jupiter .api .Assertions .assertSame ;
16
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
17
+
18
+ class AssumedTypeReferenceReplacerTest {
19
+
20
+ private final PendingReferenceRegistry registry = new PendingReferenceRegistry ();
21
+
22
+ private final AssumedTypeReferenceReplacer sut = new AssumedTypeReferenceReplacer ();
23
+
24
+ @ Test
25
+ void replacePendingReferencesWithAssumedTypedObjectsReplacesPendingSingularReferenceWithObjectWithId () throws Exception {
26
+ final URI id = Generator .generateUri ();
27
+ final Employee target = Generator .generateEmployee ();
28
+ target .setEmployer (null );
29
+ registry .addPendingReference (id .toString (), target , Employee .getEmployerField ());
30
+ sut .replacePendingReferencesWithAssumedTypedObjects (registry );
31
+ assertNotNull (target .getEmployer ());
32
+ assertEquals (id , target .getEmployer ().getUri ());
33
+ }
34
+
35
+ @ Test
36
+ void replacePendingReferenceReplacesAllReferencesWithSameObject () throws Exception {
37
+ final URI id = Generator .generateUri ();
38
+ final Employee targetOne = Generator .generateEmployee ();
39
+ targetOne .setEmployer (null );
40
+ registry .addPendingReference (id .toString (), targetOne , Employee .getEmployerField ());
41
+ final Employee targetTwo = Generator .generateEmployee ();
42
+ targetTwo .setEmployer (null );
43
+ registry .addPendingReference (id .toString (), targetTwo , Employee .getEmployerField ());
44
+ sut .replacePendingReferencesWithAssumedTypedObjects (registry );
45
+ assertNotNull (targetOne .getEmployer ());
46
+ assertEquals (id , targetOne .getEmployer ().getUri ());
47
+ assertNotNull (targetTwo .getEmployer ());
48
+ assertSame (targetOne .getEmployer (), targetTwo .getEmployer ());
49
+ }
50
+
51
+ @ Test
52
+ void replacePendingReferencesThrowsUnknownPropertyExceptionWhenTargetTypeDoesNotHaveIdentifierField () throws Exception {
53
+ final URI id = Generator .generateUri ();
54
+ final Employee target = Generator .generateEmployee ();
55
+ registry .addPendingReference (id .toString (), target , User .getUsernameField ());
56
+ assertThrows (UnknownPropertyException .class , () -> sut .replacePendingReferencesWithAssumedTypedObjects (registry ));
57
+ }
58
+
59
+ @ Test
60
+ void replacePendingReferencesSkipsPendingReferencesWithoutClearTargetType () {
61
+ final URI id = Generator .generateUri ();
62
+ registry .addPendingReference (id .toString (), new ArrayList <>());
63
+
64
+ sut .replacePendingReferencesWithAssumedTypedObjects (registry );
65
+ assertFalse (registry .getPendingReferences ().isEmpty ());
66
+ }
67
+ }
0 commit comments