Skip to content

Commit

Permalink
add mapping sample in repository
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Mar 2, 2020
1 parent 22112ae commit 334741c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public Mono<Void> delete(@PathVariable("id") String id) {
}

interface PostRepository extends ReactiveMongoRepository<Post, String> {

Flux<PostSummary> findByTitleContains(String title);
}

@Document
Expand All @@ -125,3 +127,9 @@ class Post {
@CreatedDate
private LocalDateTime createdDate;
}

@Data
class PostSummary {
private String title;
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,25 @@ public class PostRepositoryTest {
@BeforeEach
public void setup() {
this.reactiveMongoTemplate.remove(Post.class).all()
.subscribe(r -> log.debug("delete all posts: " + r), e -> log.debug("error: " + e), () -> log.debug("done"));
.subscribe(r -> log.debug("delete all posts: " + r), e -> log.debug("error: " + e), () -> log.debug("done"));
}

@Test
public void testSavePostAndFindByTitleContains() {
this.postRepository.save(Post.builder().content("my test content").title("my test title").build())
.flatMapMany(p->this.postRepository.findByTitleContains("test"))
.as(StepVerifier::create)
.consumeNextWith(p -> assertThat(p.getTitle()).isEqualTo("my test title"))
.expectComplete()
.verify();
}

@Test
public void testSavePost() {
StepVerifier.create(this.postRepository.save(Post.builder().content("my test content").title("my test title").build()))
.consumeNextWith(p -> assertThat(p.getTitle()).isEqualTo("my test title"))
.expectComplete()
.verify();
.consumeNextWith(p -> assertThat(p.getTitle()).isEqualTo("my test title"))
.expectComplete()
.verify();
}

@Test
Expand All @@ -51,13 +61,13 @@ public void testGetAllPost() {
Post post2 = Post.builder().content("content of another post").title("another post title").build();

Flux<Post> allPosts = Flux.just(post1, post2)
.flatMap(this.postRepository::save)
.thenMany(this.postRepository.findAll(Sort.by((Sort.Direction.ASC), "title")));
.flatMap(this.postRepository::save)
.thenMany(this.postRepository.findAll(Sort.by((Sort.Direction.ASC), "title")));

StepVerifier.create(allPosts)
.expectNextMatches(p -> p.getTitle().equals("another post title"))
.expectNextMatches(p -> p.getTitle().equals("my test title"))
.verifyComplete();
.expectNextMatches(p -> p.getTitle().equals("another post title"))
.expectNextMatches(p -> p.getTitle().equals("my test title"))
.verifyComplete();
}

}

0 comments on commit 334741c

Please sign in to comment.