Reputation: 33
I like to create unit tests for my unmanaged extension I wrote for a small Neo4j project.
GraphDatabaseService db = new TestGraphDatabaseFactory()
.newImpermanentDatabaseBuilder()
.setConfig(GraphDatabaseSettings.pagecache_memory, "512M")
.setConfig( GraphDatabaseSettings.string_block_size, "60" )
.setConfig( GraphDatabaseSettings.array_block_size, "300" )
.newGraphDatabase();
I want to use an approach similar to the code above in my @Before test class - which I understand is the new way to write unit tests.
I like to ask:
I manage to achieve my goal with the code below but I get bunch of deprecated warnings.
ImpermanentGraphDatabase db = new ImpermanentGraphDatabase();
ServerConfigurator config = new ServerConfigurator(db);
config.configuration().setProperty("dbms.security.auth_enabled", false);
config.getThirdpartyJaxRsPackages().add(new ThirdPartyJaxRsPackage("com.mine.graph.neo4j", "/extensions/mine"));
testBootstrapper = new WrappingNeoServerBootstrapper(db, config);
testBootstrapper.start();
Upvotes: 3
Views: 145
Reputation: 1108
My solutions is to create your own TestServer based on the Neo4j test classes so that you can set properties and load the UMX
public class Neo4jTestServer {
private AbstractNeoServer server;
private GraphDatabaseService database;
public Neo4jTestServer() {
try {
ServerControls controls = TestServerBuilders.newInProcessBuilder()
.withExtension("/fd", "org.flockdata.neo4j")
.withConfig("dbms.security.auth_enabled", "false")
.newServer();
initialise(controls);
} catch (Exception e) {
throw new RuntimeException("Error starting in-process server",e);
}
}
private void initialise(ServerControls controls) throws Exception {
Field field = InProcessServerControls.class.getDeclaredField("server");
field.setAccessible(true);
server = (AbstractNeoServer) field.get(controls);
database = server.getDatabase().getGraph();
}
/**
* Retrieves the base URL of the Neo4j database server used in the test.
*
* @return The URL of the Neo4j test server
*/
public String url() {
return server.baseUri().toString();
}}
You will also need an InProcessServer to return your TestServer with a UMX
public class Neo4jInProcessServer implements Neo4jServer{
private Neo4jTestServer testServer;
public Neo4jInProcessServer() {
this.testServer = new Neo4jTestServer();
}
public String url() {
return testServer.url();
}}
Finally, you need to return the InProcessServer from your configuration class
@Override
public Neo4jServer neo4jServer() {
return new Neo4jInProcessServer();
}
Upvotes: 3