0

I'm trying to test a server plugin from within a unit test. I've tried my best to set up the unit tests the same as this project.

The issue I am having with my code is with the InProcessServer created by the PersistenceContext class. The InProcessServer starts on the first port available from 7474, and so for my tests, several InProcessServers are created, I think because new tests run before the port from the previous test is closed?

In other words, during test execution, I will find in process servers open in my browser on localhost:7474, localhost:7475, localhost, 7476 and so on...

Of course, this means when it comes to testing server plugins, during test execution I don't know which port I'm going to be testing against, or which database I'm going to hit. If I run my server plugin integration tests individually they run fine.

My questions are:

1) Have I understood all of this right in the first place?

2) How can I test a server plugin as part of a larger integration test if I can't specify the port along with the server plugin uri in advance? Is there a clean way to find out the server port currently in use for a test at runtime?

Luanne
  • 19,145
  • 1
  • 39
  • 51
John Deverall
  • 5,954
  • 3
  • 27
  • 37
  • I have same issue as you where I need to configure the port at compile time. Did you ever solve this? – Selwyn Jan 06 '16 at 00:07
  • Hi Selwyn - yes I did, long ago - but the solution is now old with the release of 4.1. If you're still interested in how I did do things I can post something if you're still interested. I only just saw your comment now. – John Deverall Jul 24 '16 at 14:03
  • you can hold off on it. I think the InProcessServer is deprecated now? I've switched over to 'org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver' which I think removes the need to configure a port at compile time. – Selwyn Jul 24 '16 at 18:00

3 Answers3

0

The InProcessServer does not expose a separate port number, but it does expose the full url, which includes the port number. This is always of the form 'http://localhost:xxxx'. You could extract the port number from that fairly easily if you need it separately for some reason.

Vince
  • 2,181
  • 13
  • 16
  • what if we need to know the port # at compile time? – Selwyn Jan 06 '16 at 00:25
  • 1
    SDN 4.1 (coming soon) will allow you to inject a TestServer configured as you'd like. In the meantime, you could roll your own - its a very simple class. I have posted some code in a separate answer that you could use instead of the existing InProcessServer. – Vince Jan 06 '16 at 16:57
0

This tip might be of use when testing with server extensions:

https://jira.spring.io/browse/DATAGRAPH-656?focusedCommentId=117218&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-117218

Copying what it suggests here:

To test a Neo4jExtension, you need to create your own TestServer and InProcessServer so that you can wire the extension. Something like this: public Neo4jTestServer() {

    try {
        ServerControls controls = TestServerBuilders.newInProcessBuilder()
                .withExtension("/mypath", "org.mypackage")
                .withConfig("dbms.security.auth_enabled", "false")
                .newServer();

        initialise(controls); //Or try extracting the uri, port from controls this point

    } catch (Exception e) {
        throw new RuntimeException("Error starting in-process server",e);
    }

}
Luanne
  • 19,145
  • 1
  • 39
  • 51
  • Is it possible to configure the port at compile time? I am doing integration test in spring-data-neo4j and my neo4j connection config's come from a neo4j.properties file. – Selwyn Jan 06 '16 at 00:08
0

If you want full control over configuring an InProcessServer, its not hard to roll your own. This will do the job:

public class MyInProcessServer implements Neo4jServer {

  private TestServer testServer;

  public InProcessServer(TestServer testServer)  {
    this.testServer = testServer;
  }

  public String url() {
    return this.testServer.url();
  }

  @Override
  public String username() {
    return "neo4j";
  }

  @Override
  public String password() {
    return "password";
  }

  public GraphDatabaseService database() {
    return testServer.getGraphDatabaseService();
  }
}

You'll obviously need to setup the Spring config accordingly:

@Bean
@Override
public Neo4jServer neo4jServer() {
    return new MyInProcessServer(new TestServer(6666));
}

Not tested, but it should work.

Vince
  • 2,181
  • 13
  • 16