Skip to content

Commit

Permalink
clean querydsl example.
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Mar 3, 2020
1 parent 46cac7d commit 4fd23b7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 71 deletions.
21 changes: 4 additions & 17 deletions boot-data-mongo-querydsl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.M1</version>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
Expand All @@ -15,8 +15,7 @@
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<querydsl.version>4.2.1</querydsl.version>
<java.version>11</java.version>
</properties>

<dependencies>
Expand All @@ -31,12 +30,10 @@
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-mongodb</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -50,21 +47,11 @@
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.vintage</groupId>
<artifactId>:junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ public DataInitializer(PostRepository posts) {
public void run(String[] args) {
log.info("start data initialization ...");
this.posts
.deleteAll()
.thenMany(
Flux
.just("Post one", "Post two")
.flatMap(
title -> this.posts.save(Post.builder().title(title).content("content of " + title).build())
)
)
.log()
.subscribe(
null,
null,
() -> log.info("done initialization...")
);
.deleteAll()
.thenMany(
Flux
.just("Post one", "Post two")
.flatMap(
title -> this.posts.save(Post.builder().title(title).content("content of " + title).build())
)
)
.log()
.subscribe(
data -> log.info("data" + data),
error -> log.error("error" + error),
() -> log.info("done initialization...")
);

}

Expand All @@ -73,12 +73,12 @@ public PostController(PostRepository posts) {
@GetMapping("")
public Flux<Post> all(@RequestParam("q") String q) {
return Optional.ofNullable(q)
.map(
keyword -> this.posts.findAll(QPost.post.title.containsIgnoreCase(keyword)
.or(QPost.post.content.containsIgnoreCase(keyword)))
).orElse(
this.posts.findAll()
);
.map(
keyword -> this.posts.findAll(QPost.post.title.containsIgnoreCase(keyword)
.or(QPost.post.content.containsIgnoreCase(keyword)))
).orElse(
this.posts.findAll()
);
}

@PostMapping("")
Expand All @@ -94,13 +94,13 @@ public Mono<Post> get(@PathVariable("id") String id) {
@PutMapping("/{id}")
public Mono<Post> update(@PathVariable("id") String id, @RequestBody Post post) {
return this.posts.findById(id)
.map(p -> {
p.setTitle(post.getTitle());
p.setContent(post.getContent());
.map(p -> {
p.setTitle(post.getTitle());
p.setContent(post.getContent());

return p;
})
.flatMap(this.posts::save);
return p;
})
.flatMap(this.posts::save);
}

@DeleteMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,28 @@ 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 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();
}


@Test
public void testSaveAndVerifyPost() {
Post saved = this.postRepository.save(Post.builder().content("my test content").title("my test title").build()).block();
assertThat(saved.getId()).isNotNull();
assertThat(this.reactiveMongoTemplate.collectionExists(Post.class).block()).isTrue();
assertThat(this.reactiveMongoTemplate.findById(saved.getId(), Post.class).block().getTitle()).isEqualTo("my test title");
}


@Test
public void testGetAllPost() {
Post post1 = Post.builder().content("my test content").title("my test title").build();
Post post2 = Post.builder().content("content of another post").title("another post title").build();

Flux<Post> allPosts = this.postRepository
.saveAll(asList(post1, post2))
.thenMany(this.postRepository.findAll(Sort.by((Sort.Direction.ASC), "title")));
.saveAll(asList(post1, post2))
.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();


this.postRepository.findAll(QPost.post.title.containsIgnoreCase("my"))
.as(StepVerifier::create)
.expectNextMatches(p -> p.getTitle().equals("my test title"))
.verifyComplete();
.as(StepVerifier::create)
.expectNextMatches(p -> p.getTitle().equals("my test title"))
.verifyComplete();
}

}

0 comments on commit 4fd23b7

Please sign in to comment.