DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Structured Logging
  • Effective Java Collection Framework: Best Practices and Tips

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Structured Logging
  • Effective Java Collection Framework: Best Practices and Tips
  1. DZone
  2. Data Engineering
  3. Databases
  4. Neo4j: Testing an Unmanaged Extension Using CommunityServerBuilder

Neo4j: Testing an Unmanaged Extension Using CommunityServerBuilder

Mark Needham user avatar by
Mark Needham
·
Nov. 12, 13 · Interview
Like (0)
Save
Tweet
Share
6.95K Views

Join the DZone community and get the full member experience.

Join For Free

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;
 
@Path("/dummy")
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);
    }
 
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/all-nodes")
    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) {
                    resultRows.add(row.toString());
                }
            }
 
            root.put(column, resultRows);
        }
 
        return Response.status( 200 )
                .entity(OBJECT_MAPPER.writeValueAsString(root))
                .type(MediaType.APPLICATION_JSON).build();
    }
}

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;
 
    @Before
    public void before() throws IOException {
        ServerSocket serverSocket = new ServerSocket(0);
 
        server = CommunityServerBuilder
                .server()
                .onPort(serverSocket.getLocalPort())
                .withThirdPartyJaxRsPackage("org.neo4j.unmanaged", "/unmanaged")
                .build();
 
        server.start();
        db = server.getDatabase().getGraph();
    }
 
    @After
    public void after() {
        server.stop();
    }
 
    @Test
    public void shouldReturnAllTheNodes() {
        Transaction tx = db.beginTx();
        db.createNode().setProperty("name", "Mark");
        db.createNode().setProperty("name", "Dave");
        tx.success();
        tx.close();
 
        JsonNode response = jerseyClient()
                .resource(server.baseUri().toString() + "unmanaged/dummy/all-nodes")
                .get(ClientResponse.class)
                .getEntity(JsonNode.class);
 
        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();
        defaultClientConfig.getClasses().add(JacksonJsonProvider.class);
        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:

<dependency>
            <groupId>org.neo4j.app</groupId>
            <artifactId>neo4j-server</artifactId>
            <version>${neo4j.version}</version>
            <type>test-jar</type>
        </dependency>

And that’s it!


Neo4j

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Structured Logging
  • Effective Java Collection Framework: Best Practices and Tips

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: