diff --git a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplate.java b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplate.java index 4e76afd..81e3013 100644 --- a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplate.java +++ b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplate.java @@ -9,6 +9,7 @@ package com.microsoft.azure.spring.data.cosmosdb.core; import com.azure.data.cosmos.AccessCondition; import com.azure.data.cosmos.AccessConditionType; import com.azure.data.cosmos.CosmosClient; +import com.azure.data.cosmos.CosmosClientException; import com.azure.data.cosmos.CosmosContainerResponse; import com.azure.data.cosmos.CosmosItemProperties; import com.azure.data.cosmos.CosmosItemRequestOptions; @@ -16,6 +17,7 @@ import com.azure.data.cosmos.CosmosItemResponse; import com.azure.data.cosmos.FeedOptions; import com.azure.data.cosmos.FeedResponse; import com.azure.data.cosmos.SqlQuerySpec; +import com.azure.data.cosmos.internal.HttpConstants; import com.microsoft.azure.documentdb.DocumentCollection; import com.microsoft.azure.documentdb.PartitionKey; import com.microsoft.azure.spring.data.cosmosdb.CosmosDbFactory; @@ -145,7 +147,15 @@ public class DocumentDbTemplate implements DocumentDbOperations, ApplicationCont .stream() .map(cosmosItem -> mappingDocumentDbConverter.read(domainClass, cosmosItem)) .findFirst())) - .onErrorResume(this::databaseAccessExceptionHandler) + .onErrorResume(e -> { + if (e instanceof CosmosClientException) { + final CosmosClientException cosmosClientException = (CosmosClientException) e; + if (cosmosClientException.statusCode() == HttpConstants.StatusCodes.NOTFOUND) { + return Mono.empty(); + } + } + return Mono.error(e); + }) .blockFirst(); } catch (Exception e) { @@ -170,7 +180,15 @@ public class DocumentDbTemplate implements DocumentDbOperations, ApplicationCont .read() .flatMap(cosmosItemResponse -> Mono.justOrEmpty(toDomainObject(entityClass, cosmosItemResponse.properties()))) - .onErrorResume(this::databaseAccessExceptionHandler) + .onErrorResume(e -> { + if (e instanceof CosmosClientException) { + final CosmosClientException cosmosClientException = (CosmosClientException) e; + if (cosmosClientException.statusCode() == HttpConstants.StatusCodes.NOTFOUND) { + return Mono.empty(); + } + } + return Mono.error(e); + }) .block(); } catch (Exception e) { 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 74be5b4..70cf924 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 @@ -166,12 +166,12 @@ public class AddressRepositoryIT { assertThat(result.get(0).getCity()).isNotEqualTo(TEST_ADDRESS1_PARTITION1.getCity()); } - @Test(expected = DocumentDBAccessException.class) + @Test public void testDeleteByIdAndPartitionKey() { final long count = repository.count(); assertThat(count).isEqualTo(4); - final Optional
addressById = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(), + Optional addressById = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(), new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity())); assertThat(addressById.isPresent()).isTrue(); @@ -181,8 +181,9 @@ public class AddressRepositoryIT { final List result = TestUtils.toList(repository.findAll()); assertThat(result.size()).isEqualTo(3); - repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(), + addressById = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(), new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity())); + assertThat(addressById.isPresent()).isFalse(); } @Test diff --git a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/integration/ContactRepositoryIT.java b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/integration/ContactRepositoryIT.java index 00cac49..fe67b5e 100644 --- a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/integration/ContactRepositoryIT.java +++ b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/integration/ContactRepositoryIT.java @@ -160,7 +160,7 @@ public class ContactRepositoryIT { @Ignore // TODO(kuthapar): v3 doesn't support creation of items without id. public void testNullIdContact() { final Contact nullIdContact = new Contact(null, "testTitile"); - final Contact savedContact = this.repository.save(nullIdContact); + final Contact savedContact = repository.save(nullIdContact); Assert.assertNotNull(savedContact.getLogicId()); Assert.assertEquals(nullIdContact.getTitle(), savedContact.getTitle()); @@ -168,10 +168,17 @@ public class ContactRepositoryIT { @Test public void testFindById() { - final Optional