diff --git a/board/build.gradle b/board/build.gradle index cadebc49..2bf696bd 100644 --- a/board/build.gradle +++ b/board/build.gradle @@ -22,6 +22,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + + implementation 'org.springframework.boot:spring-boot-starter-data-rest' + implementation 'org.springframework.data:spring-data-rest-hal-explorer' + runtimeOnly 'com.h2database:h2' runtimeOnly 'mysql:mysql-connector-java' compileOnly 'org.projectlombok:lombok' diff --git a/board/src/main/java/com/example/board/repository/ArticleCommentRepository.java b/board/src/main/java/com/example/board/repository/ArticleCommentRepository.java index 56fffbac..74ab537f 100644 --- a/board/src/main/java/com/example/board/repository/ArticleCommentRepository.java +++ b/board/src/main/java/com/example/board/repository/ArticleCommentRepository.java @@ -2,6 +2,8 @@ package com.example.board.repository; import com.example.board.domain.ArticleComment; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; +@RepositoryRestResource public interface ArticleCommentRepository extends JpaRepository { } \ No newline at end of file diff --git a/board/src/main/java/com/example/board/repository/ArticleRepository.java b/board/src/main/java/com/example/board/repository/ArticleRepository.java index b03c6840..1b9505e3 100644 --- a/board/src/main/java/com/example/board/repository/ArticleRepository.java +++ b/board/src/main/java/com/example/board/repository/ArticleRepository.java @@ -2,7 +2,8 @@ package com.example.board.repository; import com.example.board.domain.Article; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; - +@RepositoryRestResource public interface ArticleRepository extends JpaRepository { } \ No newline at end of file diff --git a/board/src/main/resources/application.yaml b/board/src/main/resources/application.yaml index 04415d27..0eeee4da 100644 --- a/board/src/main/resources/application.yaml +++ b/board/src/main/resources/application.yaml @@ -24,6 +24,10 @@ spring: hibernate.default_batch_fetch_size: 100 # h2.console.enabled: true sql.init.mode: always + data: + rest: + base-path: /api + detection-strategy: annotated --- diff --git a/board/src/test/java/com/example/board/controller/DataRestTest.java b/board/src/test/java/com/example/board/controller/DataRestTest.java new file mode 100644 index 00000000..a8430c2b --- /dev/null +++ b/board/src/test/java/com/example/board/controller/DataRestTest.java @@ -0,0 +1,82 @@ +package com.example.board.controller; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.transaction.annotation.Transactional; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@DisplayName("Data REST - API 테스트") +@AutoConfigureMockMvc +@Transactional +@SpringBootTest +public class DataRestTest { + + private final MockMvc mockMvc; + + public DataRestTest(@Autowired MockMvc mockMvc) { + this.mockMvc = mockMvc; + } + + @DisplayName("[api] 게시글 리스트 조회") + @Test + void givenNothing_whenRequestingArticles_thenReturnsArticlesJsonResponse() throws Exception { + // given + + // when & then + mockMvc.perform(get("/api/articles")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.valueOf("application/hal+json"))); + } + + @DisplayName("[api] 게시글 단건 조회") + @Test + void givenNothing_whenRequestingArticle_thenReturnsArticleJsonResponse() throws Exception { + // given + + // when & then + mockMvc.perform(get("/api/articles/1")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.valueOf("application/hal+json"))); + } + + @DisplayName("[api] 게시글 -> 댓글 리스트 조회") + @Test + void givenNothing_whenRequestingArticleCommentsFromArticle_thenReturnsArticleCommentsJsonResponse() throws Exception { + // given + + // when & then + mockMvc.perform(get("/api/articles/1/articleComments")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.valueOf("application/hal+json"))); + } + + @DisplayName("[api] 댓글 리스트 조회") + @Test + void givenNothing_whenRequestingArticleComments_thenReturnsArticleCommentsJsonResponse() throws Exception { + // given + + // when & then + mockMvc.perform(get("/api/articleComments")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.valueOf("application/hal+json"))); + } + + @DisplayName("[api] 댓글 단건 조회") + @Test + void givenNothing_whenRequestingArticleComment_thenReturnsArticleCommentJsonResponse() throws Exception { + // given + + // when & then + mockMvc.perform(get("/api/articleComments/1")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.valueOf("application/hal+json"))); + } +} diff --git a/board/src/test/resources/application.yml b/board/src/test/resources/application.yml deleted file mode 100644 index 5758e230..00000000 --- a/board/src/test/resources/application.yml +++ /dev/null @@ -1,5 +0,0 @@ -#spring: -# jpa: -# properties: -# hibernate: -# enable_lazy_load_no_trans: true \ No newline at end of file