|
20 | 20 | import org.eclipse.rdf4j.model.ValueFactory;
|
21 | 21 | import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
|
22 | 22 | import org.eclipse.rdf4j.rio.RDFFormat;
|
| 23 | +import org.junit.Rule; |
23 | 24 | import org.junit.Test;
|
| 25 | +import org.neo4j.driver.v1.Config; |
| 26 | +import org.neo4j.driver.v1.Driver; |
| 27 | +import org.neo4j.driver.v1.GraphDatabase; |
| 28 | +import org.neo4j.driver.v1.Session; |
| 29 | +import org.neo4j.driver.v1.StatementResult; |
| 30 | +import org.neo4j.driver.v1.types.Node; |
24 | 31 | import org.neo4j.graphdb.Result;
|
25 | 32 | import org.neo4j.graphdb.Transaction;
|
26 | 33 | import org.neo4j.harness.ServerControls;
|
27 | 34 | import org.neo4j.harness.TestServerBuilder;
|
28 | 35 | import org.neo4j.harness.TestServerBuilders;
|
| 36 | +import org.neo4j.harness.junit.Neo4jRule; |
29 | 37 | import org.neo4j.kernel.configuration.ssl.LegacySslPolicyConfig;
|
30 | 38 | import org.neo4j.server.ServerTestUtils;
|
31 | 39 | import org.neo4j.test.server.HTTP;
|
|
39 | 47 | */
|
40 | 48 | public class RDFEndpointTest {
|
41 | 49 |
|
| 50 | + @Rule |
| 51 | + public Neo4jRule neo4j = new Neo4jRule() |
| 52 | + .withProcedure(RDFImport.class).withFunction(RDFImport.class) |
| 53 | + .withProcedure(MappingUtils.class); |
| 54 | + |
42 | 55 | private static final ObjectMapper jsonMapper = new ObjectMapper();
|
43 | 56 |
|
44 | 57 | private static final CollectionType collectionType = TypeFactory
|
@@ -109,6 +122,131 @@ public void testGetNodeById() throws Exception {
|
109 | 122 | }
|
110 | 123 | }
|
111 | 124 |
|
| 125 | + |
| 126 | + @Test |
| 127 | + public void ImportGetNodeById() throws Exception { |
| 128 | + // Given |
| 129 | + try (ServerControls server = getServerBuilder() |
| 130 | + .withExtension("/rdf", RDFEndpoint.class) |
| 131 | + .withFixture(graphDatabaseService -> { |
| 132 | + try (Transaction tx = graphDatabaseService.beginTx()) { |
| 133 | + String ontoCreation = "MERGE (p:Category {catName: ' Person'})\n" + |
| 134 | + "MERGE (a:Category {catName: 'Actor'})\n" + |
| 135 | + "MERGE (d:Category {catName: 'Director'})\n" + |
| 136 | + "MERGE (c:Category {catName: 'Critic'})\n" + |
| 137 | + "CREATE (a)-[:SCO]->(p)\n" + |
| 138 | + "CREATE (d)-[:SCO]->(p)\n" + |
| 139 | + "CREATE (c)-[:SCO]->(p)\n" + |
| 140 | + "RETURN *"; |
| 141 | + graphDatabaseService.execute(ontoCreation); |
| 142 | + String dataInsertion = "CREATE (Keanu:Actor {name:'Keanu Reeves', born:1964})\n" + |
| 143 | + "CREATE (Carrie:Director {name:'Carrie-Anne Moss', born:1967})\n" + |
| 144 | + "CREATE (Laurence:Director {name:'Laurence Fishburne', born:1961})\n" + |
| 145 | + "CREATE (Hugo:Critic {name:'Hugo Weaving', born:1960})\n" + |
| 146 | + "CREATE (AndyW:Actor {name:'Andy Wachowski', born:1967})\n" + |
| 147 | + "CREATE (Hugo)-[:WORKS_WITH]->(AndyW)\n" + |
| 148 | + "CREATE (Hugo)<-[:FRIEND_OF]-(Carrie)"; |
| 149 | + graphDatabaseService.execute(dataInsertion); |
| 150 | + tx.success(); |
| 151 | + } |
| 152 | + return null; |
| 153 | + }) |
| 154 | + .newServer()) { |
| 155 | + // When |
| 156 | + Result result = server.graph().execute("MATCH (n:Critic) RETURN id(n) AS id "); |
| 157 | + Long id = (Long) result.next().get("id"); |
| 158 | + assertEquals(new Long(7), id); |
| 159 | + |
| 160 | + try (Driver driver = GraphDatabase.driver(neo4j.boltURI(), |
| 161 | + Config.build().withEncryptionLevel(Config.EncryptionLevel.NONE) |
| 162 | + .toConfig()); Session session = driver.session()) { |
| 163 | + session.run("CREATE INDEX ON :Resource(uri)"); |
| 164 | + StatementResult importResults |
| 165 | + = session.run("CALL semantics.importRDF('" + |
| 166 | + HTTP.GET(server.httpURI().resolve("rdf").toString()).location() + "describe/id/" |
| 167 | + + id.toString() + |
| 168 | + "','Turtle',{ handleVocabUris: 'IGNORE', typesToLabels: true, commitSize: 500})"); |
| 169 | + |
| 170 | + Map<String, Object> singleResult = importResults |
| 171 | + .single().asMap(); |
| 172 | + |
| 173 | + assertEquals(5L, singleResult.get("triplesLoaded")); |
| 174 | + StatementResult postImport = session.run("MATCH (n:Critic) RETURN n"); |
| 175 | + Node criticPostImport = postImport.next().get("n").asNode(); |
| 176 | + result = server.graph().execute("MATCH (n:Critic) " |
| 177 | + + "RETURN n.born as born, n.name as name"); |
| 178 | + Map<String, Object> criticPreImport = result.next(); |
| 179 | + assertEquals(criticPreImport.get("name"),criticPostImport.get("name").asString()); |
| 180 | + assertEquals(criticPreImport.get("born"),criticPostImport.get("born").asLong()); |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + @Test |
| 189 | + public void ImportGetCypher() throws Exception { |
| 190 | + // Given |
| 191 | + try (ServerControls server = getServerBuilder() |
| 192 | + .withExtension("/rdf", RDFEndpoint.class) |
| 193 | + .withFixture(graphDatabaseService -> { |
| 194 | + try (Transaction tx = graphDatabaseService.beginTx()) { |
| 195 | + String ontoCreation = "MERGE (p:Category {catName: ' Person'})\n" + |
| 196 | + "MERGE (a:Category {catName: 'Actor'})\n" + |
| 197 | + "MERGE (d:Category {catName: 'Director'})\n" + |
| 198 | + "MERGE (c:Category {catName: 'Critic'})\n" + |
| 199 | + "CREATE (a)-[:SCO]->(p)\n" + |
| 200 | + "CREATE (d)-[:SCO]->(p)\n" + |
| 201 | + "CREATE (c)-[:SCO]->(p)\n" + |
| 202 | + "RETURN *"; |
| 203 | + graphDatabaseService.execute(ontoCreation); |
| 204 | + String dataInsertion = "CREATE (Keanu:Actor {name:'Keanu Reeves', born:1964})\n" + |
| 205 | + "CREATE (Carrie:Director {name:'Carrie-Anne Moss', born:1967})\n" + |
| 206 | + "CREATE (Laurence:Director {name:'Laurence Fishburne', born:1961})\n" + |
| 207 | + "CREATE (Hugo:Critic {name:'Hugo Weaving', born:1960})\n" + |
| 208 | + "CREATE (AndyW:Actor {name:'Andy Wachowski', born:1967})\n" + |
| 209 | + "CREATE (Hugo)-[:WORKS_WITH]->(AndyW)\n" + |
| 210 | + "CREATE (Hugo)<-[:FRIEND_OF]-(Carrie)"; |
| 211 | + graphDatabaseService.execute(dataInsertion); |
| 212 | + tx.success(); |
| 213 | + } |
| 214 | + return null; |
| 215 | + }) |
| 216 | + .newServer()) { |
| 217 | + // When |
| 218 | + Result result = server.graph().execute("MATCH (n:Critic) RETURN id(n) AS id "); |
| 219 | + Long id = (Long) result.next().get("id"); |
| 220 | + assertEquals(new Long(7), id); |
| 221 | + |
| 222 | + try (Driver driver = GraphDatabase.driver(neo4j.boltURI(), |
| 223 | + Config.build().withEncryptionLevel(Config.EncryptionLevel.NONE) |
| 224 | + .toConfig()); Session session = driver.session()) { |
| 225 | + session.run("CREATE INDEX ON :Resource(uri)"); |
| 226 | + |
| 227 | + StatementResult importResults |
| 228 | + = session.run("CALL semantics.importRDF('" + |
| 229 | + HTTP.GET(server.httpURI().resolve("rdf").toString()).location() + "cypher" + |
| 230 | + "','Turtle',{ handleVocabUris: 'IGNORE', " |
| 231 | + + "payload: '{ \"cypher\": \"MATCH (x:Critic) RETURN x \"}'})"); |
| 232 | + |
| 233 | + Map<String, Object> singleResult = importResults |
| 234 | + .single().asMap(); |
| 235 | + |
| 236 | + assertEquals(3L, singleResult.get("triplesLoaded")); |
| 237 | + StatementResult postImport = session.run("MATCH (n:Critic) RETURN n"); |
| 238 | + Node criticPostImport = postImport.next().get("n").asNode(); |
| 239 | + result = server.graph().execute("MATCH (n:Critic) " |
| 240 | + + "RETURN n.born as born, n.name as name"); |
| 241 | + Map<String, Object> criticPreImport = result.next(); |
| 242 | + assertEquals(criticPreImport.get("name"),criticPostImport.get("name").asString()); |
| 243 | + assertEquals(criticPreImport.get("born"),criticPostImport.get("born").asLong()); |
| 244 | + |
| 245 | + } |
| 246 | + |
| 247 | + } |
| 248 | + } |
| 249 | + |
112 | 250 | @Test
|
113 | 251 | public void testFindNodeByLabelAndProperty() throws Exception {
|
114 | 252 | // Given
|
|
0 commit comments