Hi all,
I am trying to implement a junit to test a procedure. In order to do this, I would like to load an XML using apoc, but I get the following error:
org.neo4j.driver.exceptions.ClientException: Failed to invoke procedure apoc.load.xml
: Caused by: java.lang.RuntimeException: Import from files not enabled, please set apoc.import.file.enabled=true in your neo4j.conf
To fix this, I added to the Neo4jRule the config as follow:
import java.util.Iterator;
import java.util.stream.Stream;
import org.junit.Rule;
import org.junit.Test;
import org.neo4j.driver.Config;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Record;
import org.neo4j.driver.Session;
import org.neo4j.harness.junit.rule.Neo4jRule;
import apoc.load.Xml;
import static org.junit.Assert.assertTrue;
import static org.neo4j.configuration.SettingImpl.newBuilder;
import static org.neo4j.configuration.SettingValueParsers.STRING;
public class TestImport
{
@Rule
public Neo4jRule neo4j = new Neo4jRule()
.withConfig(
newBuilder("apoc.import.file.enabled",STRING, "false").build(),
"true")
.withProcedure(Xml.class);
@Test
public void testImportProcedure() throws Throwable
{
try(Driver driver = GraphDatabase.driver(neo4j.boltURI(), Config.builder().withoutEncryption().build())) {
Session session = driver.session();
System.out.println(neo4j.config().toString());
Stream all = session.run("CALL apoc.load.xml('file:src/test/resources/xml/small.xml')").stream();
Iterator itMap = all.iterator();
while (itMap.hasNext()) {
Record record = itMap.next();
System.out.println("Test");
}
}
assertTrue(true);
}
}
I still get this error, Would you give me any hint on how I can fix this? Thanks in advance.
I am using neo4j 4.1.0 and apoc 3.5.0.14.
Kind regards,
Antonio
you cannot used APOC 3.5.x.x with Neo4j 4.1 - checkout the version compatibility matrix at Installation - APOC Extended Documentation
In APOC >= 4.0 apoc.import.file.enabled=true
needs to be set either in apoc.conf, as env variable or as a system property, see Configuration Options - APOC Extended Documentation
You can use e.g. System Rules for managing env variables and system properties with a junit rule. You might need to use RuleChain to combine that with your Neo4jRule.
Hi Stefan,
Thanks! Appreciate a lot your answer, gave me a lot of clues.
I do not know why I overlooked the compatibility between neo4j and apoc:-(.
At this moment, I included the apoc version 4.1.0.2 using Maven in eclipse, but I cannot find where it is defined the procedure "apoc.load.xml". In the previous version it was in the package apoc.load in the XML.java, but I cannot find this class anymore. I just check in the Github repo (neo4j-apoc-procedures/Xml.java at 4.1 · neo4j-contrib/neo4j-apoc-procedures · GitHub) and it seems that the class has not been deleted. Do you know why I cannot find it in this version? Thanks again.
Kind regards,
Antonio
APOC has been splitted into a "core" part and a "extended" part. Rationale: we wanted to enable most commonly used features being available on Aura.
Not sure, maybe the maven deployed one is the core variant only. Can you try using the -all
from Release 4.1.0.2 · neo4j-contrib/neo4j-apoc-procedures · GitHub?
Hi Stefan,
Thanks! I did not know this change.
Indeed, it seems that the maven deployed one is the core variant. So, in order to import the -all I use the <classifier>all</classifier>
as follows:
<dependency>
<groupId>org.neo4j.procedure</groupId>
<artifactId>apoc</artifactId>
<version>4.1.0.2</version>
<classifier>all</classifier>
<scope>test</scope>
</dependency>
The thing is now that a new error appears in the console: "APOC couln't set a URLStreamHandlerFactory since some other tool already did this". I think that it related to APOC couln't set a URLStreamHandlerFactory message always there as of apoc 4.1.0.0 - neo4j 4.1.0 · Issue #1571 · neo4j-contrib/neo4j-apoc-procedures · GitHub and a workaround is proposed in APOC couln't set a URLStreamHandlerFactory since some other tool already did this (e.g. tomcat). · Issue #1642 · neo4j-contrib/neo4j-apoc-procedures · GitHub. However, I did not make it work, because when I try to run a query I catch the error: "Timeout waiting for database to become available and allow new transactions. Waited 1s. 1 reasons for blocking: Database unavailable.". At this moment, the RuleChain is defined as follows:
@Rule
public RuleChain chain = RuleChain.outerRule(
new EnvironmentVariables()
.set("apoc.import.file.enabled", "true")
.set("NEO4JLABS_PLUGINS", "apoc"))
.around(neo4j = new Neo4jRule()
.withProcedure(Xml.class)
.withProcedure(NewProcedure.class));
How can I include this programmatically as suggested by nathanschepers
neo4j:
image: neo4j
Thanks in advance.
Kind regards,
Antonio
You don't need
.set("NEO4JLABS_PLUGINS", "apoc"))
since this is only processed by the docker entrypoint script.
The complaint about URLStreamHandlerFactory can be safely ignored. This is already fixed in the code, but not yet released.
Will take a look into your rule definition the next days - being quite busy atm.
Hi Stefan,
I just reset my environment and it seems to work atm (I think that I had some problems with maven). Probably, the above code is more accurate as follows:
public Neo4jRule neo4j = new Neo4jRule();
@Rule
public RuleChain ruleChain = RuleChain.outerRule(new EnvironmentVariables()
.set("apoc.import.file.enabled", "true"))
.around(neo4j.withProcedure(Xml.class));
Thanks.
Kind regards,
Antonio
1 Like