/
ImportToolTest.java
2257 lines (2002 loc) · 82.2 KB
/
ImportToolTest.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.tooling;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.IntPredicate;
import java.util.function.Predicate;
import org.neo4j.csv.reader.IllegalMultilineFieldException;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.collection.FilteringIterator;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.helpers.collection.PrefetchingIterator;
import org.neo4j.io.fs.FileUtils;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.format.standard.StandardV3_0;
import org.neo4j.kernel.impl.util.Validator;
import org.neo4j.kernel.impl.util.Validators;
import org.neo4j.kernel.internal.Version;
import org.neo4j.test.rule.EmbeddedDatabaseRule;
import org.neo4j.test.rule.RandomRule;
import org.neo4j.test.rule.SuppressOutput;
import org.neo4j.unsafe.impl.batchimport.cache.idmapping.string.DuplicateInputIdException;
import org.neo4j.unsafe.impl.batchimport.input.InputException;
import org.neo4j.unsafe.impl.batchimport.input.csv.Configuration;
import org.neo4j.unsafe.impl.batchimport.input.csv.Type;
import static org.apache.commons.lang3.StringUtils.repeat;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;
import static java.util.Arrays.asList;
import static org.neo4j.graphdb.Label.label;
import static org.neo4j.graphdb.RelationshipType.withName;
import static org.neo4j.helpers.ArrayUtil.join;
import static org.neo4j.helpers.Exceptions.contains;
import static org.neo4j.helpers.Exceptions.withMessage;
import static org.neo4j.helpers.collection.Iterators.asSet;
import static org.neo4j.helpers.collection.Iterators.count;
import static org.neo4j.helpers.collection.MapUtil.store;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
import static org.neo4j.tooling.ImportTool.MULTI_FILE_DELIMITER;
import static org.neo4j.unsafe.impl.batchimport.Configuration.BAD_FILE_NAME;
public class ImportToolTest
{
private static final int RELATIONSHIP_COUNT = 10_000;
private static final int NODE_COUNT = 100;
private static final IntPredicate TRUE = i -> true;
@Rule
public final EmbeddedDatabaseRule dbRule = new EmbeddedDatabaseRule( getClass() ).startLazily();
@Rule
public final RandomRule random = new RandomRule();
@Rule
public final SuppressOutput suppressOutput = SuppressOutput.suppress( SuppressOutput.System.values() );
private int dataIndex;
@Test
public void usageMessageIncludeExample() throws Exception
{
SuppressOutput.Voice outputVoice = suppressOutput.getOutputVoice();
importTool( "?" );
assertTrue( "Usage message should include example section, but was:" + outputVoice,
outputVoice.containsMessage( "Example:" ) );
}
@Test
public void usageMessagePrintedOnEmptyInputParameters() throws Exception
{
SuppressOutput.Voice outputVoice = suppressOutput.getOutputVoice();
importTool();
assertTrue( "Output should include usage section, but was:" + outputVoice,
outputVoice.containsMessage( "Example:" ) );
}
@Test
public void shouldImportWithAsManyDefaultsAsAvailable() throws Exception
{
// GIVEN
List<String> nodeIds = nodeIds();
Configuration config = Configuration.COMMAS;
// WHEN
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--nodes", nodeData( true, config, nodeIds, TRUE ).getAbsolutePath(),
"--relationships", relationshipData( true, config, nodeIds, TRUE, true ).getAbsolutePath() );
// THEN
verifyData();
}
@Test
public void shouldImportWithHeadersBeingInSeparateFiles() throws Exception
{
// GIVEN
List<String> nodeIds = nodeIds();
Configuration config = Configuration.TABS;
// WHEN
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--delimiter", "TAB",
"--array-delimiter", String.valueOf( config.arrayDelimiter() ),
"--nodes",
nodeHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER +
nodeData( false, config, nodeIds, TRUE ).getAbsolutePath(),
"--relationships",
relationshipHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER +
relationshipData( false, config, nodeIds, TRUE, true ).getAbsolutePath() );
// THEN
verifyData();
}
@Test
public void import4097Labels() throws Exception
{
// GIVEN
File header = file( fileName( "4097labels-header.csv" ) );
try ( PrintStream writer = new PrintStream( header ) )
{
writer.println( ":LABEL" );
}
File data = file( fileName( "4097labels.csv" ) );
try ( PrintStream writer = new PrintStream( data ) )
{
// Need to have unique names in order to get unique ids for labels. Want 4096 unique label ids present.
for ( int i = 0; i < 4096; i++ )
{
writer.println( "SIMPLE" + i );
}
// Then insert one with 3 array entries which will get ids greater than 4096. These cannot be inlined
// due 36 bits being divided into 3 parts of 12 bits each and 4097 > 2^12, thus these labels will be
// need to be dynamic records.
writer.println( "FIRST 4096|SECOND 4096|" );
}
// WHEN
importTool( "--into", dbRule.getStoreDirAbsolutePath(),
"--delimiter", "TAB",
"--array-delimiter", "|",
"--nodes", header.getAbsolutePath() + MULTI_FILE_DELIMITER + data.getAbsolutePath() );
// THEN
try ( Transaction tx = dbRule.beginTx() )
{
long nodeCount = Iterables.count( dbRule.getAllNodes() );
assertEquals( 4097, nodeCount );
tx.success();
ResourceIterator<Node> nodes = dbRule.findNodes( DynamicLabel.label( "FIRST 4096" ) );
assertEquals( 1, Iterators.asList( nodes ).size() );
nodes = dbRule.findNodes( DynamicLabel.label( "SECOND 4096" ) );
assertEquals( 1, Iterators.asList( nodes ).size() );
}
}
@Test
public void shouldIgnoreWhitespaceAroundIntegers() throws Exception
{
// GIVEN
// Faster to do all successful in one import than in N separate tests
List<String> values = Arrays.asList( "17", " 21", "99 ", " 34 ", "-34", " -12", "-92 " );
File data = file( fileName( "whitespace.csv" ) );
try ( PrintStream writer = new PrintStream( data ) )
{
writer.println( ":LABEL,name,s:short,b:byte,i:int,l:long,f:float,d:double" );
// For each test value
for ( String value : values )
{
// Save value as a String in name
writer.print( "PERSON,'" + value + "'" );
// For each numerical type
for ( int j = 0; j < 6; j++ )
{
writer.print( "," + value );
}
// End line
writer.println();
}
}
// WHEN
importTool( "--into", dbRule.getStoreDirAbsolutePath(), "--quote", "'", "--nodes", data.getAbsolutePath() );
// THEN
int nodeCount = 0;
try ( Transaction tx = dbRule.beginTx() )
{
for ( Node node : dbRule.getAllNodes() )
{
nodeCount++;
String name = (String) node.getProperty( "name" );
String expected = name.trim();
assertEquals( 7, node.getAllProperties().size() );
for ( String key : node.getPropertyKeys() )
{
if ( key.equals( "name" ) )
{
continue;
}
else if ( key.equals( "f" ) || key.equals( "d" ) )
{
// Floating points have decimals
expected = String.valueOf( Double.parseDouble( expected ) );
}
assertEquals( "Wrong value for " + key, expected, node.getProperty( key ).toString() );
}
}
tx.success();
}
assertEquals( values.size(), nodeCount );
}
@Test
public void shouldIgnoreWhitespaceAroundDecimalNumbers() throws Exception
{
// GIVEN
// Faster to do all successful in one import than in N separate tests
List<String> values = Arrays.asList( "1.0", " 3.5", "45.153 ", " 925.12 ", "-2.121", " -3.745",
"-412.153 ", " -5.12 " );
File data = file( fileName( "whitespace.csv" ) );
try ( PrintStream writer = new PrintStream( data ) )
{
writer.println( ":LABEL,name,f:float,d:double" );
// For each test value
for ( String value : values )
{
// Save value as a String in name
writer.print( "PERSON,'" + value + "'" );
// For each numerical type
for ( int j = 0; j < 2; j++ )
{
writer.print( "," + value );
}
// End line
writer.println();
}
}
// WHEN
importTool( "--into", dbRule.getStoreDirAbsolutePath(), "--quote", "'", "--nodes", data.getAbsolutePath() );
// THEN
int nodeCount = 0;
try ( Transaction tx = dbRule.beginTx() )
{
for ( Node node : dbRule.getAllNodes() )
{
nodeCount++;
String name = (String) node.getProperty( "name" );
double expected = Double.parseDouble( name.trim() );
assertEquals( 3, node.getAllProperties().size() );
for ( String key : node.getPropertyKeys() )
{
if ( key.equals( "name" ) )
{
continue;
}
assertTrue( "Wrong value for " + key,
expected == Double.valueOf( node.getProperty( key ).toString() ) );
}
}
tx.success();
}
assertEquals( values.size(), nodeCount );
}
@Test
public void shouldIgnoreWhitespaceAroundBooleans() throws Exception
{
// GIVEN
File data = file( fileName( "whitespace.csv" ) );
try ( PrintStream writer = new PrintStream( data ) )
{
writer.println( ":LABEL,name,adult:boolean" );
writer.println( "PERSON,'t1',true" );
writer.println( "PERSON,'t2', true" );
writer.println( "PERSON,'t3',true " );
writer.println( "PERSON,'t4', true " );
writer.println( "PERSON,'f1',false" );
writer.println( "PERSON,'f2', false" );
writer.println( "PERSON,'f3',false " );
writer.println( "PERSON,'f4', false " );
writer.println( "PERSON,'f5', truebutactuallyfalse " );
writer.println( "PERSON,'f6', non true things are interpreted as false " );
}
// WHEN
importTool( "--into", dbRule.getStoreDirAbsolutePath(), "--quote", "'", "--nodes", data.getAbsolutePath() );
// THEN
try ( Transaction tx = dbRule.beginTx() )
{
for ( Node node : dbRule.getAllNodes() )
{
String name = (String) node.getProperty( "name" );
if ( name.startsWith( "t" ) )
{
assertTrue( "Wrong value on " + name, (boolean) node.getProperty( "adult" ) );
}
else
{
assertFalse( "Wrong value on " + name, (boolean) node.getProperty( "adult" ) );
}
}
long nodeCount = Iterables.count( dbRule.getAllNodes() );
assertEquals( 10, nodeCount );
tx.success();
}
}
@Test
public void shouldIgnoreWhitespaceInAndAroundIntegerArrays() throws Exception
{
// GIVEN
// Faster to do all successful in one import than in N separate tests
String[] values = new String[]{ " 17", "21", "99 ", " 34 ", "-34", " -12", "-92 " };
File data = writeArrayCsv(
new String[]{ "s:short[]", "b:byte[]", "i:int[]", "l:long[]", "f:float[]", "d:double[]" }, values );
// WHEN
importTool( "--into", dbRule.getStoreDirAbsolutePath(), "--quote", "'", "--nodes", data.getAbsolutePath() );
// THEN
// Expected value for integer types
String iExpected = "[";
for ( String value : values )
{
iExpected += value.trim() + ", ";
}
iExpected = iExpected.substring( 0, iExpected.length() - 2 ) + "]";
// Expected value for floating point types
String fExpected = "[";
for ( String value : values )
{
fExpected += Double.valueOf( value.trim() ) + ", ";
}
fExpected = fExpected.substring( 0, fExpected.length() - 2 ) + "]";
int nodeCount = 0;
try ( Transaction tx = dbRule.beginTx() )
{
for ( Node node : dbRule.getAllNodes() )
{
nodeCount++;
assertEquals( 6, node.getAllProperties().size() );
for ( String key : node.getPropertyKeys() )
{
Object things = node.getProperty( key );
String result = "";
String expected = iExpected;
switch ( key )
{
case "s":
result = Arrays.toString( (short[]) things );
break;
case "b":
result = Arrays.toString( (byte[]) things );
break;
case "i":
result = Arrays.toString( (int[]) things );
break;
case "l":
result = Arrays.toString( (long[]) things );
break;
case "f":
result = Arrays.toString( (float[]) things );
expected = fExpected;
break;
case "d":
result = Arrays.toString( (double[]) things );
expected = fExpected;
break;
default:
break;
}
assertEquals( expected, result );
}
}
tx.success();
}
assertEquals( 1, nodeCount );
}
@Test
public void shouldIgnoreWhitespaceInAndAroundDecimalArrays() throws Exception
{
// GIVEN
// Faster to do all successful in one import than in N separate tests
String[] values =
new String[]{ "1.0", " 3.5", "45.153 ", " 925.12 ", "-2.121", " -3.745", "-412.153 ",
" -5.12 " };
File data = writeArrayCsv( new String[]{ "f:float[]", "d:double[]" }, values );
// WHEN
importTool( "--into", dbRule.getStoreDirAbsolutePath(), "--quote", "'", "--nodes", data.getAbsolutePath() );
// THEN
String expected = "[";
for ( String value : values )
{
expected += value.trim() + ", ";
}
expected = expected.substring( 0, expected.length() - 2 ) + "]";
int nodeCount = 0;
try ( Transaction tx = dbRule.beginTx() )
{
for ( Node node : dbRule.getAllNodes() )
{
nodeCount++;
assertEquals( 2, node.getAllProperties().size() );
for ( String key : node.getPropertyKeys() )
{
Object things = node.getProperty( key );
String result = "";
switch ( key )
{
case "f":
result = Arrays.toString( (float[]) things );
break;
case "d":
result = Arrays.toString( (double[]) things );
break;
default:
break;
}
assertEquals( expected, result );
}
}
tx.success();
}
assertEquals( 1, nodeCount );
}
@Test
public void shouldIgnoreWhitespaceInAndAroundBooleanArrays() throws Exception
{
// GIVEN
// Faster to do all successful in one import than in N separate tests
String[] values =
new String[]{ "true", " true", "true ", " true ", " false ", "false ", " false", "bla bla",
" truebutnotreally " };
String expected = "[";
for ( String value : values )
{
if ( value.contains( "bla" ) )
{
expected += "false, ";
}
else if ( value.contains( "notreally" ) )
{
expected += "false]";
}
else
{
expected += value.trim() + ", ";
}
}
File data = writeArrayCsv( new String[]{ "b:boolean[]" }, values );
// WHEN
importTool( "--into", dbRule.getStoreDirAbsolutePath(), "--quote", "'", "--nodes", data.getAbsolutePath() );
// THEN
int nodeCount = 0;
try ( Transaction tx = dbRule.beginTx() )
{
for ( Node node : dbRule.getAllNodes() )
{
nodeCount++;
assertEquals( 1, node.getAllProperties().size() );
for ( String key : node.getPropertyKeys() )
{
Object things = node.getProperty( key );
String result = Arrays.toString( (boolean[]) things );
assertEquals( expected, result );
}
}
tx.success();
}
assertEquals( 1, nodeCount );
}
@Test
public void shouldFailIfHeaderHasLessColumnsThanData() throws Exception
{
// GIVEN
List<String> nodeIds = nodeIds();
Configuration config = Configuration.TABS;
// WHEN data file contains more columns than header file
int extraColumns = 3;
try
{
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--delimiter", "TAB",
"--array-delimiter", String.valueOf( config.arrayDelimiter() ),
"--nodes", nodeHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER +
nodeData( false, config, nodeIds, TRUE, Charset.defaultCharset(), extraColumns )
.getAbsolutePath(),
"--relationships", relationshipHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER +
relationshipData( false, config, nodeIds, TRUE, true ).getAbsolutePath() );
fail( "Should have thrown exception" );
}
catch ( InputException e )
{
// THEN
assertFalse( suppressOutput.getErrorVoice().containsMessage( e.getClass().getName() ) );
assertTrue( e.getMessage().contains( "Extra column not present in header on line" ) );
}
}
@Test
public void shouldWarnIfHeaderHasLessColumnsThanDataWhenToldTo() throws Exception
{
// GIVEN
List<String> nodeIds = nodeIds();
Configuration config = Configuration.TABS;
File bad = badFile();
// WHEN data file contains more columns than header file
int extraColumns = 3;
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--bad", bad.getAbsolutePath(),
"--bad-tolerance", Integer.toString( nodeIds.size() * extraColumns ),
"--ignore-extra-columns",
"--delimiter", "TAB",
"--array-delimiter", String.valueOf( config.arrayDelimiter() ),
"--nodes", nodeHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER +
nodeData( false, config, nodeIds, TRUE, Charset.defaultCharset(), extraColumns )
.getAbsolutePath(),
"--relationships", relationshipHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER +
relationshipData( false, config, nodeIds, TRUE, true ).getAbsolutePath() );
// THEN
String badContents = FileUtils.readTextFile( bad, Charset.defaultCharset() );
assertTrue( badContents.contains( "Extra column not present in header on line" ) );
}
@Test
public void shouldImportSplitInputFiles() throws Exception
{
// GIVEN
List<String> nodeIds = nodeIds();
Configuration config = Configuration.COMMAS;
// WHEN
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--nodes", // One group with one header file and one data file
nodeHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER +
nodeData( false, config, nodeIds, lines( 0, NODE_COUNT / 2 ) ).getAbsolutePath(),
"--nodes", // One group with two data files, where the header sits in the first file
nodeData( true, config, nodeIds,
lines( NODE_COUNT / 2, NODE_COUNT * 3 / 4 ) ).getAbsolutePath() + MULTI_FILE_DELIMITER +
nodeData( false, config, nodeIds, lines( NODE_COUNT * 3 / 4, NODE_COUNT ) ).getAbsolutePath(),
"--relationships",
relationshipHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER +
relationshipData( false, config, nodeIds, TRUE, true ).getAbsolutePath() );
// THEN
verifyData();
}
@Test
public void shouldImportMultipleInputsWithAddedLabelsAndDefaultRelationshipType() throws Exception
{
// GIVEN
List<String> nodeIds = nodeIds();
Configuration config = Configuration.COMMAS;
final String[] firstLabels = {"AddedOne", "AddedTwo"};
final String[] secondLabels = {"AddedThree"};
final String firstType = "TYPE_1";
final String secondType = "TYPE_2";
// WHEN
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--nodes:" + join( firstLabels, ":" ),
nodeData( true, config, nodeIds, lines( 0, NODE_COUNT / 2 ) ).getAbsolutePath(),
"--nodes:" + join( secondLabels, ":" ),
nodeData( true, config, nodeIds, lines( NODE_COUNT / 2, NODE_COUNT ) ).getAbsolutePath(),
"--relationships:" + firstType,
relationshipData( true, config, nodeIds, lines( 0, RELATIONSHIP_COUNT / 2 ), false ).getAbsolutePath(),
"--relationships:" + secondType,
relationshipData( true, config, nodeIds,
lines( RELATIONSHIP_COUNT / 2, RELATIONSHIP_COUNT ), false ).getAbsolutePath() );
// THEN
verifyData(
node -> {
if ( node.getId() < NODE_COUNT / 2 )
{
assertNodeHasLabels( node, firstLabels );
}
else
{
assertNodeHasLabels( node, secondLabels );
}
},
relationship -> {
if ( relationship.getId() < RELATIONSHIP_COUNT / 2 )
{
assertEquals( firstType, relationship.getType().name() );
}
else
{
assertEquals( secondType, relationship.getType().name() );
}
} );
}
@Test
public void shouldImportOnlyNodes() throws Exception
{
// GIVEN
List<String> nodeIds = nodeIds();
Configuration config = Configuration.COMMAS;
// WHEN
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--nodes", nodeData( true, config, nodeIds, TRUE ).getAbsolutePath() );
// no relationships
// THEN
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
try ( Transaction tx = db.beginTx() )
{
int nodeCount = 0;
for ( Node node : db.getAllNodes() )
{
assertTrue( node.hasProperty( "name" ) );
nodeCount++;
assertFalse( node.hasRelationship() );
}
assertEquals( NODE_COUNT, nodeCount );
tx.success();
}
}
@Test
public void shouldImportGroupsOfOverlappingIds() throws Exception
{
// GIVEN
List<String> groupOneNodeIds = asList( "1", "2", "3" );
List<String> groupTwoNodeIds = asList( "4", "5", "2" );
List<RelationshipDataLine> rels = asList(
relationship( "1", "4", "TYPE" ),
relationship( "2", "5", "TYPE" ),
relationship( "3", "2", "TYPE" ) );
Configuration config = Configuration.COMMAS;
String groupOne = "Actor";
String groupTwo = "Movie";
// WHEN
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--nodes", nodeHeader( config, groupOne ) + MULTI_FILE_DELIMITER +
nodeData( false, config, groupOneNodeIds, TRUE ),
"--nodes", nodeHeader( config, groupTwo ) + MULTI_FILE_DELIMITER +
nodeData( false, config, groupTwoNodeIds, TRUE ),
"--relationships", relationshipHeader( config, groupOne, groupTwo, true ) + MULTI_FILE_DELIMITER +
relationshipData( false, config, rels.iterator(), TRUE, true ) );
// THEN
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
try ( Transaction tx = db.beginTx() )
{
int nodeCount = 0;
for ( Node node : db.getAllNodes() )
{
assertTrue( node.hasProperty( "name" ) );
nodeCount++;
assertEquals( 1, Iterables.count( node.getRelationships() ) );
}
assertEquals( 6, nodeCount );
tx.success();
}
}
@Test
public void shouldNotBeAbleToMixSpecifiedAndUnspecifiedGroups() throws Exception
{
// GIVEN
List<String> groupOneNodeIds = asList( "1", "2", "3" );
List<String> groupTwoNodeIds = asList( "4", "5", "2" );
Configuration config = Configuration.COMMAS;
// WHEN
try
{
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--nodes", nodeHeader( config, "MyGroup" ).getAbsolutePath() + MULTI_FILE_DELIMITER +
nodeData( false, config, groupOneNodeIds, TRUE ).getAbsolutePath(),
"--nodes", nodeHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER +
nodeData( false, config, groupTwoNodeIds, TRUE ).getAbsolutePath() );
fail( "Should have failed" );
}
catch ( Exception e )
{
assertExceptionContains( e, "Mixing specified", IllegalStateException.class );
}
}
@Test
public void shouldImportWithoutTypeSpecifiedInRelationshipHeaderbutWithDefaultTypeInArgument() throws Exception
{
// GIVEN
List<String> nodeIds = nodeIds();
Configuration config = Configuration.COMMAS;
String type = randomType();
// WHEN
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--nodes", nodeData( true, config, nodeIds, TRUE ).getAbsolutePath(),
// there will be no :TYPE specified in the header of the relationships below
"--relationships:" + type,
relationshipData( true, config, nodeIds, TRUE, false ).getAbsolutePath() );
// THEN
verifyData();
}
@Test
public void shouldIncludeSourceInformationInNodeIdCollisionError() throws Exception
{
// GIVEN
List<String> nodeIds = asList( "a", "b", "c", "d", "e", "f", "a", "g" );
Configuration config = Configuration.COMMAS;
File nodeHeaderFile = nodeHeader( config );
File nodeData1 = nodeData( false, config, nodeIds, lines( 0, 4 ) );
File nodeData2 = nodeData( false, config, nodeIds, lines( 4, nodeIds.size() ) );
// WHEN
try
{
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--nodes", nodeHeaderFile.getAbsolutePath() + MULTI_FILE_DELIMITER +
nodeData1.getAbsolutePath() + MULTI_FILE_DELIMITER +
nodeData2.getAbsolutePath() );
fail( "Should have failed with duplicate node IDs" );
}
catch ( Exception e )
{
// THEN
assertExceptionContains( e, nodeData1.getPath() + ":" + 1, DuplicateInputIdException.class );
assertExceptionContains( e, nodeData2.getPath() + ":" + 3, DuplicateInputIdException.class );
}
}
@Test
public void shouldSkipDuplicateNodesIfToldTo() throws Exception
{
// GIVEN
List<String> nodeIds = asList( "a", "b", "c", "d", "e", "f", "a", "g" );
Configuration config = Configuration.COMMAS;
File nodeHeaderFile = nodeHeader( config );
File nodeData1 = nodeData( false, config, nodeIds, lines( 0, 4 ) );
File nodeData2 = nodeData( false, config, nodeIds, lines( 4, nodeIds.size() ) );
// WHEN
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--skip-duplicate-nodes",
"--nodes", nodeHeaderFile.getAbsolutePath() + MULTI_FILE_DELIMITER +
nodeData1.getAbsolutePath() + MULTI_FILE_DELIMITER +
nodeData2.getAbsolutePath() );
// THEN
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
try ( Transaction tx = db.beginTx() )
{
Iterator<Node> nodes = db.getAllNodes().iterator();
Iterator<String> expectedIds = FilteringIterator.noDuplicates( nodeIds.iterator() );
while ( expectedIds.hasNext() )
{
assertTrue( nodes.hasNext() );
assertEquals( expectedIds.next(), nodes.next().getProperty( "id" ) );
}
assertFalse( nodes.hasNext() );
tx.success();
}
finally
{
db.shutdown();
}
}
@Test
public void shouldLogRelationshipsReferringToMissingNode() throws Exception
{
// GIVEN
List<String> nodeIds = asList( "a", "b", "c" );
Configuration config = Configuration.COMMAS;
File nodeData = nodeData( true, config, nodeIds, TRUE );
List<RelationshipDataLine> relationships = Arrays.asList(
// header line 1 of file1
relationship( "a", "b", "TYPE", "aa" ), // line 2 of file1
relationship( "c", "bogus", "TYPE", "bb" ), // line 3 of file1
relationship( "b", "c", "KNOWS", "cc" ), // line 1 of file2
relationship( "c", "a", "KNOWS", "dd" ), // line 2 of file2
relationship( "missing", "a", "KNOWS", "ee" ) ); // line 3 of file2
File relationshipData1 = relationshipData( true, config, relationships.iterator(), lines( 0, 2 ), true );
File relationshipData2 = relationshipData( false, config, relationships.iterator(), lines( 2, 5 ), true );
File bad = badFile();
// WHEN importing data where some relationships refer to missing nodes
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--nodes", nodeData.getAbsolutePath(),
"--bad", bad.getAbsolutePath(),
"--bad-tolerance", "2",
"--relationships", relationshipData1.getAbsolutePath() + MULTI_FILE_DELIMITER +
relationshipData2.getAbsolutePath() );
// THEN
String badContents = FileUtils.readTextFile( bad, Charset.defaultCharset() );
assertTrue( "Didn't contain first bad relationship",
badContents.contains( relationshipData1.getAbsolutePath() + ":3" ) );
assertTrue( "Didn't contain second bad relationship",
badContents.contains( relationshipData2.getAbsolutePath() + ":3" ) );
verifyRelationships( relationships );
}
@Test
public void skipLoggingOfBadEntries() throws Exception
{
// GIVEN
List<String> nodeIds = asList( "a", "b", "c" );
Configuration config = Configuration.COMMAS;
File nodeData = nodeData( true, config, nodeIds, TRUE );
List<RelationshipDataLine> relationships = Arrays.asList(
// header line 1 of file1
relationship( "a", "b", "TYPE", "aa" ), // line 2 of file1
relationship( "c", "bogus", "TYPE", "bb" ), // line 3 of file1
relationship( "b", "c", "KNOWS", "cc" ), // line 1 of file2
relationship( "c", "a", "KNOWS", "dd" ), // line 2 of file2
relationship( "missing", "a", "KNOWS", "ee" ) ); // line 3 of file2
File relationshipData1 = relationshipData( true, config, relationships.iterator(), lines( 0, 2 ), true );
File relationshipData2 = relationshipData( false, config, relationships.iterator(), lines( 2, 5 ), true );
// WHEN importing data where some relationships refer to missing nodes
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--nodes", nodeData.getAbsolutePath(),
"--bad-tolerance", "2",
"--skip-bad-entries-logging", "true",
"--relationships", relationshipData1.getAbsolutePath() + MULTI_FILE_DELIMITER +
relationshipData2.getAbsolutePath() );
assertFalse( badFile().exists() );
verifyRelationships( relationships );
}
@Test
public void shouldFailIfTooManyBadRelationships() throws Exception
{
// GIVEN
List<String> nodeIds = asList( "a", "b", "c" );
Configuration config = Configuration.COMMAS;
File nodeData = nodeData( true, config, nodeIds, TRUE );
List<RelationshipDataLine> relationships = Arrays.asList(
// header line 1 of file1
relationship( "a", "b", "TYPE" ), // line 2 of file1
relationship( "c", "bogus", "TYPE" ), // line 3 of file1
relationship( "b", "c", "KNOWS" ), // line 1 of file2
relationship( "c", "a", "KNOWS" ), // line 2 of file2
relationship( "missing", "a", "KNOWS" ) ); // line 3 of file2
File relationshipData1 = relationshipData( true, config, relationships.iterator(), lines( 0, 2 ), true );
File relationshipData2 = relationshipData( false, config, relationships.iterator(), lines( 2, 5 ), true );
File bad = badFile();
// WHEN importing data where some relationships refer to missing nodes
try
{
importTool(
"--into", dbRule.getStoreDirAbsolutePath(),
"--nodes", nodeData.getAbsolutePath(),
"--bad", bad.getAbsolutePath(),
"--bad-tolerance", "1",
"--relationships", relationshipData1.getAbsolutePath() + MULTI_FILE_DELIMITER +
relationshipData2.getAbsolutePath() );
}
catch ( Exception e )
{
// THEN
assertExceptionContains( e, relationshipData2.getAbsolutePath() + ":3", InputException.class );
}
}
@Test
public void shouldBeAbleToDisableSkippingOfBadRelationships() throws Exception
{
// GIVEN
List<String> nodeIds = asList( "a", "b", "c" );
Configuration config = Configuration.COMMAS;
File nodeData = nodeData( true, config, nodeIds, TRUE );
List<RelationshipDataLine> relationships = Arrays.asList(
// header line 1 of file1
relationship( "a", "b", "TYPE" ), // line 2 of file1
relationship( "c", "bogus", "TYPE" ) ); // line 3 of file1
File relationshipData1 = relationshipData( true, config, relationships.iterator(), lines( 0, 2 ), true );