From 4fa203e79856e28ddc18bed14292c939f27ea4bd Mon Sep 17 00:00:00 2001 From: Kushagra Thapar Date: Mon, 6 May 2024 08:37:09 -0700 Subject: [PATCH 1/2] Ported fix of existsByID API to SB3 --- .../azure-spring-data-cosmos/CHANGELOG.md | 1 + .../cosmos/core/ReactiveCosmosTemplate.java | 3 ++- .../cosmos/core/ReactiveCosmosTemplateIT.java | 20 +++++++++++++++++++ .../ReactiveUUIDIdDomainRepositoryIT.java | 3 +++ ...dDomainPartitionPartitionRepositoryIT.java | 3 +++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/sdk/spring/azure-spring-data-cosmos/CHANGELOG.md b/sdk/spring/azure-spring-data-cosmos/CHANGELOG.md index 82815fb8c3e43..304cad8b02427 100644 --- a/sdk/spring/azure-spring-data-cosmos/CHANGELOG.md +++ b/sdk/spring/azure-spring-data-cosmos/CHANGELOG.md @@ -9,6 +9,7 @@ #### Bugs Fixed * Fixed all saveAll/insertAll bulk functionality to populated audit data - See [PR 39811](https://github.com/Azure/azure-sdk-for-java/pull/39811). +* Fixed `existsById` API in `ReactiveCosmosTemplate` to return `Mono` containing `False` in case the item does not exist - See [PR 40022](https://github.com/Azure/azure-sdk-for-java/pull/39811). #### Other Changes diff --git a/sdk/spring/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplate.java b/sdk/spring/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplate.java index b33a473d6b2d4..20612e32dc35c 100644 --- a/sdk/spring/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplate.java +++ b/sdk/spring/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplate.java @@ -850,7 +850,8 @@ public Mono exists(CosmosQuery query, Class domainType, String conta */ public Mono existsById(Object id, Class domainType, String containerName) { return findById(containerName, id, domainType) - .flatMap(o -> Mono.just(o != null)); + .flatMap(o -> Mono.just(o != null)) + .switchIfEmpty(Mono.just(false)); } /** diff --git a/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplateIT.java b/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplateIT.java index 9a5248b2dc248..90f30b632128b 100644 --- a/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplateIT.java +++ b/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplateIT.java @@ -714,6 +714,26 @@ public void testExists() { Assertions.assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0); } + @Test + public void testNotExists() { + final Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, "firstName", + Collections.singletonList("randomFirstName"), Part.IgnoreCaseType.NEVER); + final CosmosQuery query = new CosmosQuery(criteria); + final Mono exists = cosmosTemplate.exists(query, Person.class, containerName); + StepVerifier.create(exists).expectNext(false).verifyComplete(); + + // add ignore testing + final Criteria criteriaIgnoreCase = Criteria.getInstance(CriteriaType.IS_EQUAL, "firstName", + Collections.singletonList("randomFirstName".toUpperCase()), Part.IgnoreCaseType.ALWAYS); + final CosmosQuery queryIgnoreCase = new CosmosQuery(criteriaIgnoreCase); + final Mono existsIgnoreCase = cosmosTemplate.exists(queryIgnoreCase, Person.class, containerName); + StepVerifier.create(existsIgnoreCase).expectNext(false).verifyComplete(); + + assertThat(responseDiagnosticsTestUtils.getCosmosDiagnostics()).isNotNull(); + Assertions.assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull(); + Assertions.assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0); + } + @Test public void testCount() { final Mono count = cosmosTemplate.count(containerName); diff --git a/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/ReactiveUUIDIdDomainRepositoryIT.java b/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/ReactiveUUIDIdDomainRepositoryIT.java index dd13d14ccf5a8..56a1a65418a80 100644 --- a/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/ReactiveUUIDIdDomainRepositoryIT.java +++ b/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/ReactiveUUIDIdDomainRepositoryIT.java @@ -150,6 +150,9 @@ public void testExistsById() { Mono booleanMono = this.repository.existsById(DOMAIN_1.getNumber()); StepVerifier.create(booleanMono).expectNext(true).expectComplete().verify(); + + booleanMono = this.repository.existsById(UUID.randomUUID()); + StepVerifier.create(booleanMono).expectNext(false).expectComplete().verify(); } private static class InvalidDomain { diff --git a/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveLongIdDomainPartitionPartitionRepositoryIT.java b/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveLongIdDomainPartitionPartitionRepositoryIT.java index 823b7897bc8fa..03bf88ce9359e 100644 --- a/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveLongIdDomainPartitionPartitionRepositoryIT.java +++ b/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveLongIdDomainPartitionPartitionRepositoryIT.java @@ -171,6 +171,9 @@ public void testExistsById() { Mono booleanMono = this.repository.existsById(DOMAIN_1.getNumber()); StepVerifier.create(booleanMono).expectNext(true).expectComplete().verify(); + + booleanMono = this.repository.existsById(0L); + StepVerifier.create(booleanMono).expectNext(false).expectComplete().verify(); } @Test From 3994dd5cc2f6832a1e6557b032edc82bdb21f64a Mon Sep 17 00:00:00 2001 From: Kushagra Thapar Date: Mon, 6 May 2024 08:38:24 -0700 Subject: [PATCH 2/2] Fixed changelog PR link --- sdk/spring/azure-spring-data-cosmos/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/spring/azure-spring-data-cosmos/CHANGELOG.md b/sdk/spring/azure-spring-data-cosmos/CHANGELOG.md index 304cad8b02427..a7ff934c85170 100644 --- a/sdk/spring/azure-spring-data-cosmos/CHANGELOG.md +++ b/sdk/spring/azure-spring-data-cosmos/CHANGELOG.md @@ -9,7 +9,7 @@ #### Bugs Fixed * Fixed all saveAll/insertAll bulk functionality to populated audit data - See [PR 39811](https://github.com/Azure/azure-sdk-for-java/pull/39811). -* Fixed `existsById` API in `ReactiveCosmosTemplate` to return `Mono` containing `False` in case the item does not exist - See [PR 40022](https://github.com/Azure/azure-sdk-for-java/pull/39811). +* Fixed `existsById` API in `ReactiveCosmosTemplate` to return `Mono` containing `False` in case the item does not exist - See [PR 40050](https://github.com/Azure/azure-sdk-for-java/pull/40050). #### Other Changes