Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fixed existsById API in ReactiveCosmosTemplate
  • Loading branch information
kushagraThapar committed May 3, 2024
commit 8c31d317ae88307faf0d49e84d9a5cfa7ee46f02
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