Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkJeongseop committed Oct 5, 2023
1 parent 4cb29e4 commit 844e2f6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,19 @@ class ReservationController(val reservationService: ReservationService) {
request: HttpServletRequest,
@PathVariable id: Int
): ResponseEntity<ReservationResponse> {
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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -78,11 +82,15 @@ class BookmarkControllerTest(
event_id = 1
)

val pageable: Pageable = PageRequest.of(1, 10)
val content = mutableListOf(sampleBookmark)
val page: Page<Bookmark> = 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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 844e2f6

Please sign in to comment.