-
Notifications
You must be signed in to change notification settings - Fork 15
/
TwoStepInstanceLoaderTest.java
180 lines (154 loc) · 8.36 KB
/
TwoStepInstanceLoaderTest.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
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* 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.
* <p>
* 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.jopa.environment.OWLClassA;
import cz.cvut.kbss.jopa.environment.OWLClassR;
import cz.cvut.kbss.jopa.environment.OWLClassS;
import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks;
import cz.cvut.kbss.jopa.exceptions.StorageAccessException;
import cz.cvut.kbss.jopa.sessions.LoadingParameters;
import cz.cvut.kbss.ontodriver.Types;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.AxiomImpl;
import cz.cvut.kbss.ontodriver.model.Value;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class TwoStepInstanceLoaderTest extends InstanceLoaderTestBase {
@Mock
private Types typesMock;
private LoadingParameters<OWLClassS> loadingParameters;
@BeforeAll
static void setUpBeforeClass() {
staticSetup();
}
@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
this.loadingParameters = new LoadingParameters<>(OWLClassS.class, IDENTIFIER, descriptor);
final MetamodelMocks mocks = new MetamodelMocks();
mocks.setMocks(metamodelMock);
when(connectionMock.types()).thenReturn(typesMock);
when(descriptorFactoryMock.createForEntityLoading(loadingParameters, mocks.forOwlClassR().entityType()))
.thenReturn(axiomDescriptor);
this.instanceLoader = TwoStepInstanceLoader.builder().connection(connectionMock).metamodel(metamodelMock)
.cache(cacheMock).descriptorFactory(descriptorFactoryMock)
.entityBuilder(entityConstructorMock).build();
}
@Test
void loadEntityLoadsTypesAndDeterminesEntityTypeBeforeLoadingInstance() throws Exception {
final Set<Axiom<URI>> types = Collections.singleton(
new AxiomImpl<>(INDIVIDUAL, Assertion.createClassAssertion(false),
new Value<>(URI.create(OWLClassR.getClassIri()))));
when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(types);
instanceLoader.loadEntity(loadingParameters);
verify(typesMock).getTypes(INDIVIDUAL, null, false);
verify(descriptorFactoryMock).createForEntityLoading(loadingParameters, metamodelMock.entity(OWLClassR.class));
}
@Test
void loadEntityLoadsEntityFromStorageWhenEntityTypeIsDetermined() throws Exception {
final Set<Axiom<URI>> types = Collections.singleton(
new AxiomImpl<>(INDIVIDUAL, Assertion.createClassAssertion(false),
new Value<>(URI.create(OWLClassR.getClassIri()))));
when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(types);
final OWLClassR entityR = new OWLClassR();
final Collection<Axiom<?>> axioms = new HashSet<>(types);
when(connectionMock.find(axiomDescriptor)).thenReturn(axioms);
when(entityConstructorMock
.reconstructEntity(IDENTIFIER, metamodelMock.entity(OWLClassR.class), descriptor, axioms))
.thenReturn(entityR);
final OWLClassS result = instanceLoader.loadEntity(loadingParameters);
assertNotNull(result);
assertSame(entityR, result);
}
@Test
void loadEntityReturnsNullWhenNoTypesForIndividualAreFound() throws Exception {
when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(Collections.emptySet());
assertNull(instanceLoader.loadEntity(loadingParameters));
}
@Test
void loadEntityReturnsNullWhenNoMatchingEntityTypeIsFound() throws Exception {
final Set<Axiom<URI>> types = Collections.singleton(
new AxiomImpl<>(INDIVIDUAL, Assertion.createClassAssertion(false),
new Value<>(URI.create(OWLClassA.getClassIri()))));
when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(types);
assertNull(instanceLoader.loadEntity(loadingParameters));
}
@Test
void loadEntityThrowsStorageAccessExceptionWhenOntoDriverThrowsException() throws Exception {
final String msg = "Exception message.";
when(typesMock.getTypes(INDIVIDUAL, null, false)).thenThrow(new OntoDriverException(msg));
final StorageAccessException ex =
assertThrows(StorageAccessException.class, () -> instanceLoader.loadEntity(loadingParameters));
assertThat(ex.getMessage(), containsString(msg));
}
@Test
void loadReferenceLoadsReferenceFromStorageWhenEntityTypeIsDetermined() throws Exception {
final Axiom<URI> type = new AxiomImpl<>(INDIVIDUAL, Assertion.createClassAssertion(false),
new Value<>(URI.create(OWLClassA.getClassIri())));
when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(Collections.singleton(type));
when(entityConstructorMock.createEntityInstance(IDENTIFIER, metamodelMock.entity(OWLClassA.class)))
.thenReturn(new OWLClassA(IDENTIFIER));
final OWLClassA result =
instanceLoader.loadReference(new LoadingParameters<>(OWLClassA.class, IDENTIFIER, descriptor));
assertNotNull(result);
verify(typesMock).getTypes(INDIVIDUAL, null, false);
}
@Test
void loadReferenceReturnsNullWhenTypeCannotBeResolved() throws Exception {
when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(Collections.emptySet());
when(entityConstructorMock.createEntityInstance(IDENTIFIER, metamodelMock.entity(OWLClassA.class)))
.thenReturn(new OWLClassA(IDENTIFIER));
final OWLClassA result =
instanceLoader.loadReference(new LoadingParameters<>(OWLClassA.class, IDENTIFIER, descriptor));
assertNull(result);
verify(entityConstructorMock, never()).createEntityInstance(any(), any());
}
@Test
void loadReferenceThrowsStorageAccessExceptionWhenOntoDriverExceptionIsThrown() throws Exception {
final String msg = "Exception message.";
when(typesMock.getTypes(INDIVIDUAL, null, false)).thenThrow(new OntoDriverException(msg));
final StorageAccessException ex =
assertThrows(StorageAccessException.class, () -> instanceLoader.loadReference(loadingParameters));
assertThat(ex.getMessage(), containsString(msg));
verify(entityConstructorMock, never()).createEntityInstance(any(), any());
}
@Test
void loadReferenceReturnsCachedInstanceWhenItExists() throws Exception {
final Axiom<URI> type = new AxiomImpl<>(INDIVIDUAL, Assertion.createClassAssertion(false),
new Value<>(URI.create(OWLClassA.getClassIri())));
when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(Collections.singleton(type));
when(cacheMock.contains(OWLClassA.class, IDENTIFIER, descriptor)).thenReturn(true);
when(cacheMock.get(OWLClassA.class, IDENTIFIER, descriptor)).thenReturn(new OWLClassA(IDENTIFIER));
final OWLClassA result =
instanceLoader.loadReference(new LoadingParameters<>(OWLClassA.class, IDENTIFIER, descriptor));
assertNotNull(result);
verify(cacheMock).get(OWLClassA.class, IDENTIFIER, descriptor);
verify(entityConstructorMock, never()).createEntityInstance(any(), any());
}
}