-
Notifications
You must be signed in to change notification settings - Fork 14
/
AxiomSaverTest.java
152 lines (136 loc) · 7.29 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
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.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.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyListOf;
import static org.mockito.Matchers.eq;
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;
@Before
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());
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());
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(), 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());
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());
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());
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());
final List<Statement> arg = captor.getValue();
assertEquals(1, arg.size());
assertEquals(ResourceFactory.createTypedLiteral(value), 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(anyListOf(Statement.class), 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());
assertTrue(captor.getValue().isEmpty());
}
}