@Before public void doBefore() { // Delete before just to be sure deleteFileOrDirectory( new File( PATH ) ); db = dbRule.getGraphDatabaseAPI(); }
public void obtainEntityInTerminatedTransaction( Consumer<T> f ) { GraphDatabaseService graphDatabaseService = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = graphDatabaseService.beginTx() ) { T result = obtainEntityInTransaction( graphDatabaseService ); tx.terminate(); f.accept(result); } }
public T obtainEntity() { GraphDatabaseService graphDatabaseService = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = graphDatabaseService.beginTx() ) { T result = obtainEntityInTransaction( graphDatabaseService ); tx.success(); return result; } }
@Before public void before() throws KernelException { graphDb = dbRule.getGraphDatabaseAPI(); createAlistairAndStefanNodes(); getOrCreateIds(); neoStores = graphDb.getDependencyResolver().resolveDependency( RecordStorageEngine.class ).testAccessNeoStores(); locks = mock( LockService.class ); when( locks.acquireNodeLock( anyLong(), any() ) ).thenAnswer( invocation -> { Long nodeId = invocation.getArgument( 0 ); return lockMocks.computeIfAbsent( nodeId, k -> mock( Lock.class ) ); } ); when( locks.acquireRelationshipLock( anyLong(), any() ) ).thenAnswer( invocation -> { Long nodeId = invocation.getArgument( 0 ); return lockMocks.computeIfAbsent( nodeId, k -> mock( Lock.class ) ); } ); storeView = new NeoStoreIndexStoreView( locks, neoStores ); reader = new RecordStorageReader( neoStores ); }
@Test public void shouldDisableLegacyStyleQuotingIfToldTo() throws Exception { // GIVEN String nodeId = "me"; String labelName = "Alive"; List<String> lines = new ArrayList<>(); lines.add( ":ID,name,:LABEL" ); lines.add( nodeId + "," + "\"abc\"\"def\\\"\"ghi\"" + "," + labelName ); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", data( lines.toArray( new String[lines.size()] ) ).getAbsolutePath(), "--legacy-style-quoting", "false", "--stacktrace" ); // THEN GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { assertNotNull( db.findNode( label( labelName ), "name", "abc\"def\\\"ghi" ) ); } }
private void verifyRelationships( List<RelationshipDataLine> relationships ) { GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); Map<String,Node> nodesById = allNodesById( db ); try ( Transaction tx = db.beginTx() ) { for ( RelationshipDataLine relationship : relationships ) { Node startNode = nodesById.get( relationship.startNodeId ); Node endNode = nodesById.get( relationship.endNodeId ); if ( startNode == null || endNode == null ) { // OK this is a relationship referring to a missing node, skip it continue; } assertNotNull( relationship.toString(), findRelationship( startNode, endNode, relationship ) ); } tx.success(); } }
@Test public void shouldNotDetectNonPeriodicCommitQueriesAsPeriodicCommitQueries() { // GIVEN QueryExecutionEngine engine = rule.getGraphDatabaseAPI().getDependencyResolver() .resolveDependency( QueryExecutionEngine.class ); // WHEN boolean result = engine.isPeriodicCommit("CREATE ()"); // THEN assertFalse( "Did detect non-periodic commit query as periodic commit query", result ); }
@Test public void shouldDetectPeriodicCommitQueries() { // GIVEN QueryExecutionEngine engine = rule.getGraphDatabaseAPI().getDependencyResolver() .resolveDependency( QueryExecutionEngine.class ); // WHEN boolean result = engine.isPeriodicCommit("USING PERIODIC COMMIT LOAD CSV FROM 'file:///tmp/foo.csv' AS line CREATE ()"); // THEN assertTrue( "Did not detect periodic commit query", result ); }
@Test public void shouldNotDetectInvalidQueriesAsPeriodicCommitQueries() { // GIVEN QueryExecutionEngine engine = rule.getGraphDatabaseAPI().getDependencyResolver() .resolveDependency( QueryExecutionEngine.class ); // WHEN boolean result = engine.isPeriodicCommit("MATCH n RETURN m"); // THEN assertFalse( "Did detect an invalid query as periodic commit query", result ); } }
@Before public void setUp() { monitor = new CountingMonitor(); template = new TransactionTemplate() .with( databaseRule.getGraphDatabaseAPI() ) .monitor( monitor ) .retries( 5 ) .backoff( 3, TimeUnit.MILLISECONDS ); }
@Test public void shouldSkipEmptyFiles() throws Exception { // GIVEN File data = data( "" ); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", data.getAbsolutePath() ); // THEN GraphDatabaseService graphDatabaseService = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = graphDatabaseService.beginTx() ) { ResourceIterator<Node> allNodes = graphDatabaseService.getAllNodes().iterator(); assertFalse( "Expected database to be empty", allNodes.hasNext() ); tx.success(); } }
@Test public void shouldBeAbleToMakeRepeatedCallsToSetNodeProperty() throws Exception { File file = dbRule.databaseLayout().databaseDirectory(); BatchInserter inserter = BatchInserters.inserter( file, fileSystemRule.get() ); long nodeId = inserter.createNode( Collections.emptyMap() ); final Object finalValue = 87; inserter.setNodeProperty( nodeId, "a", "some property value" ); inserter.setNodeProperty( nodeId, "a", 42 ); inserter.setNodeProperty( nodeId, "a", 3.14 ); inserter.setNodeProperty( nodeId, "a", true ); inserter.setNodeProperty( nodeId, "a", finalValue ); inserter.shutdown(); GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction ignored = db.beginTx() ) { assertThat( db.getNodeById( nodeId ).getProperty( "a" ), equalTo( finalValue ) ); } finally { db.shutdown(); } }
@Test public void shouldNotIndexNodesWithWrongLabel() throws Exception { // Given File file = new File( dbRule.getDatabaseDirAbsolutePath() ); BatchInserter inserter = BatchInserters.inserter( file, fileSystemRule.get() ); inserter.createNode( map("name", "Bob"), label( "User" ), label("Admin")); inserter.createDeferredSchemaIndex( label( "Banana" ) ).on( "name" ).create(); // When inserter.shutdown(); // Then GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { assertThat( count( db.findNodes( label( "Banana" ), "name", "Bob" ) ), equalTo( 0L ) ); } finally { db.shutdown(); } }
@Test public void shouldAllowMultilineFieldsWhenEnabled() throws Exception { // GIVEN File data = data( ":ID,name", "1,\"This is a line with\nnewlines in\"" ); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", data.getAbsolutePath(), "--multiline-fields", "true" ); // THEN GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { ResourceIterator<Node> allNodes = db.getAllNodes().iterator(); Node node = Iterators.single( allNodes ); allNodes.close(); assertEquals( "This is a line with\nnewlines in", node.getProperty( "name" ) ); tx.success(); } }
@Test public void useProvidedAdditionalConfig() throws Exception { // GIVEN int arrayBlockSize = 10; int stringBlockSize = 12; File dbConfig = file( "neo4j.properties" ); store( stringMap( GraphDatabaseSettings.array_block_size.name(), String.valueOf( arrayBlockSize ), GraphDatabaseSettings.string_block_size.name(), String.valueOf( stringBlockSize ) ), dbConfig ); List<String> nodeIds = nodeIds(); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--additional-config", dbConfig.getAbsolutePath(), "--nodes", nodeData( true, Configuration.COMMAS, nodeIds, value -> true ).getAbsolutePath() ); // THEN NeoStores stores = dbRule.getGraphDatabaseAPI().getDependencyResolver() .resolveDependency( RecordStorageEngine.class ).testAccessNeoStores(); int headerSize = Standard.LATEST_RECORD_FORMATS.dynamic().getRecordHeaderSize(); assertEquals( arrayBlockSize + headerSize, stores.getPropertyStore().getArrayStore().getRecordSize() ); assertEquals( stringBlockSize + headerSize, stores.getPropertyStore().getStringStore().getRecordSize() ); }
@Test public void shouldNotTrimStringsByDefault() throws Exception { // GIVEN String name = " This is a line with leading and trailing whitespaces "; File data = data( ":ID,name", "1,\"" + name + "\""); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", data.getAbsolutePath() ); // THEN GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { ResourceIterator<Node> allNodes = db.getAllNodes().iterator(); Node node = Iterators.single( allNodes ); allNodes.close(); assertEquals( name, node.getProperty( "name" ) ); tx.success(); } }
@Test public void shouldRespectDbConfig() throws Exception { // GIVEN int arrayBlockSize = 10; int stringBlockSize = 12; File dbConfig = file( "neo4j.properties" ); store( stringMap( GraphDatabaseSettings.array_block_size.name(), String.valueOf( arrayBlockSize ), GraphDatabaseSettings.string_block_size.name(), String.valueOf( stringBlockSize ) ), dbConfig ); List<String> nodeIds = nodeIds(); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--db-config", dbConfig.getAbsolutePath(), "--nodes", nodeData( true, Configuration.COMMAS, nodeIds, value -> true ).getAbsolutePath() ); // THEN NeoStores stores = dbRule.getGraphDatabaseAPI().getDependencyResolver() .resolveDependency( RecordStorageEngine.class ).testAccessNeoStores(); int headerSize = Standard.LATEST_RECORD_FORMATS.dynamic().getRecordHeaderSize(); assertEquals( arrayBlockSize + headerSize, stores.getPropertyStore().getArrayStore().getRecordSize() ); assertEquals( stringBlockSize + headerSize, stores.getPropertyStore().getStringStore().getRecordSize() ); }
@Test public void shouldIgnoreEmptyQuotedStringsIfConfiguredTo() throws Exception { // GIVEN File data = data( ":ID,one,two,three", "1,\"\",,value" ); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", data.getAbsolutePath(), "--ignore-empty-strings", "true" ); // THEN GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { Node node = Iterables.single( db.getAllNodes() ); assertFalse( node.hasProperty( "one" ) ); assertFalse( node.hasProperty( "two" ) ); assertEquals( "value", node.getProperty( "three" ) ); tx.success(); } }
@Test public void shouldIndexNodesWithMultipleLabels() throws Exception { // Given File path = dbRule.databaseLayout().databaseDirectory(); BatchInserter inserter = BatchInserters.inserter( path, fileSystemRule.get() ); inserter.createNode( map( "name", "Bob" ), label( "User" ), label( "Admin" ) ); inserter.createDeferredSchemaIndex( label( "User" ) ).on( "name" ).create(); inserter.createDeferredSchemaIndex( label( "Admin" ) ).on( "name" ).create(); // When inserter.shutdown(); // Then GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { assertThat( count( db.findNodes( label( "User" ), "name", "Bob" ) ), equalTo(1L) ); assertThat( count( db.findNodes( label( "Admin" ), "name", "Bob" ) ), equalTo(1L) ); } finally { db.shutdown(); } }
@Test public void shouldImportOnlyNodes() throws Exception { // GIVEN List<String> nodeIds = nodeIds(); Configuration config = Configuration.COMMAS; // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--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(); } }