Skip to content

Commit

Permalink
test: added gallery tests to Project
Browse files Browse the repository at this point in the history
  • Loading branch information
AvilaAndre committed Nov 6, 2024
1 parent 12d6616 commit 2322b14
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,18 @@ class ProjectController(private val service: ProjectService) {
@PathVariable idProject: Long,
@PathVariable idAccount: Long
) = service.removeHallOfFameMemberById(idProject, idAccount)

@PutMapping("/{idProject}/gallery", consumes = ["multipart/form-data"])
fun addGalleryImage(
@PathVariable idProject: Long,
@RequestParam
@ValidImage
image: MultipartFile
) = service.addGalleryImage(idProject, image, ProjectService.IMAGE_FOLDER)

@DeleteMapping("/{idProject}/gallery")
fun removeGalleryImage(
@PathVariable idProject: Long,
@RequestPart imageUrl: String
) = service.removeGalleryImage(idProject, imageUrl)
}
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,8 @@ internal class EventControllerTest @Autowired constructor(

@Test
fun `should fail if image does not exist`() {
val wrongImageUrl = "${uploadConfigProperties.staticServe}/gallery/Another${testEvent.title}-$uuid.jpeg"
val wrongImageUrl =
"${uploadConfigProperties.staticServe}/gallery/events/Another${testEvent.title}-$uuid.jpeg"

mockMvc.multipartBuilder("/events/${testEvent.id}/gallery")
.asDeleteMethod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,198 @@ internal class ProjectControllerTest @Autowired constructor(
}
}

@NestedTest
@DisplayName("PUT /projects/{idProject}/gallery")
inner class AddGalleryImage {

private val uuid: UUID = UUID.randomUUID()
private val mockedSettings = Mockito.mockStatic(UUID::class.java)

@BeforeAll
fun setupMocks() {
Mockito.`when`(UUID.randomUUID()).thenReturn(uuid)
}

@AfterAll
fun cleanup() {
mockedSettings.close()
}

@BeforeEach
fun addToRepositories() {
accountRepository.save(testAccount)
repository.save(testProject)
}

private val parameters = listOf(
parameterWithName("idProject").description("The id of the project being targeted")
)

@Test
fun `should add an image`() {
val expectedImagePath =
"${uploadConfigProperties.staticServe}/projects/gallery/${testProject.title}-$uuid.jpeg"

mockMvc.multipartBuilder("/projects/${testProject.id}/gallery")
.asPutMethod()
.addFile("image", contentType = MediaType.IMAGE_JPEG_VALUE)
.perform()
.andExpectAll(
status().isOk,
content().contentType(MediaType.APPLICATION_JSON),
jsonPath("$.title").value(testProject.title),
jsonPath("$.description").value(testProject.description),
jsonPath("$.teamMembers.length()").value(testProject.teamMembers.size),
jsonPath("$.isArchived").value(testProject.isArchived),
jsonPath("$.slug").value(testProject.slug),
jsonPath("$.slogan").value(testProject.slogan),
jsonPath("$.targetAudience").value(testProject.targetAudience),
jsonPath("$.github").value(testProject.github),
jsonPath("$.links.length()").value(testProject.links.size),
jsonPath("$.timeline.length()").value(testProject.timeline.size),
jsonPath("$.image").value(testProject.image),
jsonPath("$.gallery.length()").value(1),
jsonPath("$.gallery[0]").value(expectedImagePath)
)
/*
.andDocument(
documentation,
"Adds an image to the gallery of the selected project",
"This endpoint adds an image to the gallery of an project, returning the project's updated data.",
urlParameters = parameters
)
*/
}

@Test
fun `should fail if project does not exist`() {
val nonexistentID = 5

mockMvc.multipartBuilder("/projects/$nonexistentID/gallery")
.asPutMethod()
.addFile("image", contentType = MediaType.IMAGE_JPEG_VALUE)
.perform()
.andExpectAll(
status().isNotFound,
content().contentType(MediaType.APPLICATION_JSON),
jsonPath("$.errors.length()").value(1),
jsonPath("$.errors[0].message").value("activity not found with id $nonexistentID")
).andDocumentErrorResponse(documentation)
}

@Test
fun `should fail if image in wrong format`() {
mockMvc.multipartBuilder("/projects/${testProject.id}/gallery")
.asPutMethod()
.addFile("image", filename = "image.gif", contentType = MediaType.IMAGE_JPEG_VALUE)
.perform()
.andExpectAll(
status().isBadRequest,
content().contentType(MediaType.APPLICATION_JSON),
jsonPath("$.errors.length()").value(1),
jsonPath("$.errors[0].message").value("invalid image type (png, jpg, jpeg or webp)")
).andDocumentErrorResponse(documentation)
}
}

@NestedTest
@DisplayName("DELETE /projects/{idProject}/gallery")
inner class RemoveGalleryImage {

private val uuid: UUID = UUID.randomUUID()
private val mockedSettings = Mockito.mockStatic(UUID::class.java)
private val mockImageUrl =
"${uploadConfigProperties.staticServe}/projects/gallery/${testProject.title}-$uuid.jpeg"

@BeforeAll
fun setupMocks() {
Mockito.`when`(UUID.randomUUID()).thenReturn(uuid)

testProject.gallery.add(mockImageUrl)
}

@AfterAll
fun cleanup() {
mockedSettings.close()
}

@BeforeEach
fun addToRepositories() {
accountRepository.save(testAccount)

repository.save(testProject)
}

private val parameters = listOf(
parameterWithName("idProject").description("The id of the project being targeted")
)

@Test
fun `should remove an image`() {
mockMvc.multipartBuilder("/projects/${testProject.id}/gallery")
.asDeleteMethod()
.addPart("imageUrl", mockImageUrl)
.perform()
.andExpectAll(
status().isOk,
content().contentType(MediaType.APPLICATION_JSON),
jsonPath("$.title").value(testProject.title),
jsonPath("$.description").value(testProject.description),
jsonPath("$.teamMembers.length()").value(testProject.teamMembers.size),
jsonPath("$.isArchived").value(testProject.isArchived),
jsonPath("$.slug").value(testProject.slug),
jsonPath("$.slogan").value(testProject.slogan),
jsonPath("$.targetAudience").value(testProject.targetAudience),
jsonPath("$.github").value(testProject.github),
jsonPath("$.links.length()").value(testProject.links.size),
jsonPath("$.timeline.length()").value(testProject.timeline.size),
jsonPath("$.image").value(testProject.image),
jsonPath("$.gallery.length()").value(0)
)
/*
.andDocument(
documentation,
"Removes an image from the gallery of a selected project",
"This endpoint removes an image to the gallery of a project, returning the project's updated data.",
urlParameters = parameters
)
*/
}

@Test
fun `should fail if project does not exist`() {
val nonexistentID = 5

mockMvc.multipartBuilder("/projects/$nonexistentID/gallery")
.asDeleteMethod()
.addPart("imageUrl", mockImageUrl)
.perform()
.andExpectAll(
status().isNotFound,
content().contentType(MediaType.APPLICATION_JSON),
jsonPath("$.errors.length()").value(1),
jsonPath("$.errors[0].message").value("activity not found with id $nonexistentID")
).andDocumentErrorResponse(documentation)
}

@Test
fun `should fail if image does not exist`() {
val wrongImageUrl =
"${uploadConfigProperties.staticServe}/gallery/projects/Another${testProject.title}-$uuid.jpeg"

mockMvc.multipartBuilder("/projects/${testProject.id}/gallery")
.asDeleteMethod()
.addPart("imageUrl", wrongImageUrl)
.perform()
.andExpectAll(
status().isNotFound,
content().contentType(MediaType.APPLICATION_JSON),
jsonPath("$.errors.length()").value(1),
jsonPath("$.errors[0].message").value("file not found with name $wrongImageUrl")
).andDocumentErrorResponse(documentation)
}
}

fun Date?.toJson(): String {
val quotedDate = objectMapper.writeValueAsString(this)
// objectMapper adds quotes to the date, so remove them
Expand Down

0 comments on commit 2322b14

Please sign in to comment.