-
Notifications
You must be signed in to change notification settings - Fork 4
/
TabularModuleTest.java
256 lines (199 loc) · 9.92 KB
/
TabularModuleTest.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package cz.cvut.spipes.modules;
import cz.cvut.spipes.config.ExecutionConfig;
import cz.cvut.spipes.constants.CSVW;
import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.engine.ExecutionContextFactory;
import cz.cvut.spipes.exception.ResourceNotUniqueException;
import cz.cvut.spipes.modules.exception.TableSchemaException;
import cz.cvut.spipes.test.JenaTestUtils;
import cz.cvut.spipes.util.StreamResourceUtils;
import org.apache.jena.rdf.model.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import static cz.cvut.spipes.test.JenaTestUtils.assertIsomorphic;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
class TabularModuleTest extends AbstractModuleTestHelper {
private TabularModule module;
private final String DATA_PREFIX = "http://onto.fel.cvut.cz/data/";
private static final Logger LOG = LoggerFactory.getLogger(TabularModuleTest.class);
@BeforeEach
void setUp() {
module = new TabularModule();
module.setReplace(true);
module.setDelimiter('\t');
module.setQuoteCharacter('"');
module.setDataPrefix(DATA_PREFIX);
module.setOutputMode(Mode.STANDARD);
module.setInputContext(ExecutionContextFactory.createEmptyContext());
}
@Test
void executeWithSimpleTransformation() throws URISyntaxException, IOException {
module.setSourceResource(
StreamResourceUtils.getStreamResource(
"http://test-file",
getFilePath("countries.tsv"))
);
ExecutionContext outputContext = module.executeSelf();
assertTrue(outputContext.getDefaultModel().size() > 0);
}
@Test
void executeWithSimpleTransformationXls() throws URISyntaxException, IOException {
module.setSourceResource(
StreamResourceUtils.getStreamResource(
"http://test-file",
getFilePath("countries.xls"))
);
module.setSourceResourceFormat(ResourceFormat.EXCEL);
module.setProcessSpecificSheetInXLSFile(2);
ExecutionContext outputContext = module.executeSelf();
assertTrue(outputContext.getDefaultModel().size() > 0);
}
@Test
void executeWithDuplicateColumnsThrowsResourceNotUniqueException()
throws URISyntaxException, IOException {
module.setSourceResource(StreamResourceUtils.getStreamResource(
"http://test-file-2",
getFilePath("duplicate_column_countries.tsv"))
);
ResourceNotUniqueException exception = assertThrows(
ResourceNotUniqueException.class,
module::executeSelf
);
String expectedMessage = "latitude";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
@Test
void checkDefaultConfigurationAgainstExemplaryModelOutput() throws URISyntaxException, IOException {
module.setSourceResource(
StreamResourceUtils.getStreamResource(
"http://test-file",
getFilePath("countries.tsv"))
);
ExecutionContext outputContext = module.executeSelf();
Model actualModel = outputContext.getDefaultModel();
Model expectedModel = ModelFactory.createDefaultModel()
.read(getFilePath("countries-model-output.ttl").toString());
assertTrue(actualModel.isIsomorphicWith(expectedModel));
}
@DisplayName("Executes Tabular module with or without csvw:property.")
@ParameterizedTest(name = "{index} => message=''Test {0} (csvw:property) in the schema''")
@ValueSource(strings = {"withProperty", "withoutProperty"})
void executeSelfChecksSchemaWithoutProperty(String folderName) throws URISyntaxException, IOException {
module.setSourceResource(
StreamResourceUtils.getStreamResource(DATA_PREFIX, getFilePath("examples/" + folderName + "/input.tsv"))
);
Model inputModel = JenaTestUtils.laodModelFromResource("/examples/" + folderName + "/input-data-schema.ttl");
module.setInputContext(ExecutionContextFactory.createContext(inputModel));
ExecutionContext outputContext = module.executeSelf();
Model actualModel = outputContext.getDefaultModel();
Model expectedModel = ModelFactory.createDefaultModel()
.read(getFilePath("examples/" + folderName + "/expected-output.ttl").toString());
assertTrue(actualModel.isIsomorphicWith(expectedModel));
}
@DisplayName("Executes Tabular module with the different number of columns in the schema.")
@ParameterizedTest(name = "{index} => message=''{0} in the schema''")
@ValueSource(strings = {"moreColumns", "lessColumns", "noColumns"})
void executeSelfThrowsException(String folderName) throws URISyntaxException, IOException {
assumeTrue(ExecutionConfig.isExitOnError());
module.setSourceResource(
StreamResourceUtils.getStreamResource(DATA_PREFIX,getFilePath("examples/" + folderName + "/input.tsv"))
);
Model inputModel = JenaTestUtils.laodModelFromResource("/examples/" + folderName + "/input-data-schema.ttl");
module.setInputContext(ExecutionContextFactory.createContext(inputModel));
assertThrows(TableSchemaException.class, () -> module.executeSelf());
}
@Test
void executeSelfWithDataSchemaNoHeaderReturnsNamedColumnsFromSchema()
throws URISyntaxException, IOException {
module.setSkipHeader(true);
module.setSourceResource(
StreamResourceUtils.getStreamResource(DATA_PREFIX,getFilePath("examples/noHeader/schemaExample/input.csv"))
);
Model inputModel = JenaTestUtils.laodModelFromResource("/examples/noHeader/schemaExample/input-data-schema.ttl");
module.setInputContext(ExecutionContextFactory.createContext(inputModel));
ExecutionContext outputContext = module.executeSelf();
String[] columns = new String[]{"col_1", "col_2", "col_3", "col_4", "col_5"};
for (int i = 2; i <= 4; i++) {
Resource resource = ResourceFactory.createResource(DATA_PREFIX + "#row-" + i);
for (String column: columns) {
Property property = ResourceFactory.createProperty(DATA_PREFIX, column);
assertTrue(outputContext.getDefaultModel().contains(resource, property));
}
}
}
@Test
void executeSelfWithNoDataSchemaNoHeaderReturnsAutonamedColumns()
throws URISyntaxException, IOException {
module.setSkipHeader(true);
module.setSourceResource(
StreamResourceUtils
.getStreamResource(DATA_PREFIX,getFilePath("examples/noHeader/noSchemaExample/input.tsv"))
);
ExecutionContext outputContext = module.executeSelf();
for (int i = 2; i <= 4; i++) {
Resource resource = ResourceFactory.createResource(DATA_PREFIX + "#row-" + i);
for (int j = 1; j <= 6; j++) {
String columnName = "column_" + j;
Property property = ResourceFactory.createProperty(DATA_PREFIX, columnName);
assertTrue(outputContext.getDefaultModel().contains(resource, property));
}
}
}
@Test
void executeSelfWithBNodesInSchema() throws IOException, URISyntaxException {
module.setSourceResource(
StreamResourceUtils.getStreamResource(DATA_PREFIX, getFilePath("examples/blankNodes/input.tsv"))
);
Model inputModel = JenaTestUtils.laodModelFromResource("/examples/blankNodes/input-data-schema.ttl");
module.setInputContext(ExecutionContextFactory.createContext(inputModel));
ExecutionContext outputContext = module.executeSelf();
Model actualModel = outputContext.getDefaultModel();
Model expectedModel = ModelFactory.createDefaultModel()
.read(getFilePath("examples/blankNodes/expected-output.ttl").toString());
assertIsomorphic(actualModel, expectedModel);
}
@Test
void executeSelfWithHTMLFileInput() throws URISyntaxException, IOException {
module.setSourceResourceFormat(ResourceFormat.HTML);
module.setSourceResource(
StreamResourceUtils.getStreamResource(DATA_PREFIX, getFilePath("examples/htmlFile/input.html"))
);
ExecutionContext outputContext = module.executeSelf();
Model actualModel = outputContext.getDefaultModel();
List<String> header = Arrays.asList("No_", "Test_1", "Test_2", "Description");
List<List<String>> rows = Arrays.asList(
Arrays.asList("1.", "123", "456", "description 1"),
Arrays.asList("2.", "789", "123", "description 2"));
header.forEach(headerValue -> assertTrue(actualModel.contains(null, CSVW.name, headerValue)));
for (List<String> row: rows){
for(int idx = 0; idx < header.size(); idx++) {
String headerValue = header.get(idx);
String rowValue = row.get(idx);
assertTrue(actualModel
.contains(null, actualModel.getProperty(DATA_PREFIX + headerValue), rowValue)
);
}
}
}
@Override
public String getModuleName() {
return "tabular";
}
public Path getFilePath(String fileName) throws URISyntaxException {
return Paths.get(getClass().getResource("/" + fileName).toURI());
}
}