Over a million developers have joined DZone.
Platinum Partner

Neo4j: Testing an Unmanaged Extension Using CommunityServerBuilder

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

I’ve been playing around with Neo4j unmanaged extensions recently and I wanted to be able to check that it worked properly without having to deploy it to a real Neo4j server.

I’d previously used ImpermanentGraphDatabase when using Neo4j embedded and Ian pointed me towards CommunityServerBuilder which allows us to do a similar thing in Neo4j server world.

I’ve created an example of a dummy unmanaged extension and test showing this approach but it’s reasonably simple.

Given the following unmanaged extension:

package org.neo4j.unmanaged;
public class DummyResource
    private final ExecutionEngine executionEngine;
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    public DummyResource( @Context GraphDatabaseService database )
        this.executionEngine = new ExecutionEngine(database);
    public Response uploadNodesFile(  ) throws IOException
        ExecutionResult result = executionEngine.execute("START n = node(*) RETURN n.name ORDER BY n.name");
        ObjectNode root = JsonNodeFactory.instance.objectNode();
        for (String column : result.columns()) {
            ResourceIterator<Object> rows = result.columnAs(column);
            ArrayNode resultRows = JsonNodeFactory.instance.arrayNode();
            while(rows.hasNext()) {
                Object row = rows.next();
                if(row != null) {
            root.put(column, resultRows);
        return Response.status( 200 )

We would write the following test which exposes the ‘all-nodes’ resource at ‘/unmanaged/dummy/all-nodes’:

package org.neo4j.unmanaged;
public class DummyResourceTest {
    private GraphDatabaseAPI db;
    private CommunityNeoServer server;
    public void before() throws IOException {
        ServerSocket serverSocket = new ServerSocket(0);
        server = CommunityServerBuilder
                .withThirdPartyJaxRsPackage("org.neo4j.unmanaged", "/unmanaged")
        db = server.getDatabase().getGraph();
    public void after() {
    public void shouldReturnAllTheNodes() {
        Transaction tx = db.beginTx();
        db.createNode().setProperty("name", "Mark");
        db.createNode().setProperty("name", "Dave");
        JsonNode response = jerseyClient()
                .resource(server.baseUri().toString() + "unmanaged/dummy/all-nodes")
        assertEquals("Dave", response.get("n.name").get(0).asText());
        assertEquals("Mark", response.get("n.name").get(1).asText());
    private Client jerseyClient() {
        DefaultClientConfig defaultClientConfig = new DefaultClientConfig();
        return Client.create(defaultClientConfig);

Here we add a couple of nodes directly against the Java API and then call our unmanaged extension using a Jersey HTTP client. CommunityServerBuilder spins up an ephemeral store under the covers and we pick a random free port for Neo4j server by using ServerSocket#getLocalPort.

We import CommunityServerBuilder by including the following dependency:


And that’s it!

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


Published at DZone with permission of Mark Needham , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}