Fixed behaviour when no images or tags have been added.

Added test for this case.
This commit is contained in:
neviyn 2021-05-21 23:00:23 +01:00
parent 0b1bb099df
commit 71fcbd5ef0
3 changed files with 28 additions and 5 deletions

View File

@ -162,7 +162,7 @@ class ImageController
}
} ?: return "gallery"
model.addAttribute("pageNumber", pageNumber)
val commonTags: List<Tag> = tagRepository.find20TagsOnImages(images.content.map { it.id })
val commonTags: List<Tag> = try { tagRepository.find20TagsOnImages(images.content.map { it.id }) } catch (e: Exception) {listOf()}
model.addAttribute("commonTags", commonTags)
model.addAttribute("imagePage", images)
return "gallery"

View File

@ -6,9 +6,9 @@
</head>
<body>
<div th:replace="fragments :: navbar"></div>
<div class="container-fluid">
<div class="container-fluid" th:with="validPage = ${imagePage != null && !imagePage.isEmpty()}">
<div th:replace="fragments :: searchBarRow"></div>
<div class="row mt-3" th:if="${imagePage != null}">
<div class="row mt-3" th:if="${validPage}">
<div class="col-xl-2 col-md-3 col-4">
<div class="list-group w-75">
<a class="list-group-item d-flex justify-content-between align-items-center text-decoration-none" th:each="tag : ${commonTags}" th:href="${#mvc.url('IC#getGalleryPage').arg(1, tag.tag).build()}">
@ -30,12 +30,12 @@
</div>
</div>
</div>
<div class="row" th:if="${imagePage == null}">
<div class="row" th:if="${!validPage}">
<div class="col text-center mt-3">
<p class="lead">No images found with all the specified tags.</p>
</div>
</div>
<div class="row justify-content-center mt-3" th:if="${imagePage != null}">
<div class="row justify-content-center mt-3" th:if="${validPage}">
<div class="col d-flex justify-content-center">
<ul class="pagination">
<li class="page-item" th:classappend="${imagePage.isFirst() ? 'disabled' : ''}">

View File

@ -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<Image>(listOf())
doReturn(page).`when`(imageRepository).findAll(Mockito.any(PageRequest::class.java))
doReturn(listOf<Tag>()).`when`(tagRepository).findAllByTagIn(Mockito.anyList())
println(listOf<Tag>().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<Int>("totalPages", Matchers.equalTo(1))
)
)
.andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("No images found with all the specified tags.")))
}
}