cancel
Showing results for 
Search instead for 
Did you mean: 

Use apoc functions inside Java plugin

marc_roth
Node Clone

Hi community,

I'm running some cypher statements with apoc functions inside my Java plugin. Problem is, that the tests fail saying apoc functions are unknown. How can I include apoc to my project? Is there public lib I can import from?

Kind regards,
Marc

3 REPLIES 3

You can add APOC (with Maven) as follows:

        <dependency>
            <groupId>org.neo4j.procedure</groupId>
            <artifactId>apoc</artifactId>
            <version>${apoc.version}</version>
            <classifier>all</classifier>
            <scope>provided</scope>
        </dependency>

If you plan to only use APOC via its defined Cypher procedures/functions, provided scope is fine.
If you plan to use APOC Java methods directly (which I would not necessarily recommend), then you need to remove the scope setting.

For testing (assuming you only need APOC Cypher API, not the Java API), you can do something like this with TestContainers:

    @Container
    private static final Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>("neo4j:4.3")
            .withAdminPassword("s3cr3t")
            .withPlugins(MountableFile.forClasspathResource(String.format("/apoc-%s-all.jar", apocVersion())));

Hi Florent,

it's primarly for testing using the APOC Cypher API. I've got the following class describing my test.

package SDM.BOM;

import org.junit.*;
import org.neo4j.driver.v1.*;
import org.neo4j.harness.junit.Neo4jRule;

import java.util.HashMap;
import java.util.Map;

public class ProceduresTest {
    @Rule
    public Neo4jRule neo4j = new Neo4jRule().withFunction(Procedures.class);

    @Test
    public void testRelevantComponent() {
        try (Driver driver = GraphDatabase.driver(
            neo4j.boltURI(), Config.build().withoutEncryption().toConfig()))
        {
          String query = "return SDM.BOM.doSomething() as result // run some nested cyphers from the plugin ";
            Map<String, Object> params = new HashMap<>();

            Session session = driver.session();
            StatementResult result = session.run(query);
            ...
        }
    }
}

Where do I setup the Neo4jContainer as the environment does all neo4j stuff itself?

You are using Neo4j Test Harness, not TestContainers, which is fine 🙂
This issue captures what you are trying to do.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.