use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class JdbcTransactionManagerTest method whenCommitFails_shouldThrowCommitExceptionAndRollback.
@Test
public void whenCommitFails_shouldThrowCommitExceptionAndRollback() throws Exception {
// Arrange
doThrow(sqlException).when(connection).commit();
// Act Assert
assertThatThrownBy(() -> {
JdbcTransaction transaction = manager.start();
Get get = new Get(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
transaction.get(get);
Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").forNamespace(NAMESPACE).forTable(TABLE);
transaction.put(put);
transaction.commit();
}).isInstanceOf(CommitException.class);
verify(connection).rollback();
verify(connection).close();
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class JdbcTransactionManagerTest method whenPutOperationsExecutedAndJdbcServiceThrowsSQLException_shouldThrowCrudException.
@Test
public void whenPutOperationsExecutedAndJdbcServiceThrowsSQLException_shouldThrowCrudException() throws Exception {
// Arrange
when(jdbcService.put(any(), any())).thenThrow(sqlException);
// Act Assert
assertThatThrownBy(() -> {
JdbcTransaction transaction = manager.start();
Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").forNamespace(NAMESPACE).forTable(TABLE);
transaction.put(put);
}).isInstanceOf(CrudException.class);
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class JdbcTransactionManagerTest method whenRollbackFails_shouldThrowAbortException.
@Test
public void whenRollbackFails_shouldThrowAbortException() throws Exception {
// Arrange
doThrow(sqlException).when(connection).rollback();
// Act Assert
assertThatThrownBy(() -> {
JdbcTransaction transaction = manager.start();
Get get = new Get(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
transaction.get(get);
Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").forNamespace(NAMESPACE).forTable(TABLE);
transaction.put(put);
transaction.abort();
}).isInstanceOf(AbortException.class);
verify(connection).close();
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class JdbcTransactionManagerTest method whenSomeOperationsExecutedAndCommit_shouldCallJdbcService.
@Test
public void whenSomeOperationsExecutedAndCommit_shouldCallJdbcService() throws Exception {
// Arrange
when(jdbcService.scan(any(), any())).thenReturn(Collections.emptyList());
when(jdbcService.put(any(), any())).thenReturn(true);
when(jdbcService.delete(any(), any())).thenReturn(true);
// Act
JdbcTransaction transaction = manager.start();
Get get = new Get(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
transaction.get(get);
Scan scan = new Scan(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
transaction.scan(scan);
Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").forNamespace(NAMESPACE).forTable(TABLE);
transaction.put(put);
Delete delete = new Delete(new Key("p1", "val1")).forNamespace(NAMESPACE).forTable(TABLE);
transaction.delete(delete);
transaction.mutate(Arrays.asList(put, delete));
transaction.commit();
// Assert
verify(jdbcService).get(any(), any());
verify(jdbcService).scan(any(), any());
verify(jdbcService, times(2)).put(any(), any());
verify(jdbcService, times(2)).delete(any(), any());
verify(connection).commit();
verify(connection).close();
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class ScalarDbUtilsTest method copyAndSetTargetToIfNot_MutationsGiven_ShouldReturnDifferentInstance.
@Test
public void copyAndSetTargetToIfNot_MutationsGiven_ShouldReturnDifferentInstance() {
// Arrange
Put put = new Put(new Key("c1", "v1"));
Delete delete = new Delete(new Key("c1", "v1"));
List<Mutation> mutations = Arrays.asList(put, delete);
// Act
List<Mutation> actual = ScalarDbUtils.copyAndSetTargetToIfNot(mutations, NAMESPACE, TABLE);
// Assert
assertThat(actual == mutations).isFalse();
assertThat(actual.get(0) == put).isFalse();
assertThat(actual.get(1) == delete).isFalse();
assertThat(put.forNamespace()).isNotPresent();
assertThat(put.forTable()).isNotPresent();
assertThat(delete.forNamespace()).isNotPresent();
assertThat(delete.forTable()).isNotPresent();
assertThat(actual.get(0).forNamespace()).isEqualTo(NAMESPACE);
assertThat(actual.get(0).forTable()).isEqualTo(TABLE);
assertThat(actual.get(1).forNamespace()).isEqualTo(NAMESPACE);
assertThat(actual.get(1).forTable()).isEqualTo(TABLE);
}
Aggregations