-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ImportCommandTest.java
203 lines (184 loc) · 9.24 KB
/
ImportCommandTest.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
/*
* Copyright (c) 2002-2016 "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.commandline.dbms;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Consumer;
import org.neo4j.commandline.admin.CommandLocator;
import org.neo4j.commandline.admin.IncorrectUsage;
import org.neo4j.commandline.admin.NullOutsideWorld;
import org.neo4j.commandline.admin.OutsideWorld;
import org.neo4j.commandline.admin.RealOutsideWorld;
import org.neo4j.commandline.admin.Usage;
import org.neo4j.helpers.Args;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.store.MetaDataStore;
import org.neo4j.kernel.impl.storemigration.StoreFileType;
import org.neo4j.test.rule.TestDirectory;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
public class ImportCommandTest
{
@Rule
public final TestDirectory testDir = TestDirectory.testDirectory();
@Test
public void defaultsToCsvWhenModeNotSpecified() throws Exception
{
File homeDir = testDir.directory( "home" );
ImporterFactory mockImporterFactory = mock( ImporterFactory.class );
when( mockImporterFactory
.getImporterForMode( eq( "csv" ), any( Args.class ), any( Config.class ), any( OutsideWorld.class ) ) )
.thenReturn( mock( Importer.class ) );
ImportCommand importCommand =
new ImportCommand( homeDir.toPath(), testDir.directory( "conf" ).toPath(), new RealOutsideWorld(),
mockImporterFactory );
String[] arguments = {"--database=foo", "--from=bar"};
importCommand.execute( arguments );
verify( mockImporterFactory )
.getImporterForMode( eq( "csv" ), any( Args.class ), any( Config.class ), any( OutsideWorld.class ) );
}
@Test
public void requiresDatabaseArgument() throws Exception
{
ImportCommand importCommand =
new ImportCommand( testDir.directory( "home" ).toPath(), testDir.directory( "conf" ).toPath(),
new NullOutsideWorld() );
String[] arguments = {"--mode=database", "--from=bar"};
try
{
importCommand.execute( arguments );
fail( "Should have thrown an exception." );
}
catch ( IncorrectUsage e )
{
assertThat( e.getMessage(), containsString( "database" ) );
}
}
@Test
public void failIfInvalidModeSpecified() throws Exception
{
ImportCommand importCommand =
new ImportCommand( testDir.directory( "home" ).toPath(), testDir.directory( "conf" ).toPath(),
new NullOutsideWorld() );
String[] arguments = {"--mode=foo", "--database=bar", "--from=baz"};
try
{
importCommand.execute( arguments );
fail( "Should have thrown an exception." );
}
catch ( IncorrectUsage e )
{
assertThat( e.getMessage(), containsString( "foo" ) );
}
}
@Test
public void failIfDestinationDatabaseAlreadyExists() throws Exception
{
Path homeDir = testDir.directory( "home" ).toPath();
ImportCommand importCommand =
new ImportCommand( homeDir, testDir.directory( "conf" ).toPath(), new NullOutsideWorld() );
putStoreInDirectory( homeDir.resolve( "data" ).resolve( "databases" ).resolve( "existing.db" ) );
String[] arguments = {"--mode=csv", "--database=existing.db"};
try
{
importCommand.execute( arguments );
fail( "Should have thrown an exception." );
}
catch ( Exception e )
{
assertThat( e.getMessage(), containsString( "already contains a database" ) );
}
}
@Test
public void shouldPrintNiceHelp() throws Throwable
{
Usage usage = new Usage( "neo4j-admin", mock( CommandLocator.class ) );
Consumer<String> out = mock( Consumer.class );
usage.printUsageForCommand( new ImportCommand.Provider(), out );
verify( out ).accept( "usage: neo4j-admin import [--mode=csv] [--database=<name>]\n" +
" [--additional-config=<config-file-path>]\n" +
" [--report-file=<filename>]\n" +
" [--nodes[:Label1:Label2]=<\"file1,file2,...\">]\n" +
" [--relationships[:RELATIONSHIP_TYPE]=<\"file1,file2,...\">]\n" +
" [--id-type=<STRING|INTEGER|ACTUAL>]\n" +
" [--input-encoding=<character-set>]" );
verify( out ).accept( "usage: neo4j-admin import --mode=database [--database=<name>]\n" +
" [--additional-config=<config-file-path>]\n" +
" [--from=<source-directory>]" );
verify( out ).accept( "" );
verify( out ).accept( "Import a collection of CSV files with --mode=csv (default), or a database from a\n" +
"pre-3.0 installation with --mode=database.\n" +
"\n" +
"options:\n" +
" --database=<name>\n" +
" Name of database. [default:graph.db]\n" +
" --additional-config=<config-file-path>\n" +
" Configuration file to supply additional configuration in. [default:]\n" +
" --mode=<database|csv>\n" +
" Import a collection of CSV files or a pre-3.0 installation.\n" +
" [default:csv]\n" +
" --from=<source-directory>\n" +
" The location of the pre-3.0 database (e.g. <neo4j-root>/data/graph.db).\n" +
" [default:]\n" +
" --report-file=<filename>\n" +
" File in which to store the report of the csv-import.\n" +
" [default:import.report]\n" +
" --nodes[:Label1:Label2]=<\"file1,file2,...\">\n" +
" Node CSV header and data. Multiple files will be logically seen as one\n" +
" big file from the perspective of the importer. The first line must\n" +
" contain the header. Multiple data sources like these can be specified in\n" +
" one import, where each data source has its own header. Note that file\n" +
" groups must be enclosed in quotation marks. [default:]\n" +
" --relationships[:RELATIONSHIP_TYPE]=<\"file1,file2,...\">\n" +
" Relationship CSV header and data. Multiple files will be logically seen\n" +
" as one big file from the perspective of the importer. The first line\n" +
" must contain the header. Multiple data sources like these can be\n" +
" specified in one import, where each data source has its own header. Note\n" +
" that file groups must be enclosed in quotation marks. [default:]\n" +
" --id-type=<STRING|INTEGER|ACTUAL>\n" +
" Each node must provide a unique id. This is used to find the correct\n" +
" nodes when creating relationships. Possible values are STRING: arbitrary\n" +
" strings for identifying nodes, INTEGER: arbitrary integer values for\n" +
" identifying nodes, ACTUAL: (advanced) actual node ids. For more\n" +
" information on id handling, please see the Neo4j Manual:\n" +
" http://neo4j.com/docs/operations-manual/current/deployment/#import-tool\n" +
" [default:STRING]\n" +
" --input-encoding=<character-set>\n" +
" Character set that input data is encoded in. [default:UTF-8]" );
verifyNoMoreInteractions( out );
}
private void putStoreInDirectory( Path storeDir ) throws IOException
{
Files.createDirectories( storeDir );
Path storeFile = storeDir.resolve( StoreFileType.STORE.augment( MetaDataStore.DEFAULT_NAME ) );
Files.createFile( storeFile );
}
}