Testing Neo4j managed extensions

2019-05-23 21:51发布

问题:

I've written a server extension for Neo4j, following the guidelines at http://neo4j.com/docs/stable/server-plugins.html . I wrote unit tests for some of the internal methods, but I would also like to test the REST interface if possible.

http://neo4j.com/docs/stable/_testing_your_extension.html seems to only show how to test unmanaged extensions, but I'm looking for something closely analogous to that for managed extensions. Any pointers?

I do know how to load the extension into a server and query it with curl (as follows), but when things go wrong it's not a fun debug cycle.

curl -X POST -v http://localhost:7474/db/data/ext/NeoPlugin/graphdb/myExt \
  -H "Content-Type: application/json" \
  -d '{"argFoo": "ACTED_IN", "argBar": ["0", "10"]}'

UPDATE: The main (or just first?) part I'm getting stuck at is spinning up a lightweight test REST server that contains my managed extension. I think once there's a server, I can query it using various strategies and check the results.

回答1:

Correct me if I'm wrong but this sounds like a more general question about testing RESTful APIs.

Since you've tagged junit, you might want to consider something like restfuse which is a RESTful API testing extension for JUnit. If you're already testing your unmanaged extension using the harnesses provided by neo4j, this might be the best fit.

RESTful tests might look something like this:

@RunWith( HttpJUnitRunner.class )
public class RestfuseTest {
  @Rule
  public Destination destination = new Destination( "http://localhost:7474/db/data/ext/NeoPlugin/graphdb/myExt" ); 

  @Context
  private Response response; // will be injected after every request

  @HttpTest( method = Method.GET, path = "/" )
  public void checkRestfuseOnlineStatus() {
    assertOk( response );
  }  
}

For more "retail" testing instead of "wholesale" programmatic testing, I would tend to use the Postman extension on Chrome, which makes crafting requests and inspecting the results really easy (think an easier interface to curl, which you're using now).

Finally FWIW there's a gajillion options for other approaches to testing RESTful APIs. Pick your favorite tech or stack, whether frisby on javascript or vREST.



回答2:

It's a bit hidden in the docs, either you spin up a CommunityNeoServer with ServerBuilder (which lives in org.neo4j.app:neo4j-server:test-jar) or you use the new test-harness which is documented here: http://neo4j.com/docs/stable/_testing_your_extension.html

<dependency>
   <groupId>org.neo4j.test</groupId>
   <artifactId>neo4j-harness</artifactId>
   <version>${neo4j.version}</version>
   <scope>provided</scope>
</dependency>

The test toolkit provides a mechanism to start a Neo4j instance with custom configuration and with extensions of your choice. It also provides mechanisms to specify data fixtures to include when starting Neo4j.

Usage example.

@Path("")
public static class MyUnmanagedExtension
{
    @GET
    public Response myEndpoint()
    {
        return Response.ok().build();
    }
}

@Test
public void testMyExtension() throws Exception
{
    // Given
    try ( ServerControls server = TestServerBuilders.newInProcessBuilder()
            .withExtension( "/myExtension", MyUnmanagedExtension.class )
            .newServer() )
    {
        // When
        HTTP.Response response = HTTP.GET( server.httpURI().resolve( "myExtension" ).toString() );

        // Then
        assertEquals( 200, response.status() );
    }
}