use of org.neo4j.driver.Result in project spring-boot by spring-projects.
the class Neo4jHealthIndicator method runHealthCheckQuery.
private void runHealthCheckQuery(Health.Builder builder) {
// all possible workloads
try (Session session = this.driver.session(DEFAULT_SESSION_CONFIG)) {
Result result = session.run(CYPHER);
Record record = result.single();
ResultSummary resultSummary = result.consume();
this.healthDetailsHandler.addHealthDetails(builder, new Neo4jHealthDetails(record, resultSummary));
}
}
use of org.neo4j.driver.Result in project neo4j by neo4j.
the class BoltStateHandlerTest method whenInTransactionHandlerLetsTransactionDoTheWork.
@Test
public void whenInTransactionHandlerLetsTransactionDoTheWork() throws CommandException {
Transaction transactionMock = mock(Transaction.class);
Session sessionMock = mock(Session.class);
when(sessionMock.beginTransaction()).thenReturn(transactionMock);
Driver driverMock = stubResultSummaryInAnOpenSession(mock(Result.class), sessionMock, "neo4j-version");
Result result = mock(Result.class);
when(transactionMock.run(any(Query.class))).thenReturn(result);
OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock);
boltStateHandler.connect();
boltStateHandler.beginTransaction();
BoltResult boltResult = boltStateHandler.runCypher("UNWIND [1,2] as num RETURN *", Collections.emptyMap()).get();
assertEquals(result, boltResult.iterate());
boltStateHandler.commitTransaction();
assertFalse(boltStateHandler.isTransactionOpen());
}
use of org.neo4j.driver.Result in project neo4j by neo4j.
the class BoltStateHandlerTest method shouldRunCypherQuery.
@Test
public void shouldRunCypherQuery() throws CommandException {
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))).thenReturn(resultMock);
OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock);
boltStateHandler.connect();
BoltResult boltResult = boltStateHandler.runCypher("RETURN 999", new HashMap<>()).get();
verify(sessionMock).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 exceptionFromRunQueryDoesNotResetActualDatabaseNameToUnresolved.
@Test
public void exceptionFromRunQueryDoesNotResetActualDatabaseNameToUnresolved() throws CommandException {
Session sessionMock = mock(Session.class);
Result resultMock = mock(Result.class);
Driver driverMock = stubResultSummaryInAnOpenSession(resultMock, sessionMock, "9.4.1-ALPHA", "my_default_db");
ClientException databaseNotFound = new ClientException("Neo.ClientError.Database.DatabaseNotFound", "blah");
when(sessionMock.run(any(Query.class))).thenThrow(databaseNotFound).thenReturn(resultMock);
BoltStateHandler handler = new BoltStateHandler((s, authToken, config) -> driverMock, false);
handler.connect(config);
try {
handler.runCypher("RETURN \"hello\"", Collections.emptyMap());
fail("should fail on runCypher");
} catch (Exception e) {
assertThat(e, is(databaseNotFound));
assertEquals("my_default_db", handler.getActualDatabaseAsReportedByServer());
}
}
use of org.neo4j.driver.Result in project neo4j by neo4j.
the class BoltStateHandler method changePassword.
public void changePassword(@Nonnull ConnectionConfig connectionConfig) {
if (!connectionConfig.passwordChangeRequired()) {
return;
}
if (isConnected()) {
silentDisconnect();
}
final AuthToken authToken = AuthTokens.basic(connectionConfig.username(), connectionConfig.password());
try {
driver = getDriver(connectionConfig, authToken);
activeDatabaseNameAsSetByUser = SYSTEM_DB_NAME;
// Supply empty command, so that we do not run ping.
reconnect(SYSTEM_DB_NAME, SYSTEM_DB_NAME, () -> {
});
try {
String command = "ALTER CURRENT USER SET PASSWORD FROM $o TO $n";
Value parameters = Values.parameters("o", connectionConfig.password(), "n", connectionConfig.newPassword());
Result run = session.run(command, parameters);
run.consume();
} catch (Neo4jException e) {
if (isPasswordChangeRequiredException(e)) {
// In < 4.0 versions use legacy method.
String oldCommand = "CALL dbms.security.changePassword($n)";
Value oldParameters = Values.parameters("n", connectionConfig.newPassword());
Result run = session.run(oldCommand, oldParameters);
run.consume();
} else {
throw e;
}
}
// If successful, use the new password when reconnecting
connectionConfig.setPassword(connectionConfig.newPassword());
connectionConfig.setNewPassword(null);
silentDisconnect();
} catch (Throwable t) {
try {
silentDisconnect();
} catch (Exception e) {
t.addSuppressed(e);
}
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
// we cannot get that since we supply an empty command.
throw new RuntimeException(t);
}
}
Aggregations