From 0b1bb099df75887d263dc6c6ce6b9ba262fdb0d2 Mon Sep 17 00:00:00 2001 From: neviyn Date: Fri, 21 May 2021 22:59:24 +0100 Subject: [PATCH] Added test for when there are multiple pages of images --- .../uk/co/neviyn/booru/ImageControllerTest.kt | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/test/kotlin/uk/co/neviyn/booru/ImageControllerTest.kt b/src/test/kotlin/uk/co/neviyn/booru/ImageControllerTest.kt index 939685b..79f0c83 100644 --- a/src/test/kotlin/uk/co/neviyn/booru/ImageControllerTest.kt +++ b/src/test/kotlin/uk/co/neviyn/booru/ImageControllerTest.kt @@ -1,5 +1,6 @@ package uk.co.neviyn.booru +import org.hamcrest.Matchers import org.junit.jupiter.api.Test import org.mockito.Mockito @@ -26,7 +27,7 @@ internal class ImageControllerTest { lateinit var imageRepository: ImageRepository @Test - fun `get an empty gallery page`(){ + fun `get an empty gallery page`() { mockMvc.perform(MockMvcRequestBuilders.get("/gallery")) .andExpect(MockMvcResultMatchers.status().isOk) .andExpect(MockMvcResultMatchers.view().name("gallery")) @@ -48,5 +49,30 @@ internal class ImageControllerTest { .andExpect(MockMvcResultMatchers.view().name("gallery")) .andExpect(MockMvcResultMatchers.model().attribute("imagePage", page)) .andExpect(MockMvcResultMatchers.model().attribute("pageNumber", 1)) + .andExpect( + MockMvcResultMatchers.model().attribute( + "imagePage", + Matchers.hasProperty("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("totalPages", Matchers.equalTo(4)) + ) + ) + } + } \ No newline at end of file