/
TestSourceBuilder.java
187 lines (155 loc) · 5.96 KB
/
TestSourceBuilder.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
package org.aksw.rdfunit.sources;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.BufferedInputStream;
import java.util.Collection;
import java.util.Collections;
import org.aksw.rdfunit.exceptions.UndefinedSerializationException;
import org.aksw.rdfunit.io.format.FormatService;
import org.aksw.rdfunit.io.format.SerializationFormat;
import org.aksw.rdfunit.io.format.SerializationFormatGraphType;
import org.aksw.rdfunit.io.reader.RdfReader;
import org.aksw.rdfunit.io.reader.RdfReaderFactory;
import org.aksw.rdfunit.io.reader.RdfStreamReader;
/**
* TestSource builder
*
* @author Dimitris Kontokostas
* @since 8/19/15 11:58 PM
*/
public class TestSourceBuilder {
private TestSourceType testSourceType = TestSourceType.InMemSingle;
private SourceConfig sourceConfig = null;
private Collection<SchemaSource> referenceSchemata = null;
private QueryingConfig queryingConfig = null;
private RdfReader inMemReader = null;
private String sparqlEndpoint = null;
private Collection<String> endpointGraphs = null;
private String endpointUsername = null;
private String endpointPassword = null;
public TestSourceBuilder setPrefixUri(String prefix, String uri) {
this.sourceConfig = new SourceConfig(prefix, uri);
return this;
}
public TestSourceBuilder setEndpoint(String sparqlEndpoint, Collection<String> endpointGraphs) {
return setEndpoint(sparqlEndpoint, endpointGraphs, "", "");
}
public TestSourceBuilder setEndpoint(String sparqlEndpoint, Collection<String> endpointGraphs,
String username, String password) {
testSourceType = TestSourceType.Endpoint;
this.sparqlEndpoint = sparqlEndpoint.trim();
this.endpointGraphs = endpointGraphs;
this.endpointUsername = username.trim();
this.endpointPassword = password.trim();
if (queryingConfig == null) {
queryingConfig = QueryingConfig.createEndpoint();
}
return this;
}
public TestSourceBuilder setImMemSingle() {
testSourceType = TestSourceType.InMemSingle;
if (queryingConfig == null) {
queryingConfig = QueryingConfig.createInMemory();
}
return this;
}
public TestSourceBuilder setImMemDataset() {
testSourceType = TestSourceType.InMemDataset;
if (queryingConfig == null) {
queryingConfig = QueryingConfig.createInMemory();
}
return this;
}
public TestSourceBuilder setImMemFromUri(String uri) {
SerializationFormat format = FormatService
.getInputFormat(FormatService.getFormatFromExtension(uri));
if (format != null && format.getGraphType().equals(SerializationFormatGraphType.dataset)) {
setImMemDataset();
} else {
setImMemSingle();
}
this.inMemReader = RdfReaderFactory.createDereferenceReader(uri);
return this;
}
public TestSourceBuilder setInMemFromPipe() {
this.inMemReader = new RdfStreamReader(new BufferedInputStream(System.in), "TURTLE");
setImMemSingle();
return this;
}
public TestSourceBuilder setInMemFromCustomText(String customTextSource, String customTextFormat)
throws UndefinedSerializationException {
SerializationFormat format = FormatService.getInputFormat(customTextFormat);
if (format == null) {
throw new UndefinedSerializationException(customTextFormat);
}
this.inMemReader = RdfReaderFactory.createReaderFromText(customTextSource, format.getName());
if (queryingConfig == null) {
queryingConfig = QueryingConfig.createInMemory();
}
return this;
}
public TestSourceBuilder setReferenceSchemata(Collection<SchemaSource> referenceSchemata) {
this.referenceSchemata = referenceSchemata;
return this;
}
public TestSourceBuilder setReferenceSchemata(SchemaSource referenceSchema) {
this.referenceSchemata = Collections.singletonList(referenceSchema);
return this;
}
public TestSourceBuilder setCacheTTL(long cacheTTL) {
queryingConfig = queryingConfig.copyWithNewCacheTTL(cacheTTL);
return this;
}
public TestSourceBuilder setQueryLimit(long queryLimit) {
queryingConfig = queryingConfig.copyWithNewQueryLimit(queryLimit);
return this;
}
public TestSourceBuilder setQueryDelay(long queryDelay) {
queryingConfig = queryingConfig.copyWithNewQueryDelay(queryDelay);
return this;
}
public TestSourceBuilder setPagination(long pagination) {
queryingConfig = queryingConfig.copyWithNewPagination(pagination);
return this;
}
public TestSource build() {
checkNotNull(sourceConfig, "Source configuration not set in TestSourceBuilder");
checkNotNull(referenceSchemata, "Referenced schemata not set in TestSourceBuilder");
if (testSourceType.equals(TestSourceType.Endpoint)) {
if (queryingConfig == null) {
queryingConfig = QueryingConfig.createEndpoint();
}
checkNotNull(sparqlEndpoint);
checkNotNull(endpointGraphs);
return new EndpointTestSource(sourceConfig, queryingConfig, referenceSchemata, sparqlEndpoint,
endpointGraphs, endpointUsername, endpointPassword);
}
if (queryingConfig == null) {
queryingConfig = QueryingConfig.createEndpoint();
}
checkNotNull(inMemReader);
if (testSourceType.equals(TestSourceType.InMemSingle)) {
return new DumpTestSource(sourceConfig, queryingConfig, referenceSchemata, inMemReader);
}
if (testSourceType.equals(TestSourceType.InMemDataset)) {
return new DatasetTestSource(sourceConfig, queryingConfig, referenceSchemata, inMemReader);
}
throw new IllegalStateException("Should not be here");
}
public String getSparqlEndpoint() {
return sparqlEndpoint;
}
public Collection<String> getEndpointGraphs() {
return endpointGraphs;
}
public RdfReader getInMemReader() {
return this.inMemReader;
}
public TestSourceBuilder setInMemReader(RdfReader reader) {
this.inMemReader = reader;
if (queryingConfig == null) {
queryingConfig = QueryingConfig.createInMemory();
}
return this;
}
private enum TestSourceType {Endpoint, InMemSingle, InMemDataset}
}