From 658138ccaf5bf3c2b6b4c80a60ad30e6de832611 Mon Sep 17 00:00:00 2001 From: junbo dai Date: Wed, 17 Jan 2024 10:34:10 +0800 Subject: [PATCH] ADM-715[backend]feat: put branch in request body (#948) --- .../controller/source/GithubController.java | 12 +-- .../source/dto/VerifyBranchRequest.java | 3 + .../source/GithubControllerTest.java | 75 ++++++++++++------- 3 files changed, 55 insertions(+), 35 deletions(-) diff --git a/backend/src/main/java/heartbeat/controller/source/GithubController.java b/backend/src/main/java/heartbeat/controller/source/GithubController.java index b3fddcef7e..ced3203a11 100644 --- a/backend/src/main/java/heartbeat/controller/source/GithubController.java +++ b/backend/src/main/java/heartbeat/controller/source/GithubController.java @@ -5,7 +5,6 @@ import heartbeat.controller.source.dto.VerifyBranchRequest; import heartbeat.service.source.github.GitHubService; import jakarta.validation.Valid; -import jakarta.validation.constraints.NotBlank; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; @@ -58,14 +57,15 @@ public ResponseEntity verifyToken(@PathVariable SourceType sourceType, return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); } - @PostMapping("/{sourceType}/repos/branches/{branch}/verify") - public ResponseEntity verifyBranch(@PathVariable SourceType sourceType, @PathVariable @NotBlank String branch, + @PostMapping("/{sourceType}/repos/branches/verify") + public ResponseEntity verifyBranch(@PathVariable SourceType sourceType, @RequestBody @Valid VerifyBranchRequest request) { - log.info("Start to verify source type: {} branch: {}.", sourceType, branch); + log.info("Start to verify source type: {} branch: {}.", sourceType, request.getBranch()); switch (sourceType) { case GITHUB -> { - gitHubService.verifyCanReadTargetBranch(request.getRepository(), branch, request.getToken()); - log.info("Successfully verify source type: {} branch: {}.", sourceType, branch); + gitHubService.verifyCanReadTargetBranch(request.getRepository(), request.getBranch(), + request.getToken()); + log.info("Successfully verify source type: {} branch: {}.", sourceType, request.getBranch()); } default -> { } diff --git a/backend/src/main/java/heartbeat/controller/source/dto/VerifyBranchRequest.java b/backend/src/main/java/heartbeat/controller/source/dto/VerifyBranchRequest.java index dc25ef6f80..508ddd5f8b 100644 --- a/backend/src/main/java/heartbeat/controller/source/dto/VerifyBranchRequest.java +++ b/backend/src/main/java/heartbeat/controller/source/dto/VerifyBranchRequest.java @@ -25,4 +25,7 @@ public class VerifyBranchRequest { @NotBlank(message = "Repository is required.") private String repository; + @NotBlank(message = "Branch cannot be empty.") + private String branch; + } diff --git a/backend/src/test/java/heartbeat/controller/source/GithubControllerTest.java b/backend/src/test/java/heartbeat/controller/source/GithubControllerTest.java index e4417a82dc..085db417f3 100644 --- a/backend/src/test/java/heartbeat/controller/source/GithubControllerTest.java +++ b/backend/src/test/java/heartbeat/controller/source/GithubControllerTest.java @@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters; @@ -26,6 +27,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @@ -79,6 +82,8 @@ void shouldReturnNoContentStatusWhenVerifyToken() throws Exception { .content(new ObjectMapper().writeValueAsString(sourceControlDTO)) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNoContent()); + + verify(gitHubVerifyService, times(1)).verifyTokenV2(GITHUB_TOKEN); } @Test @@ -86,15 +91,17 @@ void shouldReturnNoContentStatusWhenVerifyTargetBranch() throws Exception { VerifyBranchRequest verifyBranchRequest = VerifyBranchRequest.builder() .repository(GITHUB_REPOSITORY) .token(GITHUB_TOKEN) + .branch(MAIN_BRANCH) .build(); - doNothing().when(gitHubVerifyService).verifyCanReadTargetBranch("fake/repo", MAIN_BRANCH, GITHUB_TOKEN); + doNothing().when(gitHubVerifyService).verifyCanReadTargetBranch(any(), any(), any()); mockMvc - .perform( - post("/source-control/{sourceType}/repos/branches/{branch}/verify", NORMAL_SOURCE_TYPE, MAIN_BRANCH) - .content(new ObjectMapper().writeValueAsString(verifyBranchRequest)) - .contentType(MediaType.APPLICATION_JSON)) + .perform(post("/source-control/{sourceType}/repos/branches/verify", NORMAL_SOURCE_TYPE) + .content(new ObjectMapper().writeValueAsString(verifyBranchRequest)) + .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNoContent()); + + verify(gitHubVerifyService, times(1)).verifyCanReadTargetBranch(GITHUB_REPOSITORY, MAIN_BRANCH, GITHUB_TOKEN); } @Test @@ -132,14 +139,16 @@ void shouldReturnBadRequestGivenRequestBodyIsNullWhenVerifyToken() throws Except } @Test - void shouldReturnBadRequestGivenRequestBodyIsNullWhenVerifyBranch() throws Exception { - VerifyBranchRequest verifyBranchRequest = VerifyBranchRequest.builder().build(); + void shouldReturnBadRequestGivenTokenIsNullWhenVerifyBranch() throws Exception { + VerifyBranchRequest verifyBranchRequest = VerifyBranchRequest.builder() + .repository(GITHUB_REPOSITORY) + .branch(MAIN_BRANCH) + .build(); final var response = mockMvc - .perform( - post("/source-control/{sourceType}/repos/branches/{branch}/verify", NORMAL_SOURCE_TYPE, MAIN_BRANCH) - .content(new ObjectMapper().writeValueAsString(verifyBranchRequest)) - .contentType(MediaType.APPLICATION_JSON)) + .perform(post("/source-control/{sourceType}/repos/branches/verify", NORMAL_SOURCE_TYPE, MAIN_BRANCH) + .content(new ObjectMapper().writeValueAsString(verifyBranchRequest)) + .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()) .andReturn() .getResponse(); @@ -151,13 +160,15 @@ void shouldReturnBadRequestGivenRequestBodyIsNullWhenVerifyBranch() throws Excep @Test void shouldReturnBadRequestGivenRepositoryIsNullWhenVerifyBranch() throws Exception { - VerifyBranchRequest verifyBranchRequest = VerifyBranchRequest.builder().token(GITHUB_TOKEN).build(); + VerifyBranchRequest verifyBranchRequest = VerifyBranchRequest.builder() + .token(GITHUB_TOKEN) + .branch(MAIN_BRANCH) + .build(); final var response = mockMvc - .perform( - post("/source-control/{sourceType}/repos/branches/{branch}/verify", NORMAL_SOURCE_TYPE, MAIN_BRANCH) - .content(new ObjectMapper().writeValueAsString(verifyBranchRequest)) - .contentType(MediaType.APPLICATION_JSON)) + .perform(post("/source-control/{sourceType}/repos/branches/verify", NORMAL_SOURCE_TYPE, MAIN_BRANCH) + .content(new ObjectMapper().writeValueAsString(verifyBranchRequest)) + .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()) .andReturn() .getResponse(); @@ -183,25 +194,28 @@ void shouldReturnBadRequestGivenSourceTypeIsWrongWhenVerifyBranch() throws Excep VerifyBranchRequest request = VerifyBranchRequest.builder() .repository(GITHUB_REPOSITORY) .token(GITHUB_TOKEN) + .branch(MAIN_BRANCH) .build(); mockMvc - .perform(post("/source-control/{sourceType}/repos/branches/{branch}/verify", BAD_SOURCE_TYPE, MAIN_BRANCH) + .perform(post("/source-control/{sourceType}/repos/branches/verify", BAD_SOURCE_TYPE) .content(new ObjectMapper().writeValueAsString(request)) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()); } - @Test - void shouldReturnBadRequestGivenSourceTypeIsBlankWhenVerifyBranch() throws Exception { + @ParameterizedTest + @NullSource + @ValueSource(strings = { EMPTY_BRANCH_NAME, "" }) + void shouldReturnBadRequestGivenSourceTypeIsBlankWhenVerifyBranch(String branch) throws Exception { VerifyBranchRequest request = VerifyBranchRequest.builder() .token(GITHUB_TOKEN) .repository(GITHUB_REPOSITORY) + .branch(branch) .build(); var response = mockMvc - .perform(post("/source-control/{sourceType}/repos/branches/{branch}/verify", NORMAL_SOURCE_TYPE, - EMPTY_BRANCH_NAME) + .perform(post("/source-control/{sourceType}/repos/branches/verify", NORMAL_SOURCE_TYPE, EMPTY_BRANCH_NAME) .content(new ObjectMapper().writeValueAsString(request)) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()) @@ -209,8 +223,8 @@ void shouldReturnBadRequestGivenSourceTypeIsBlankWhenVerifyBranch() throws Excep .getResponse(); final var content = response.getContentAsString(); - final var result = JsonPath.parse(content).read("$.message").toString(); - assertThat(result).contains("verifyBranch.branch: must not be blank"); + final var result = JsonPath.parse(content).read("$.branch").toString(); + assertThat(result).contains("Branch cannot be empty."); } @Test @@ -249,15 +263,18 @@ void shouldReturnBadRequestGivenRequestParamPatternIsIncorrectWhenVerifyToken(St } @ParameterizedTest - @ValueSource(strings = { "12345", EMPTY_BRANCH_NAME, "" }) + @ValueSource(strings = { "12345", " ", "" }) void shouldReturnBadRequestGivenRequestParamPatternIsIncorrectWhenVerifyBranch(String token) throws Exception { - VerifyBranchRequest request = VerifyBranchRequest.builder().token(token).build(); + VerifyBranchRequest request = VerifyBranchRequest.builder() + .token(token) + .branch(MAIN_BRANCH) + .repository(GITHUB_REPOSITORY) + .build(); final var response = mockMvc - .perform( - post("/source-control/{sourceType}/repos/branches/{branch}/verify", NORMAL_SOURCE_TYPE, MAIN_BRANCH) - .content(new ObjectMapper().writeValueAsString(request)) - .contentType(MediaType.APPLICATION_JSON)) + .perform(post("/source-control/{sourceType}/repos/branches/verify", NORMAL_SOURCE_TYPE) + .content(new ObjectMapper().writeValueAsString(request)) + .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()) .andReturn() .getResponse();