|
| 1 | +package com.baeldung.graph; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.DriverManager; |
| 5 | +import java.sql.ResultSet; |
| 6 | +import java.sql.Statement; |
| 7 | + |
| 8 | +import org.junit.Ignore; |
| 9 | +import org.junit.Test; |
| 10 | +import org.neo4j.driver.v1.AuthTokens; |
| 11 | +import org.neo4j.driver.v1.Driver; |
| 12 | +import org.neo4j.driver.v1.GraphDatabase; |
| 13 | +import org.neo4j.driver.v1.Session; |
| 14 | +import org.neo4j.driver.v1.StatementResult; |
| 15 | +import org.testng.Assert; |
| 16 | + |
| 17 | +@Ignore |
| 18 | +public class Neo4JServerTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void standAloneDriver() { |
| 22 | + Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "12345")); |
| 23 | + Session session = driver.session(); |
| 24 | + |
| 25 | + session.run("CREATE (baeldung:Company {name:\"Baeldung\"}) " + |
| 26 | + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + |
| 27 | + "RETURN baeldung, tesla"); |
| 28 | + |
| 29 | + StatementResult result = session.run("MATCH (company:Company)-[:owns]-> (car:Car)" + |
| 30 | + "WHERE car.make='tesla' and car.model='modelX'" + |
| 31 | + "RETURN company.name"); |
| 32 | + |
| 33 | + Assert.assertTrue(result.hasNext()); |
| 34 | + Assert.assertEquals(result.next().get("company.name").asString(), "Baeldung"); |
| 35 | + |
| 36 | + session.close(); |
| 37 | + driver.close(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void standAloneJdbc() throws Exception { |
| 42 | + Connection con = DriverManager.getConnection("jdbc:neo4j:bolt://localhost/?user=neo4j,password=12345,scheme=basic"); |
| 43 | + |
| 44 | + // Querying |
| 45 | + try (Statement stmt = con.createStatement()) { |
| 46 | + stmt.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + |
| 47 | + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + |
| 48 | + "RETURN baeldung, tesla"); |
| 49 | + |
| 50 | + ResultSet rs = stmt.executeQuery("MATCH (company:Company)-[:owns]-> (car:Car)" + |
| 51 | + "WHERE car.make='tesla' and car.model='modelX'" + |
| 52 | + "RETURN company.name"); |
| 53 | + |
| 54 | + while (rs.next()) { |
| 55 | + Assert.assertEquals(rs.getString("company.name"), "Baeldung"); |
| 56 | + } |
| 57 | + } |
| 58 | + con.close(); |
| 59 | + } |
| 60 | +} |
0 commit comments