-
Notifications
You must be signed in to change notification settings - Fork 14
/
ListTestUtil.java
121 lines (107 loc) · 4.97 KB
/
ListTestUtil.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
package cz.cvut.kbss.ontodriver.jena.list;
import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
import cz.cvut.kbss.ontodriver.jena.environment.Generator;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.apache.jena.rdf.model.ResourceFactory.createResource;
import static org.apache.jena.rdf.model.ResourceFactory.createStatement;
import static org.mockito.Mockito.when;
public class ListTestUtil {
private static final int SIZE = 5;
private final Resource owner;
private final Property hasList;
private final Property hasNext;
private final StorageConnector connectorMock;
private Property hasContent;
ListTestUtil(Resource owner, Property hasList, Property hasNext, StorageConnector connectorMock) {
this.owner = owner;
this.hasList = hasList;
this.hasNext = hasNext;
this.connectorMock = connectorMock;
}
void setHasContent(Property hasContent) {
this.hasContent = hasContent;
}
List<URI> generateSimpleList() {
final List<URI> list = generateList();
URI previous = null;
for (final URI node : list) {
if (previous != null) {
final Resource prevResource = createResource(previous.toString());
when(connectorMock.find(prevResource, hasNext, null)).thenReturn(Collections
.singletonList(createStatement(prevResource, hasNext, createResource(node.toString()))));
}
previous = node;
}
when(connectorMock.find(owner, hasList, null)).thenReturn(
Collections.singletonList(createStatement(owner, hasList, createResource(list.get(0).toString()))));
return list;
}
private static List<URI> generateList() {
return IntStream.range(0, SIZE).mapToObj(i -> Generator.generateUri()).collect(Collectors.toList());
}
List<URI> generateSimpleList(String context) {
final List<URI> list = generateList();
URI previous = null;
for (final URI node : list) {
if (previous != null) {
final Resource prevResource = createResource(previous.toString());
when(connectorMock.find(prevResource, hasNext, null, context)).thenReturn(Collections
.singletonList(createStatement(prevResource, hasNext, createResource(node.toString()))));
}
previous = node;
}
when(connectorMock.find(owner, hasList, null, context)).thenReturn(
Collections.singletonList(createStatement(owner, hasList, createResource(list.get(0).toString()))));
return list;
}
List<URI> generateReferencedList() {
final List<URI> list = generateList();
Resource firstNode = null;
URI previous = null;
for (final URI content : list) {
final Resource node = createResource(Generator.generateUri().toString());
if (previous != null) {
final Resource prevResource = createResource(previous.toString());
when(connectorMock.find(prevResource, hasNext, null)).thenReturn(Collections
.singletonList(createStatement(prevResource, hasNext, node)));
}
when(connectorMock.find(node, hasContent, null)).thenReturn(
Collections.singletonList(createStatement(node, hasContent, createResource(content.toString()))));
if (firstNode == null) {
firstNode = node;
}
previous = content;
}
when(connectorMock.find(owner, hasList, null))
.thenReturn(Collections.singletonList(createStatement(owner, hasList, firstNode)));
return list;
}
List<URI> generateReferencedList(String context) {
final List<URI> list = generateList();
Resource firstNode = null;
URI previous = null;
for (final URI content : list) {
final Resource node = createResource(Generator.generateUri().toString());
if (previous != null) {
final Resource prevResource = createResource(previous.toString());
when(connectorMock.find(prevResource, hasNext, null, context)).thenReturn(Collections
.singletonList(createStatement(prevResource, hasNext, node)));
}
when(connectorMock.find(node, hasContent, null, context)).thenReturn(
Collections.singletonList(createStatement(node, hasContent, createResource(content.toString()))));
if (firstNode == null) {
firstNode = node;
}
previous = content;
}
when(connectorMock.find(owner, hasList, null, context))
.thenReturn(Collections.singletonList(createStatement(owner, hasList, firstNode)));
return list;
}
}