Search in sources :

Example 1 with CosmosStoredProcedureResponse

use of com.azure.cosmos.models.CosmosStoredProcedureResponse in project DataSpaceConnector by eclipse-dataspaceconnector.

the class CosmosTransferProcessStoreIntegrationTest method invokeStoreProcedure.

@Test
void invokeStoreProcedure() {
    // create one item
    var tp = createTransferProcess("proc1");
    store.create(tp);
    List<Object> procedureParams = Arrays.asList(100, 5, connectorId);
    CosmosStoredProcedureRequestOptions options = new CosmosStoredProcedureRequestOptions();
    options.setPartitionKey(PartitionKey.NONE);
    CosmosStoredProcedureResponse sprocResponse = container.getScripts().getStoredProcedure("nextForState").execute(procedureParams, options);
    var result = sprocResponse.getResponseAsString();
    var l = typeManager.readValue(result, List.class);
    List<TransferProcessDocument> documents = (List<TransferProcessDocument>) l.stream().map(o -> typeManager.writeValueAsString(o)).map(json -> typeManager.readValue(json.toString(), TransferProcessDocument.class)).collect(Collectors.toList());
    assertThat(documents).allSatisfy(document -> {
        assertThat(document.getLease()).isNotNull().hasFieldOrPropertyWithValue("leasedBy", connectorId).hasFieldOrProperty("leasedAt");
    });
}
Also used : BadRequestException(com.azure.cosmos.implementation.BadRequestException) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) CosmosTestClient(org.eclipse.dataspaceconnector.azure.testfixtures.CosmosTestClient) TypeManager(org.eclipse.dataspaceconnector.spi.types.TypeManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CosmosDatabase(com.azure.cosmos.CosmosDatabase) PartitionKey(com.azure.cosmos.models.PartitionKey) Scanner(java.util.Scanner) TransferProcess(org.eclipse.dataspaceconnector.spi.types.domain.transfer.TransferProcess) TestHelper.createTransferProcess(org.eclipse.dataspaceconnector.transfer.store.cosmos.TestHelper.createTransferProcess) AfterAll(org.junit.jupiter.api.AfterAll) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BeforeAll(org.junit.jupiter.api.BeforeAll) CosmosDbApiImpl(org.eclipse.dataspaceconnector.azure.cosmos.CosmosDbApiImpl) ResourceManifest(org.eclipse.dataspaceconnector.spi.types.domain.transfer.ResourceManifest) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) DataRequest(org.eclipse.dataspaceconnector.spi.types.domain.transfer.DataRequest) Set(java.util.Set) RetryPolicy(net.jodah.failsafe.RetryPolicy) EdcException(org.eclipse.dataspaceconnector.spi.EdcException) CosmosStoredProcedureResponse(com.azure.cosmos.models.CosmosStoredProcedureResponse) UUID(java.util.UUID) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) TransferProcessDocument(org.eclipse.dataspaceconnector.transfer.store.cosmos.model.TransferProcessDocument) Collectors(java.util.stream.Collectors) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) CosmosScripts(com.azure.cosmos.CosmosScripts) CosmosPagedIterable(com.azure.cosmos.util.CosmosPagedIterable) IntegrationTest(org.eclipse.dataspaceconnector.common.annotations.IntegrationTest) CosmosContainer(com.azure.cosmos.CosmosContainer) TransferProcessStates(org.eclipse.dataspaceconnector.spi.types.domain.transfer.TransferProcessStates) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) TransferProcessDocument(org.eclipse.dataspaceconnector.transfer.store.cosmos.model.TransferProcessDocument) List(java.util.List) CosmosStoredProcedureResponse(com.azure.cosmos.models.CosmosStoredProcedureResponse) Test(org.junit.jupiter.api.Test) IntegrationTest(org.eclipse.dataspaceconnector.common.annotations.IntegrationTest)

Example 2 with CosmosStoredProcedureResponse

use of com.azure.cosmos.models.CosmosStoredProcedureResponse in project DataSpaceConnector by eclipse-dataspaceconnector.

the class CosmosDbApiImpl method invokeStoredProcedure.

@Override
public String invokeStoredProcedure(String procedureName, String partitionKey, Object... args) {
    var sproc = getStoredProcedure(procedureName);
    List<Object> params = Arrays.asList(args);
    var options = new CosmosStoredProcedureRequestOptions();
    options.setPartitionKey(new PartitionKey(partitionKey));
    CosmosStoredProcedureResponse response = sproc.execute(params, options);
    handleResponse(response.getStatusCode(), "Failed to invoke stored procedure: " + procedureName);
    return response.getResponseAsString();
}
Also used : CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosStoredProcedureResponse(com.azure.cosmos.models.CosmosStoredProcedureResponse)

Example 3 with CosmosStoredProcedureResponse

use of com.azure.cosmos.models.CosmosStoredProcedureResponse in project DataSpaceConnector by eclipse-dataspaceconnector.

the class CosmosTransferProcessStoreIntegrationTest method invokeStoredProcedure.

@Test
void invokeStoredProcedure() {
    // create one item
    var tp = createTransferProcess("proc1");
    store.create(tp);
    List<Object> procedureParams = Arrays.asList(100, 5, connectorId);
    CosmosStoredProcedureRequestOptions options = new CosmosStoredProcedureRequestOptions();
    options.setPartitionKey(PartitionKey.NONE);
    CosmosStoredProcedureResponse sprocResponse = container.getScripts().getStoredProcedure("nextForState").execute(procedureParams, options);
    var result = sprocResponse.getResponseAsString();
    var l = typeManager.readValue(result, List.class);
    List<TransferProcessDocument> documents = (List<TransferProcessDocument>) l.stream().map(o -> typeManager.writeValueAsString(o)).map(json -> typeManager.readValue(json.toString(), TransferProcessDocument.class)).collect(Collectors.toList());
    assertThat(documents).allSatisfy(document -> {
        assertThat(document.getLease()).isNotNull().hasFieldOrPropertyWithValue("leasedBy", connectorId).hasFieldOrProperty("leasedAt");
    });
}
Also used : SortOrder(org.eclipse.dataspaceconnector.spi.query.SortOrder) IntStream(java.util.stream.IntStream) BadRequestException(com.azure.cosmos.implementation.BadRequestException) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) CosmosTestClient(org.eclipse.dataspaceconnector.azure.testfixtures.CosmosTestClient) TypeManager(org.eclipse.dataspaceconnector.spi.types.TypeManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CosmosDatabase(com.azure.cosmos.CosmosDatabase) PartitionKey(com.azure.cosmos.models.PartitionKey) TransferProcess(org.eclipse.dataspaceconnector.spi.types.domain.transfer.TransferProcess) TestHelper.createTransferProcess(org.eclipse.dataspaceconnector.transfer.store.cosmos.TestHelper.createTransferProcess) AfterAll(org.junit.jupiter.api.AfterAll) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) AzureCosmosDbIntegrationTest(org.eclipse.dataspaceconnector.azure.testfixtures.annotations.AzureCosmosDbIntegrationTest) CosmosDbApiImpl(org.eclipse.dataspaceconnector.azure.cosmos.CosmosDbApiImpl) ResourceManifest(org.eclipse.dataspaceconnector.spi.types.domain.transfer.ResourceManifest) Awaitility.await(org.awaitility.Awaitility.await) DataRequest(org.eclipse.dataspaceconnector.spi.types.domain.transfer.DataRequest) RetryPolicy(net.jodah.failsafe.RetryPolicy) EdcException(org.eclipse.dataspaceconnector.spi.EdcException) CosmosStoredProcedureResponse(com.azure.cosmos.models.CosmosStoredProcedureResponse) UUID(java.util.UUID) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) TransferProcessDocument(org.eclipse.dataspaceconnector.transfer.store.cosmos.model.TransferProcessDocument) Collectors(java.util.stream.Collectors) QuerySpec(org.eclipse.dataspaceconnector.spi.query.QuerySpec) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) TestHelper.createTransferProcessDocument(org.eclipse.dataspaceconnector.transfer.store.cosmos.TestHelper.createTransferProcessDocument) ChronoUnit(java.time.temporal.ChronoUnit) CosmosPagedIterable(com.azure.cosmos.util.CosmosPagedIterable) CosmosContainer(com.azure.cosmos.CosmosContainer) TransferProcessStates(org.eclipse.dataspaceconnector.spi.types.domain.transfer.TransferProcessStates) Comparator(java.util.Comparator) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) TransferProcessDocument(org.eclipse.dataspaceconnector.transfer.store.cosmos.model.TransferProcessDocument) TestHelper.createTransferProcessDocument(org.eclipse.dataspaceconnector.transfer.store.cosmos.TestHelper.createTransferProcessDocument) List(java.util.List) CosmosStoredProcedureResponse(com.azure.cosmos.models.CosmosStoredProcedureResponse) AzureCosmosDbIntegrationTest(org.eclipse.dataspaceconnector.azure.testfixtures.annotations.AzureCosmosDbIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with CosmosStoredProcedureResponse

use of com.azure.cosmos.models.CosmosStoredProcedureResponse in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SampleStoredProcedure method executeStoredProcedureArrayArg.

public void executeStoredProcedureArrayArg() throws Exception {
    logger.info(String.format("Executing stored procedure %s...\n\n", sprocId + "ArrayArg"));
    String partitionValue = "Seattle";
    CosmosStoredProcedureRequestOptions options = new CosmosStoredProcedureRequestOptions();
    options.setPartitionKey(new PartitionKey(partitionValue));
    List<Object> pojos = new ArrayList<>();
    pojos.add(new CustomPOJO("idA", partitionValue));
    pojos.add(new CustomPOJO("idB", partitionValue));
    pojos.add(new CustomPOJO("idC", partitionValue));
    List<Object> sproc_args = new ArrayList<>();
    sproc_args.add(OBJECT_MAPPER.writeValueAsString(pojos));
    CosmosStoredProcedureResponse executeResponse = container.getScripts().getStoredProcedure(sprocId + "ArrayArg").execute(sproc_args, options);
    logger.info(String.format("Stored procedure %s returned %s (HTTP %d), at cost %.3f RU.\n", sprocId + "ArrayArg", executeResponse.getResponseAsString(), executeResponse.getStatusCode(), executeResponse.getRequestCharge()));
}
Also used : CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) ArrayList(java.util.ArrayList) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosStoredProcedureResponse(com.azure.cosmos.models.CosmosStoredProcedureResponse) CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO)

Example 5 with CosmosStoredProcedureResponse

use of com.azure.cosmos.models.CosmosStoredProcedureResponse in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SampleStoredProcedure method executeStoredProcedure.

public void executeStoredProcedure() throws Exception {
    logger.info(String.format("Executing stored procedure %s...\n\n", sprocId));
    CosmosStoredProcedureRequestOptions options = new CosmosStoredProcedureRequestOptions();
    options.setPartitionKey(new PartitionKey("Seattle"));
    CosmosStoredProcedureResponse executeResponse = container.getScripts().getStoredProcedure(sprocId).execute(null, options);
    logger.info(String.format("Stored procedure %s returned %s (HTTP %d), at cost %.3f RU.\n", sprocId, executeResponse.getResponseAsString(), executeResponse.getStatusCode(), executeResponse.getRequestCharge()));
}
Also used : CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosStoredProcedureResponse(com.azure.cosmos.models.CosmosStoredProcedureResponse)

Aggregations

CosmosStoredProcedureResponse (com.azure.cosmos.models.CosmosStoredProcedureResponse)6 CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)5 PartitionKey (com.azure.cosmos.models.PartitionKey)5 CosmosContainer (com.azure.cosmos.CosmosContainer)2 CosmosDatabase (com.azure.cosmos.CosmosDatabase)2 CosmosScripts (com.azure.cosmos.CosmosScripts)2 BadRequestException (com.azure.cosmos.implementation.BadRequestException)2 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)2 CosmosStoredProcedureProperties (com.azure.cosmos.models.CosmosStoredProcedureProperties)2 CosmosPagedIterable (com.azure.cosmos.util.CosmosPagedIterable)2 ChronoUnit (java.time.temporal.ChronoUnit)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Scanner (java.util.Scanner)2 UUID (java.util.UUID)2 Collectors (java.util.stream.Collectors)2 RetryPolicy (net.jodah.failsafe.RetryPolicy)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)2 CosmosDbApiImpl (org.eclipse.dataspaceconnector.azure.cosmos.CosmosDbApiImpl)2