-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CypherOverBoltIT.java
283 lines (263 loc) · 12 KB
/
CypherOverBoltIT.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* 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 Affero General Public License for more details.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.bolt;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.neo4j.driver.v1.Config;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.types.Node;
import org.neo4j.harness.junit.Neo4jRule;
import org.neo4j.io.fs.FileUtils;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.server.configuration.ServerSettings;
import org.neo4j.test.rule.SuppressOutput;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class CypherOverBoltIT
{
@Rule
public final SuppressOutput suppressOutput = SuppressOutput.suppressAll();
@Rule
public Neo4jRule graphDb = new Neo4jRule().withConfig( ServerSettings.script_enabled, "true" );
private URL url;
private final int lineCountInCSV = 3; // needs to be >= 2
@Before
public void setUp() throws Exception
{
url = prepareTestImportFile( lineCountInCSV );
}
@Test
public void mixingPeriodicCommitAndLoadCSVShouldWork()
{
for ( int i = lineCountInCSV - 1; i < lineCountInCSV + 1; i++ ) // test with different periodic commit sizes
{
try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() )
{
StatementResult result = session.run( "USING PERIODIC COMMIT " + i + "\n" + "LOAD CSV FROM \"" + url + "\" as row fieldterminator \" \"\n" +
"MERGE (currentnode:Label1 {uuid:row[0]})\n" + "RETURN currentnode;" );
int countOfNodes = 0;
while ( result.hasNext() )
{
Node node = result.next().get( 0 ).asNode();
assertTrue( node.hasLabel( "Label1" ) );
assertEquals( String.valueOf( countOfNodes ), node.get( "uuid" ).asString() );
countOfNodes++;
}
assertEquals( lineCountInCSV, countOfNodes );
session.reset();
}
}
}
@Test
public void mixingPeriodicCommitAndLoadCSVShouldWork2()
{
try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() )
{
StatementResult result = session.run(
"USING PERIODIC COMMIT " + (lineCountInCSV + 1) + "\n" + "LOAD CSV FROM \"" + url + "\" as row fieldterminator \" \"\n" +
"MERGE (currentnode:Label1 {uuid:row[0]})\n" + "RETURN currentnode;" );
int countOfNodes = 0;
while ( result.hasNext() )
{
Node node = result.next().get( 0 ).asNode();
assertTrue( node.hasLabel( "Label1" ) );
assertEquals( String.valueOf( countOfNodes ), node.get( "uuid" ).asString() );
countOfNodes++;
}
assertEquals( lineCountInCSV, countOfNodes );
}
}
@Test
public void mixingPeriodicCommitAndLoadCSVShouldWork3()
{
try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() )
{
StatementResult result = session.run(
"USING PERIODIC COMMIT " + lineCountInCSV + "\n" + "LOAD CSV FROM \"" + url + "\" as row fieldterminator \" \"\n" +
"MERGE (currentnode:Label1 {uuid:row[0]})\n" + "RETURN currentnode;" );
int countOfNodes = 0;
while ( result.hasNext() )
{
Node node = result.next().get( 0 ).asNode();
assertTrue( node.hasLabel( "Label1" ) );
assertEquals( String.valueOf( countOfNodes ), node.get( "uuid" ).asString() );
countOfNodes++;
}
assertEquals( lineCountInCSV, countOfNodes );
}
}
@Test
public void mixingPeriodicCommitAndLoadCSVShouldWorkWithLists() throws Exception
{
try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() )
{
StatementResult result = session.run(
"USING PERIODIC COMMIT " + (lineCountInCSV - 1) + "\n" + "LOAD CSV FROM \"" + url + "\" as row fieldterminator \" \"\n" +
"MERGE (currentnode:Label2 {uuid:row[0]})\n" + "RETURN [currentnode];" );
int countOfNodes = 0;
while ( result.hasNext() )
{
Iterator<Object> iterator = result.next().get( 0 ).asList().iterator();
while ( iterator.hasNext() )
{
Node node = (Node) iterator.next();
assertTrue( node.hasLabel( "Label2" ) );
assertEquals( String.valueOf( countOfNodes ), node.get( "uuid" ).asString() );
countOfNodes++;
}
}
assertEquals( lineCountInCSV, countOfNodes );
}
}
@Test
public void mixingPeriodicCommitAndLoadCSVShouldWorkWithListsOfLists() throws Exception
{
try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() )
{
StatementResult result = session.run(
"USING PERIODIC COMMIT " + (lineCountInCSV - 1) + "\n" + "LOAD CSV FROM \"" + url + "\" as row fieldterminator \" \"\n" +
"MERGE (currentnode:Label3 {uuid:row[0]})\n" + "RETURN [[currentnode]];" );
int countOfNodes = 0;
while ( result.hasNext() )
{
Iterator<Object> iterator = result.next().get( 0 ).asList().iterator(); // iterator over outer list
assertTrue( iterator.hasNext() );
iterator = ((List<Object>) iterator.next()).iterator(); // iterator over inner list
while ( iterator.hasNext() )
{
Node node = (Node) iterator.next();
assertTrue( node.hasLabel( "Label3" ) );
assertEquals( String.valueOf( countOfNodes ), node.get( "uuid" ).asString() );
countOfNodes++;
}
}
assertEquals( lineCountInCSV, countOfNodes );
}
}
@Test
public void mixingPeriodicCommitAndLoadCSVShouldWorkWithMaps() throws Exception
{
try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() )
{
StatementResult result = session.run(
"USING PERIODIC COMMIT " + (lineCountInCSV - 1) + "\n" + "LOAD CSV FROM \"" + url + "\" as row fieldterminator \" \"\n" +
"MERGE (currentnode:Label4 {uuid:row[0]})\n" + "RETURN {node:currentnode};" );
int countOfNodes = 0;
while ( result.hasNext() )
{
Iterator<Map.Entry<String,Object>> iterator = result.next().get( 0 ).asMap().entrySet().iterator();
while ( iterator.hasNext() )
{
Map.Entry<String,Object> entry = iterator.next();
assertEquals( "node", entry.getKey() );
Node node = (Node) entry.getValue();
assertTrue( node.hasLabel( "Label4" ) );
assertEquals( String.valueOf( countOfNodes ), node.get( "uuid" ).asString() );
countOfNodes++;
}
}
assertEquals( lineCountInCSV, countOfNodes );
}
}
@Test
public void mixingPeriodicCommitAndLoadCSVShouldWorkWithMapsWithinMaps() throws Exception
{
try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() )
{
StatementResult result = session.run(
"USING PERIODIC COMMIT " + (lineCountInCSV - 1) + "\n" + "LOAD CSV FROM \"" + url + "\" as row fieldterminator \" \"\n" +
"MERGE (currentnode:Label5 {uuid:row[0]})\n" + "RETURN {outer:{node:currentnode}};" );
int countOfNodes = 0;
while ( result.hasNext() )
{
Iterator<Map.Entry<String,Object>> iterator = result.next().get( 0 ).asMap().entrySet().iterator();
assertTrue( iterator.hasNext() );
iterator = ((Map<String,Object>) iterator.next().getValue()).entrySet().iterator();
while ( iterator.hasNext() )
{
Map.Entry<String,Object> entry = iterator.next();
assertEquals( "node", entry.getKey() );
Node node = (Node) entry.getValue();
assertTrue( node.hasLabel( "Label5" ) );
assertEquals( String.valueOf( countOfNodes ), node.get( "uuid" ).asString() );
countOfNodes++;
}
}
assertEquals( lineCountInCSV, countOfNodes );
}
}
@Test
public void mixingPeriodicCommitAndLoadCSVShouldWorkWithMapsWithLists() throws Exception
{
try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() )
{
StatementResult result = session.run(
"USING PERIODIC COMMIT " + (lineCountInCSV - 1) + "\n" + "LOAD CSV FROM \"" + url + "\" as row fieldterminator \" \"\n" +
"MERGE (currentnode:Label6 {uuid:row[0]})\n" + "RETURN {outer:[currentnode]};" );
int countOfNodes = 0;
while ( result.hasNext() )
{
Iterator<Map.Entry<String,Object>> mapIterator = result.next().get( 0 ).asMap().entrySet().iterator();
assertTrue( mapIterator.hasNext() );
Iterator<Object> iterator = ((List<Object>) mapIterator.next().getValue()).iterator();
while ( iterator.hasNext() )
{
Node node = (Node) iterator.next();
assertTrue( node.hasLabel( "Label6" ) );
assertEquals( String.valueOf( countOfNodes ), node.get( "uuid" ).asString() );
countOfNodes++;
}
}
assertEquals( lineCountInCSV, countOfNodes );
}
}
private Config configuration()
{
return Config.build().withEncryptionLevel( Config.EncryptionLevel.NONE ).toConfig();
}
private URL prepareTestImportFile( int lines ) throws IOException
{
File tempFile = File.createTempFile( "testImport", ".csv" );
try ( PrintWriter writer = FileUtils.newFilePrintWriter( tempFile, StandardCharsets.UTF_8 ) )
{
for ( int i = 0; i < lines; i++ )
{
writer.println( i + " " + i + " " + i );
}
}
return tempFile.toURI().toURL();
}
}