如何使用自定义非托管扩展对Neo4j进行测试? [英] How to run tests against Neo4j with custom unmanaged extension?

查看:74
本文介绍了如何使用自定义非托管扩展对Neo4j进行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Neo4j数据库有自己的自定义编写的非托管扩展.

I have my own custom-written unmanaged extension for Neo4j database.

我想再次运行功能齐全的数据库的集成测试,并在该数据库中使用不受管的扩展.

I want to run integration tests againt fully-functional database, with unmanaged extension available there.

推荐答案

Neo4j提供了一个名为neo4j-harness的工具,该工具可以更轻松地为未管理的扩展编写集成测试. 在此处可用.

Neo4j provides tool called neo4j-harness that makes it easier to write integration tests for unmanged extensions. More iformation is available here.

博客发布

1)确定所需(使用)的Neo4j版本.

1) Determine Neo4j version that is needed (used).

行家:

<properties>
    <version.neo4j>2.2.5</version.neo4j>
</properties>

2)为neo4j-harness添加依赖项.

行家:

<dependency>
    <groupId>org.neo4j.test</groupId>
    <artifactId>neo4j-harness</artifactId>
    <version>${version.neo4j}</version>
    <!-- If you want to use Neo4j server in sources, instead of tests, then remove this line -->
    <scope>test</scope> 
</dependency>

3)确保非托管扩展源在测试中可用.

3) Be sure that you unmanaged extension sources is available in tests.

行家:
如果您将测试写入具有扩展名的同一模块中,则一切正常.
如果您在单独的模块(即integration-tests)中编写测试,请确保该扩展模块在那里可用.

Maven:
If you write tests into same module with extension, then everything is OK.
If you write tests in seperate module (i.e. integration-tests), then make sure that extension is available there.

<dependency>
    <groupId>my.company</groupId>
    <artifactId>unmanaged-extension</artifactId>
    <version>${project.version}</version>
    <scope>test</scope> 
</dependency>    

4)创建负责数据库启动和关闭的Neo4jTestServer类.

4) Create Neo4jTestServer class that is responsible for database start-up and shutdown.

/**
 * Spin-up Neo4j server with loaded unmanaged extension.
 */
public final class Neo4jTestServer {

    public static final String EXTENSION_MOUNT_POINT = "/ext";
    public static final String EXTENSION_RESOURCES = "my.company.extension.resources";
    // Alternative way to get package
    // public static final String EXTENSION_RESOURCES = SomeResource.class.getPackage().getName();

    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)
            // Resource can be specified directly
            // .withExtension(EXTENSION_MOUNT_POINT, SomeResource.class)
            .newServer();
    }

    public ServerControls getServerControls() {
        return serverControls;
    }

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

用法:

Neo4jTestServer server = Neo4jTestServer.getInstance();
// Get Neo4j server URI, with port
server.getServerControls().getHttpUri();
// Shutdown server
server.shutdown();

注意:

  • SomeResource是提供自定义功能的JAX-RS资源
  • 如果您拥有多个资源,并且想要使用class来指定非托管扩展,而不是使用string,则无需指定所有thoose类. Neo4j将扫描指定的类程序包以查找其他资源并自动加载它们.
  • 所有资源应放在同一包中
  • SomeResource is JAX-RS resource that provides custom functionality
  • If you have more than 1 resource, and want to use class to specify unmanaged extension, instead of string - there is no need to specify all thoose classes. Neo4j will scan specified class package for other resources and load them automatically.
  • All resources should be in same package

提示:您可以在所有资源都驻留在同一程序包中创建ResourcesRootPackageMarker类,并使用该类指定程序包.它使代码对未来的代码重构更具弹性.

Tip: You can create ResourcesRootPackageMarker class in same package, where all resources reside and use this class to specify package. It makes code more resilient to future code refactorings.

5)可选.指定JVM shutdown挂钩以关闭数据库.

5) Optional. Specify JVM shutdown hook to shutdown database.

final Neo4jTestServer server = Neo4jTestServer.getInstance();
Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
        server.shutdown();
    }
});

6)要验证一切正常并且您的非托管扩展可用-执行测试,启动数据库并检查Neo4j服务器生成的输出.

6) To verify that everything is working and your unmanaged extension is available - execute tests, start database and examine output generated by Neo4j server.

您应该会看到类似这样的内容:

You should see something like this:

INFO: Scanning for root resource and provider classes in the packages:
  my.company.extension.resources
Sep 14, 2015 5:25:15 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class my.company.extension.resources.SomeResource
  class my.company.extension.resources.AnotherResource


编辑:Graphaware测试框架,由 MicTech


Graphaware test framework, information provided by MicTech

或者,由 GraphAware测试框架href ="http://graphaware.com/" rel ="nofollow noreferrer"> graphaware ,从而可以测试任何与Neo4j相关的代码.

Alternatively, there is GraphAware Test framework provided by graphaware, that gives possibility to test any Neo4j-related code.

此模块提供了轻松测试以一种或另一种方式与Neo4j数据库对话的代码的方法.该模块的目标读者是编写Neo4j相关代码的Java开发人员,以及GraphAware模块和API的作者.

This module provides means of easily testing code that talks to the Neo4j database in one way or another. The target audience of this module are Java developers who write Neo4j-related code, as well as authors of GraphAware Modules and APIs.

此处,您会发现一些有关如何使用框架的帖子(由Graphaware开发人员自动完成)

Here you can find some posts about how framework can be used (autored by Graphaware developers).

基本上,您需要做的是:

Basically what you need to do is:

1)创建扩展名:

@Path("/helloworld")
public class HelloWorldUnmanagedExtension {

    private final HelloWorldNodeCreator nodeCreator;

    public HelloWorldUnmanagedExtension(@Context GraphDatabaseService database) {
        nodeCreator = new HelloWorldNodeCreator(database);
    }

    @POST
    @Path("/create")
    public Response createHelloWorldNode() {
        Node node = nodeCreator.createHelloWorldNode();
        return Response.ok(String.valueOf(node.getId())).build();
    }
}

2)使用WrappingServerIntegrationTest和必要的配置扩展测试.

2) Extend your test with WrappingServerIntegrationTest and necessary configuration.

public class HelloWorldUnmanagedExtensionApiTest extends WrappingServerIntegrationTest {

    @Override
    protected Map<String, String> thirdPartyJaxRsPackageMappings() {
        return Collections.singletonMap("com.graphaware.example.unmanaged", "/ext");
    }

    @Test
    public void shouldCreateAndReturnNode() {
        String result = TestUtils.post(baseNeoUrl() + "/ext/helloworld/create", 200);
        assertEquals("0", result);

        GraphUnit.assertSameGraph(getDatabase(), "CREATE (:HelloWorld {hello:'world'})");
    }
}

此处可以找到更详细的说明如何使用Graphaware测试框架测试非托管扩展.

Here can be found more detailed instructions on how to test unmanaged extension with Graphaware test framework.

所有内容均应立即运行并可以进行测试.祝你好运!

Everything should be up-and-running now and ready for testing. Good luck!

这篇关于如何使用自定义非托管扩展对Neo4j进行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆