From f7e06b79d412f344958da5df36c5d6396910753a Mon Sep 17 00:00:00 2001 From: Kushagra Thapar Date: Wed, 9 Oct 2019 11:35:21 -0500 Subject: [PATCH] Added Delete by Id and partition key API (#437) * Added Delete by Id and partition key API * Added request units to collection create implementation. Updated composite token to be used when checking last page --- .../data/cosmosdb/core/CosmosTemplate.java | 2 +- .../cosmosdb/core/ReactiveCosmosTemplate.java | 2 +- .../cosmosdb/repository/CosmosRepository.java | 10 +++++++++- .../repository/ReactiveCosmosRepository.java | 11 +++++++++- .../support/SimpleCosmosRepository.java | 8 ++++++++ .../SimpleReactiveCosmosRepository.java | 9 +++++++++ .../data/cosmosdb/common/PageTestUtils.java | 2 +- .../integration/AddressRepositoryIT.java | 20 +++++++++++++++++++ 8 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/CosmosTemplate.java b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/CosmosTemplate.java index 7110dba..4603ced 100644 --- a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/CosmosTemplate.java +++ b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/CosmosTemplate.java @@ -254,7 +254,7 @@ public class CosmosTemplate implements CosmosOperations, ApplicationContextAware .flatMap(cosmosDatabaseResponse -> cosmosDatabaseResponse .database() .createContainerIfNotExists(information.getCollectionName(), - "/" + information.getPartitionKeyFieldName()) + "/" + information.getPartitionKeyFieldName(), information.getRequestUnit()) .map(cosmosContainerResponse -> cosmosContainerResponse)) .block(); if (response == null) { diff --git a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/ReactiveCosmosTemplate.java b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/ReactiveCosmosTemplate.java index aaa8035..17fa5ad 100644 --- a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/ReactiveCosmosTemplate.java +++ b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/ReactiveCosmosTemplate.java @@ -90,7 +90,7 @@ public class ReactiveCosmosTemplate implements ReactiveCosmosOperations, Applica .flatMap(cosmosDatabaseResponse -> cosmosDatabaseResponse .database() .createContainerIfNotExists(information.getCollectionName(), - "/" + information.getPartitionKeyFieldName()) + "/" + information.getPartitionKeyFieldName(), information.getRequestUnit()) .map(cosmosContainerResponse -> { this.collectionCache.add(information.getCollectionName()); return cosmosContainerResponse; diff --git a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/CosmosRepository.java b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/CosmosRepository.java index 29dcda1..f4f6c20 100644 --- a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/CosmosRepository.java +++ b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/CosmosRepository.java @@ -20,11 +20,19 @@ public interface CosmosRepository extends PagingAndS * Retrieves an entity by its id. * * @param id must not be {@literal null}. - * @param partitionKey partition key value of entity + * @param partitionKey partition key value of entity, must not be null. * @return the entity with the given id or {@literal Optional#empty()} if none found * @throws IllegalArgumentException if {@code id} is {@literal null}. */ Optional findById(ID id, PartitionKey partitionKey); + /** + * Deletes an entity by its id and partition key. + * @param id must not be {@literal null}. + * @param partitionKey partition key value of the entity, must not be null. + * @throws IllegalArgumentException in case the given {@code id} is {@literal null}. + */ + void deleteById(ID id, PartitionKey partitionKey); + } diff --git a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/ReactiveCosmosRepository.java b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/ReactiveCosmosRepository.java index 92dd3cb..7ea73d9 100644 --- a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/ReactiveCosmosRepository.java +++ b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/ReactiveCosmosRepository.java @@ -16,9 +16,18 @@ public interface ReactiveCosmosRepository extends ReactiveSortingRepositor /** * Retrieves an entity by its id and partition key. * @param id must not be {@literal null}. - * @param partitionKey partition key value of the entity. + * @param partitionKey partition key value of the entity, must not be null. * @return {@link Mono} emitting the entity with the given id or {@link Mono#empty()} if none found. * @throws IllegalArgumentException in case the given {@code id} is {@literal null}. */ Mono findById(K id, PartitionKey partitionKey); + + /** + * Deletes an entity by its id and partition key. + * @param id must not be {@literal null}. + * @param partitionKey partition key value of the entity, must not be null. + * @return {@link Mono} emitting the void Mono. + * @throws IllegalArgumentException in case the given {@code id} is {@literal null}. + */ + Mono deleteById(K id, PartitionKey partitionKey); } diff --git a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/SimpleCosmosRepository.java b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/SimpleCosmosRepository.java index 05501f4..aa6ed8b 100644 --- a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/SimpleCosmosRepository.java +++ b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/SimpleCosmosRepository.java @@ -172,6 +172,14 @@ public class SimpleCosmosRepository implements Cosmo operation.deleteById(information.getCollectionName(), id, null); } + @Override + public void deleteById(ID id, PartitionKey partitionKey) { + Assert.notNull(id, "id to be deleted should not be null"); + Assert.notNull(partitionKey, "partitionKey to be deleted should not be null"); + + operation.deleteById(information.getCollectionName(), id, partitionKey); + } + /** * delete one document per entity * diff --git a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/SimpleReactiveCosmosRepository.java b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/SimpleReactiveCosmosRepository.java index 111a637..db14724 100644 --- a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/SimpleReactiveCosmosRepository.java +++ b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/SimpleReactiveCosmosRepository.java @@ -168,6 +168,15 @@ public class SimpleReactiveCosmosRepository implement id, null)).then(); } + @Override + public Mono deleteById(K id, PartitionKey partitionKey) { + Assert.notNull(id, "Id must not be null!"); + Assert.notNull(partitionKey, "PartitionKey must not be null!"); + + return cosmosOperations.deleteById(entityInformation.getCollectionName(), id, partitionKey); + + } + @Override public Mono delete(@NonNull T entity) { Assert.notNull(entity, "entity to be deleted must not be null!"); diff --git a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/common/PageTestUtils.java b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/common/PageTestUtils.java index d813f8f..c9d0505 100644 --- a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/common/PageTestUtils.java +++ b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/common/PageTestUtils.java @@ -39,6 +39,6 @@ public class PageTestUtils { final JSONObject jsonObject = new JSONObject(tokenJson); - return jsonObject.isNull("token"); + return jsonObject.isNull("compositeToken"); } } diff --git a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/integration/AddressRepositoryIT.java b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/integration/AddressRepositoryIT.java index 1eb867b..846f06e 100644 --- a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/integration/AddressRepositoryIT.java +++ b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/integration/AddressRepositoryIT.java @@ -10,6 +10,7 @@ import com.microsoft.azure.spring.data.cosmosdb.common.TestConstants; import com.microsoft.azure.spring.data.cosmosdb.common.TestUtils; import com.microsoft.azure.spring.data.cosmosdb.core.CosmosTemplate; import com.microsoft.azure.spring.data.cosmosdb.domain.Address; +import com.microsoft.azure.spring.data.cosmosdb.exception.CosmosDBAccessException; import com.microsoft.azure.spring.data.cosmosdb.repository.TestRepositoryConfig; import com.microsoft.azure.spring.data.cosmosdb.repository.repository.AddressRepository; import com.microsoft.azure.spring.data.cosmosdb.repository.support.CosmosEntityInformation; @@ -165,6 +166,25 @@ public class AddressRepositoryIT { assertThat(result.get(0).getCity()).isNotEqualTo(TEST_ADDRESS1_PARTITION1.getCity()); } + @Test(expected = CosmosDBAccessException.class) + public void testDeleteByIdAndPartitionKey() { + final long count = repository.count(); + assertThat(count).isEqualTo(4); + + final Optional
addressById = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(), + new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity())); + assertThat(addressById.isPresent()).isTrue(); + + repository.deleteById(TEST_ADDRESS1_PARTITION1.getPostalCode(), + new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity())); + + final List
result = TestUtils.toList(repository.findAll()); + assertThat(result.size()).isEqualTo(3); + + repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(), + new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity())); + } + @Test public void testUpdateEntity() { final Address updatedAddress = new Address(TEST_ADDRESS1_PARTITION1.getPostalCode(), TestConstants.NEW_STREET,