use of com.scalar.db.api.PutIfExists in project scalardb by scalar-labs.
the class PutStatementHandlerTest method handle_PutIfExistsGiven_ShouldCallStoredProcedure.
@Test
public void handle_PutIfExistsGiven_ShouldCallStoredProcedure() {
// Arrange
when(container.getScripts()).thenReturn(cosmosScripts);
when(cosmosScripts.getStoredProcedure(anyString())).thenReturn(storedProcedure);
when(storedProcedure.execute(anyList(), any(CosmosStoredProcedureRequestOptions.class))).thenReturn(spResponse);
Put put = preparePut().withCondition(new PutIfExists());
CosmosMutation cosmosMutation = new CosmosMutation(put, metadata);
Record record = cosmosMutation.makeRecord();
String query = cosmosMutation.makeConditionalQuery();
// Act Assert
assertThatCode(() -> handler.handle(put)).doesNotThrowAnyException();
// Assert
verify(cosmosScripts).getStoredProcedure("mutate.js");
verify(storedProcedure).execute(captor.capture(), any(CosmosStoredProcedureRequestOptions.class));
assertThat(captor.getValue().get(0)).isEqualTo(1);
assertThat(captor.getValue().get(1)).isEqualTo(CosmosMutation.MutationType.PUT_IF.ordinal());
assertThat(captor.getValue().get(2)).isEqualTo(record);
assertThat(captor.getValue().get(3)).isEqualTo(query);
}
use of com.scalar.db.api.PutIfExists in project scalardb by scalar-labs.
the class CosmosMutation method getMutationType.
@Nonnull
public MutationType getMutationType() {
Mutation mutation = (Mutation) getOperation();
if (!mutation.getCondition().isPresent()) {
if (mutation instanceof Put) {
return MutationType.PUT;
} else {
return MutationType.DELETE_IF;
}
}
MutationCondition condition = mutation.getCondition().get();
if (condition instanceof PutIfNotExists) {
return MutationType.PUT_IF_NOT_EXISTS;
} else if (condition instanceof PutIfExists || condition instanceof PutIf) {
return MutationType.PUT_IF;
} else {
return MutationType.DELETE_IF;
}
}
use of com.scalar.db.api.PutIfExists in project scalardb by scalar-labs.
the class BatchHandlerTest method handle_CorrectHandlerAndConditionalPutAndUpdateGiven_ShouldExecuteProperly.
@Test
public void handle_CorrectHandlerAndConditionalPutAndUpdateGiven_ShouldExecuteProperly() {
// Arrange
configureBehavior();
mutations = prepareConditionalPuts();
mutations.get(1).withCondition(new PutIfExists());
when(session.execute(any(Statement.class))).thenReturn(results);
when(results.wasApplied()).thenReturn(true);
// Act Assert
assertThatCode(() -> batch.handle(mutations)).doesNotThrowAnyException();
// Assert
verify(insert).prepare(mutations.get(0));
verify(insert).bind(prepared, mutations.get(0));
verify(update).prepare(mutations.get(1));
verify(update).bind(prepared, mutations.get(1));
}
use of com.scalar.db.api.PutIfExists in project scalardb by scalar-labs.
the class JdbcServiceTest method whenPutOperationWithPutIfExistsConditionExecuted_shouldReturnTrueAndCallQueryBuilder.
@Test
public void whenPutOperationWithPutIfExistsConditionExecuted_shouldReturnTrueAndCallQueryBuilder() throws Exception {
// Arrange
when(queryBuilder.update(any(), any(), any())).thenReturn(updateQueryBuilder);
when(updateQueryBuilder.set(any())).thenReturn(updateQueryBuilder);
when(updateQueryBuilder.where(any(), any())).thenReturn(updateQueryBuilder);
when(updateQueryBuilder.build()).thenReturn(updateQuery);
when(connection.prepareStatement(any())).thenReturn(preparedStatement);
when(preparedStatement.executeUpdate()).thenReturn(1);
// Act
Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").withCondition(new PutIfExists()).forNamespace(NAMESPACE).forTable(TABLE);
boolean ret = jdbcService.put(put, connection);
// Assert
assertThat(ret).isTrue();
verify(operationChecker).check(any(Put.class));
verify(queryBuilder).update(any(), any(), any());
}
use of com.scalar.db.api.PutIfExists in project scalardb by scalar-labs.
the class PutStatementHandlerTest method handle_PutWithConditionCosmosExceptionThrown_ShouldThrowExecutionException.
@Test
public void handle_PutWithConditionCosmosExceptionThrown_ShouldThrowExecutionException() {
// Arrange
when(container.getScripts()).thenReturn(cosmosScripts);
when(cosmosScripts.getStoredProcedure(anyString())).thenReturn(storedProcedure);
CosmosException toThrow = mock(CosmosException.class);
doThrow(toThrow).when(storedProcedure).execute(anyList(), any(CosmosStoredProcedureRequestOptions.class));
when(toThrow.getSubStatusCode()).thenReturn(CosmosErrorCode.RETRY_WITH.get());
Put put = preparePut().withCondition(new PutIfExists());
// Act Assert
assertThatThrownBy(() -> handler.handle(put)).isInstanceOf(RetriableExecutionException.class).hasCause(toThrow);
}
Aggregations