-
Notifications
You must be signed in to change notification settings - Fork 14
/
AxiomSaverTest.java
210 lines (191 loc) · 10.4 KB
/
AxiomSaverTest.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
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* 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.
*
* 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.ontodriver.jena;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
import cz.cvut.kbss.ontodriver.jena.environment.Generator;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.LangString;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.model.Value;
import cz.cvut.kbss.ontodriver.util.Vocabulary;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.Statement;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.verify;
public class AxiomSaverTest {
private static final NamedResource SUBJECT = NamedResource.create(Generator.generateUri());
@Mock
private StorageConnector connectorMock;
private AxiomSaver saver;
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
this.saver = new AxiomSaver(connectorMock);
}
@Test
public void saveAxiomsAddsClassAssertionToStorage() {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
final Assertion a = Assertion.createClassAssertion(false);
final NamedResource type = NamedResource.create(Generator.generateUri());
descriptor.addAssertionValue(a, new Value<>(type));
saver.saveAxioms(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(connectorMock).add(captor.capture(), eq(null));
final List<Statement> arg = captor.getValue();
assertEquals(1, arg.size());
assertEquals(ResourceFactory.createResource(SUBJECT.toString()), arg.get(0).getSubject());
assertEquals(ResourceFactory.createProperty(Vocabulary.RDF_TYPE), arg.get(0).getPredicate());
assertEquals(ResourceFactory.createResource(type.getIdentifier().toString()), arg.get(0).getObject());
}
@Test
public void saveAxiomsAddsResourceStatementsForObjectPropertyAssertionAxioms() {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
final Assertion assertion = Assertion.createObjectPropertyAssertion(Generator.generateUri(), false);
final NamedResource value = NamedResource.create(Generator.generateUri());
descriptor.addAssertionValue(assertion, new Value<>(value));
saver.saveAxioms(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(connectorMock).add(captor.capture(), eq(null));
final List<Statement> arg = captor.getValue();
assertEquals(1, arg.size());
assertEquals(ResourceFactory.createProperty(assertion.getIdentifier().toString()), arg.get(0).getPredicate());
assertEquals(ResourceFactory.createResource(value.getIdentifier().toString()), arg.get(0).getObject());
}
@Test
public void saveAxiomsAddsLiteralStatementsForDatatypePropertyAssertionAxioms() {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
final Assertion assertion = Assertion.createDataPropertyAssertion(Generator.generateUri(), "en", false);
final Integer value = 117;
descriptor.addAssertionValue(assertion, new Value<>(value));
saver.saveAxioms(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(connectorMock).add(captor.capture(), eq(null));
final List<Statement> arg = captor.getValue();
assertEquals(1, arg.size());
assertEquals(ResourceFactory.createTypedLiteral(value), arg.get(0).getObject());
}
@Test
public void saveAxiomsCreatesLanguageTaggedLiteralsForDatatypePropertyAssertionsWithLanguageTag() {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
final String lang = "en";
final Assertion assertion = Assertion.createDataPropertyAssertion(Generator.generateUri(), lang, false);
final String value = "english text";
descriptor.addAssertionValue(assertion, new Value<>(value));
saver.saveAxioms(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(connectorMock).add(captor.capture(), eq(null));
final List<Statement> arg = captor.getValue();
assertEquals(1, arg.size());
assertEquals(ResourceFactory.createLangLiteral(value, lang), arg.get(0).getObject());
}
@Test
public void saveAxiomsAddsResourceStatementsForAnnotationPropertyAxiomsWithIriValue() {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
final Assertion assertion = Assertion.createAnnotationPropertyAssertion(Generator.generateUri(), false);
final String value = Generator.generateUri().toString();
descriptor.addAssertionValue(assertion, new Value<>(value));
saver.saveAxioms(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(connectorMock).add(captor.capture(), eq(null));
final List<Statement> arg = captor.getValue();
assertEquals(1, arg.size());
assertEquals(ResourceFactory.createResource(value), arg.get(0).getObject());
}
@Test
public void saveAxiomsAddsLiteralStatementForAnnotationPropertyAxiomWithLiteralValue() {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
final Assertion assertion = Assertion.createAnnotationPropertyAssertion(Generator.generateUri(), false);
final Integer value = 117;
descriptor.addAssertionValue(assertion, new Value<>(value));
saver.saveAxioms(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(connectorMock).add(captor.capture(), eq(null));
final List<Statement> arg = captor.getValue();
assertEquals(1, arg.size());
assertEquals(ResourceFactory.createTypedLiteral(value), arg.get(0).getObject());
}
@Test
public void saveAxiomsAddsCorrectDateTimeLiteralForDateDataPropertyAxiomValue() {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
final Assertion assertion = Assertion.createDataPropertyAssertion(Generator.generateUri(), false);
final Date value = new Date();
descriptor.addAssertionValue(assertion, new Value<>(value));
saver.saveAxioms(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(connectorMock).add(captor.capture(), eq(null));
final List<Statement> arg = captor.getValue();
assertEquals(1, arg.size());
final GregorianCalendar cal = new GregorianCalendar();
cal.setTime(value);
assertEquals(ResourceFactory.createTypedLiteral(cal), arg.get(0).getObject());
}
@Test
public void saveAxiomsAddsStatementsIntoCorrectContextWhenAssertionHasContext() {
final URI context = Generator.generateUri();
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
final Assertion assertion = Assertion.createDataPropertyAssertion(Generator.generateUri(), false);
final Integer value = 117;
descriptor.addAssertionValue(assertion, new Value<>(value));
descriptor.setAssertionContext(assertion, context);
saver.saveAxioms(descriptor);
verify(connectorMock).add(anyList(), eq(context.toString()));
}
@Test
public void saveAxiomsSkipsNullValues() {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
final Assertion assertion = Assertion.createObjectPropertyAssertion(Generator.generateUri(), false);
descriptor.addAssertionValue(assertion, Value.nullValue());
saver.saveAxioms(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(connectorMock).add(captor.capture(), eq(null));
assertTrue(captor.getValue().isEmpty());
}
@Test
void saveAxiomsSavesOntoDriverLangStringAxiomValuesAsRDFLangStringValues() {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
final Assertion assertion = Assertion.createDataPropertyAssertion(Generator.generateUri(), false);
descriptor.addAssertionValue(assertion, new Value<>(new LangString("test", "en")));
saver.saveAxioms(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(connectorMock).add(captor.capture(), eq(null));
final List<Statement> arg = captor.getValue();
assertEquals(1, arg.size());
assertEquals(ResourceFactory.createLangLiteral("test", "en"), arg.get(0).getObject());
}
@Test
void saveAxiomsSavesOntoDriverLangStringAxiomWithoutLanguageAsSimpleLiteral() {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
final Assertion assertion = Assertion.createDataPropertyAssertion(Generator.generateUri(), false);
descriptor.addAssertionValue(assertion, new Value<>(new LangString("test")));
saver.saveAxioms(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(connectorMock).add(captor.capture(), eq(null));
final List<Statement> arg = captor.getValue();
assertEquals(1, arg.size());
assertEquals(ResourceFactory.createPlainLiteral("test"), arg.get(0).getObject());
}
}