Skip to content

Commit

Permalink
Changed to use reactive test class StepVerifier to confirm test result (
Browse files Browse the repository at this point in the history
Azure#13370)

* Allow Long as ID type in Entity for Cosmos DB

* Unified processing of ID type conversion and added test cases
Azure#12712

* Updated code smell

* Updated code smell

* Updated getStringIDValue API to return String instead of Object

* Changed to use reactive test class StepVerifier to confirm test result; Azure#12712

* Optimize use case logic

Co-authored-by: Kushagra Thapar <[email protected]>
  • Loading branch information
moarychan and kushagraThapar authored Jul 22, 2020
1 parent b0da2b0 commit 1d5560c
Showing 1 changed file with 74 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@
import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.Objects;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestRepositoryConfig.class)
Expand All @@ -46,8 +44,6 @@ public class ReactiveLongIdDomainPartitionPartitionRepositoryIT {
private static CosmosTemplate staticTemplate;
private static boolean isSetupDone;

private static final Duration DEFAULT_TIME_OUT = Duration.ofSeconds(10);

@Autowired
private CosmosTemplate template;

Expand All @@ -60,8 +56,8 @@ public void setUp() {
staticTemplate = template;
template.createContainerIfNotExists(entityInformation);
}
this.repository.save(DOMAIN_1).block(DEFAULT_TIME_OUT);
this.repository.save(DOMAIN_2).block(DEFAULT_TIME_OUT);
Flux<LongIdDomainPartition> savedAllFlux = this.repository.saveAll(Arrays.asList(DOMAIN_1, DOMAIN_2));
StepVerifier.create(savedAllFlux).thenConsumeWhile(domain -> true).expectComplete().verify();
isSetupDone = true;
}

Expand All @@ -78,19 +74,26 @@ public static void afterClassCleanup() {

@Test
public void testLongIdDomainPartition() {
this.repository.deleteAll().block(DEFAULT_TIME_OUT);
Assert.assertFalse(this.repository.findById(ID_1).blockOptional(DEFAULT_TIME_OUT).isPresent());
Mono<Void> deletedMono = this.repository.deleteAll();
StepVerifier.create(deletedMono).thenAwait().verifyComplete();

this.repository.save(DOMAIN_1).block(DEFAULT_TIME_OUT);
Optional<LongIdDomainPartition> foundOptional = this.repository.findById(ID_1).blockOptional(DEFAULT_TIME_OUT);
Mono<LongIdDomainPartition> idMono = this.repository.findById(ID_1,
new PartitionKey(entityInformation.getPartitionKeyFieldValue(DOMAIN_1)));
StepVerifier.create(idMono).expectNextCount(0).verifyComplete();

Assert.assertTrue(foundOptional.isPresent());
Assert.assertEquals(DOMAIN_1.getNumber(), foundOptional.get().getNumber());
Assert.assertEquals(DOMAIN_1.getName(), foundOptional.get().getName());
Mono<LongIdDomainPartition> saveMono = this.repository.save(DOMAIN_1);
StepVerifier.create(saveMono).expectNext(DOMAIN_1).expectComplete().verify();

this.repository.delete(DOMAIN_1).block(DEFAULT_TIME_OUT);
Mono<LongIdDomainPartition> findIdMono = this.repository.findById(ID_1,
new PartitionKey(entityInformation.getPartitionKeyFieldValue(DOMAIN_1)));
StepVerifier.create(findIdMono).expectNext(DOMAIN_1).expectComplete().verify();

Assert.assertFalse(this.repository.findById(ID_1).blockOptional(DEFAULT_TIME_OUT).isPresent());
Mono<Void> deleteMono = this.repository.delete(DOMAIN_1);
StepVerifier.create(deleteMono).verifyComplete();

Mono<LongIdDomainPartition> afterDelIdMono = this.repository.findById(ID_1,
new PartitionKey(entityInformation.getPartitionKeyFieldValue(DOMAIN_1)));
StepVerifier.create(afterDelIdMono).expectNextCount(0).verifyComplete();
}

@Test(expected = IllegalArgumentException.class)
Expand All @@ -99,32 +102,21 @@ public void testInvalidDomain() {
}

@Test
public void testBasicQuery() {
LongIdDomainPartition save = this.repository.save(DOMAIN_1).block(DEFAULT_TIME_OUT);
Assert.assertNotNull(save);
}
public void testSaveAllAndFindAll() {
final Mono<Void> deletedMono = repository.deleteAll();
StepVerifier.create(deletedMono).thenAwait().verifyComplete();

@Test
public void testSaveAndFindById() {
Assert.assertNotNull(this.repository.save(DOMAIN_1).block(DEFAULT_TIME_OUT));
Optional<LongIdDomainPartition> longIdDomainPartitionOptional = this.repository
.findById(DOMAIN_1.getNumber()).blockOptional(DEFAULT_TIME_OUT);
Assert.assertTrue(longIdDomainPartitionOptional.isPresent());
Assert.assertEquals(DOMAIN_1, longIdDomainPartitionOptional.get());
}
Flux<LongIdDomainPartition> savedAllFlux = this.repository.saveAll(Arrays.asList(DOMAIN_1, DOMAIN_2));
StepVerifier.create(savedAllFlux).expectNextCount(2).verifyComplete();

@Test
public void testSaveAllAndFindAll() {
this.repository.deleteAll().block(DEFAULT_TIME_OUT);
List<LongIdDomainPartition> savedEntities = Stream.of(DOMAIN_1, DOMAIN_2).collect(Collectors.toList());
this.repository.saveAll(savedEntities).collectList().block(DEFAULT_TIME_OUT);
List<LongIdDomainPartition> longIdDomainPartitionList = this.repository.findAll().collectList().block(DEFAULT_TIME_OUT);
Assert.assertTrue(longIdDomainPartitionList.containsAll(savedEntities));
final Flux<LongIdDomainPartition> allFlux = repository.findAll();
StepVerifier.create(allFlux).expectNextCount(2).verifyComplete();
}

@Test
public void testCount() {
Assert.assertTrue(2 == repository.count().block(DEFAULT_TIME_OUT));
Mono<Long> countMono = repository.count();
StepVerifier.create(countMono).expectNext(2L).verifyComplete();
}

@Test
Expand All @@ -139,66 +131,77 @@ public void testDeleteByIdAndPartitionKey() {
new PartitionKey(entityInformation.getPartitionKeyFieldValue(DOMAIN_1)));
StepVerifier.create(deleteMono).verifyComplete();

final Mono<LongIdDomainPartition> byId = repository.findById(DOMAIN_1.getNumber(),
Mono<LongIdDomainPartition> findIdMono = this.repository.findById(ID_1,
new PartitionKey(entityInformation.getPartitionKeyFieldValue(DOMAIN_1)));
Assert.assertNull(byId.block(DEFAULT_TIME_OUT));
StepVerifier.create(findIdMono).expectNextCount(0).verifyComplete();
}

@Test(expected = CosmosAccessException.class)
@Test
public void testDeleteByIdShouldFailIfNothingToDelete() {
this.repository.deleteAll().block(DEFAULT_TIME_OUT);
this.repository.deleteById(DOMAIN_1.getNumber()).block(DEFAULT_TIME_OUT);
final Mono<Void> deletedMono = repository.deleteAll();
StepVerifier.create(deletedMono).thenAwait().verifyComplete();

final Mono<Void> deleteIdMono = repository.deleteById(DOMAIN_1.getNumber(),
new PartitionKey(entityInformation.getPartitionKeyFieldValue(DOMAIN_1)));
StepVerifier.create(deleteIdMono).expectError(CosmosAccessException.class).verify();
}

@Test
public void testDelete() {
this.repository.save(DOMAIN_1).block(DEFAULT_TIME_OUT);
this.repository.delete(DOMAIN_1).block(DEFAULT_TIME_OUT);
Assert.assertTrue(1 == this.repository.count().block(DEFAULT_TIME_OUT));
Mono<LongIdDomainPartition> saveMono = this.repository.save(DOMAIN_1);
StepVerifier.create(saveMono).expectNext(DOMAIN_1).expectComplete().verify();

Mono<Void> deleteMono = this.repository.delete(DOMAIN_1);
StepVerifier.create(deleteMono).verifyComplete();

Mono<Long> countMono = repository.count();
StepVerifier.create(countMono).expectNext(1L).verifyComplete();
}

@Test(expected = CosmosAccessException.class)
@Test
public void testDeleteShouldFailIfNothingToDelete() {
this.repository.deleteAll().block(DEFAULT_TIME_OUT);
this.repository.delete(DOMAIN_1).block(DEFAULT_TIME_OUT);
final Mono<Void> deletedMono = repository.deleteAll();
StepVerifier.create(deletedMono).thenAwait().verifyComplete();

Mono<Void> deleteIdMono = this.repository.delete(DOMAIN_1);
StepVerifier.create(deleteIdMono).expectError(CosmosAccessException.class).verify();
}

@Test
public void testDeleteAll() {
this.repository.save(DOMAIN_1).block(DEFAULT_TIME_OUT);
this.repository.save(DOMAIN_2).block(DEFAULT_TIME_OUT);
this.repository.deleteAll(Arrays.asList(DOMAIN_1, DOMAIN_2)).block(DEFAULT_TIME_OUT);
Assert.assertTrue(0 == this.repository.count().block(DEFAULT_TIME_OUT));
Flux<LongIdDomainPartition> savedAllFlux = this.repository.saveAll(Arrays.asList(DOMAIN_1, DOMAIN_2));
StepVerifier.create(savedAllFlux).expectNextCount(2).verifyComplete();

final Mono<Void> deletedMono = repository.deleteAll();
StepVerifier.create(deletedMono).thenAwait().verifyComplete();

Mono<Long> countMono = repository.count();
StepVerifier.create(countMono).expectNext(0L).verifyComplete();
}

@Test
public void testExistsById() {
this.repository.save(DOMAIN_1).block(DEFAULT_TIME_OUT);
Assert.assertTrue(this.repository.existsById(DOMAIN_1.getNumber()).block(DEFAULT_TIME_OUT));
Mono<LongIdDomainPartition> saveMono = this.repository.save(DOMAIN_1);
StepVerifier.create(saveMono).expectNext(DOMAIN_1).expectComplete().verify();

Mono<Boolean> booleanMono = this.repository.existsById(DOMAIN_1.getNumber());
StepVerifier.create(booleanMono).expectNext(true).expectComplete().verify();
}

@Test
public void testFindAllSort() {
final LongIdDomainPartition other = new LongIdDomainPartition(DOMAIN_1.getNumber() + 1, "other-name");
this.repository.save(other).block(DEFAULT_TIME_OUT);
this.repository.save(DOMAIN_1).block(DEFAULT_TIME_OUT);
final LongIdDomainPartition other = new LongIdDomainPartition(
DOMAIN_1.getNumber() + 1, "other-name");
Flux<LongIdDomainPartition> savedAllFlux = this.repository.saveAll(Arrays.asList(DOMAIN_1, other));
StepVerifier.create(savedAllFlux).thenConsumeWhile(domain -> true).expectComplete().verify();

final Sort ascSort = Sort.by(Sort.Direction.ASC, "number");
final List<LongIdDomainPartition> ascending = this.repository.findAll(ascSort)
.collectList().block(DEFAULT_TIME_OUT);
Assert.assertEquals(3, ascending.size());
Assert.assertEquals(DOMAIN_1, ascending.get(0));
Assert.assertEquals(other, ascending.get(1));
Assert.assertEquals(DOMAIN_2, ascending.get(2));
Flux<LongIdDomainPartition> ascAllFlux = this.repository.findAll(ascSort);
StepVerifier.create(ascAllFlux).expectNext(DOMAIN_1, other, DOMAIN_2).verifyComplete();

final Sort descSort = Sort.by(Sort.Direction.DESC, "number");
final List<LongIdDomainPartition> descending = this.repository.findAll(descSort)
.collectList().block(DEFAULT_TIME_OUT);
Assert.assertEquals(3, descending.size());
Assert.assertEquals(DOMAIN_2, descending.get(0));
Assert.assertEquals(other, descending.get(1));
Assert.assertEquals(DOMAIN_1, descending.get(2));

Flux<LongIdDomainPartition> descAllFlux = this.repository.findAll(descSort);
StepVerifier.create(descAllFlux).expectNext(DOMAIN_2, other, DOMAIN_1).verifyComplete();
}

private static class InvalidDomain {
Expand Down

0 comments on commit 1d5560c

Please sign in to comment.