From 80b7e39e27795379ffd3f588134e938a61245a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Costa?= Date: Sat, 22 Jul 2023 17:55:40 +0100 Subject: [PATCH] Refactor posts endpoints --- .../backend/controller/PostController.kt | 2 +- .../backend/controller/ProjectController.kt | 6 ++-- .../controller/AccountControllerTest.kt | 2 +- .../backend/controller/EventControllerTest.kt | 8 +++--- .../backend/controller/PostControllerTest.kt | 10 +++---- .../controller/ProjectControllerTest.kt | 28 +++++++++---------- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/pt/up/fe/ni/website/backend/controller/PostController.kt b/src/main/kotlin/pt/up/fe/ni/website/backend/controller/PostController.kt index efb1dbef..de90e257 100644 --- a/src/main/kotlin/pt/up/fe/ni/website/backend/controller/PostController.kt +++ b/src/main/kotlin/pt/up/fe/ni/website/backend/controller/PostController.kt @@ -24,7 +24,7 @@ class PostController(private val service: PostService) { @GetMapping("/{postSlug}**") fun getPost(@PathVariable postSlug: String) = service.getPostBySlug(postSlug) - @PostMapping("/new") + @PostMapping fun createPost(@RequestBody dto: PostDto) = service.createPost(dto) @PutMapping("/{postId}") diff --git a/src/main/kotlin/pt/up/fe/ni/website/backend/controller/ProjectController.kt b/src/main/kotlin/pt/up/fe/ni/website/backend/controller/ProjectController.kt index 053b0cad..cdb36031 100644 --- a/src/main/kotlin/pt/up/fe/ni/website/backend/controller/ProjectController.kt +++ b/src/main/kotlin/pt/up/fe/ni/website/backend/controller/ProjectController.kt @@ -30,7 +30,7 @@ class ProjectController(private val service: ProjectService) { @GetMapping("/{projectSlug}**") fun getProjectBySlug(@PathVariable projectSlug: String) = service.getProjectBySlug(projectSlug) - @PostMapping("/new", consumes = ["multipart/form-data"]) + @PostMapping(consumes = ["multipart/form-data"]) fun createProject( @RequestPart project: ProjectDto, @RequestParam @@ -65,13 +65,13 @@ class ProjectController(private val service: ProjectService) { @PutMapping("/{id}/unarchive") fun unarchiveProjectById(@PathVariable id: Long) = service.unarchiveProjectById(id) - @PutMapping("/{idProject}/addTeamMember/{idAccount}") + @PutMapping("/{idProject}/team/{idAccount}") fun addTeamMemberById( @PathVariable idProject: Long, @PathVariable idAccount: Long ) = service.addTeamMemberById(idProject, idAccount) - @PutMapping("/{idProject}/removeTeamMember/{idAccount}") + @DeleteMapping("/{idProject}/team/{idAccount}") fun removeTeamMemberById( @PathVariable idProject: Long, @PathVariable idAccount: Long diff --git a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/AccountControllerTest.kt b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/AccountControllerTest.kt index 6ffef971..41d21c12 100644 --- a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/AccountControllerTest.kt +++ b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/AccountControllerTest.kt @@ -1,6 +1,5 @@ package pt.up.fe.ni.website.backend.controller -import pt.up.fe.ni.website.backend.model.constants.AccountConstants as Constants import com.epages.restdocs.apispec.ResourceDocumentation.parameterWithName import com.fasterxml.jackson.databind.ObjectMapper import java.util.Calendar @@ -30,6 +29,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import pt.up.fe.ni.website.backend.config.upload.UploadConfigProperties import pt.up.fe.ni.website.backend.model.Account import pt.up.fe.ni.website.backend.model.CustomWebsite +import pt.up.fe.ni.website.backend.model.constants.AccountConstants as Constants import pt.up.fe.ni.website.backend.repository.AccountRepository import pt.up.fe.ni.website.backend.utils.TestUtils import pt.up.fe.ni.website.backend.utils.ValidationTester diff --git a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/EventControllerTest.kt b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/EventControllerTest.kt index dc5eb200..85bd2afb 100644 --- a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/EventControllerTest.kt +++ b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/EventControllerTest.kt @@ -1,6 +1,5 @@ package pt.up.fe.ni.website.backend.controller -import pt.up.fe.ni.website.backend.model.constants.EventConstants as Constants import com.epages.restdocs.apispec.ResourceDocumentation.parameterWithName import com.fasterxml.jackson.databind.ObjectMapper import java.util.Calendar @@ -27,6 +26,7 @@ import pt.up.fe.ni.website.backend.model.Account import pt.up.fe.ni.website.backend.model.CustomWebsite import pt.up.fe.ni.website.backend.model.Event import pt.up.fe.ni.website.backend.model.constants.ActivityConstants +import pt.up.fe.ni.website.backend.model.constants.EventConstants as Constants import pt.up.fe.ni.website.backend.model.embeddable.DateInterval import pt.up.fe.ni.website.backend.repository.AccountRepository import pt.up.fe.ni.website.backend.repository.EventRepository @@ -143,7 +143,7 @@ internal class EventControllerTest @Autowired constructor( null ), null, - null, + null ), Event( "Mid event", @@ -158,7 +158,7 @@ internal class EventControllerTest @Autowired constructor( null ), null, - "Other category", + "Other category" ), Event( "Cool event", @@ -173,7 +173,7 @@ internal class EventControllerTest @Autowired constructor( null ), null, - "Great Events", + "Great Events" ) ) diff --git a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/PostControllerTest.kt b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/PostControllerTest.kt index ee616532..a21edf3e 100644 --- a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/PostControllerTest.kt +++ b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/PostControllerTest.kt @@ -187,7 +187,7 @@ internal class PostControllerTest @Autowired constructor( } @NestedTest - @DisplayName("POST /posts/new") + @DisplayName("POST /posts") inner class CreatePost { @BeforeEach fun clearPosts() { @@ -197,7 +197,7 @@ internal class PostControllerTest @Autowired constructor( @Test fun `should create a new post`() { mockMvc.perform( - post("/posts/new") + post("/posts") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(testPost)) ) @@ -221,13 +221,13 @@ internal class PostControllerTest @Autowired constructor( @Test fun `should fail to create post with existing slug`() { - mockMvc.post("/posts/new") { + mockMvc.post("/posts") { contentType = MediaType.APPLICATION_JSON content = objectMapper.writeValueAsString(testPost) }.andExpect { status { isOk() } } mockMvc.perform( - post("/posts/new") + post("/posts") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(testPost)) ) @@ -246,7 +246,7 @@ internal class PostControllerTest @Autowired constructor( private val validationTester = ValidationTester( req = { params: Map -> mockMvc.perform( - post("/posts/new") + post("/posts") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(params)) ) diff --git a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/ProjectControllerTest.kt b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/ProjectControllerTest.kt index 132ff20c..eaa2c733 100644 --- a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/ProjectControllerTest.kt +++ b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/ProjectControllerTest.kt @@ -274,7 +274,7 @@ internal class ProjectControllerTest @Autowired constructor( } @NestedTest - @DisplayName("POST /projects/new") + @DisplayName("POST /projects") inner class CreateProject { private val uuid: UUID = UUID.randomUUID() private val mockedSettings = Mockito.mockStatic(UUID::class.java) @@ -315,7 +315,7 @@ internal class ProjectControllerTest @Autowired constructor( ) ) - mockMvc.multipartBuilder("/projects/new") + mockMvc.multipartBuilder("/projects") .addPart("project", projectPart) .addFile(name = "image") .perform() @@ -368,13 +368,13 @@ internal class ProjectControllerTest @Autowired constructor( mutableListOf(testAccount) ) - mockMvc.multipartBuilder("/projects/new") + mockMvc.multipartBuilder("/projects") .addPart("project", objectMapper.writeValueAsString(testProject)) .addFile(name = "image") .perform() .andExpect { status().isOk } - mockMvc.multipartBuilder("/projects/new") + mockMvc.multipartBuilder("/projects") .addPart("project", objectMapper.writeValueAsString(duplicatedSlugProject)) .addFile(name = "image") .perform() @@ -389,7 +389,7 @@ internal class ProjectControllerTest @Autowired constructor( @Test fun `should fail to create project with invalid filename extension`() { - mockMvc.multipartBuilder("/projects/new") + mockMvc.multipartBuilder("/projects") .addPart("project", objectMapper.writeValueAsString(testProject)) .addFile(name = "image", filename = "image.pdf") .perform() @@ -405,7 +405,7 @@ internal class ProjectControllerTest @Autowired constructor( @Test fun `should fail to create project with invalid filename media type`() { - mockMvc.multipartBuilder("/projects/new") + mockMvc.multipartBuilder("/projects") .addPart("project", objectMapper.writeValueAsString(testProject)) .addFile(name = "image", contentType = MediaType.APPLICATION_PDF_VALUE) .perform() @@ -421,7 +421,7 @@ internal class ProjectControllerTest @Autowired constructor( @Test fun `should fail when missing project part`() { - mockMvc.multipartBuilder("/projects/new") + mockMvc.multipartBuilder("/projects") .addFile(name = "image") .perform() .andExpectAll( @@ -438,7 +438,7 @@ internal class ProjectControllerTest @Autowired constructor( inner class InputValidation { private val validationTester = ValidationTester( req = { params: Map -> - mockMvc.multipartBuilder("/projects/new") + mockMvc.multipartBuilder("/projects") .addPart("project", objectMapper.writeValueAsString(params)) .addFile(name = "image") .perform() @@ -1289,7 +1289,7 @@ internal class ProjectControllerTest @Autowired constructor( } @NestedTest - @DisplayName("PUT /projects/{projectId}/addTeamMember/{accountId}") + @DisplayName("PUT /projects/{projectId}/team/{accountId}") inner class AddTeamMember { private val newAccount = Account( @@ -1324,7 +1324,7 @@ internal class ProjectControllerTest @Autowired constructor( @Test fun `should add a team member`() { mockMvc.perform( - put("/projects/{projectId}/addTeamMember/{accountId}", testProject.id, newAccount.id) + put("/projects/{projectId}/team/{accountId}", testProject.id, newAccount.id) ) .andExpectAll( status().isOk, content().contentType(MediaType.APPLICATION_JSON), @@ -1358,7 +1358,7 @@ internal class ProjectControllerTest @Autowired constructor( @Test fun `should fail if the team member does not exist`() { - mockMvc.perform(put("/projects/{projectId}/addTeamMember/{accountId}", testProject.id, 1234)) + mockMvc.perform(put("/projects/{projectId}/team/{accountId}", testProject.id, 1234)) .andExpectAll( status().isNotFound, content().contentType(MediaType.APPLICATION_JSON), @@ -1373,7 +1373,7 @@ internal class ProjectControllerTest @Autowired constructor( } @NestedTest - @DisplayName("PUT /projects/{projectId}/removeTeamMember/{accountId}") + @DisplayName("DELETE /projects/{projectId}/team/{accountId}") inner class RemoveTeamMember { @BeforeEach @@ -1392,7 +1392,7 @@ internal class ProjectControllerTest @Autowired constructor( @Test fun `should remove a team member`() { - mockMvc.perform(put("/projects/{projectId}/removeTeamMember/{accountId}", testProject.id, testAccount.id)) + mockMvc.perform(delete("/projects/{projectId}/team/{accountId}", testProject.id, testAccount.id)) .andExpectAll( status().isOk, content().contentType(MediaType.APPLICATION_JSON), @@ -1408,7 +1408,7 @@ internal class ProjectControllerTest @Autowired constructor( @Test fun `should fail if the team member does not exist`() { - mockMvc.perform(put("/projects/{projectId}/removeTeamMember/{accountId}", testProject.id, 1234)) + mockMvc.perform(delete("/projects/{projectId}/team/{accountId}", testProject.id, 1234)) .andExpectAll( status().isNotFound, content().contentType(MediaType.APPLICATION_JSON),