Search in sources :

Example 11 with PutIfExists

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);
}
Also used : CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Put(com.scalar.db.api.Put) PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Example 12 with PutIfExists

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;
    }
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) PutIf(com.scalar.db.api.PutIf) MutationCondition(com.scalar.db.api.MutationCondition) Mutation(com.scalar.db.api.Mutation) Put(com.scalar.db.api.Put) PutIfExists(com.scalar.db.api.PutIfExists) Nonnull(javax.annotation.Nonnull)

Example 13 with PutIfExists

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));
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Example 14 with PutIfExists

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());
}
Also used : Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Example 15 with PutIfExists

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);
}
Also used : CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) CosmosException(com.azure.cosmos.CosmosException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) Put(com.scalar.db.api.Put) PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Aggregations

PutIfExists (com.scalar.db.api.PutIfExists)21 Test (org.junit.jupiter.api.Test)18 Put (com.scalar.db.api.Put)15 Key (com.scalar.db.io.Key)6 MutationCondition (com.scalar.db.api.MutationCondition)5 IntValue (com.scalar.db.io.IntValue)4 CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)3 BooleanValue (com.scalar.db.io.BooleanValue)3 DoubleValue (com.scalar.db.io.DoubleValue)3 TextValue (com.scalar.db.io.TextValue)3 Value (com.scalar.db.io.Value)3 CosmosException (com.azure.cosmos.CosmosException)2 Get (com.scalar.db.api.Get)2 Result (com.scalar.db.api.Result)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 UpdateItemRequest (software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest)2 BatchStatement (com.datastax.driver.core.BatchStatement)1 BoundStatement (com.datastax.driver.core.BoundStatement)1