Skip to content

Commit

Permalink
Improve DatabaseIO: use autoclosable drivers, consume and return resu…
Browse files Browse the repository at this point in the history
…lts from queries, new createAndDeleteNode method to check writes AND format with spaces not tabs
  • Loading branch information
Andrew Jefferson committed Jun 22, 2020
1 parent 6cc7a54 commit f85e167
Showing 1 changed file with 85 additions and 68 deletions.
153 changes: 85 additions & 68 deletions src/test/java/com/neo4j/docker/utils/DatabaseIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,81 +16,98 @@

public class DatabaseIO
{
private static Config TEST_DRIVER_CONFIG = Config.builder().withoutEncryption().build();
private static Config TEST_DRIVER_CONFIG = Config.builder().withoutEncryption().build();

private GenericContainer container;
private String boltUri;
private GenericContainer container;
private String boltUri;

public DatabaseIO( GenericContainer container )
{
this.container = container;
this.boltUri = getBoltURIFromContainer( container );
}
public DatabaseIO( GenericContainer container )
{
this.container = container;
this.boltUri = getBoltURIFromContainer( container );
}

public static String getBoltURIFromContainer( GenericContainer container )
{
return "bolt://"+container.getContainerIpAddress()+":"+container.getMappedPort( 7687 );
}
public static String getBoltURIFromContainer( GenericContainer container )
{
return "bolt://" + container.getContainerIpAddress() + ":" + container.getMappedPort( 7687 );
}

public void putInitialDataIntoContainer( String user, String password )
{
Driver driver = GraphDatabase.driver( boltUri, AuthTokens.basic( user, password ), TEST_DRIVER_CONFIG );
try ( Session session = driver.session())
{
Result rs = session.run( "CREATE (arne:dog {name:'Arne'})-[:SNIFFS]->(bosse:dog {name:'Bosse'}) RETURN arne.name");
Assertions.assertEquals( "Arne", rs.single().get( 0 ).asString(), "did not receive expected result from cypher CREATE query" );
}
driver.close();
}
public void createAndDeleteNode( String user, String password )
{

public void verifyDataInContainer( String user, String password )
{
Driver driver = GraphDatabase.driver( boltUri, AuthTokens.basic( user, password ), TEST_DRIVER_CONFIG );
try ( Session session = driver.session())
{
Result rs = session.run( "MATCH (a:dog)-[:SNIFFS]->(b:dog) RETURN a.name");
Assertions.assertEquals( "Arne", rs.single().get( 0 ).asString(), "did not receive expected result from cypher CREATE query" );
}
driver.close();
}
try (
Driver driver = GraphDatabase.driver( boltUri, AuthTokens.basic( user, password ), TEST_DRIVER_CONFIG );
Session session = driver.session()
)
{
Result rs = session.run( "CREATE (node:createAndDeleteNode {val:1}) RETURN node.val" );
Assertions.assertEquals( 1, rs.single().get( 0 ).asInt(), "did not receive expected result from cypher CREATE query" );
rs = session.run( "MATCH (node:createAndDeleteNode {val:1}) WITH node LIMIT 2 DETACH DELETE node" );
Assertions.assertEquals( 1, rs.consume().counters().nodesDeleted(), "did not receive expected result from cypher DELETE query" );
}
}

public void changePassword(String user, String oldPassword, String newPassword)
{
if(TestSettings.NEO4J_VERSION.isAtLeastVersion( Neo4jVersion.NEO4J_VERSION_400 ))
{
Driver driver = GraphDatabase.driver( boltUri, AuthTokens.basic( user, oldPassword ), TEST_DRIVER_CONFIG );
try ( Session session = driver.session( SessionConfig.forDatabase( "system" )))
{
Result rs = session.run( "ALTER CURRENT USER SET PASSWORD FROM '"+oldPassword+"' TO '"+newPassword+"'" );
}
driver.close();
}
else
{
runCypherProcedure( user, oldPassword, "CALL dbms.changePassword('"+newPassword+"')" );
}
}
public void putInitialDataIntoContainer( String user, String password )
{
try (
Driver driver = GraphDatabase.driver( boltUri, AuthTokens.basic( user, password ), TEST_DRIVER_CONFIG );
Session session = driver.session()
)
{
Result rs = session.run( "CREATE (arne:dog {name:'Arne'})-[:SNIFFS]->(bosse:dog {name:'Bosse'}) RETURN arne.name" );
Assertions.assertEquals( "Arne", rs.single().get( 0 ).asString(), "did not receive expected result from cypher CREATE query" );
}
}

public List<Record> runCypherProcedure( String user, String password, String cypher )
{
try( Driver driver = GraphDatabase.driver( boltUri, AuthTokens.basic( user, password ), TEST_DRIVER_CONFIG ) )
{
try ( Session session = driver.session() )
{
return session.run( cypher ).list();
}
}
}
public void verifyDataInContainer( String user, String password )
{
try (
Driver driver = GraphDatabase.driver( boltUri, AuthTokens.basic( user, password ), TEST_DRIVER_CONFIG );
Session session = driver.session()
)
{
Result rs = session.run( "MATCH (a:dog)-[:SNIFFS]->(b:dog) RETURN a.name" );
Assertions.assertEquals( "Arne", rs.single().get( 0 ).asString(), "did not receive expected result from cypher CREATE query" );
}
}

public void verifyConnectivity( String user, String password )
{
try(var driver = GraphDatabase.driver(
getBoltURIFromContainer(container),
AuthTokens.basic( user, password ),
TEST_DRIVER_CONFIG ) )
{
driver.verifyConnectivity();
}
public void changePassword( String user, String oldPassword, String newPassword )
{
if ( TestSettings.NEO4J_VERSION.isAtLeastVersion( Neo4jVersion.NEO4J_VERSION_400 ) )
{
try (
Driver driver = GraphDatabase.driver( boltUri, AuthTokens.basic( user, oldPassword ), TEST_DRIVER_CONFIG );
Session session = driver.session( SessionConfig.forDatabase( "system" ) )
)
{
Result rs = session.run( "ALTER CURRENT USER SET PASSWORD FROM '" + oldPassword + "' TO '" + newPassword + "'" );
}
}
else
{
runCypherProcedure( user, oldPassword, "CALL dbms.changePassword('" + newPassword + "')" );
}
}

}
public List<Record> runCypherProcedure( String user, String password, String cypher )
{
try (
Driver driver = GraphDatabase.driver( boltUri, AuthTokens.basic( user, password ), TEST_DRIVER_CONFIG );
Session session = driver.session()
)
{
return session.run( cypher ).list();
}
}

public void verifyConnectivity( String user, String password )
{
try ( Driver driver = GraphDatabase.driver(
getBoltURIFromContainer( container ),
AuthTokens.basic( user, password ),
TEST_DRIVER_CONFIG ) )
{
driver.verifyConnectivity();
}
}
}

0 comments on commit f85e167

Please sign in to comment.