use of org.neo4j.driver.Result in project neo4j by neo4j.
the class BoltStateHandlerTest method triesAgainOnSessionExpired.
@Test
public void triesAgainOnSessionExpired() throws Exception {
Session sessionMock = mock(Session.class);
Result resultMock = mock(Result.class);
Record recordMock = mock(Record.class);
Value valueMock = mock(Value.class);
Driver driverMock = stubResultSummaryInAnOpenSession(resultMock, sessionMock, "neo4j-version");
when(resultMock.list()).thenReturn(singletonList(recordMock));
when(valueMock.toString()).thenReturn("999");
when(recordMock.get(0)).thenReturn(valueMock);
when(sessionMock.run(any(Query.class))).thenThrow(new SessionExpiredException("leaderswitch")).thenReturn(resultMock);
OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock);
boltStateHandler.connect();
BoltResult boltResult = boltStateHandler.runCypher("RETURN 999", new HashMap<>()).get();
verify(driverMock, times(2)).session(any());
verify(sessionMock, times(2)).run(any(Query.class));
assertEquals("999", boltResult.getRecords().get(0).get(0).toString());
}
use of org.neo4j.driver.Result in project neo4j by neo4j.
the class BoltStateHandlerTest method shouldKeepOneBookmarkPerDatabase.
@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void shouldKeepOneBookmarkPerDatabase() throws CommandException {
ConnectionConfig config = new ConnectionConfig("bolt", "", -1, "", "", Encryption.DEFAULT, "database1");
Bookmark db1Bookmark = InternalBookmark.parse("db1");
Bookmark db2Bookmark = InternalBookmark.parse("db2");
// A couple of these mock calls are now redundant with what is called in stubResultSummaryInAnOpenSession
Result resultMock = mock(Result.class);
Session db1SessionMock = mock(Session.class);
when(db1SessionMock.isOpen()).thenReturn(true);
when(db1SessionMock.lastBookmark()).thenReturn(db1Bookmark);
when(db1SessionMock.run("CALL db.ping()")).thenReturn(resultMock);
Session db2SessionMock = mock(Session.class);
when(db2SessionMock.isOpen()).thenReturn(true);
when(db2SessionMock.lastBookmark()).thenReturn(db2Bookmark);
when(db2SessionMock.run("CALL db.ping()")).thenReturn(resultMock);
Driver driverMock = stubResultSummaryInAnOpenSession(resultMock, db1SessionMock, "Neo4j/9.4.1-ALPHA", "database1");
when(driverMock.session(any())).thenAnswer(arg -> {
SessionConfig sc = (SessionConfig) arg.getArguments()[0];
switch(sc.database().get()) {
case "database1":
return db1SessionMock;
case "database2":
return db2SessionMock;
default:
return null;
}
});
BoltStateHandler handler = new OfflineBoltStateHandler(driverMock);
// When
handler.connect(config);
// Then no bookmark yet for db1
verify(driverMock).session(SessionConfig.builder().withDefaultAccessMode(AccessMode.WRITE).withDatabase("database1").build());
// When
handler.setActiveDatabase("database2");
// Then no bookmark yet for db2
verify(driverMock).session(SessionConfig.builder().withDefaultAccessMode(AccessMode.WRITE).withDatabase("database2").build());
// When
handler.setActiveDatabase("database1");
// Then use bookmark for db1
verify(driverMock).session(SessionConfig.builder().withDefaultAccessMode(AccessMode.WRITE).withDatabase("database1").withBookmarks(db1Bookmark).build());
// When
handler.setActiveDatabase("database2");
// Then use bookmark for db2
verify(driverMock).session(SessionConfig.builder().withDefaultAccessMode(AccessMode.WRITE).withDatabase("database2").withBookmarks(db2Bookmark).build());
}
use of org.neo4j.driver.Result in project neo4j by neo4j.
the class ProcedureTest method callsSimplisticProcedure.
@Test
void callsSimplisticProcedure(Neo4j neo4j) {
try (Driver driver = GraphDatabase.driver(neo4j.boltURI(), configuration());
Session session = driver.session()) {
Result result = session.run("CALL " + procedureNamespace + ".theAnswer()");
assertThat(result.single().get("value").asLong()).isEqualTo(42L);
}
}
use of org.neo4j.driver.Result in project beam by apache.
the class Neo4jIOIT method testLargeWriteUnwind.
@Test
public void testLargeWriteUnwind() throws Exception {
final int startId = 5000;
final int endId = 6000;
// Create 1000 IDs
List<Integer> idList = new ArrayList<>();
for (int id = startId; id < endId; id++) {
idList.add(id);
}
PCollection<Integer> idCollection = largeWriteUnwindPipeline.apply(Create.of(idList));
// Every row is represented by a Map<String, Object> in the parameters map.
// We accumulate the rows and 'unwind' those to Neo4j for performance reasons.
//
SerializableFunction<Integer, Map<String, Object>> parametersFunction = id -> ImmutableMap.of("id", id, "name", "Casters", "firstName", "Matt");
// 1000 rows with a batch size of 123 should trigger most scenarios we can think of
// We've put a unique constraint on Something.id
//
Neo4jIO.WriteUnwind<Integer> read = Neo4jIO.<Integer>writeUnwind().withDriverConfiguration(Neo4jTestUtil.getDriverConfiguration(containerHostname, containerPort)).withSessionConfig(SessionConfig.forDatabase(Neo4jTestUtil.NEO4J_DATABASE)).withBatchSize(123).withUnwindMapName("rows").withCypher("UNWIND $rows AS row CREATE(n:Something { id : row.id })").withParametersFunction(parametersFunction).withCypherLogging();
idCollection.apply(read);
// Now run this pipeline
//
PipelineResult pipelineResult = largeWriteUnwindPipeline.run();
Assert.assertEquals(PipelineResult.State.DONE, pipelineResult.getState());
//
try (Driver driver = Neo4jTestUtil.getDriver(containerHostname, containerPort)) {
try (Session session = Neo4jTestUtil.getSession(driver, true)) {
List<Integer> values = session.readTransaction(tx -> {
List<Integer> v = null;
int nrRows = 0;
Result result = tx.run("MATCH(n:Something) RETURN count(n), min(n.id), max(n.id)");
while (result.hasNext()) {
Record record = result.next();
v = Arrays.asList(record.get(0).asInt(), record.get(1).asInt(), record.get(2).asInt(), ++nrRows);
}
return v;
});
Assert.assertNotNull(values);
assertThat(values, contains(endId - startId, startId, endId - 1, 1));
}
}
}
use of org.neo4j.driver.Result in project beam by apache.
the class Neo4jIOIT method testWriteUnwind.
@Test
public void testWriteUnwind() throws Exception {
PCollection<String> stringsCollections = writeUnwindPipeline.apply(Create.of(Arrays.asList("one", "two", "three")));
// Every row is represented by a Map<String, Object> in the parameters map.
// We accumulate the rows and 'unwind' those to Neo4j for performance reasons.
//
SerializableFunction<String, Map<String, Object>> parametersMapper = name -> Collections.singletonMap("name", name);
Neo4jIO.WriteUnwind<String> read = Neo4jIO.<String>writeUnwind().withDriverConfiguration(Neo4jTestUtil.getDriverConfiguration(containerHostname, containerPort)).withSessionConfig(SessionConfig.forDatabase(Neo4jTestUtil.NEO4J_DATABASE)).withBatchSize(5000).withUnwindMapName("rows").withCypher("UNWIND $rows AS row MERGE(n:Num { name : row.name })").withParametersFunction(parametersMapper).withCypherLogging();
stringsCollections.apply(read);
// Now run this pipeline
//
PipelineResult pipelineResult = writeUnwindPipeline.run();
Assert.assertEquals(PipelineResult.State.DONE, pipelineResult.getState());
//
try (Driver driver = Neo4jTestUtil.getDriver(containerHostname, containerPort)) {
try (Session session = Neo4jTestUtil.getSession(driver, true)) {
List<String> names = session.readTransaction(tx -> {
List<String> list = new ArrayList<>();
Result result = tx.run("MATCH(n:Num) RETURN n.name");
while (result.hasNext()) {
Record record = result.next();
list.add(record.get(0).asString());
}
return list;
});
assertThat(names, containsInAnyOrder("one", "two", "three"));
}
}
}
Aggregations