Added test for when there are multiple pages of images

This commit is contained in:
neviyn 2021-05-21 22:59:24 +01:00
parent 87b77cf5aa
commit 0b1bb099df

View File

@ -1,5 +1,6 @@
package uk.co.neviyn.booru package uk.co.neviyn.booru
import org.hamcrest.Matchers
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.Mockito import org.mockito.Mockito
@ -26,7 +27,7 @@ internal class ImageControllerTest {
lateinit var imageRepository: ImageRepository lateinit var imageRepository: ImageRepository
@Test @Test
fun `get an empty gallery page`(){ fun `get an empty gallery page`() {
mockMvc.perform(MockMvcRequestBuilders.get("/gallery")) mockMvc.perform(MockMvcRequestBuilders.get("/gallery"))
.andExpect(MockMvcResultMatchers.status().isOk) .andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.view().name("gallery")) .andExpect(MockMvcResultMatchers.view().name("gallery"))
@ -48,5 +49,30 @@ internal class ImageControllerTest {
.andExpect(MockMvcResultMatchers.view().name("gallery")) .andExpect(MockMvcResultMatchers.view().name("gallery"))
.andExpect(MockMvcResultMatchers.model().attribute("imagePage", page)) .andExpect(MockMvcResultMatchers.model().attribute("imagePage", page))
.andExpect(MockMvcResultMatchers.model().attribute("pageNumber", 1)) .andExpect(MockMvcResultMatchers.model().attribute("pageNumber", 1))
.andExpect(
MockMvcResultMatchers.model().attribute(
"imagePage",
Matchers.hasProperty<Int>("totalPages", Matchers.equalTo(1))
)
)
} }
@Test
fun `get a gallery page with multiple page of images`() {
val images = (1..25).map { Image(id = it.toLong(), filename = "example$it.jpg") }
val page = PageImpl(images, PageRequest.of(2, 25), 100)
doReturn(page).`when`(imageRepository).findAll(Mockito.any(PageRequest::class.java))
mockMvc.perform(MockMvcRequestBuilders.get("/gallery?page=2"))
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.view().name("gallery"))
.andExpect(MockMvcResultMatchers.model().attribute("imagePage", page))
.andExpect(MockMvcResultMatchers.model().attribute("pageNumber", 2))
.andExpect(
MockMvcResultMatchers.model().attribute(
"imagePage",
Matchers.hasProperty<Int>("totalPages", Matchers.equalTo(4))
)
)
}
} }