#14 simple blog : get post api

This commit is contained in:
haerong22
2022-07-25 03:30:35 +09:00
parent dd47b0019d
commit bcd8ef83ed
4 changed files with 107 additions and 4 deletions

View File

@@ -1,12 +1,11 @@
package com.example.simpleblog.controller;
import com.example.simpleblog.domain.Post;
import com.example.simpleblog.request.PostCreate;
import com.example.simpleblog.service.PostService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@@ -21,4 +20,9 @@ public class PostController {
public void post(@RequestBody @Valid PostCreate request) {
postService.write(request);
}
@GetMapping("/posts/{postId}")
public Post getPost(@PathVariable(name = "postId") Long id) {
return postService.getPost(id);
}
}

View File

@@ -23,4 +23,9 @@ public class PostService {
postRepository.save(post);
}
public Post getPost(Long id) {
return postRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 글입니다."));
}
}

View File

@@ -14,6 +14,7 @@ import org.springframework.test.web.servlet.MockMvc;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@@ -82,7 +83,7 @@ class PostControllerTest {
@Test
@DisplayName("/posts 요청시 DB에 값이 저장된다.")
void save_post() throws Exception {
void save_post_success() throws Exception {
// given
PostCreate request = PostCreate.builder()
.title("글 제목입니다.")
@@ -107,4 +108,28 @@ class PostControllerTest {
assertEquals("글 제목입니다.", post.getTitle());
assertEquals("글 내용입니다.", post.getContent());
}
@Test
@DisplayName("postId 로 글 조회")
void get_post_by_id_success() throws Exception {
// given
Post post = Post.builder()
.title("글 제목입니다.")
.content("글 내용입니다.")
.build();
postRepository.save(post);
// expected
mockMvc.perform(get("/posts/{postId}", post.getId())
.contentType(APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(post.getId()))
.andExpect(jsonPath("$.title").value("글 제목입니다."))
.andExpect(jsonPath("$.content").value("글 내용입니다."))
.andDo(print())
;
}
}

View File

@@ -0,0 +1,69 @@
package com.example.simpleblog.service;
import com.example.simpleblog.domain.Post;
import com.example.simpleblog.repository.PostRepository;
import com.example.simpleblog.request.PostCreate;
import org.junit.jupiter.api.BeforeEach;
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 static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class PostServiceTest {
@Autowired
private PostService postService;
@Autowired
private PostRepository postRepository;
@BeforeEach
void clean() {
postRepository.deleteAll();
}
@Test
@DisplayName("글 작성")
void post_service_test() throws Exception {
// given
PostCreate postCreate = PostCreate.builder()
.title("글 제목입니다.")
.content("글 내용입니다.")
.build();
// when
postService.write(postCreate);
// then
assertEquals(1L, postRepository.count());
Post post = postRepository.findAll().get(0);
assertEquals("글 제목입니다.", post.getTitle());
assertEquals("글 내용입니다.", post.getContent());
}
@Test
@DisplayName("postId 로 글 조회")
void get_post_by_id_test() {
// given
Post post = Post.builder()
.title("글 제목입니다.")
.content("글 내용입니다.")
.build();
postRepository.save(post);
Long postId = 1L;
// when
Post postEntity = postService.getPost(postId);
// then
assertNotNull(post);
assertEquals("글 제목입니다.", postEntity.getTitle());
assertEquals("글 내용입니다.", postEntity.getContent());
}
}