Skip to content

Commit

Permalink
Ported fix of existsByID API to SB3 (#40050)
Browse files Browse the repository at this point in the history
* Ported fix of existsByID API to SB3

* Fixed changelog PR link
  • Loading branch information
kushagraThapar authored May 6, 2024
1 parent be982ca commit 9c3e766
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions sdk/spring/azure-spring-data-cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>` containing `False` in case the item does not exist - See [PR 40050](https://github.com/Azure/azure-sdk-for-java/pull/40050).

#### Other Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,8 @@ public Mono<Boolean> exists(CosmosQuery query, Class<?> domainType, String conta
*/
public Mono<Boolean> 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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> 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<Boolean> 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<Long> count = cosmosTemplate.count(containerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ public void testExistsById() {

Mono<Boolean> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ public void testExistsById() {

Mono<Boolean> 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
Expand Down

0 comments on commit 9c3e766

Please sign in to comment.