Skip to content

Commit

Permalink
Fixed Bolt Test
Browse files Browse the repository at this point in the history
  • Loading branch information
jexp committed Oct 21, 2017
1 parent b1777d9 commit bedfa61
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 64 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dependencies {

testCompile group: 'junit', name: 'junit', version:'4.12'
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version:'1.3'
testCompile group: 'org.neo4j.test', name: 'neo4j-harness', version:neo4jVersion
testCompile group: 'org.apache.derby', name: 'derby', version:'10.12.1.1'
testCompile group: 'org.neo4j', name: 'neo4j-enterprise', version:neo4jVersion
testCompile group: 'org.neo4j', name: 'neo4j-common', version:neo4jVersion, classifier: "tests"
Expand Down
95 changes: 31 additions & 64 deletions src/test/java/apoc/bolt/BoltTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import apoc.util.Util;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.neo4j.graphdb.*;
import org.neo4j.harness.ServerControls;
import org.neo4j.harness.TestServerBuilders;
import org.neo4j.harness.junit.Neo4jRule;
import org.neo4j.test.TestGraphDatabaseFactory;

import java.net.ConnectException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -22,38 +25,41 @@
*/
public class BoltTest {

private static final String BOLT_URL = "'bolt://neo4j:test@localhost:7687'";
protected static GraphDatabaseService db;

private static String setup = "CREATE (m:Person {name:'Michael',surname:'Jordan',age:54, state:true})\n" +
" CREATE (q:Person {name:'Tom',surname:'Burton',age:23})\n" +
" CREATE (p:Person {name:'John',surname:'William',age:22})\n" +
" CREATE (q)-[:KNOWS{since:2016}]->(p)\n" +
" CREATE (a:Person{name:'Tom', surname:'Loagan'})\n" +
" CREATE (b:Person{name:'John', surname:'Green'})\n" +
" CREATE (c:Person{name:'Jim', surname:'Brown'})\n" +
" CREATE (d:Person{name:'Anne', surname:'Olsson'})\n" +
" CREATE (a)-[:KNOWS{since:2010}]->(b)\n" +
" CREATE (b)-[:KNOWS{since:2014}]->(c)\n" +
" CREATE (c)-[:KNOWS{since:2013}]->(d)";

public static ServerControls server;
private static String BOLT_URL;

@BeforeClass
public static void setUp() throws Exception {
server = TestServerBuilders.newInProcessBuilder().withFixture(setup)
.withConfig("dbms.security.auth_enabled", "false").newServer();
BOLT_URL = "'"+server.boltURI().toString()+"'";

db = new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().newGraphDatabase();
TestUtil.registerProcedure(db, Bolt.class);
}

@AfterClass
public static void tearDown() {
db.shutdown();
server.close();
}

//DATASET
/*
CREATE (m:Person {name:'Michael',surname:'Jordan',age:54, state:true})
CREATE (q:Person {name:'Tom',surname:'Burton',age:23})
CREATE (p:Person {name:'John',surname:'William',age:22})
CREATE (q)-[:KNOWS{since:2016}]->(p)
CREATE (a:Person{name:'Tom', surname:'Loagan'})
CREATE (b:Person{name:'John', surname:'Green'})
CREATE (c:Person{name:'Jim', surname:'Brown'})
CREATE (d:Person{name:'Anne', surname:'Olsson'})
CREATE (a)-[:KNOWS{since:2010}]->(b)
CREATE (b)-[:KNOWS{since:2014}]->(c)
CREATE (c)-[:KNOWS{since:2013}]->(d)
*/

@Test
public void testLoadNodeVirtual() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'match(p:Person {name:{name}}) return p', {name:'Michael'}, {virtual:true})", r -> {
assertNotNull(r);
Map<String, Object> row = (Map<String, Object>) r.get("row");
Expand All @@ -64,12 +70,10 @@ public void testLoadNodeVirtual() throws Exception {
assertEquals(true, node.getProperty("state"));
assertEquals(54L, node.getProperty("age"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadNodesVirtual() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testResult(db, "call apoc.bolt.load(" + BOLT_URL + ",'match(n) return n', {}, {virtual:true})", r -> {
assertNotNull(r);
Map<String, Object> row = r.next();
Expand All @@ -95,12 +99,10 @@ public void testLoadNodesVirtual() throws Exception {
assertEquals("William", node.getProperty("surname"));
assertEquals(22L, node.getProperty("age"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadPathVirtual() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'START neo=node({idNode}) MATCH path= (neo)-[r:KNOWS*..3]->(other) return path', {idNode:1}, {virtual:true})", r -> {
assertNotNull(r);
Map<String, Object> row = (Map<String, Object>) r.get("row");
Expand All @@ -117,25 +119,21 @@ public void testLoadPathVirtual() throws Exception {
assertEquals("KNOWS", rel.getType().name());
assertEquals(2016L, rel.getProperty("since"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadRel() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'match(p)-[r]->(c) return r limit 1', {}, {virtual:true})", r -> {
assertNotNull(r);
Map<String, Object> row = (Map<String, Object>) r.get("row");
Relationship rel = (Relationship) row.get("r");
assertEquals("KNOWS", rel.getType().name());
assertEquals(2016L, rel.getProperty("since"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadRelsAndNodes() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'match(p:Person {surname:{surnameP}})-[r]->(c:Person {surname:{surnameC}}) return *', {surnameP:\"Burton\", surnameC:\"William\"}, {virtual:true})", r -> {
Map result = (Map) r.get("row");
Node node = (Node) result.get("p");
Expand All @@ -149,13 +147,11 @@ public void testLoadRelsAndNodes() throws Exception {
assertEquals("William", node.getProperty("surname"));
assertEquals(22L, node.getProperty("age"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadNullParams() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(\"bolt://neo4j:test@localhost:7687\",\"match(p:Person {name:'Michael'}) return p\")", r -> {
TestUtil.testCall(db, "call apoc.bolt.load("+BOLT_URL+",\"match(p:Person {name:'Michael'}) return p\")", r -> {
assertNotNull(r);
Map<String, Object> row = (Map<String, Object>) r.get("row");
Map<String, Object> node = (Map<String, Object>) row.get("p");
Expand All @@ -168,12 +164,10 @@ public void testLoadNullParams() throws Exception {
assertEquals(54L, properties.get("age"));
assertEquals(true, properties.get("state"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadNode() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'match (p:Person {name:{name}}) return p', {name:'Michael'})", r -> {
assertNotNull(r);
Map<String, Object> row = (Map<String, Object>) r.get("row");
Expand All @@ -187,24 +181,20 @@ public void testLoadNode() throws Exception {
assertEquals(54L, properties.get("age"));
assertEquals(true, properties.get("state"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadScalarSingleReusult() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'match (n:Person {name:{name}}) return n.age as Age', {name:'Michael'})", (r) -> {
assertNotNull(r);
Map<String, Object> row = (Map<String, Object>) r.get("row");
assertTrue(row.containsKey("Age"));
assertEquals(54L, row.get("Age"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadMixedContent() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'match (n:Person {name:{name}}) return n.age, n.name, n.state', {name:'Michael'})",
r -> {
assertNotNull(r);
Expand All @@ -216,12 +206,10 @@ public void testLoadMixedContent() throws Exception {
assertTrue(row.containsKey("n.state"));
assertEquals(true, row.get("n.state"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadList() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'match (p:Person {name:{name}}) with collect({personName:p.name}) as rows return rows', {name:'Michael'})", r -> {
assertNotNull(r);
Map<String, Object> row = (Map<String, Object>) r.get("row");
Expand All @@ -230,12 +218,10 @@ public void testLoadList() throws Exception {
assertTrue(result.containsKey("personName"));
assertEquals("Michael", result.get("personName"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadMap() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'match (p:Person {name:{name}}) with p,collect({personName:p.name}) as rows return p{.*, rows:rows}', {name:'Michael'})", r -> {
assertNotNull(r);
Map<String, Object> row = (Map<String, Object>) r.get("row");
Expand All @@ -249,13 +235,11 @@ public void testLoadMap() throws Exception {
assertTrue(p.containsKey("state"));
assertEquals(true, p.get("state"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadPath() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'START neo=node({idNode}) MATCH path= (neo)-[r:KNOWS*..3]->(other) return path', {idNode:1}, {})", r -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'MATCH path= (neo)-[r:KNOWS*..3]->(other) where id(neo) = {idNode} return path', {idNode:1}, {})", r -> {
assertNotNull(r);
Map<String, Object> row = (Map<String, Object>) r.get("row");
List<Object> path = (List<Object>) row.get("path");
Expand All @@ -277,12 +261,10 @@ public void testLoadPath() throws Exception {
Map<String, Object> relProperties = (Map<String, Object>) rel.get("properties");
assertEquals(2016L, relProperties.get("since"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadRels() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'match (n)-[r]->(c) return r as rel limit 1', {})", (r) -> {
assertNotNull(r);
Map<String, Object> row = (Map<String, Object>) r.get("row");
Expand All @@ -294,28 +276,24 @@ public void testLoadRels() throws Exception {
Map<String, Object> properties = (Map<String, Object>) rel.get("properties");
assertEquals(2016L, properties.get("since"));
});
}, QueryExecutionException.class);
}

@Test
public void testExecuteCreateNodeStatistic() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testResult(db, "call apoc.bolt.execute(" + BOLT_URL + ",'create(n:Node {name:{name}})', {name:'Node1'}, {statistics:true})", Collections.emptyMap(),
r -> {
assertNotNull(r);
Map<String, Object> row = r.next();
Map result = (Map) row.get("row");
assertEquals(1L, toLong(result.get("nodesCreated")));
assertEquals(1L, toLong(result.get("labelsAdded")));
assertEquals(1L, toLong(result.get("propertiesSet")));
assertEquals(1L, (long) Util.toLong(result.get("nodesCreated")));
assertEquals(1L, (long) Util.toLong(result.get("labelsAdded")));
assertEquals(1L, (long) Util.toLong(result.get("propertiesSet")));
assertEquals(false, r.hasNext());
});
}, QueryExecutionException.class);
}

@Test
public void testExecuteCreateVirtualNode() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.execute(" + BOLT_URL + ",'create(n:Node {name:{name}}) return n', {name:'Node1'}, {virtual:true})",
r -> {
assertNotNull(r);
Expand All @@ -324,13 +302,11 @@ public void testExecuteCreateVirtualNode() throws Exception {
assertEquals(true, node.hasLabel(Label.label("Node")));
assertEquals("Node1", node.getProperty("name"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadNoVirtual() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(\"bolt://neo4j:test@localhost:7687\",\"match(p:Person {name:'Michael'}) return p\", {}, {virtual:false, test:false})",
TestUtil.testCall(db, "call apoc.bolt.load("+BOLT_URL+",\"match(p:Person {name:'Michael'}) return p\", {}, {virtual:false, test:false})",
r -> {
assertNotNull(r);
Map<String, Object> row = (Map<String, Object>) r.get("row");
Expand All @@ -344,12 +320,10 @@ public void testLoadNoVirtual() throws Exception {
assertEquals(54L, properties.get("age"));
assertEquals(true, properties.get("state"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadNodeWithDriverConfig() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",\"match(p:Person {name:{nameP}}) return p\", {nameP:'Michael'}, " +
"{driverConfig:{logging:'WARNING', encryption: false,logLeakedSessions:true, maxIdleConnectionPoolSize:10, idleTimeBeforeConnectionTest:-1," +
" routingFailureLimit: 1, routingRetryDelayMillis:500, connectionTimeoutMillis:500, maxRetryTimeMs:30000 , trustStrategy:'TRUST_ALL_CERTIFICATES'}})",
Expand All @@ -366,13 +340,11 @@ public void testLoadNodeWithDriverConfig() throws Exception {
assertEquals(54L, properties.get("age"));
assertEquals(true, properties.get("state"));
});
}, QueryExecutionException.class);
}

@Test
public void testLoadBigPathVirtual() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'START neo=node({idNode}) MATCH path= (neo)-[r:KNOWS*3]->(other) return path', {idNode:3}, {virtual:true})", r -> {
TestUtil.testCall(db, "call apoc.bolt.load(" + BOLT_URL + ",'MATCH path= (neo)-[r:KNOWS*3]->(other) WHERE id(neo) = {idNode} return path', {idNode:3}, {virtual:true})", r -> {
Map<String, Object> row = (Map<String, Object>) r.get("row");
List<Object> path = (List<Object>) row.get("path");
Node start = (Node) path.get(0);
Expand Down Expand Up @@ -409,11 +381,6 @@ public void testLoadBigPathVirtual() throws Exception {
assertEquals("Anne", end.getProperty("name"));
assertEquals("Olsson", end.getProperty("surname"));
});
}, QueryExecutionException.class);
}

private long toLong(Object value) {
return Util.toLong(value);
}
}

0 comments on commit bedfa61

Please sign in to comment.