20
20
import org .culturegraph .mf .types .Triple ;
21
21
import org .culturegraph .mf .types .Triple .ObjectType ;
22
22
import org .culturegraph .mf .util .StreamConstants ;
23
+ import org .junit .After ;
24
+ import org .junit .Before ;
23
25
import org .junit .Test ;
26
+ import org .mockito .Mock ;
24
27
import org .mockito .Mockito ;
28
+ import org .mockito .MockitoAnnotations ;
25
29
26
30
/**
27
31
* Tests {@link StreamToTriples}
@@ -36,15 +40,27 @@ public final class StreamToTriplesTest {
36
40
private static final String ENTITY_NAME = "ename" ;
37
41
private static final String REC_ID = "id" ;
38
42
private static final String REC_ALT_ID = "altid" ;
39
-
43
+ private static final String RECORD_PREDICATE = "rec_pred" ;
44
+
45
+ private StreamToTriples toTriples ;
46
+
47
+ @ Mock
48
+ private ObjectReceiver <Triple > receiver ;
49
+
50
+ @ Before
51
+ public void setup () {
52
+ MockitoAnnotations .initMocks (this );
53
+ toTriples = new StreamToTriples ();
54
+ toTriples .setReceiver (receiver );
55
+ }
56
+
57
+ @ After
58
+ public void cleanup () {
59
+ toTriples .closeStream ();
60
+ }
61
+
40
62
@ Test
41
63
public void testShouldBuildTripleFromLiteral () {
42
- final StreamToTriples toTriples = new StreamToTriples ();
43
- @ SuppressWarnings ("unchecked" )
44
- final ObjectReceiver <Triple > receiver = Mockito .mock (ObjectReceiver .class );
45
-
46
- toTriples .setReceiver (receiver );
47
-
48
64
toTriples .startRecord (REC_ID );
49
65
toTriples .literal (NAME , VALUE );
50
66
toTriples .endRecord ();
@@ -54,12 +70,6 @@ public void testShouldBuildTripleFromLiteral() {
54
70
55
71
@ Test
56
72
public void testShouldEncodeEntities () {
57
- final StreamToTriples toTriples = new StreamToTriples ();
58
- @ SuppressWarnings ("unchecked" )
59
- final ObjectReceiver <Triple > receiver = Mockito .mock (ObjectReceiver .class );
60
-
61
- toTriples .setReceiver (receiver );
62
-
63
73
toTriples .startRecord (REC_ID );
64
74
toTriples .startEntity (ENTITY_NAME );
65
75
toTriples .literal (NAME , VALUE );
@@ -69,20 +79,19 @@ public void testShouldEncodeEntities() {
69
79
toTriples .endEntity ();
70
80
toTriples .endRecord ();
71
81
72
- Mockito .verify (receiver ).process (
73
- new Triple (REC_ID , ENTITY_NAME , Formeta .GROUP_START +NAME + Formeta .NAME_VALUE_SEPARATOR + VALUE
74
- + Formeta .ITEM_SEPARATOR + ENTITY_NAME + Formeta .GROUP_START + NAME
75
- + Formeta .NAME_VALUE_SEPARATOR + VALUE + Formeta .GROUP_END + Formeta .GROUP_END ,
76
- ObjectType .ENTITY ));
82
+ final String objectValue =
83
+ Formeta .GROUP_START +
84
+ NAME + Formeta .NAME_VALUE_SEPARATOR + VALUE + Formeta .ITEM_SEPARATOR +
85
+ ENTITY_NAME + Formeta .GROUP_START +
86
+ NAME + Formeta .NAME_VALUE_SEPARATOR + VALUE +
87
+ Formeta .GROUP_END +
88
+ Formeta .GROUP_END ;
89
+ Mockito .verify (receiver ).process (new Triple (REC_ID , ENTITY_NAME , objectValue , ObjectType .ENTITY ));
77
90
}
78
91
79
92
@ Test
80
93
public void testShouldRedirectOnMoveToInName () {
81
- final StreamToTriples toTriples = new StreamToTriples ();
82
94
toTriples .setRedirect (true );
83
- @ SuppressWarnings ("unchecked" )
84
- final ObjectReceiver <Triple > receiver = Mockito .mock (ObjectReceiver .class );
85
- toTriples .setReceiver (receiver );
86
95
87
96
toTriples .startRecord (REC_ID );
88
97
toTriples .literal ("{to:" + REC_ALT_ID + "}" + NAME , VALUE );
@@ -93,11 +102,7 @@ public void testShouldRedirectOnMoveToInName() {
93
102
94
103
@ Test
95
104
public void testShouldRedirectIfAltIdGiven () {
96
- final StreamToTriples toTriples = new StreamToTriples ();
97
105
toTriples .setRedirect (true );
98
- @ SuppressWarnings ("unchecked" )
99
- final ObjectReceiver <Triple > receiver = Mockito .mock (ObjectReceiver .class );
100
- toTriples .setReceiver (receiver );
101
106
102
107
toTriples .startRecord (REC_ID );
103
108
toTriples .literal (StreamConstants .ID , REC_ALT_ID );
@@ -107,4 +112,45 @@ public void testShouldRedirectIfAltIdGiven() {
107
112
Mockito .verify (receiver ).process (new Triple (REC_ALT_ID , NAME , VALUE ));
108
113
}
109
114
115
+ @ Test
116
+ public void testShouldEncodeWholeRecordsIfRecordPredicateIsGiven () {
117
+ toTriples .setRecordPredicate (RECORD_PREDICATE );
118
+
119
+ toTriples .startRecord (REC_ID );
120
+ toTriples .startEntity (ENTITY_NAME );
121
+ toTriples .literal (NAME , VALUE );
122
+ toTriples .endEntity ();
123
+ toTriples .startEntity (ENTITY_NAME );
124
+ toTriples .literal (NAME , VALUE );
125
+ toTriples .endEntity ();
126
+ toTriples .endRecord ();
127
+
128
+ final String objectValue =
129
+ Formeta .GROUP_START +
130
+ ENTITY_NAME + Formeta .GROUP_START +
131
+ NAME + Formeta .NAME_VALUE_SEPARATOR + VALUE +
132
+ Formeta .GROUP_END +
133
+ ENTITY_NAME + Formeta .GROUP_START +
134
+ NAME + Formeta .NAME_VALUE_SEPARATOR + VALUE +
135
+ Formeta .GROUP_END +
136
+ Formeta .GROUP_END ;
137
+ Mockito .verify (receiver ).process (new Triple (REC_ID , RECORD_PREDICATE , objectValue , ObjectType .ENTITY ));
138
+ }
139
+
140
+ @ Test
141
+ public void testShouldRedirectEvenIfRecordPredicateIsGiven () {
142
+ toTriples .setRecordPredicate (RECORD_PREDICATE );
143
+ toTriples .setRedirect (true );
144
+
145
+ toTriples .startRecord (REC_ID );
146
+ toTriples .literal (StreamConstants .ID , REC_ALT_ID );
147
+ toTriples .literal (NAME , VALUE );
148
+ toTriples .endRecord ();
149
+
150
+ final String objectValue =
151
+ Formeta .GROUP_START +
152
+ NAME + Formeta .NAME_VALUE_SEPARATOR + VALUE +
153
+ Formeta .GROUP_END ;
154
+ Mockito .verify (receiver ).process (new Triple (REC_ALT_ID , RECORD_PREDICATE , objectValue ));
155
+ }
110
156
}
0 commit comments