#14 simple blog : pageable
This commit is contained in:
@@ -5,6 +5,8 @@ import com.example.simpleblog.response.PostResponse;
|
||||
import com.example.simpleblog.service.PostService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.web.PageableDefault;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
@@ -28,7 +30,7 @@ public class PostController {
|
||||
}
|
||||
|
||||
@GetMapping("/posts")
|
||||
public List<PostResponse> getPostList() {
|
||||
return postService.getPostList();
|
||||
public List<PostResponse> getPostList(@PageableDefault Pageable pageable) {
|
||||
return postService.getPostList(pageable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.example.simpleblog.request.PostCreate;
|
||||
import com.example.simpleblog.response.PostResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
@@ -39,8 +40,9 @@ public class PostService {
|
||||
.build();
|
||||
}
|
||||
|
||||
public List<PostResponse> getPostList() {
|
||||
return postRepository.findAll().stream()
|
||||
public List<PostResponse> getPostList(Pageable pageable) {
|
||||
// Pageable pageable = PageRequest.of(page, 5, Sort.Direction.DESC, "id");
|
||||
return postRepository.findAll(pageable).stream()
|
||||
.map(PostResponse::new)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -4,6 +4,12 @@ spring:
|
||||
enabled: true
|
||||
path: /h2-console
|
||||
|
||||
data:
|
||||
web:
|
||||
pageable:
|
||||
one-indexed-parameters: true
|
||||
default-page-size: 5
|
||||
|
||||
datasource:
|
||||
url: jdbc:h2:mem:simpleblog
|
||||
username: sa
|
||||
|
||||
@@ -12,6 +12,10 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||
@@ -140,30 +144,25 @@ class PostControllerTest {
|
||||
@DisplayName("글 리스트 조회")
|
||||
void get_post_list_success() throws Exception {
|
||||
// given
|
||||
Post post1 = postRepository.save(Post.builder()
|
||||
.title("제목1")
|
||||
.content("내용1")
|
||||
.build());
|
||||
List<Post> requestPosts = IntStream.range(1, 31)
|
||||
.mapToObj(i -> Post.builder()
|
||||
.title("제목 " + i)
|
||||
.content("내용 " + i)
|
||||
.build()
|
||||
)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Post post2 = postRepository.save(Post.builder()
|
||||
.title("제목2")
|
||||
.content("내용2")
|
||||
.build());
|
||||
|
||||
// json 응답에서 title 값 길이를 최대 10글자
|
||||
postRepository.saveAll(requestPosts);
|
||||
|
||||
// expected
|
||||
mockMvc.perform(get("/posts")
|
||||
mockMvc.perform(get("/posts?page=1&size=5&sort=id,desc")
|
||||
.contentType(APPLICATION_JSON)
|
||||
)
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.length()", is(2)))
|
||||
.andExpect(jsonPath("$[0].id").value(post1.getId()))
|
||||
.andExpect(jsonPath("$[0].title").value("제목1"))
|
||||
.andExpect(jsonPath("$[0].content").value("내용1"))
|
||||
.andExpect(jsonPath("$[1].id").value(post2.getId()))
|
||||
.andExpect(jsonPath("$[1].title").value("제목2"))
|
||||
.andExpect(jsonPath("$[1].content").value("내용2"))
|
||||
.andExpect(jsonPath("$.length()", is(5)))
|
||||
.andExpect(jsonPath("$[0].id").value("30"))
|
||||
.andExpect(jsonPath("$[0].title").value("제목 30"))
|
||||
.andExpect(jsonPath("$[0].content").value("내용 30"))
|
||||
.andDo(print())
|
||||
;
|
||||
|
||||
|
||||
@@ -9,10 +9,15 @@ import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.springframework.data.domain.Sort.Direction.DESC;
|
||||
|
||||
@SpringBootTest
|
||||
class PostServiceTest {
|
||||
@@ -67,26 +72,29 @@ class PostServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("글 리스트 조회")
|
||||
@DisplayName("글 1페이지 조회")
|
||||
void get_posts_list_test() {
|
||||
// given
|
||||
postRepository.saveAll(List.of(
|
||||
Post.builder()
|
||||
.title("글 1제목입니다.")
|
||||
.content("글 1내용입니다.")
|
||||
.build(),
|
||||
Post.builder()
|
||||
.title("글 2제목입니다.")
|
||||
.content("글 2내용입니다.")
|
||||
.build()
|
||||
));
|
||||
List<Post> requestPosts = IntStream.range(1, 31)
|
||||
.mapToObj(i -> Post.builder()
|
||||
.title("제목 " + i)
|
||||
.content("내용 " + i)
|
||||
.build()
|
||||
)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Long postId = 1L;
|
||||
postRepository.saveAll(requestPosts);
|
||||
|
||||
// when
|
||||
List<PostResponse> posts = postService.getPostList();
|
||||
List<PostResponse> posts = postService.getPostList(
|
||||
PageRequest.of(0, 5, DESC, "id")
|
||||
);
|
||||
|
||||
// then
|
||||
assertEquals(2L, posts.size());
|
||||
assertEquals(5L, posts.size());
|
||||
assertEquals("제목 30", posts.get(0).getTitle());
|
||||
assertEquals("내용 30", posts.get(0).getContent());
|
||||
assertEquals("제목 26", posts.get(4).getTitle());
|
||||
assertEquals("내용 26", posts.get(4).getContent());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user