0
votes

I develop some unmanaged extensions for neo4j 2.3.0. Now I want to test the functionality of my code with junit. Is there a way to test my methods locally without an neo4j instance running on my pc?

I want something like this: - create a temporary instance of neo4j before executing test - fill instance with data - call my extension via rest - check the results

I have created a Neo4JTestServer class (using neo4j-harness):

public final class Neo4jTestServer {

    public static final String EXTENSION_MOUNT_POINT = "/v1";
    public static final String EXTENSION_RESOURCES = "my.company.neo4j.extension";

    private static Neo4jTestServer INSTANCE = null;

    public static synchronized Neo4jTestServer getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new Neo4jTestServer();
        }
        return INSTANCE;
    }

    private final ServerControls serverControls;

    private Neo4jTestServer() {
         serverControls = TestServerBuilders.newInProcessBuilder()
             .withExtension(EXTENSION_MOUNT_POINT, EXTENSION_RESOURCES)
             .newServer();
    }


     public ServerControls getServerControls() {
         return serverControls;
     }

     public void shutdown() {
         serverControls.close();
     }
 }

And my test class is looking like this:

public class TestResource {

private Neo4jTestServer server;

@Before
public void prepare(){
    this.server = Neo4jTestServer.getInstance();


}

@After
public void endup(){
    // Shutdown server
    this.server.shutdown();
}


@Test
public void test(){
    //TODO fill neo4j with data
    HTTP.Response response = HTTP.GET(this.server.getServerControls().httpURI().resolve("/v1/calculation/test").toString());
}
}

Here is a link! on the resource i have used.

I also checked the questions from here! but i don't understand if they are running the tests on a 'real' neo4j instance or not.

Can this only be executed on the server or is there a way to run such tests locally?

EDIT: I always get a 500 response calling the rest method.

And this is the output when executing the test:

Nov 04, 2015 10:33:17 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFORMATION: Scanning for root resource and provider classes in the packages:my.company.neo4j.extension
Nov 04, 2015 10:33:17 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFORMATION: Root resource classes found: class my.company.neo4j.extension.Resource
Nov 04, 2015 10:33:17 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFORMATION: No provider classes found.
Nov 04, 2015 10:33:17 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFORMATION: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 03:25 AM'
Nov 04, 2015 10:33:17 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFORMATION: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 03:25 AM'
Nov 04, 2015 10:33:17 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFORMATION: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 03:25 AM'
Nov 04, 2015 10:33:17 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFORMATION: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 03:25 AM'

Is this "INFORMATION: No provider classes found." a problem maybe?

EDIT - this is my extension (it is just for testing)

@Path("/calculation")
public class ResourceV1 {


    @Path("/test")
    @GET
    public Response test() throws Exception{
        return Response.ok().build();
    }
}

I also found the neo4j.log file from the temporary database:

2015-11-04 11:52:39.876+0100 INFO  [o.n.s.d.LifecycleManagingDatabase] Successfully started database
2015-11-04 11:52:39.896+0100 INFO  [o.n.s.CommunityNeoServer] Starting HTTP on port 7474 (4 threads available)
2015-11-04 11:52:40.087+0100 INFO  [o.n.s.m.ThirdPartyJAXRSModule] Mounted unmanaged extension [my.company.neo4j.extension] at [/v1]
2015-11-04 11:52:40.156+0100 INFO  [o.n.s.w.Jetty9WebServer] Mounting static content at /webadmin
2015-11-04 11:52:40.233+0100 WARN  [o.n.s.w.Jetty9WebServer] No static content available for Neo Server at port 7474, management console may not be available.
2015-11-04 11:52:40.252+0100 INFO  [o.n.s.w.Jetty9WebServer] Mounting static content at /browser
2015-11-04 11:52:41.267+0100 INFO  [o.n.s.CommunityNeoServer] Remote interface ready and available at http://localhost:7474/
2
Anything in the body of the 500 response?Michael Hunger

2 Answers

2
votes

What you are trying is following
- Neo4JTestServer class is a factory for an in-memory Neo4j server. It's basically same as "real" one.
- TestResource class is a simple test for your extension.

Problem could be how do you load your extension into the test. Is your extension in the package name "my.company.neo4j.extension"?

Could you please show us code of your extension?

My suggestions are following
- read http://neo4j.com/docs/stable/server-unmanaged-extensions-testing.html
- look on GraphUnit which provide much better possibilities how to test extensions - https://github.com/graphaware/neo4j-framework/tree/master/tests

0
votes

After setting up a new project I found the solution for my problem. In my maven dependencies I had both

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

and

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
</dependency>

And in my case, here was the problem - after removing the dependency for javax ws the testcase was successful and I can call the rest methods.

EDIT: After removing the jax rs dependency it was not possible for me to build the extension because of the missing dependencies.

My solution: Adding following dependency instead of javax.ws.rs-api.

<dependency>
    <groupId>org.neo4j.3rdparty.javax.ws.rs</groupId>
    <artifactId>jsr311-api</artifactId>
    <scope>provided</scope>
    <version>1.1.2.r612</version>
</dependency>

It seems that it is working fine with neo4j-harness.