Skip to content

Commit

Permalink
added count by title
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Jan 23, 2018
1 parent 5ad056f commit 607a026
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 2 deletions.
5 changes: 5 additions & 0 deletions data-mongo-pageable/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.Map;

/**
* @author hantsy
*/
Expand All @@ -30,11 +33,17 @@ public Flux<Post> all() {
return this.posts.findAll();
}

@GetMapping("/count")
public Mono<Map> count(@RequestParam() String q) {
return this.posts.countByTitleLike(q)
.map(c -> Collections.singletonMap("count", c));
}

@GetMapping("/search")
public Flux<Post> search(
@RequestParam() String q,
@RequestParam(name="page", defaultValue = "0") Integer page,
@RequestParam(name="size", defaultValue = "10") Integer size
@RequestParam(name = "page", defaultValue = "0") Integer page,
@RequestParam(name = "size", defaultValue = "10") Integer size
) {
return this.posts.findByTitleLike(q, PageRequest.of(page, size));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

interface PostRepository extends ReactiveMongoRepository<Post, String>{
Flux<Post> findByTitleLike(String title, Pageable page);

Mono<Long> countByTitleLike(String title);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -48,5 +49,30 @@ public void searchPostsByKeyword_shouldBeOK() throws Exception {
.expectBodyList(Post.class).hasSize(10);
}

@Test
@Ignore
public void countPostsByKeyword_shouldBeOK() throws Exception {
this.rest
.get()
.uri("/posts/count?q=0")
.exchange()
.expectStatus().isOk()
.expectBody().jsonPath("$.count").isEqualTo(10);

this.rest
.get()
.uri("/posts/count?q=5")
.exchange()
.expectStatus().isOk()
.expectBody().jsonPath("$.count").isEqualTo(19);

this.rest
.get()
.uri("/posts/count?q=post")
.exchange()
.expectStatus().isOk()
.expectBody().jsonPath("$.count").isEqualTo(100);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.example.demo;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;

import java.time.Duration;

/**
*
* @author hantsy
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Application.class)
public class IntegrationTests {

@Value("#{@nettyContext.address().getPort()}")
int port;

WebTestClient rest;

@Before
public void setup() {
this.rest =WebTestClient
.bindToServer()
.responseTimeout(Duration.ofDays(1))
.baseUrl("http://localhost:" + this.port)
.build();
}

@Test
public void searchPostsByKeyword_shouldBeOK() throws Exception {
this.rest
.get()
.uri("/posts/search?q=post")
.exchange()
.expectStatus().isOk()
.expectBodyList(Post.class).hasSize(10);
}

@Test
public void countPostsByKeyword_shouldBeOK() throws Exception {
this.rest
.get()
.uri("/posts/count?q=0")
.exchange()
.expectStatus().isOk()
.expectBody().jsonPath("$.count").isEqualTo(10);

this.rest
.get()
.uri("/posts/count?q=5")
.exchange()
.expectStatus().isOk()
.expectBody().jsonPath("$.count").isEqualTo(19);

this.rest
.get()
.uri("/posts/count?q=post")
.exchange()
.expectStatus().isOk()
.expectBody().jsonPath("$.count").isEqualTo(100);
}


}

0 comments on commit 607a026

Please sign in to comment.