-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ShellDocTest.java
297 lines (271 loc) · 12.9 KB
/
ShellDocTest.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
/*
* 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.shell;
import org.junit.Test;
import java.io.PrintWriter;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.kernel.GraphDatabaseAPI;
import org.neo4j.shell.impl.CollectingOutput;
import org.neo4j.shell.impl.RemoteClient;
import org.neo4j.shell.kernel.GraphDatabaseShellServer;
import org.neo4j.test.TestGraphDatabaseFactory;
import static java.lang.System.lineSeparator;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.neo4j.shell.ShellLobby.NO_INITIAL_SESSION;
import static org.neo4j.shell.ShellLobby.remoteLocation;
import static org.neo4j.visualization.asciidoc.AsciidocHelper.createGraphViz;
public class ShellDocTest
{
private AppCommandParser parse( final String line ) throws Exception
{
return new AppCommandParser( new GraphDatabaseShellServer( null ), line );
}
@Test
public void testParserEasy() throws Exception
{
AppCommandParser parser = parse( "ls -la" );
assertEquals( "ls", parser.getAppName() );
assertEquals( 2, parser.options().size() );
assertTrue( parser.options().containsKey( "l" ) );
assertTrue( parser.options().containsKey( "a" ) );
assertTrue( parser.arguments().isEmpty() );
}
@Test
public void parsingUnrecognizedOptionShouldFail() throws Exception
{
String unrecognizedOption = "unrecognized-option";
try
{
parse( "ls --" + unrecognizedOption );
fail( "Should fail when encountering unrecognized option" );
}
catch ( ShellException e )
{
assertThat( e.getMessage(), containsString( unrecognizedOption ) );
}
}
@Test
public void testParserArguments() throws Exception
{
AppCommandParser parser = parse( "set -t java.lang.Integer key value" );
assertEquals( "set", parser.getAppName() );
assertTrue( parser.options().containsKey( "t" ) );
assertEquals( "java.lang.Integer", parser.options().get( "t" ) );
assertEquals( 2, parser.arguments().size() );
assertEquals( "key", parser.arguments().get( 0 ) );
assertEquals( "value", parser.arguments().get( 1 ) );
assertShellException( "set -tsd" );
}
@Test
public void testEnableRemoteShellOnCustomPort() throws Exception
{
int port = 8085;
GraphDatabaseService graphDb = new TestGraphDatabaseFactory().
newImpermanentDatabaseBuilder().
setConfig( ShellSettings.remote_shell_enabled, "true" ).
setConfig( ShellSettings.remote_shell_port, "" + port ).
newGraphDatabase();
RemoteClient client = new RemoteClient( NO_INITIAL_SESSION, remoteLocation( port ), new CollectingOutput() );
client.evaluate( "help" );
client.shutdown();
graphDb.shutdown();
}
@Test
public void testEnableServerOnDefaultPort() throws Exception
{
GraphDatabaseService graphDb = new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().
setConfig( ShellSettings.remote_shell_enabled, Settings.TRUE ).
newGraphDatabase();
try
{
RemoteClient client = new RemoteClient( NO_INITIAL_SESSION, remoteLocation(), new CollectingOutput() );
client.evaluate( "help" );
client.shutdown();
}
finally
{
graphDb.shutdown();
}
}
@Test
public void testRemoveReferenceNode() throws Exception
{
GraphDatabaseAPI db = (GraphDatabaseAPI)new TestGraphDatabaseFactory().newImpermanentDatabase();
final GraphDatabaseShellServer server = new GraphDatabaseShellServer( db, false );
Documenter doc = new Documenter( "sample session", server );
doc.add( "mknode --cd", "", "Create a node");
doc.add( "pwd", "", "where are we?" );
doc.add( "set name \"Jon\"", "", "On the current node, set the key \"name\" to value \"Jon\"" );
doc.add( "match (n) where id(n) = 0 return n;", "Jon", "send a cypher query" );
doc.add( "mkrel -c -d i -t LIKES --np \"{'app':'foobar'}\"", "", "make an incoming relationship of type " +
"LIKES, create the end node with the node properties specified." );
doc.add( "ls", "1", "where are we?" );
doc.add( "cd 1", "", "change to the newly created node" );
doc.add( "ls -avr", "LIKES", "list relationships, including relationship id" );
doc.add( "mkrel -c -d i -t KNOWS --np \"{'name':'Bob'}\"", "", "create one more KNOWS relationship and the " +
"end node" );
doc.add( "pwd", "0", "print current history stack" );
doc.add( "ls -avr", "KNOWS", "verbose list relationships" );
db.beginTx();
doc.run();
doc.add( "rmnode -f 0", "", "delete node 0" );
doc.add( "cd 0", "", "cd back to node 0" );
doc.add( "pwd", "(?)", "the node doesn't exist now" );
doc.add( "mknode --cd --np \"{'name':'Neo'}\"", "", "create a new node and go to it" );
server.shutdown();
db.shutdown();
}
@Test
public void testDumpCypherResultSimple() throws Exception
{
GraphDatabaseAPI db = (GraphDatabaseAPI)new TestGraphDatabaseFactory().newImpermanentDatabase();
final GraphDatabaseShellServer server = new GraphDatabaseShellServer( db, false );
try ( Transaction tx = db.beginTx() )
{
Documenter doc = new Documenter( "simple cypher result dump", server );
doc.add( "mknode --cd --np \"{'name':'Neo'}\"", "", "create a new node and go to it" );
doc.add( "mkrel -c -d i -t LIKES --np \"{'app':'foobar'}\"", "", "create a relationship" );
doc.add( "dump MATCH (n)-[r]-(m) WHERE n = {self} return n,r,m;",
"create (_0 {`name`:\"Neo\"})", "Export the cypher statement results" );
doc.run();
}
server.shutdown();
db.shutdown();
}
@Test
public void testDumpDatabase() throws Exception
{
GraphDatabaseAPI db = (GraphDatabaseAPI)new TestGraphDatabaseFactory().newImpermanentDatabase();
final GraphDatabaseShellServer server = new GraphDatabaseShellServer( db, false );
Documenter doc = new Documenter( "database dump", server );
doc.add( "create index on :Person(name);", "", "create an index" );
doc.add( "create (m:Person:Hacker {name:'Mattias'}), (m)-[:KNOWS]->(m);", "", "create one labeled node and a relationship" );
doc.add( "dump", "begin" +
lineSeparator() + "create index on :`Person`(`name`);" +
lineSeparator() + "commit" +
lineSeparator() + "begin" +
lineSeparator() + "create (_0:`Person`:`Hacker` {`name`:\"Mattias\"})" +
lineSeparator() + "create (_0)-[:`KNOWS`]->(_0)" +
lineSeparator() + ";" +
lineSeparator() + "commit", "Export the whole database including indexes" );
doc.run();
server.shutdown();
db.shutdown();
}
@Test
public void testMatrix() throws Exception
{
GraphDatabaseAPI db = (GraphDatabaseAPI) new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder()
.loadPropertiesFromURL( getClass().getResource( "/autoindex.properties" ) ).newGraphDatabase();
final GraphDatabaseShellServer server = new GraphDatabaseShellServer( db, false );
Documenter doc = new Documenter( "a matrix example", server );
doc.add("mknode --cd", "", "Create a reference node");
doc.add( "mkrel -t ROOT -c -v", "created",
"create the Thomas Andersson node" );
doc.add( "cd 1", "", "go to the new node" );
doc.add( "set name \"Thomas Andersson\"", "", "set the name property" );
doc.add( "mkrel -t KNOWS -cv", "", "create Thomas direct friends" );
doc.add( "cd 2", "", "go to the new node" );
doc.add( "set name \"Trinity\"", "", "set the name property" );
doc.add( "cd ..", "", "go back in the history stack" );
doc.add( "mkrel -t KNOWS -cv", "", "create Thomas direct friends" );
doc.add( "cd 3", "", "go to the new node" );
doc.add( "set name \"Morpheus\"", "", "set the name property" );
doc.add( "mkrel -t KNOWS 2", "", "create relationship to Trinity" );
doc.add( "ls -rv", "", "list the relationships of node 3" );
doc.add( "cd -r 2", "", "change the current position to relationship #2" );
doc.add( "set -t int age 3", "", "set the age property on the relationship" );
doc.add( "cd ..", "", "back to Morpheus" );
doc.add( "cd -r 3", "", "next relationship" );
doc.add( "set -t int age 90", "", "set the age property on the relationship" );
doc.add( "cd start", "", "position to the start node of the current relationship" );
doc.add( "", "", "We're now standing on Morpheus node, so let's create the rest of the friends." );
doc.add( "mkrel -t KNOWS -c", "", "new node" );
doc.add( "ls -r", "", "list relationships on the current node" );
doc.add( "cd 4", "", "go to Cypher" );
doc.add( "set name Cypher", "", "set the name" );
doc.add( "mkrel -ct KNOWS", "", "create new node from Cypher" );
//TODO: how to list outgoing relationships?
//doc.add( "ls -rd out", "", "list relationships" );
doc.add( "ls -r", "", "list relationships" );
doc.add( "cd 5", "", "go to the Agent Smith node" );
doc.add( "set name \"Agent Smith\"", "", "set the name" );
doc.add( "mkrel -cvt CODED_BY", "", "outgoing relationship and new node" );
doc.add( "cd 6", "", "go there" );
doc.add( "set name \"The Architect\"", "", "set the name" );
doc.add( "cd", "", "go to the first node in the history stack" );
doc.add( "", "", "" );
doc.add( "start morpheus = node:node_auto_index(name='Morpheus') " +
"match (morpheus)-[:KNOWS]-(zionist) " +
"return zionist.name;",
"Trinity",
"Morpheus' friends, looking up Morpheus by name in the Neo4j autoindex" );
doc.run(); // wrapping this in a tx will cause problems, so we don't
server.shutdown();
try (Transaction tx = db.beginTx())
{
assertEquals( 7, Iterables.count( db.getAllRelationships() ) );
assertEquals( 7, Iterables.count( db.getAllNodes() ) );
boolean foundRootAndNeoRelationship = false;
for ( Relationship relationship : db.getAllRelationships() )
{
if ( relationship.getType().name().equals( "ROOT" ) )
{
foundRootAndNeoRelationship = true;
assertFalse( "The root node should not have a name property.", relationship.getStartNode()
.hasProperty( "name" ) );
assertEquals( "Thomas Andersson", relationship.getEndNode()
.getProperty( "name", null ) );
}
}
assertTrue( "Could not find the node connecting the root and Neo nodes.", foundRootAndNeoRelationship );
tx.success();
}
try ( PrintWriter writer = doc.getWriter( "shell-matrix-example-graph" );
Transaction tx = db.beginTx() )
{
writer.println( createGraphViz( "Shell Matrix Example", db, "graph" ) );
writer.flush();
tx.success();
}
db.shutdown();
}
private void assertShellException( final String command ) throws Exception
{
try
{
this.parse( command );
fail( "Should fail with " + ShellException.class.getSimpleName() );
}
catch ( ShellException e )
{
// Good
}
}
}