Search in sources :

Example 1 with CassandraConnectionFailureException

use of org.springframework.data.cassandra.CassandraConnectionFailureException in project spring-data-cassandra by spring-projects.

the class CassandraTemplateUnitTests method selectShouldTranslateException.

// DATACASS-292
@Test
void selectShouldTranslateException() {
    when(resultSet.iterator()).thenThrow(new NoNodeAvailableException());
    try {
        template.select("SELECT * FROM users", User.class);
        fail("Missing CassandraConnectionFailureException");
    } catch (CassandraConnectionFailureException e) {
        assertThat(e).hasRootCauseInstanceOf(NoNodeAvailableException.class);
    }
}
Also used : NoNodeAvailableException(com.datastax.oss.driver.api.core.NoNodeAvailableException) CassandraConnectionFailureException(org.springframework.data.cassandra.CassandraConnectionFailureException) Test(org.junit.jupiter.api.Test)

Example 2 with CassandraConnectionFailureException

use of org.springframework.data.cassandra.CassandraConnectionFailureException in project spring-data-cassandra by spring-projects.

the class AsyncCqlTemplateUnitTests method queryCqlShouldTranslateExceptions.

// DATACASS-292
@Test
void queryCqlShouldTranslateExceptions() throws Exception {
    TestResultSetFuture resultSetFuture = TestResultSetFuture.failed(new NoNodeAvailableException());
    when(session.executeAsync(any(Statement.class))).thenReturn(resultSetFuture);
    ListenableFuture<Boolean> future = template.query("UPDATE user SET a = 'b';", (AsyncResultSetExtractor<Boolean>) it -> new AsyncResult<>(it.wasApplied()));
    try {
        future.get();
        fail("Missing CassandraConnectionFailureException");
    } catch (ExecutionException e) {
        assertThat(e).hasCauseInstanceOf(CassandraConnectionFailureException.class).hasMessageContaining("No node was available");
    }
}
Also used : Strictness(org.mockito.quality.Strictness) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) ArgumentMatchers(org.mockito.ArgumentMatchers) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) CassandraInvalidQueryException(org.springframework.data.cassandra.CassandraInvalidQueryException) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) Mock(org.mockito.Mock) CassandraConnectionFailureException(org.springframework.data.cassandra.CassandraConnectionFailureException) CompletableFuture(java.util.concurrent.CompletableFuture) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Future(java.util.concurrent.Future) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) CqlSession(com.datastax.oss.driver.api.core.CqlSession) NoNodeAvailableException(com.datastax.oss.driver.api.core.NoNodeAvailableException) ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) Assertions(org.assertj.core.api.Assertions) Nullable(org.springframework.lang.Nullable) AsyncResult(org.springframework.scheduling.annotation.AsyncResult) Row(com.datastax.oss.driver.api.core.cql.Row) InvalidQueryException(com.datastax.oss.driver.api.core.servererrors.InvalidQueryException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Collections(java.util.Collections) Statement(com.datastax.oss.driver.api.core.cql.Statement) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) NoNodeAvailableException(com.datastax.oss.driver.api.core.NoNodeAvailableException) ExecutionException(java.util.concurrent.ExecutionException) AsyncResult(org.springframework.scheduling.annotation.AsyncResult) Test(org.junit.jupiter.api.Test)

Example 3 with CassandraConnectionFailureException

use of org.springframework.data.cassandra.CassandraConnectionFailureException in project spring-data-cassandra by spring-projects.

the class CqlTemplateUnitTests method queryCqlShouldTranslateExceptions.

// DATACASS-292
@Test
void queryCqlShouldTranslateExceptions() {
    when(session.execute(any(Statement.class))).thenThrow(new NoNodeAvailableException());
    try {
        template.query("UPDATE user SET a = 'b';", ResultSet::wasApplied);
        fail("Missing CassandraConnectionFailureException");
    } catch (CassandraConnectionFailureException e) {
        assertThat(e).hasMessageContaining("No node was available");
    }
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) NoNodeAvailableException(com.datastax.oss.driver.api.core.NoNodeAvailableException) CassandraConnectionFailureException(org.springframework.data.cassandra.CassandraConnectionFailureException) Test(org.junit.jupiter.api.Test)

Example 4 with CassandraConnectionFailureException

use of org.springframework.data.cassandra.CassandraConnectionFailureException in project spring-data-cassandra by spring-projects.

the class CqlTemplateUnitTests method queryPreparedStatementCreatorAndBinderShouldTranslateExecutionExceptions.

// DATACASS-292
@Test
void queryPreparedStatementCreatorAndBinderShouldTranslateExecutionExceptions() {
    when(session.execute(boundStatement)).thenThrow(new NoNodeAvailableException());
    try {
        template.query(session -> preparedStatement, ps -> {
            ps.bind("a", "b");
            return boundStatement;
        }, rs -> rs);
        fail("Missing CassandraConnectionFailureException");
    } catch (CassandraConnectionFailureException e) {
        assertThat(e).hasCauseInstanceOf(NoNodeAvailableException.class);
    }
}
Also used : NoNodeAvailableException(com.datastax.oss.driver.api.core.NoNodeAvailableException) CassandraConnectionFailureException(org.springframework.data.cassandra.CassandraConnectionFailureException) Test(org.junit.jupiter.api.Test)

Example 5 with CassandraConnectionFailureException

use of org.springframework.data.cassandra.CassandraConnectionFailureException in project spring-data-cassandra by spring-projects.

the class CqlTemplateUnitTests method executeCqlShouldTranslateExceptions.

// DATACASS-292
@Test
void executeCqlShouldTranslateExceptions() {
    when(session.execute(any(Statement.class))).thenThrow(new NoNodeAvailableException());
    try {
        template.execute("UPDATE user SET a = 'b';");
        fail("Missing CassandraConnectionFailureException");
    } catch (CassandraConnectionFailureException e) {
        assertThat(e).hasMessageContaining("No node was available");
    }
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) NoNodeAvailableException(com.datastax.oss.driver.api.core.NoNodeAvailableException) CassandraConnectionFailureException(org.springframework.data.cassandra.CassandraConnectionFailureException) Test(org.junit.jupiter.api.Test)

Aggregations

NoNodeAvailableException (com.datastax.oss.driver.api.core.NoNodeAvailableException)11 Test (org.junit.jupiter.api.Test)11 CassandraConnectionFailureException (org.springframework.data.cassandra.CassandraConnectionFailureException)11 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)8 Statement (com.datastax.oss.driver.api.core.cql.Statement)8 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)5 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)5 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)3 ExecutionException (java.util.concurrent.ExecutionException)3 User (org.springframework.data.cassandra.domain.User)3 VersionedUser (org.springframework.data.cassandra.domain.VersionedUser)3 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)2 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 InvalidQueryException (com.datastax.oss.driver.api.core.servererrors.InvalidQueryException)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2