From 844e2f67ec063ca7cfabc9bae0af13c4f109cbba Mon Sep 17 00:00:00 2001 From: Park Jeongseop Date: Thu, 5 Oct 2023 09:27:48 +0000 Subject: [PATCH] test: add test --- .../Bookmark/BookmarkRepositoryTest.kt | 10 +++++--- .../controller/ReservationController.kt | 20 +++++++--------- .../bookmark/BookmarkControllerTest.kt | 24 ++++++++++++------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/integrationTest/kotlin/com/group4/ticketingservice/Bookmark/BookmarkRepositoryTest.kt b/src/integrationTest/kotlin/com/group4/ticketingservice/Bookmark/BookmarkRepositoryTest.kt index cd351ad9..5124126a 100644 --- a/src/integrationTest/kotlin/com/group4/ticketingservice/Bookmark/BookmarkRepositoryTest.kt +++ b/src/integrationTest/kotlin/com/group4/ticketingservice/Bookmark/BookmarkRepositoryTest.kt @@ -12,6 +12,9 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Assertions.assertInstanceOf import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired +import org.springframework.data.domain.Page +import org.springframework.data.domain.PageRequest +import org.springframework.data.domain.Pageable import org.springframework.data.repository.findByIdOrNull import org.springframework.transaction.annotation.Transactional import java.time.OffsetDateTime @@ -93,12 +96,13 @@ class BookmarkRepositoryTest( val savedUser = userRepository.save(sampleUser) val savedEvent = eventRepository.save(sampleEvent) bookmarkRepository.save(Bookmark(user = savedUser, event = savedEvent)) + val pageable: Pageable = PageRequest.of(1, 10) // when - val listofBookmarks = bookmarkRepository.findByUserId(savedUser.id!!) + val listofBookmarks = bookmarkRepository.findByUserId(savedUser.id!!, pageable) // then - assertInstanceOf(ArrayList::class.java, listofBookmarks) - assertInstanceOf(Bookmark::class.java, listofBookmarks[0]) + assertInstanceOf(Page::class.java, listofBookmarks) + assertInstanceOf(Bookmark::class.java, listofBookmarks.content[0]) } } diff --git a/src/main/kotlin/com/group4/ticketingservice/controller/ReservationController.kt b/src/main/kotlin/com/group4/ticketingservice/controller/ReservationController.kt index ac9de872..1eaab2ec 100644 --- a/src/main/kotlin/com/group4/ticketingservice/controller/ReservationController.kt +++ b/src/main/kotlin/com/group4/ticketingservice/controller/ReservationController.kt @@ -55,21 +55,19 @@ class ReservationController(val reservationService: ReservationService) { request: HttpServletRequest, @PathVariable id: Int ): ResponseEntity { - val foundReservation = reservationService.getReservation(id)?.let { - ReservationResponse( - id = it.id!!, - eventId = it.event.id!!, - userId = it.user.id!!, - bookedAt = it.bookedAt - ) - } ?: kotlin.run { - null - } + val foundReservation = reservationService.getReservation(id) + + val response = ReservationResponse( + id = foundReservation.id!!, + eventId = foundReservation.event.id!!, + userId = foundReservation.user.id!!, + bookedAt = foundReservation.bookedAt + ) val headers = HttpHeaders() headers.set("Content-Location", request.requestURI) - return ResponseEntity(foundReservation, headers, HttpStatus.OK) + return ResponseEntity(response, headers, HttpStatus.OK) } @PutMapping("/{id}") diff --git a/src/test/kotlin/com/group4/ticketingservice/bookmark/BookmarkControllerTest.kt b/src/test/kotlin/com/group4/ticketingservice/bookmark/BookmarkControllerTest.kt index 41ee4595..f7e7cfc6 100644 --- a/src/test/kotlin/com/group4/ticketingservice/bookmark/BookmarkControllerTest.kt +++ b/src/test/kotlin/com/group4/ticketingservice/bookmark/BookmarkControllerTest.kt @@ -34,6 +34,10 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import java.time.OffsetDateTime +import org.springframework.data.domain.Page +import org.springframework.data.domain.PageRequest +import org.springframework.data.domain.Pageable +import org.springframework.data.domain.PageImpl @ExtendWith(MockKExtension::class) @WebMvcTest( @@ -78,11 +82,15 @@ class BookmarkControllerTest( event_id = 1 ) + val pageable: Pageable = PageRequest.of(1, 10) + val content = mutableListOf(sampleBookmark) + val page: Page = PageImpl(content, pageable, content.size.toLong()) + @Test @WithAuthUser(email = testUserName, id = testUserId) fun `POST_api_bookmark should invoke service_create`() { // given - every { service.create(testUserId, sampleBookmarkDto) } returns 1 + every { service.create(testUserId, sampleBookmarkDto) } returns sampleBookmark // when mockMvc.perform( @@ -99,7 +107,7 @@ class BookmarkControllerTest( @WithAuthUser(email = testUserName, id = testUserId) fun `POST_api_bookmark should return saved bookmark id with HTTP 201 Created`() { // given - every { service.create(testUserId, sampleBookmarkDto) } returns 1 + every { service.create(testUserId, sampleBookmarkDto) } returns sampleBookmark // when val resultActions: ResultActions = mockMvc.perform( @@ -117,7 +125,7 @@ class BookmarkControllerTest( @WithAuthUser(email = testUserName, id = testUserId) fun `POST_api_bookmark should return HTTP ERROR 400 for invalid parameter`() { // given - every { service.create(testUserId, sampleBookmarkDto) } returns 1 + every { service.create(testUserId, sampleBookmarkDto) } returns sampleBookmark // when val resultActions: ResultActions = mockMvc.perform( @@ -132,28 +140,28 @@ class BookmarkControllerTest( @Test @WithAuthUser(email = testUserName, id = testUserId) fun `GET_api_bookmarks should invoke service_getList`() { - // given - every { service.getList(testUserId) } returns mutableListOf(sampleBookmark) + // given + every { service.getBookmarks(testUserId, pageable) } returns page // when mockMvc.perform(MockMvcRequestBuilders.get("/bookmarks")) // then - verify(exactly = 1) { service.getList(testUserId) } + verify(exactly = 1) { service.getBookmarks(testUserId, pageable) } } @Test @WithAuthUser(email = testUserName, id = testUserId) fun `GET_api_bookmarks should return list of bookmarks with HTTP 200 OK`() { // given - every { service.getList(testUserId) } returns mutableListOf(sampleBookmark) + every { service.getBookmarks(testUserId, pageable) } returns page // when val resultActions: ResultActions = mockMvc.perform(MockMvcRequestBuilders.get("/bookmarks")) // then resultActions.andExpect(status().isOk) - .andExpect(jsonPath("$[0].id").value(1)) + .andExpect(jsonPath("$[0].data.id").value(1)) } @Test