Search in sources :

Example 1 with BeforeSaveCallback

use of org.springframework.data.cassandra.core.mapping.event.BeforeSaveCallback in project spring-data-cassandra by spring-projects.

the class CassandraTemplateUnitTests method setUp.

@BeforeEach
void setUp() {
    when(driverContext.getCodecRegistry()).thenReturn(codecRegistry);
    when(session.getContext()).thenReturn(driverContext);
    when(session.execute(any(Statement.class))).thenReturn(resultSet);
    when(row.getColumnDefinitions()).thenReturn(columnDefinitions);
    EntityCallbacks callbacks = EntityCallbacks.create();
    callbacks.addEntityCallback((BeforeSaveCallback<Object>) (entity, tableName, statement) -> {
        assertThat(tableName).isNotNull();
        assertThat(statement).isNotNull();
        beforeSave = entity;
        return entity;
    });
    callbacks.addEntityCallback((BeforeConvertCallback<Object>) (entity, tableName) -> {
        assertThat(tableName).isNotNull();
        beforeConvert = entity;
        return entity;
    });
    template = new CassandraTemplate(session);
    template.setUsePreparedStatements(false);
    template.setEntityCallbacks(callbacks);
}
Also used : Strictness(org.mockito.quality.Strictness) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers(org.mockito.ArgumentMatchers) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) EntityCallbacks(org.springframework.data.mapping.callback.EntityCallbacks) DefaultCodecRegistry(com.datastax.oss.driver.internal.core.type.codec.registry.DefaultCodecRegistry) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Mock(org.mockito.Mock) CassandraConnectionFailureException(org.springframework.data.cassandra.CassandraConnectionFailureException) BeforeSaveCallback(org.springframework.data.cassandra.core.mapping.event.BeforeSaveCallback) Filter(org.springframework.data.cassandra.core.query.Filter) VersionedUser(org.springframework.data.cassandra.domain.VersionedUser) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Captor(org.mockito.Captor) BeforeConvertCallback(org.springframework.data.cassandra.core.mapping.event.BeforeConvertCallback) ArgumentCaptor(org.mockito.ArgumentCaptor) User(org.springframework.data.cassandra.domain.User) 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) Assertions(org.assertj.core.api.Assertions) Row(com.datastax.oss.driver.api.core.cql.Row) ColumnDefinition(com.datastax.oss.driver.api.core.cql.ColumnDefinition) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) Criteria(org.springframework.data.cassandra.core.query.Criteria) Update(org.springframework.data.cassandra.core.query.Update) DriverContext(com.datastax.oss.driver.api.core.context.DriverContext) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Query(org.springframework.data.cassandra.core.query.Query) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) DataTypes(com.datastax.oss.driver.api.core.type.DataTypes) Collections(java.util.Collections) Statement(com.datastax.oss.driver.api.core.cql.Statement) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) EntityCallbacks(org.springframework.data.mapping.callback.EntityCallbacks) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with BeforeSaveCallback

use of org.springframework.data.cassandra.core.mapping.event.BeforeSaveCallback in project spring-data-cassandra by spring-projects.

the class AsyncCassandraTemplateUnitTests method setUp.

@BeforeEach
void setUp() {
    when(driverContext.getCodecRegistry()).thenReturn(codecRegistry);
    when(session.getContext()).thenReturn(driverContext);
    when(session.executeAsync(any(Statement.class))).thenReturn(new TestResultSetFuture(resultSet));
    when(row.getColumnDefinitions()).thenReturn(columnDefinitions);
    EntityCallbacks callbacks = EntityCallbacks.create();
    callbacks.addEntityCallback((BeforeSaveCallback<Object>) (entity, tableName, statement) -> {
        assertThat(tableName).isNotNull();
        assertThat(statement).isNotNull();
        beforeSave = entity;
        return entity;
    });
    callbacks.addEntityCallback((BeforeConvertCallback<Object>) (entity, tableName) -> {
        assertThat(tableName).isNotNull();
        beforeConvert = entity;
        return entity;
    });
    template = new AsyncCassandraTemplate(session);
    template.setUsePreparedStatements(false);
    template.setEntityCallbacks(callbacks);
}
Also used : Strictness(org.mockito.quality.Strictness) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers(org.mockito.ArgumentMatchers) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) EntityCallbacks(org.springframework.data.mapping.callback.EntityCallbacks) DefaultCodecRegistry(com.datastax.oss.driver.internal.core.type.codec.registry.DefaultCodecRegistry) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) Mock(org.mockito.Mock) CassandraConnectionFailureException(org.springframework.data.cassandra.CassandraConnectionFailureException) BeforeSaveCallback(org.springframework.data.cassandra.core.mapping.event.BeforeSaveCallback) CompletableFuture(java.util.concurrent.CompletableFuture) Filter(org.springframework.data.cassandra.core.query.Filter) VersionedUser(org.springframework.data.cassandra.domain.VersionedUser) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Captor(org.mockito.Captor) ArrayList(java.util.ArrayList) BeforeConvertCallback(org.springframework.data.cassandra.core.mapping.event.BeforeConvertCallback) Future(java.util.concurrent.Future) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) ArgumentCaptor(org.mockito.ArgumentCaptor) User(org.springframework.data.cassandra.domain.User) 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) Assertions(org.assertj.core.api.Assertions) Row(com.datastax.oss.driver.api.core.cql.Row) ColumnDefinition(com.datastax.oss.driver.api.core.cql.ColumnDefinition) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) Criteria(org.springframework.data.cassandra.core.query.Criteria) Update(org.springframework.data.cassandra.core.query.Update) DriverContext(com.datastax.oss.driver.api.core.context.DriverContext) Query(org.springframework.data.cassandra.core.query.Query) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) DataTypes(com.datastax.oss.driver.api.core.type.DataTypes) Collections(java.util.Collections) Statement(com.datastax.oss.driver.api.core.cql.Statement) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) EntityCallbacks(org.springframework.data.mapping.callback.EntityCallbacks) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 NoNodeAvailableException (com.datastax.oss.driver.api.core.NoNodeAvailableException)2 DriverContext (com.datastax.oss.driver.api.core.context.DriverContext)2 ColumnDefinition (com.datastax.oss.driver.api.core.cql.ColumnDefinition)2 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)2 Statement (com.datastax.oss.driver.api.core.cql.Statement)2 DataTypes (com.datastax.oss.driver.api.core.type.DataTypes)2 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)2 DefaultCodecRegistry (com.datastax.oss.driver.internal.core.type.codec.registry.DefaultCodecRegistry)2 Collections (java.util.Collections)2 List (java.util.List)2 Assertions (org.assertj.core.api.Assertions)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 ArgumentMatchers (org.mockito.ArgumentMatchers)2