Skip to content

Commit

Permalink
Formatting for BuiltInProceduresTest
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed Feb 14, 2017
1 parent c34b81c commit 2fa11d1
Showing 1 changed file with 94 additions and 75 deletions.
Expand Up @@ -29,6 +29,7 @@
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;
Expand All @@ -46,10 +47,8 @@
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.IndexDescriptor;
import org.neo4j.kernel.api.schema.IndexDescriptorFactory;
import org.neo4j.kernel.api.schema.NodePropertyDescriptor;
import org.neo4j.kernel.api.schema_new.SchemaBoundary;
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;
Expand Down Expand Up @@ -78,15 +77,15 @@ 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 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 GraphDatabaseAPI graphDatabaseAPI = mock( GraphDatabaseAPI.class );

private final Procedures procs = new Procedures();

Expand All @@ -97,8 +96,8 @@ public void shouldListAllIndexes() throws Throwable
givenIndex( "User", "name" );

// When/Then
assertThat( call("db.indexes"),
contains( record( "INDEX ON :User(name)", "ONLINE", "node_label_property" ) ) );
assertThat( call( "db.indexes" ),
contains( record( "INDEX ON :User(name)", "ONLINE", "node_label_property" ) ) );
}

@Test
Expand All @@ -119,10 +118,10 @@ public void shouldListPropertyKeys() throws Throwable
givenPropertyKeys( "name", "age" );

// When/Then
assertThat( call("db.propertyKeys"),
assertThat( call( "db.propertyKeys" ),
containsInAnyOrder(
record( "age" ),
record( "name" ) ) );
record( "age" ),
record( "name" ) ) );
}

@Test
Expand All @@ -132,7 +131,7 @@ public void shouldListLabels() throws Throwable
givenLabels( "Banana", "Fruit" );

// When/Then
assertThat( call("db.labels"),
assertThat( call( "db.labels" ),
containsInAnyOrder(
record( "Banana" ),
record( "Fruit" ) ) );
Expand All @@ -145,7 +144,7 @@ public void shouldListRelTypes() throws Throwable
givenRelationshipTypes( "EATS", "SPROUTS" );

// When/Then
assertThat( call("db.relationshipTypes"),
assertThat( call( "db.relationshipTypes" ),
containsInAnyOrder(
record( "EATS" ),
record( "SPROUTS" ) ) );
Expand All @@ -159,10 +158,10 @@ public void shouldListConstraints() throws Throwable
givenNodePropExistenceConstraint( "User", "name" );

// When/Then
assertThat( call("db.constraints"),
assertThat( call( "db.constraints" ),
contains(
record( "CONSTRAINT ON ( user:User ) ASSERT exists(user.name)" ),
record( "CONSTRAINT ON ( user:User ) ASSERT user.name IS UNIQUE" ) ) );
record( "CONSTRAINT ON ( user:User ) ASSERT exists(user.name)" ),
record( "CONSTRAINT ON ( user:User ) ASSERT user.name IS UNIQUE" ) ) );
}

@Test
Expand All @@ -187,23 +186,40 @@ public void shouldListCorrectBuiltinProcedures() throws Throwable
assertThat( call( "dbms.procedures" ), containsInAnyOrder(
record( "dbms.listConfig",
"dbms.listConfig(searchString = :: STRING?) :: (name :: STRING?, description :: STRING?, " +
"value" +
" :: 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:*\"")
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:*\"" )
) );
}

Expand All @@ -212,11 +228,11 @@ public void shouldListSystemComponents() throws Throwable
{
// When/Then
assertThat( call( "dbms.components" ), contains(
record( "Neo4j Kernel", singletonList( "1.3.37" ), "enterprise" )
record( "Neo4j Kernel", singletonList( "1.3.37" ), "enterprise" )
) );
}

private Matcher<Object[]> record( Object ... fields )
private Matcher<Object[]> record( Object... fields )
{
return equalTo( fields );
}
Expand Down Expand Up @@ -250,40 +266,42 @@ private void givenNodePropExistenceConstraint( String label, String propKey )
constraints.add( new NodePropertyExistenceConstraint( descriptor ) );
}

private void givenPropertyKeys( String ... keys )
private void givenPropertyKeys( String... keys )
{
for ( String key : keys )
{
token(key, propKeys);
token( key, propKeys );
}
}

private void givenLabels( String ... labelNames )
private void givenLabels( String... labelNames )
{
for ( String key : labelNames )
{
token(key, labels);
token( key, labels );
}
}

private void givenRelationshipTypes( String ... types )
private void givenRelationshipTypes( String... types )
{
for ( String key : types )
{
token(key, relTypes);
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( () -> {
int newIndex = tokens.size();
tokens.put( newIndex, name );
return newIndex;
});
.filter( ( entry ) -> entry.getValue().equals( name ) )
.map( Map.Entry::getKey )
.findFirst().orElseGet( allocateFromMap );
}

@Before
Expand All @@ -297,55 +315,56 @@ public void setup() throws Exception
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 );
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( 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.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] ) );
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 );
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();
return ( i ) -> tokens.entrySet().stream()
.map( ( entry ) -> new Token( entry.getValue(), entry.getKey() ) )
.iterator();
}

private List<Object[]> call(String name, Object ... args) throws ProcedureException
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);
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 ) );
return Iterators
.asList( procs.callProcedure( ctx, ProcedureSignature.procedureName( name.split( "\\." ) ), args ) );
}

private static final Key<DependencyResolver> DEPENDENCY_RESOLVER =
Expand Down

0 comments on commit 2fa11d1

Please sign in to comment.