From 71fcbd5ef0638ee5a889bc7abd3d406e867e61df Mon Sep 17 00:00:00 2001 From: neviyn Date: Fri, 21 May 2021 23:00:23 +0100 Subject: [PATCH] Fixed behaviour when no images or tags have been added. Added test for this case. --- .../kotlin/uk/co/neviyn/booru/Controller.kt | 2 +- src/main/resources/templates/gallery.html | 8 +++---- .../uk/co/neviyn/booru/ImageControllerTest.kt | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt index 9620435..0fcc886 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt @@ -162,7 +162,7 @@ class ImageController } } ?: return "gallery" model.addAttribute("pageNumber", pageNumber) - val commonTags: List = tagRepository.find20TagsOnImages(images.content.map { it.id }) + val commonTags: List = try { tagRepository.find20TagsOnImages(images.content.map { it.id }) } catch (e: Exception) {listOf()} model.addAttribute("commonTags", commonTags) model.addAttribute("imagePage", images) return "gallery" diff --git a/src/main/resources/templates/gallery.html b/src/main/resources/templates/gallery.html index e611fa6..9e451f4 100644 --- a/src/main/resources/templates/gallery.html +++ b/src/main/resources/templates/gallery.html @@ -6,9 +6,9 @@
-
+
-
+ -
+

No images found with all the specified tags.

-
+
  • diff --git a/src/test/kotlin/uk/co/neviyn/booru/ImageControllerTest.kt b/src/test/kotlin/uk/co/neviyn/booru/ImageControllerTest.kt index 79f0c83..05c8468 100644 --- a/src/test/kotlin/uk/co/neviyn/booru/ImageControllerTest.kt +++ b/src/test/kotlin/uk/co/neviyn/booru/ImageControllerTest.kt @@ -26,6 +26,9 @@ internal class ImageControllerTest { @MockBean lateinit var imageRepository: ImageRepository + @MockBean + lateinit var tagRepository: TagRepository + @Test fun `get an empty gallery page`() { mockMvc.perform(MockMvcRequestBuilders.get("/gallery")) @@ -75,4 +78,24 @@ internal class ImageControllerTest { ) } + @Test + fun `get a gallery page but no images exist`() { + val page = PageImpl(listOf()) + doReturn(page).`when`(imageRepository).findAll(Mockito.any(PageRequest::class.java)) + doReturn(listOf()).`when`(tagRepository).findAllByTagIn(Mockito.anyList()) + println(listOf().isEmpty()) + 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)) + .andExpect( + MockMvcResultMatchers.model().attribute( + "imagePage", + Matchers.hasProperty("totalPages", Matchers.equalTo(1)) + ) + ) + .andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("No images found with all the specified tags."))) + } + } \ No newline at end of file