-
Notifications
You must be signed in to change notification settings - Fork 14
/
AxiomValueGathererTest.java
190 lines (168 loc) · 8.43 KB
/
AxiomValueGathererTest.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
/**
* Copyright (C) 2016 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.jopa.oom;
import cz.cvut.kbss.ontodriver.Connection;
import cz.cvut.kbss.ontodriver.Lists;
import cz.cvut.kbss.ontodriver.Types;
import cz.cvut.kbss.ontodriver.Properties;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.model.Value;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class AxiomValueGathererTest {
private static final NamedResource SUBJECT = NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa#Subject");
private static final Assertion DATA_ASSERTION = Assertion.createDataPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#dataProperty"), false);
private static final Assertion OBJECT_ASSERTION = Assertion.createObjectPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#dataProperty"), false);
private static final URI CONTEXT = URI.create("http://krizik.felk.cvutcz/ontologies/jopa#contextOne");
@Mock
private Connection connectionMock;
@Mock
private Lists listsMock;
@Mock
private Types typesMock;
@Mock
private Properties propertiesMock;
private AxiomValueGatherer gatherer;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
when(connectionMock.lists()).thenReturn(listsMock);
when(connectionMock.types()).thenReturn(typesMock);
when(connectionMock.properties()).thenReturn(propertiesMock);
this.gatherer = new AxiomValueGatherer(SUBJECT, null);
}
@Test
public void testAddValue() throws Exception {
final Value<String> val = addValue();
final Field axDescField = AxiomValueGatherer.class.getDeclaredField("axiomDescriptor");
axDescField.setAccessible(true);
final AxiomValueDescriptor desc = (AxiomValueDescriptor) axDescField.get(gatherer);
assertNotNull(desc);
final List<Value<?>> values = desc.getAssertionValues(DATA_ASSERTION);
assertNotNull(values);
assertEquals(1, values.size());
assertTrue(values.contains(val));
assertEquals(CONTEXT, desc.getAssertionContext(DATA_ASSERTION));
}
private Value<String> addValue() {
final Value<String> val = new Value<>("SomeValue");
gatherer.addValue(DATA_ASSERTION, val, CONTEXT);
return val;
}
@Test
public void testAddPropertiesTwice() throws Exception {
final Map<Assertion, Set<Value<?>>> propsOne = new HashMap<>();
propsOne.put(DATA_ASSERTION, new HashSet<>(Arrays.asList(new Value<>("one"), new Value<>("two"))));
gatherer.addProperties(propsOne, null);
final Map<Assertion, Set<Value<?>>> propsTwo = new HashMap<>();
propsTwo.put(OBJECT_ASSERTION, Collections.<Value<?>>singleton(new Value<>("http://krizik.felk.cvut.cz/pp")));
propsTwo.put(DATA_ASSERTION, Collections.<Value<?>>singleton(new Value<>("117")));
gatherer.addProperties(propsTwo, null);
final Map<Assertion, Set<Value<?>>> res = OOMTestUtils.getPropertiesToAdd(gatherer);
verifyResultContainsAll(res, propsOne);
verifyResultContainsAll(res, propsTwo);
}
private void verifyResultContainsAll(Map<Assertion, Set<Value<?>>> res, Map<Assertion, Set<Value<?>>> props) {
for (Map.Entry<Assertion, Set<Value<?>>> e : props.entrySet()) {
assertTrue(res.containsKey(e.getKey()));
assertTrue(res.get(e.getKey()).containsAll(e.getValue()));
}
}
@Test
public void testPersist() throws Exception {
addValue();
final SimpleListValueDescriptor simpleD = mock(SimpleListValueDescriptor.class);
final ReferencedListValueDescriptor referencedD = mock(ReferencedListValueDescriptor.class);
gatherer.addSimpleListValues(simpleD);
gatherer.addReferencedListValues(referencedD);
final Set<URI> typesToAdd = typesToAdd();
gatherer.addTypes(typesToAdd, null);
final Map<Assertion, Set<Value<?>>> propsToAdd = propertiesToAdd();
gatherer.addProperties(propsToAdd, null);
gatherer.persist(connectionMock);
verify(connectionMock).persist(any(AxiomValueDescriptor.class));
verify(listsMock).persistSimpleList(simpleD);
verify(listsMock).persistReferencedList(referencedD);
verify(typesMock).addTypes(SUBJECT, null, typesToAdd);
verify(propertiesMock).addProperties(SUBJECT, null, propsToAdd);
}
private Set<URI> typesToAdd() {
final Set<URI> set = new HashSet<>();
set.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#typeOne"));
set.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#typeTwo"));
set.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#typeThre"));
return set;
}
private Map<Assertion, Set<Value<?>>> propertiesToAdd() {
final Map<Assertion, Set<Value<?>>> props = new HashMap<>();
props.put(DATA_ASSERTION, Collections.<Value<?>>singleton(new Value<>("valueOne")));
props.put(OBJECT_ASSERTION, Collections.<Value<?>>singleton(
new Value<>(URI.create("http://krizik.felk.cvut.cz/valueTwo"))));
return props;
}
@Test
public void testUpdate() throws Exception {
addValue();
final SimpleListValueDescriptor simpleD = mock(SimpleListValueDescriptor.class);
final ReferencedListValueDescriptor referencedD = mock(ReferencedListValueDescriptor.class);
gatherer.addSimpleListValues(simpleD);
gatherer.addReferencedListValues(referencedD);
final Set<URI> typesToAdd = typesToAdd();
gatherer.addTypes(typesToAdd, null);
final Set<URI> typesToRemove = typesToRemove();
gatherer.removeTypes(typesToRemove, null);
final Map<Assertion, Set<Value<?>>> propsToAdd = propertiesToAdd();
gatherer.addProperties(propsToAdd, null);
final Map<Assertion, Set<Value<?>>> propsToRemove = propertiesToRemove();
gatherer.removeProperties(propsToRemove, null);
gatherer.update(connectionMock);
verify(connectionMock).update(any(AxiomValueDescriptor.class));
verify(listsMock).updateSimpleList(simpleD);
verify(listsMock).updateReferencedList(referencedD);
verify(typesMock).addTypes(SUBJECT, null, typesToAdd);
verify(typesMock).removeTypes(SUBJECT, null, typesToRemove);
verify(propertiesMock).addProperties(SUBJECT, null, propsToAdd);
verify(propertiesMock).removeProperties(SUBJECT, null, propsToRemove);
}
private Set<URI> typesToRemove() {
final Set<URI> set = new HashSet<>();
set.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#typeFour"));
set.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#typeFive"));
set.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#typeSix"));
return set;
}
private Map<Assertion, Set<Value<?>>> propertiesToRemove() {
final Map<Assertion, Set<Value<?>>> props = new HashMap<>();
props.put(DATA_ASSERTION, Collections.<Value<?>>singleton(new Value<>("valueThree")));
props.put(DATA_ASSERTION, Collections.<Value<?>>singleton(new Value<>("valueFour")));
return props;
}
}