Skip to content

Commit

Permalink
ADM-715[backend]feat: put branch in request body (#948)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBoDai authored Jan 17, 2024
1 parent bcca032 commit 658138c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -58,14 +57,15 @@ public ResponseEntity<Void> verifyToken(@PathVariable SourceType sourceType,
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

@PostMapping("/{sourceType}/repos/branches/{branch}/verify")
public ResponseEntity<Void> verifyBranch(@PathVariable SourceType sourceType, @PathVariable @NotBlank String branch,
@PostMapping("/{sourceType}/repos/branches/verify")
public ResponseEntity<Void> 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 -> {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ public class VerifyBranchRequest {
@NotBlank(message = "Repository is required.")
private String repository;

@NotBlank(message = "Branch cannot be empty.")
private String branch;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -79,22 +82,26 @@ void shouldReturnNoContentStatusWhenVerifyToken() throws Exception {
.content(new ObjectMapper().writeValueAsString(sourceControlDTO))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());

verify(gitHubVerifyService, times(1)).verifyTokenV2(GITHUB_TOKEN);
}

@Test
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
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -183,34 +194,37 @@ 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())
.andReturn()
.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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 658138c

Please sign in to comment.