diff --git a/core/demo-reactive-core/src/main/kotlin/me/jiniworld/demohx/application/notice/port/input/GetNoticeQuery.kt b/core/demo-reactive-core/src/main/kotlin/me/jiniworld/demohx/application/notice/port/input/GetNoticeQuery.kt index ea3617c..7e905e1 100644 --- a/core/demo-reactive-core/src/main/kotlin/me/jiniworld/demohx/application/notice/port/input/GetNoticeQuery.kt +++ b/core/demo-reactive-core/src/main/kotlin/me/jiniworld/demohx/application/notice/port/input/GetNoticeQuery.kt @@ -1,9 +1,10 @@ package me.jiniworld.demohx.application.notice.port.input +import kotlinx.coroutines.flow.Flow import me.jiniworld.demohx.application.notice.domain.NoticeDetail import me.jiniworld.demohx.application.notice.domain.NoticeSimple interface GetNoticeQuery { - suspend fun getNoticeSimples(command: GetNoticesCommand): List? + fun getNoticeSimples(command: GetNoticesCommand): Flow suspend fun getNoticeDetail(id: String): NoticeDetail? } \ No newline at end of file diff --git a/core/demo-reactive-core/src/main/kotlin/me/jiniworld/demohx/application/notice/service/GetNoticeService.kt b/core/demo-reactive-core/src/main/kotlin/me/jiniworld/demohx/application/notice/service/GetNoticeService.kt index 5a1be8a..4053836 100644 --- a/core/demo-reactive-core/src/main/kotlin/me/jiniworld/demohx/application/notice/service/GetNoticeService.kt +++ b/core/demo-reactive-core/src/main/kotlin/me/jiniworld/demohx/application/notice/service/GetNoticeService.kt @@ -1,6 +1,5 @@ package me.jiniworld.demohx.application.notice.service -import kotlinx.coroutines.flow.toList import me.jiniworld.demohx.annotation.UseCase import me.jiniworld.demohx.application.notice.domain.NoticeDetail import me.jiniworld.demohx.application.notice.port.input.GetNoticeQuery @@ -16,10 +15,9 @@ internal class GetNoticeService( private val loadNoticePort: LoadNoticePort, ) : GetNoticeQuery { - override suspend fun getNoticeSimples(command: GetNoticesCommand) = + override fun getNoticeSimples(command: GetNoticesCommand) = loadNoticePort.loadNotices(PageRequest.of(command.page, command.size, Sort.by( Sort.Order.desc("id")))) - .toList() override suspend fun getNoticeDetail(id: String): NoticeDetail? = loadNoticePort.loadNotice(id) diff --git a/server/demo-all-in-one-app/src/test/kotlin/me/jiniworld/demohx/notice/adapter/input/web/GetNoticeControllerTest.kt b/server/demo-all-in-one-app/src/test/kotlin/me/jiniworld/demohx/notice/adapter/input/web/GetNoticeControllerTest.kt new file mode 100644 index 0000000..76202a9 --- /dev/null +++ b/server/demo-all-in-one-app/src/test/kotlin/me/jiniworld/demohx/notice/adapter/input/web/GetNoticeControllerTest.kt @@ -0,0 +1,105 @@ +package me.jiniworld.demohx.notice.adapter.input.web + +import io.mockk.mockk +import me.jiniworld.demohx.notice.application.port.input.GetNoticeQuery +import org.hamcrest.Matchers +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc +import org.springframework.http.MediaType +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders +import org.springframework.test.web.servlet.result.MockMvcResultHandlers +import org.springframework.test.web.servlet.result.MockMvcResultMatchers +import org.springframework.test.web.servlet.setup.MockMvcBuilders + +@AutoConfigureRestDocs +@AutoConfigureMockMvc +internal class GetNoticeControllerTest { + + @Autowired + private lateinit var mockMvc: MockMvc + + private lateinit var getNoticeController: GetNoticeController + private lateinit var getNoticeQuery: GetNoticeQuery + + @BeforeEach + fun setUp() { + val registerNoticeUseCase = mockk(relaxed = true) + this.getNoticeQuery = registerNoticeUseCase + val cont = GetNoticeController(registerNoticeUseCase) + this.getNoticeController = cont + this.mockMvc = MockMvcBuilders.standaloneSetup(cont).build() + } + + @Test + fun getNotices() { + val resultActions = mockMvc.perform( + MockMvcRequestBuilders.get("/v1/notices") + .header("Content-Type", "application/json") + ) + + // then + resultActions + .andExpect(MockMvcResultMatchers.status().isOk) + .andExpect(MockMvcResultMatchers.jsonPath("$.result", Matchers.`is`("success"))) + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_VALUE)) + .andDo(MockMvcResultHandlers.print()) + } + + @Test + fun getNotice() { + val resultActions = mockMvc.perform( + MockMvcRequestBuilders.get("/v1/notices/{id}", 1) + .accept(MediaType.APPLICATION_JSON) + ) + + // then + resultActions + .andExpect(MockMvcResultMatchers.status().isOk) + .andExpect(MockMvcResultMatchers.jsonPath("$.result", Matchers.`is`("success"))) + .andExpect(MockMvcResultMatchers.jsonPath("$.data").exists()) + .andExpect(MockMvcResultMatchers.jsonPath("$.data.id").isNotEmpty) + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_VALUE)) + .andDo(MockMvcResultHandlers.print()) +// +// resultActions.andDo(document("notice-search/success", +// requestFields( +// fieldWithPath("id").description("notice id") +// ) +// )); +// prepa +// resource( +// ResourceSnippetParameters.builder() +// .description("v1 test") +// .pathParameters( +// parameterWithName("name").description("이름")) +// .responseFields( +// fieldWithPath("data").description("입력한 이름") +//// fieldWithPath("products").description("The product line item of the cart."), +//// subsectionWithPath("products[]._links.product").description("Link to the product."), +//// fieldWithPath("products[].quantity").description("The quantity of the line item."), +//// subsectionWithPath("products[].product").description("The product the line item relates to."), +//// subsectionWithPath("_links").description("Links section.") +// ) + + } + + @Test + fun test1() { + val resultActions = mockMvc.perform( + MockMvcRequestBuilders.get("/v1/notices/test") + .header("Content-Type", "application/json") + ) + + // then + resultActions + .andExpect(MockMvcResultMatchers.status().isOk) + .andExpect(MockMvcResultMatchers.jsonPath("$.result", Matchers.`is`("success"))) + .andExpect(MockMvcResultMatchers.jsonPath("$.data", Matchers.`is`("test"))) + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_VALUE)) + .andDo(MockMvcResultHandlers.print()) + } +} \ No newline at end of file diff --git a/server/demo-app/src/main/kotlin/me/jiniworld/demohx/web/notice/GetNoticeController.kt b/server/demo-app/src/main/kotlin/me/jiniworld/demohx/web/notice/GetNoticeController.kt index c82a2f9..cc70714 100644 --- a/server/demo-app/src/main/kotlin/me/jiniworld/demohx/web/notice/GetNoticeController.kt +++ b/server/demo-app/src/main/kotlin/me/jiniworld/demohx/web/notice/GetNoticeController.kt @@ -5,7 +5,6 @@ import io.swagger.v3.oas.annotations.tags.Tag import me.jiniworld.demohx.annotation.WebAdapter import me.jiniworld.demohx.application.notice.port.input.GetNoticeQuery import me.jiniworld.demohx.application.notice.port.input.GetNoticesCommand -import me.jiniworld.demohx.model.DataResponse import me.jiniworld.demohx.model.NotFoundException import org.springframework.web.bind.annotation.* @@ -23,18 +22,16 @@ internal class GetNoticeController( @RequestParam(value = "page", required = false, defaultValue = "0") page: Int, @RequestParam(value = "size", required = false, defaultValue = "10") size: Int, ) = getNoticeQuery.getNoticeSimples(GetNoticesCommand(page = page, size = size)) - ?.let { DataResponse(data = it) } ?: throw NotFoundException("공지사항이 없습니다.") @Operation(summary = "공지사항 상세조회") @GetMapping("/{notice_id}") fun getNotice(@PathVariable("notice_id") noticeId: Long, ) = getNoticeQuery.getNoticeDetail(noticeId) - ?.let { DataResponse(data = it) } ?: throw NotFoundException("조회되는 공지사항이 없습니다.") @Operation(summary = "test") @GetMapping("/test") - fun test() = DataResponse(data = "test") + fun test() = "test" } \ No newline at end of file diff --git a/server/demo-reactive-app/src/main/kotlin/me/jiniworld/demohx/web/notice/GetNoticeController.kt b/server/demo-reactive-app/src/main/kotlin/me/jiniworld/demohx/web/notice/GetNoticeController.kt index 55cebdb..2ae0029 100644 --- a/server/demo-reactive-app/src/main/kotlin/me/jiniworld/demohx/web/notice/GetNoticeController.kt +++ b/server/demo-reactive-app/src/main/kotlin/me/jiniworld/demohx/web/notice/GetNoticeController.kt @@ -19,12 +19,10 @@ internal class GetNoticeController( @Operation(summary = "공지사항 목록") @GetMapping("") - suspend fun getNotices( + fun getNotices( @RequestParam(value = "page", required = false, defaultValue = "0") page: Int, @RequestParam(value = "size", required = false, defaultValue = "10") size: Int, ) = getNoticeQuery.getNoticeSimples(GetNoticesCommand(page = page, size = size)) - ?.let { DataResponse(data = it) } - ?: throw NotFoundException("공지사항이 없습니다.") @Operation(summary = "공지사항 상세조회") @GetMapping("/{notice_id}")