/
NodeSetTest.java
213 lines (172 loc) · 7.25 KB
/
NodeSetTest.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
/* Copyright (c) 2017 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Gabriel Roldan (Boundless) - initial implementation
*/
package org.locationtech.geogig.storage.datastream.v2_3;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import org.locationtech.geogig.model.Node;
import org.locationtech.geogig.model.ObjectId;
import org.locationtech.geogig.model.RevObject.TYPE;
import org.locationtech.geogig.model.impl.RevObjectTestSupport;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
public class NodeSetTest {
private ByteArrayOutputStream buff;
private DataOutput out;
private StringTable stringTable;
@Before
public void before() {
buff = new ByteArrayOutputStream();
out = ByteStreams.newDataOutput(buff);// this one does not need flush
stringTable = StringTable.unique();
// add some initial content to the buffer to avoid zero index matching by coincidence
buff.write(1);
buff.write(2);
// add some garbage to the string table to avoid index matches by coincidence
stringTable.add("fake string 1");
stringTable.add("fake string 2");
}
@Test
public void emptyList() throws IOException {
encodedDecode(featureNodes(0), TYPE.FEATURE);
}
@Test
public void singleFeature() throws IOException {
encodedDecode(featureNodes(1), TYPE.FEATURE);
}
@Test
public void severalFeatures() throws IOException {
encodedDecode(nodes(TYPE.FEATURE, 1024, false, false, false), TYPE.FEATURE);
encodedDecode(nodes(TYPE.FEATURE, 1024, false, false, true), TYPE.FEATURE);
encodedDecode(nodes(TYPE.FEATURE, 1024, false, true, true), TYPE.FEATURE);
encodedDecode(nodes(TYPE.FEATURE, 1024, true, true, true), TYPE.FEATURE);
encodedDecode(nodes(TYPE.FEATURE, 1024, true, false, false), TYPE.FEATURE);
encodedDecode(nodes(TYPE.FEATURE, 1024, true, true, false), TYPE.FEATURE);
}
@Test
public void severalFeaturesWithExtraData() throws IOException {
encodedDecode(featureNodesWithExtraData(1024), TYPE.FEATURE);
}
@Test
public void emptyTreeList() throws IOException {
encodedDecode(treeNodes(0), TYPE.TREE);
}
@Test
public void singleTree() throws IOException {
encodedDecode(treeNodes(1), TYPE.TREE);
}
@Test
public void singleSeveralTrees() throws IOException {
encodedDecode(treeNodes(1024), TYPE.TREE);
}
@Test
public void testLazyNodeGetExtaData() throws IOException {
final Map<String, Object> expected = extraData(0);
final Node originalNode = Node.create("feature-0", RevObjectTestSupport.hashString("1"),
ObjectId.NULL, TYPE.FEATURE, null, expected);
NodeSet nodeset = encodedDecode(Collections.singletonList(originalNode), TYPE.FEATURE);
Node lazyNode = nodeset.build().get(0);
Map<String, Object> extraData = lazyNode.getExtraData();
assertEquals(expected, extraData);
}
@Test
public void testLazyNodeGetExtaDataValue() throws IOException {
final Map<String, Object> expected = extraData(0);
final Node originalNode = Node.create("feature-0", RevObjectTestSupport.hashString("1"),
ObjectId.NULL, TYPE.FEATURE, null, expected);
assertEquals(expected.size(), originalNode.getExtraData().size());
assertEquals(expected, originalNode.getExtraData());
NodeSet nodeset = encodedDecode(Collections.singletonList(originalNode), TYPE.FEATURE);
Node lazyNode = nodeset.build().get(0);
for (Map.Entry<String, Object> e : expected.entrySet()) {
String key = e.getKey();
Object value = e.getValue();
Object lazyVal = lazyNode.getExtraData(key);
assertEquals("key=" + key, value, lazyVal);
}
}
private NodeSet encodedDecode(List<Node> nodes, TYPE type) throws IOException {
final int offset = buff.size();
NodeSet.encode(out, nodes, stringTable);
byte[] rawData = buff.toByteArray();
DataBuffer data = DataBuffer.wrap(rawData, stringTable);
NodeSet decoded = NodeSet.decode(data, offset, type);
assertEquals(type, decoded.getType());
ImmutableList<Node> built = decoded.build();
TestSupport.assertEqualsFully(nodes, built);
return decoded;
}
private List<Node> featureNodes(int count) {
return nodes(TYPE.FEATURE, count, true, true, false);
}
private List<Node> featureNodesWithExtraData(int count) {
return nodes(TYPE.FEATURE, count, true, true, true);
}
private List<Node> treeNodes(int count) {
return nodes(TYPE.TREE, count, true, true, false);
}
private List<Node> nodes(final TYPE type, final int count, boolean withMetadataId,
boolean withBounds, boolean withExtraData) {
List<Node> nodes = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
String name;
ObjectId oid;
ObjectId metadataId;
Envelope bounds = null;
Map<String, Object> extraData;
name = "node-" + i;
oid = RevObjectTestSupport.hashString(name);
metadataId = withMetadataId ? RevObjectTestSupport.hashString(name + "md")
: ObjectId.NULL;
if (withBounds) {
boolean point = i % 2 == 0;
double x = i / 1000d, y = i / 1000d;
bounds = point ? new Envelope(x, x, y, y) : new Envelope(x, x + 1, y, y + 1);
}
extraData = withExtraData ? extraData(i) : null;
Node node = Node.create(name, oid, metadataId, type, bounds, extraData);
nodes.add(node);
}
return nodes;
}
private Map<String, Object> extraData(int i) {
Map<String, Object> map = new HashMap<>();
map.put("prop1", Integer.valueOf(i));
map.put("prop2", "String prop value " + i);
map.put("uuid", UUID.randomUUID());
map.put("duplicatedValue", "same value for all");
Geometry geom;
try {
geom = new WKTReader().read(String.format("LINESTRING(0 0, %d %d)", i, i));
} catch (ParseException e) {
throw new RuntimeException(e);
}
map.put("mapProp", ImmutableMap.of("k1", "v1-" + i, //
"k2", "v2-" + i, //
"k3", Long.valueOf(i), //
"nested-geom", geom));
map.put("geom", geom);
return map;
}
}