Skip to content

Commit

Permalink
replace endpoints to be restful and add delete method to testing
Browse files Browse the repository at this point in the history
  • Loading branch information
AvilaAndre committed Jul 20, 2024
1 parent 250bb18 commit dd4818b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ class EventController(private val service: EventService) {
@PathVariable idAccount: Long
) = service.removeTeamMemberById(idEvent, idAccount)

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

@PutMapping("/{idEvent}/gallery/removeImage")
@DeleteMapping("/{idEvent}/gallery")
fun removeGalleryImage(
@PathVariable idEvent: Long,
@RequestPart imageUrl: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import pt.up.fe.ni.website.backend.dto.entity.ActivityDto
import pt.up.fe.ni.website.backend.model.Activity
import pt.up.fe.ni.website.backend.model.Event
import pt.up.fe.ni.website.backend.model.Project
import pt.up.fe.ni.website.backend.repository.ActivityRepository
import pt.up.fe.ni.website.backend.service.AccountService
import pt.up.fe.ni.website.backend.service.ErrorMessages
Expand Down Expand Up @@ -85,17 +83,9 @@ abstract class AbstractActivityService<T : Activity>(
return repository.save(activity)
}

fun addGalleryImage(activityId: Long, image: MultipartFile): Activity {
fun addGalleryImage(activityId: Long, image: MultipartFile, imageFolder: String): Activity {
val activity = getActivityById(activityId)

var imageFolder = "activities"

if (activity is Event) {
imageFolder = EventService.IMAGE_FOLDER
} else if (activity is Project) {
imageFolder = ProjectService.IMAGE_FOLDER
}

val fileName = fileUploader.buildFileName(image, activity.title)
val imageName = fileUploader.uploadImage(imageFolder + "/gallery", fileName, image.bytes)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ internal class EventControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("PUT /events/{idEvent}/gallery/addImage")
@DisplayName("PUT /events/{idEvent}/gallery")
inner class AddGalleryImage {

private val uuid: UUID = UUID.randomUUID()
Expand All @@ -839,7 +839,7 @@ internal class EventControllerTest @Autowired constructor(
fun `should add an image`() {
val expectedImagePath = "${uploadConfigProperties.staticServe}/events/gallery/${testEvent.title}-$uuid.jpeg"

mockMvc.multipartBuilder("/events/${testEvent.id}/gallery/addImage")
mockMvc.multipartBuilder("/events/${testEvent.id}/gallery")
.asPutMethod()
.addFile("image", contentType = MediaType.IMAGE_JPEG_VALUE)
.perform()
Expand All @@ -865,7 +865,7 @@ internal class EventControllerTest @Autowired constructor(
fun `should fail if event does not exist`() {
val unexistentID = 5

mockMvc.multipartBuilder("/events/$unexistentID/gallery/addImage")
mockMvc.multipartBuilder("/events/$unexistentID/gallery")
.asPutMethod()
.addFile("image", contentType = MediaType.IMAGE_JPEG_VALUE)
.perform()
Expand All @@ -879,7 +879,7 @@ internal class EventControllerTest @Autowired constructor(

@Test
fun `should fail if image in wrong format`() {
mockMvc.multipartBuilder("/events/${testEvent.id}/gallery/addImage")
mockMvc.multipartBuilder("/events/${testEvent.id}/gallery")
.asPutMethod()
.addFile("image", filename = "image.gif", contentType = MediaType.IMAGE_JPEG_VALUE)
.perform()
Expand All @@ -892,8 +892,9 @@ internal class EventControllerTest @Autowired constructor(
}
}

@Nested
@NestedTest
@DisplayName("PUT /events/{idEvent}/gallery/removeImage")
@DisplayName("DELETE /events/{idEvent}/gallery")
inner class RemoveGalleryImage {

private val uuid: UUID = UUID.randomUUID()
Expand Down Expand Up @@ -921,8 +922,8 @@ internal class EventControllerTest @Autowired constructor(

@Test
fun `should remove an image`() {
mockMvc.multipartBuilder("/events/${testEvent.id}/gallery/removeImage")
.asPutMethod()
mockMvc.multipartBuilder("/events/${testEvent.id}/gallery")
.asDeleteMethod()
.addPart("imageUrl", mockImageUrl)
.perform()
.andExpectAll(
Expand All @@ -946,8 +947,8 @@ internal class EventControllerTest @Autowired constructor(
fun `should fail if event does not exist`() {
val unexistentID = 5

mockMvc.multipartBuilder("/events/$unexistentID/gallery/removeImage")
.asPutMethod()
mockMvc.multipartBuilder("/events/$unexistentID/gallery")
.asDeleteMethod()
.addPart("imageUrl", mockImageUrl)
.perform()
.andExpectAll(
Expand All @@ -962,8 +963,8 @@ internal class EventControllerTest @Autowired constructor(
fun `should fail if image does not exist`() {
val wrongImageUrl = "${uploadConfigProperties.staticServe}/gallery/Another${testEvent.title}-$uuid.jpeg"

mockMvc.multipartBuilder("/events/${testEvent.id}/gallery/removeImage")
.asPutMethod()
mockMvc.multipartBuilder("/events/${testEvent.id}/gallery")
.asDeleteMethod()
.addPart("imageUrl", wrongImageUrl)
.perform()
.andExpectAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ class MockMvcMultipartBuilder(
return this
}

fun asDeleteMethod(): MockMvcMultipartBuilder {
multipart.with(
{
it.method = "DELETE"
it
}
)
return this
}

fun perform(): ResultActions {
return mockMvc.perform(multipart)
}
Expand Down

0 comments on commit dd4818b

Please sign in to comment.