diff --git a/src/test/kotlin/uk/co/neviyn/booru/ImageControllerTest.kt b/src/test/kotlin/uk/co/neviyn/booru/ImageControllerTest.kt new file mode 100644 index 0000000..939685b --- /dev/null +++ b/src/test/kotlin/uk/co/neviyn/booru/ImageControllerTest.kt @@ -0,0 +1,52 @@ +package uk.co.neviyn.booru + +import org.junit.jupiter.api.Test + +import org.mockito.Mockito +import org.mockito.Mockito.doReturn +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.mock.mockito.MockBean +import org.springframework.data.domain.PageImpl +import org.springframework.data.domain.PageRequest +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders +import org.springframework.test.web.servlet.result.MockMvcResultMatchers + + +@SpringBootTest +@AutoConfigureMockMvc +internal class ImageControllerTest { + + @Autowired + lateinit var mockMvc: MockMvc + + @MockBean + lateinit var imageRepository: ImageRepository + + @Test + fun `get an empty gallery page`(){ + mockMvc.perform(MockMvcRequestBuilders.get("/gallery")) + .andExpect(MockMvcResultMatchers.status().isOk) + .andExpect(MockMvcResultMatchers.view().name("gallery")) + .andExpect(MockMvcResultMatchers.model().attribute("imagePage", null)) + .andExpect(MockMvcResultMatchers.model().attribute("commonTags", null)) + .andExpect(MockMvcResultMatchers.model().attribute("pageNumber", null)) + } + + @Test + fun `get a gallery page with a single page of images`() { + val images = listOf( + Image(id = 1, filename = "image1.jpg"), + Image(id = 2, filename = "image2.jpg") + ) + val page = PageImpl(images) + doReturn(page).`when`(imageRepository).findAll(Mockito.any(PageRequest::class.java)) + mockMvc.perform(MockMvcRequestBuilders.get("/gallery")) + .andExpect(MockMvcResultMatchers.status().isOk) + .andExpect(MockMvcResultMatchers.view().name("gallery")) + .andExpect(MockMvcResultMatchers.model().attribute("imagePage", page)) + .andExpect(MockMvcResultMatchers.model().attribute("pageNumber", 1)) + } +} \ No newline at end of file