-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BuiltInProceduresTest.java
375 lines (330 loc) · 15.8 KB
/
BuiltInProceduresTest.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
/*
* 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.kernel.builtinprocs;
import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Test;
import org.mockito.stubbing.Answer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.ReadOperations;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.constraints.NodePropertyExistenceConstraint;
import org.neo4j.kernel.api.constraints.PropertyConstraint;
import org.neo4j.kernel.api.constraints.UniquenessConstraint;
import org.neo4j.kernel.api.exceptions.ProcedureException;
import org.neo4j.kernel.api.index.InternalIndexState;
import org.neo4j.kernel.api.proc.BasicContext;
import org.neo4j.kernel.api.proc.Key;
import org.neo4j.kernel.api.proc.ProcedureSignature;
import org.neo4j.kernel.api.schema.IndexDescriptorFactory;
import org.neo4j.kernel.api.schema.NodePropertyDescriptor;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory;
import org.neo4j.kernel.impl.factory.Edition;
import org.neo4j.kernel.impl.proc.Procedures;
import org.neo4j.kernel.impl.proc.TypeMappers;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.storageengine.api.Token;
import static java.util.Collections.emptyIterator;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.neo4j.kernel.api.proc.Context.KERNEL_TRANSACTION;
import static org.neo4j.kernel.api.proc.Neo4jTypes.NTNode;
import static org.neo4j.kernel.api.proc.Neo4jTypes.NTPath;
import static org.neo4j.kernel.api.proc.Neo4jTypes.NTRelationship;
public class BuiltInProceduresTest
{
private final List<NewIndexDescriptor> indexes = new LinkedList<>();
private final List<NewIndexDescriptor> uniqueIndexes = new LinkedList<>();
private final List<PropertyConstraint> constraints = new LinkedList<>();
private final Map<Integer,String> labels = new HashMap<>();
private final Map<Integer,String> propKeys = new HashMap<>();
private final Map<Integer,String> relTypes = new HashMap<>();
private final ReadOperations read = mock( ReadOperations.class );
private final Statement statement = mock( Statement.class );
private final KernelTransaction tx = mock( KernelTransaction.class );
private final DependencyResolver resolver = mock( DependencyResolver.class );
private final GraphDatabaseAPI graphDatabaseAPI = mock( GraphDatabaseAPI.class );
private final Procedures procs = new Procedures();
@Test
public void shouldListAllIndexes() throws Throwable
{
// Given
givenIndex( "User", "name" );
// When/Then
assertThat( call( "db.indexes" ),
contains( record( "INDEX ON :User(name)", "ONLINE", "node_label_property" ) ) );
}
@Test
public void shouldListAllUniqueIndexes() throws Throwable
{
// Given
givenUniqueConstraint( "User", "name" );
// When/Then
assertThat( call( "db.indexes" ),
contains( record( "INDEX ON :User(name)", "ONLINE", "node_unique_property" ) ) );
}
@Test
public void shouldListPropertyKeys() throws Throwable
{
// Given
givenPropertyKeys( "name", "age" );
// When/Then
assertThat( call( "db.propertyKeys" ),
containsInAnyOrder(
record( "age" ),
record( "name" ) ) );
}
@Test
public void shouldListLabels() throws Throwable
{
// Given
givenLabels( "Banana", "Fruit" );
// When/Then
assertThat( call( "db.labels" ),
containsInAnyOrder(
record( "Banana" ),
record( "Fruit" ) ) );
}
@Test
public void shouldListRelTypes() throws Throwable
{
// Given
givenRelationshipTypes( "EATS", "SPROUTS" );
// When/Then
assertThat( call( "db.relationshipTypes" ),
containsInAnyOrder(
record( "EATS" ),
record( "SPROUTS" ) ) );
}
@Test
public void shouldListConstraints() throws Throwable
{
// Given
givenUniqueConstraint( "User", "name" );
givenNodePropExistenceConstraint( "User", "name" );
// When/Then
assertThat( call( "db.constraints" ),
contains(
record( "CONSTRAINT ON ( user:User ) ASSERT exists(user.name)" ),
record( "CONSTRAINT ON ( user:User ) ASSERT user.name IS UNIQUE" ) ) );
}
@Test
public void shouldEscapeLabelNameContainingColons() throws Throwable
{
// Given
givenUniqueConstraint( "FOO:BAR", "x.y" );
givenNodePropExistenceConstraint( "FOO:BAR", "x.y" );
// When/Then
List<Object[]> call = call( "db.constraints" );
assertThat( call,
contains(
record( "CONSTRAINT ON ( `foo:bar`:`FOO:BAR` ) ASSERT `foo:bar`.x.y IS UNIQUE" ),
record( "CONSTRAINT ON ( `foo:bar`:`FOO:BAR` ) ASSERT exists(`foo:bar`.x.y)" ) ) );
}
@Test
public void shouldListCorrectBuiltinProcedures() throws Throwable
{
// When/Then
assertThat( call( "dbms.procedures" ), containsInAnyOrder(
record( "dbms.listConfig",
"dbms.listConfig(searchString = :: STRING?) :: (name :: STRING?, description :: STRING?, " +
"value" +
" :: STRING?)",
"List the currently active config of Neo4j." ),
record( "db.awaitIndex", "db.awaitIndex(index :: STRING?, timeOutSeconds = 300 :: INTEGER?) :: VOID",
"Wait for an index to come online (for example: CALL db.awaitIndex(\":Person(name)\"))." ),
record( "db.constraints", "db.constraints() :: (description :: STRING?)",
"List all constraints in the database." ),
record( "db.indexes", "db.indexes() :: (description :: STRING?, state :: STRING?, type :: STRING?)",
"List all indexes in the database." ),
record( "db.labels", "db.labels() :: (label :: STRING?)", "List all labels in the database." ),
record( "db.propertyKeys", "db.propertyKeys() :: (propertyKey :: STRING?)",
"List all property keys in the database." ),
record( "db.relationshipTypes", "db.relationshipTypes() :: (relationshipType :: STRING?)",
"List all relationship types in the database." ),
record( "db.resampleIndex", "db.resampleIndex(index :: STRING?) :: VOID",
"Schedule resampling of an index (for example: CALL db.resampleIndex(\":Person(name)\"))." ),
record( "db.resampleOutdatedIndexes", "db.resampleOutdatedIndexes() :: VOID",
"Schedule resampling of all outdated indexes." ),
record( "db.schema",
"db.schema() :: (nodes :: LIST? OF NODE?, relationships :: LIST? OF RELATIONSHIP?)",
"Show the schema of the data." ),
record( "dbms.components",
"dbms.components() :: (name :: STRING?, versions :: LIST? OF STRING?, edition :: STRING?)",
"List DBMS components and their versions." ),
record( "dbms.procedures",
"dbms.procedures() :: (name :: STRING?, signature :: STRING?, description :: STRING?)",
"List all procedures in the DBMS." ),
record( "dbms.functions",
"dbms.functions() :: (name :: STRING?, signature :: STRING?, description :: STRING?)",
"List all user functions in the DBMS." ),
record( "dbms.queryJmx",
"dbms.queryJmx(query :: STRING?) :: (name :: STRING?, description :: STRING?, attributes :: " +
"MAP?)",
"Query JMX management data by domain and name. For instance, \"org.neo4j:*\"" )
) );
}
@Test
public void shouldListSystemComponents() throws Throwable
{
// When/Then
assertThat( call( "dbms.components" ), contains(
record( "Neo4j Kernel", singletonList( "1.3.37" ), "enterprise" )
) );
}
private Matcher<Object[]> record( Object... fields )
{
return equalTo( fields );
}
private void givenIndex( String label, String propKey )
{
int labelId = token( label, labels );
int propId = token( propKey, propKeys );
NewIndexDescriptor index = NewIndexDescriptorFactory.forLabel( labelId, propId );
indexes.add( index );
}
private void givenUniqueConstraint( String label, String propKey )
{
int labelId = token( label, labels );
int propId = token( propKey, propKeys );
NewIndexDescriptor index = NewIndexDescriptorFactory.uniqueForLabel( labelId, propId );
uniqueIndexes.add( index );
constraints.add( new UniquenessConstraint(
IndexDescriptorFactory.getNodePropertyDescriptor( labelId, propId ) ) );
}
private void givenNodePropExistenceConstraint( String label, String propKey )
{
int labelId = token( label, labels );
int propId = token( propKey, propKeys );
NodePropertyDescriptor descriptor = new NodePropertyDescriptor( labelId, propId );
constraints.add( new NodePropertyExistenceConstraint( descriptor ) );
}
private void givenPropertyKeys( String... keys )
{
for ( String key : keys )
{
token( key, propKeys );
}
}
private void givenLabels( String... labelNames )
{
for ( String key : labelNames )
{
token( key, labels );
}
}
private void givenRelationshipTypes( String... types )
{
for ( String key : types )
{
token( key, relTypes );
}
}
private Integer token( String name, Map<Integer,String> tokens )
{
Supplier<Integer> allocateFromMap = () ->
{
int newIndex = tokens.size();
tokens.put( newIndex, name );
return newIndex;
};
return tokens.entrySet().stream()
.filter( ( entry ) -> entry.getValue().equals( name ) )
.map( Map.Entry::getKey )
.findFirst().orElseGet( allocateFromMap );
}
@Before
public void setup() throws Exception
{
procs.registerComponent( KernelTransaction.class, ( ctx ) -> ctx.get( KERNEL_TRANSACTION ), false );
procs.registerComponent( DependencyResolver.class, ( ctx ) -> ctx.get( DEPENDENCY_RESOLVER ), false );
procs.registerComponent( GraphDatabaseAPI.class, ( ctx ) -> ctx.get( GRAPHDATABASEAPI ), false );
procs.registerType( Node.class, new TypeMappers.SimpleConverter( NTNode, Node.class ) );
procs.registerType( Relationship.class, new TypeMappers.SimpleConverter( NTRelationship, Relationship.class ) );
procs.registerType( Path.class, new TypeMappers.SimpleConverter( NTPath, Path.class ) );
new SpecialBuiltInProcedures( "1.3.37", Edition.enterprise.toString() ).accept( procs );
procs.registerProcedure( BuiltInProcedures.class );
procs.registerProcedure( BuiltInDbmsProcedures.class );
when( tx.acquireStatement() ).thenReturn( statement );
when( statement.readOperations() ).thenReturn( read );
when( read.propertyKeyGetAllTokens() ).thenAnswer( asTokens( propKeys ) );
when( read.labelsGetAllTokens() ).thenAnswer( asTokens( labels ) );
when( read.relationshipTypesGetAllTokens() ).thenAnswer( asTokens( relTypes ) );
when( read.indexesGetAll() ).thenAnswer( ( i ) -> indexes.iterator() );
when( read.uniqueIndexesGetAll() ).thenAnswer( ( i ) -> uniqueIndexes.iterator() );
when( read.constraintsGetAll() ).thenAnswer( ( i ) -> constraints.iterator() );
when( read.proceduresGetAll() ).thenReturn( procs.getAllProcedures() );
when( read.propertyKeyGetName( anyInt() ) )
.thenAnswer( ( invocation ) -> propKeys.get( (int) invocation.getArguments()[0] ) );
when( read.labelGetName( anyInt() ) )
.thenAnswer( ( invocation ) -> labels.get( (int) invocation.getArguments()[0] ) );
when( read.relationshipTypeGetName( anyInt() ) )
.thenAnswer( ( invocation ) -> relTypes.get( (int) invocation.getArguments()[0] ) );
// Make it appear that labels are in use
// TODO: We really should just have `labelsInUse()` on the Kernel API directly,
// it'd make testing much easier.
when( read.constraintsGetForRelationshipType( anyInt() ) ).thenReturn( emptyIterator() );
when( read.indexesGetForLabel( anyInt() ) ).thenReturn( emptyIterator() );
when( read.constraintsGetForLabel( anyInt() ) ).thenReturn( emptyIterator() );
when( read.countsForNode( anyInt() ) ).thenReturn( 1L );
when( read.countsForRelationship( anyInt(), anyInt(), anyInt() ) ).thenReturn( 1L );
when( read.indexGetState( any( NewIndexDescriptor.class ) ) ).thenReturn( InternalIndexState.ONLINE );
}
private Answer<Iterator<Token>> asTokens( Map<Integer,String> tokens )
{
return ( i ) -> tokens.entrySet().stream()
.map( ( entry ) -> new Token( entry.getValue(), entry.getKey() ) )
.iterator();
}
private List<Object[]> call( String name, Object... args ) throws ProcedureException
{
BasicContext ctx = new BasicContext();
ctx.put( KERNEL_TRANSACTION, tx );
ctx.put( DEPENDENCY_RESOLVER, resolver );
ctx.put( GRAPHDATABASEAPI, graphDatabaseAPI );
when( graphDatabaseAPI.getDependencyResolver() ).thenReturn( resolver );
when( resolver.resolveDependency( Procedures.class ) ).thenReturn( procs );
return Iterators
.asList( procs.callProcedure( ctx, ProcedureSignature.procedureName( name.split( "\\." ) ), args ) );
}
private static final Key<DependencyResolver> DEPENDENCY_RESOLVER =
Key.key( "DependencyResolver", DependencyResolver.class );
private static final Key<GraphDatabaseAPI> GRAPHDATABASEAPI =
Key.key( "GraphDatabaseAPI", GraphDatabaseAPI.class );
}