From 02da8c104eb9141cdb75949bd4e3e2e3e18229ae Mon Sep 17 00:00:00 2001 From: Andrea Date: Fri, 5 Jan 2024 10:43:15 +0800 Subject: [PATCH 01/20] ADM-709:[backend]feat: add verify token and get info for buildKite controller --- .../pipeline/PipelineController.java | 27 +++++++++ .../pipeline/dto/request/TokenParam.java | 20 +++++++ .../pipeline/buildkite/BuildKiteService.java | 56 +++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 backend/src/main/java/heartbeat/controller/pipeline/dto/request/TokenParam.java diff --git a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java index 4d449f11f4..59fadf6956 100644 --- a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java +++ b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java @@ -1,14 +1,17 @@ package heartbeat.controller.pipeline; +import heartbeat.controller.pipeline.dto.request.TokenParam; import heartbeat.controller.pipeline.dto.request.PipelineParam; import heartbeat.controller.pipeline.dto.request.PipelineStepsParam; import heartbeat.controller.pipeline.dto.response.BuildKiteResponseDTO; import heartbeat.controller.pipeline.dto.response.PipelineStepsDTO; +import heartbeat.exception.PermissionDenyException; import heartbeat.service.pipeline.buildkite.BuildKiteService; import jakarta.validation.Valid; import jakarta.validation.constraints.NotBlank; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; @@ -35,6 +38,30 @@ public BuildKiteResponseDTO getBuildKiteInfo(@PathVariable String pipelineType, return buildKiteService.fetchPipelineInfo(pipelineParam); } + @PostMapping("/{pipelineType}/verify") + public ResponseEntity verifyBuildKiteToken(@PathVariable String pipelineType, + @Valid @RequestBody TokenParam tokenParam) { + if (buildKiteService.getBuildKiteVerify(tokenParam.getToken())) { + return ResponseEntity.noContent().build(); + } + else { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + } + } + + @PostMapping("/{pipelineType}/info") + public ResponseEntity fetchBuildKiteInfo(@PathVariable String pipelineType, + @Valid @RequestBody PipelineParam pipelineParam) { + BuildKiteResponseDTO buildKiteResponse = buildKiteService.getBuildKiteInfo(pipelineParam); + + if (buildKiteResponse.getPipelineList().isEmpty()) { + return ResponseEntity.noContent().build(); + } + else { + return ResponseEntity.ok(buildKiteResponse); + } + } + @GetMapping("/{pipelineType}/{organizationId}/pipelines/{buildId}/steps") public ResponseEntity getPipelineSteps( @RequestHeader("Authorization") @NotBlank(message = "Token must not be blank") String token, diff --git a/backend/src/main/java/heartbeat/controller/pipeline/dto/request/TokenParam.java b/backend/src/main/java/heartbeat/controller/pipeline/dto/request/TokenParam.java new file mode 100644 index 0000000000..3952fae8f6 --- /dev/null +++ b/backend/src/main/java/heartbeat/controller/pipeline/dto/request/TokenParam.java @@ -0,0 +1,20 @@ +package heartbeat.controller.pipeline.dto.request; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotBlank; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class TokenParam { + + @Valid + @NotBlank(message = "Token cannot be empty.") + private String token; + +} diff --git a/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java b/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java index ab69452c31..446995bee9 100644 --- a/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java +++ b/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java @@ -300,4 +300,60 @@ private List getBuildsByState(List buildInfos, .toList(); } + public boolean getBuildKiteVerify(String token) { + try { + String buildKiteToken = "Bearer " + token; + log.info("Start to query token permissions by token"); + BuildKiteTokenInfo buildKiteTokenInfo = buildKiteFeignClient.getTokenInfo(buildKiteToken); + log.info("Successfully query token permissions by token, token info scopes: {}", + buildKiteTokenInfo.getScopes()); + verifyToken(buildKiteTokenInfo); + return true; + } + catch (RuntimeException e) { + Throwable cause = Optional.ofNullable(e.getCause()).orElse(e); + log.error("Failed to call BuildKite, e: {}", cause.getMessage()); + if (cause instanceof BaseException baseException) { + throw baseException; + } + throw new InternalServerErrorException( + String.format("Failed to call BuildKite, cause is %s", cause.getMessage())); + } + } + + public BuildKiteResponseDTO getBuildKiteInfo(PipelineParam pipelineParam) { + try { + String buildKiteToken = "Bearer " + pipelineParam.getToken(); + log.info("Start to query BuildKite organizations by token"); + List buildKiteOrganizationsInfo = buildKiteFeignClient + .getBuildKiteOrganizationsInfo(buildKiteToken); + log.info("Successfully query BuildKite organizations by token, slug: {}", buildKiteOrganizationsInfo); + + log.info("Start to query buildKite pipelineInfo by organizations slug: {}", buildKiteOrganizationsInfo); + List buildKiteInfoList = buildKiteOrganizationsInfo.stream() + .flatMap(org -> buildKiteFeignClient + .getPipelineInfo(buildKiteToken, org.getSlug(), "1", "100", pipelineParam.getStartTime(), + pipelineParam.getEndTime()) + .stream() + .map(pipeline -> PipelineTransformer.fromBuildKitePipelineDto(pipeline, org.getSlug(), + org.getName()))) + .collect(Collectors.toList()); + log.info("Successfully get buildKite pipelineInfo, slug:{}, pipelineInfoList size:{}", + buildKiteOrganizationsInfo, buildKiteInfoList.size()); + + return BuildKiteResponseDTO.builder().pipelineList(buildKiteInfoList).build(); + } + catch (RuntimeException e) { + Throwable cause = Optional.ofNullable(e.getCause()).orElse(e); + log.error("Failed to call BuildKite, start time: {}, e: {}", pipelineParam.getStartTime(), + cause.getMessage()); + if (cause instanceof BaseException baseException) { + throw baseException; + } + throw new InternalServerErrorException( + String.format("Failed to call BuildKite, cause is %s", cause.getMessage())); + + } + } + } From 2cbd26b7cd08c2e722f816abf39e1188ba734e6f Mon Sep 17 00:00:00 2001 From: Andrea Date: Fri, 5 Jan 2024 14:33:09 +0800 Subject: [PATCH 02/20] ADM-709:[backend]feat: add test for buildKite service --- .../buildkite/BuildKiteServiceTest.java | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java b/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java index be3b88207b..e56d1921f4 100644 --- a/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java +++ b/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java @@ -9,6 +9,7 @@ import static org.hibernate.validator.internal.util.Contracts.assertNotNull; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; @@ -551,4 +552,101 @@ void shouldReturnStepBeforeEndStepsGivenStepsArray() { assertEquals(targetStep, result.get(2)); } + @Test + void shouldReturnTrueWhenTokenIsCorrect() { + BuildKiteTokenInfo buildKiteTokenInfo = BuildKiteTokenInfo.builder() + .scopes(List.of("read_builds", "read_organizations", "read_pipelines")) + .build(); + + when(buildKiteFeignClient.getTokenInfo(any())).thenReturn(buildKiteTokenInfo); + + boolean result = buildKiteService.getBuildKiteVerify("test_token"); + assertTrue(result); + } + + @Test + void shouldThrowUnauthorizedExceptionWhenTokenIsIncorrect(){ + when(buildKiteFeignClient.getTokenInfo(any())) + .thenThrow(new UnauthorizedException("unauthorized")); + + Assertions + .assertThatThrownBy( + () -> buildKiteService.getBuildKiteVerify("test_token")) + .isInstanceOf(UnauthorizedException.class) + .hasMessageContaining("unauthorized"); + } + + @Test + void shouldThrowInternalServerErrorExceptionWhenGetBuildKiteVerify500Exception(){ + when(buildKiteFeignClient.getTokenInfo(any())).thenReturn(null); + + assertThatThrownBy(() -> buildKiteService.getBuildKiteVerify("test_token")) + .isInstanceOf(InternalServerErrorException.class) + .hasMessageContaining("Failed to call BuildKite, cause is"); + } + + @Test + void shouldReturnBuildKiteResponseWhenGetBuildKiteInfo() throws IOException { + PipelineParam pipelineParam = PipelineParam.builder() + .token("test_token") + .startTime("startTime") + .endTime("endTime") + .build(); + ObjectMapper mapper = new ObjectMapper(); + List pipelineDTOS = mapper.readValue( + new File("src/test/java/heartbeat/controller/pipeline/buildKitePipelineInfoData.json"), + new TypeReference<>() { + }); + + when(buildKiteFeignClient.getBuildKiteOrganizationsInfo(any())) + .thenReturn(List.of(BuildKiteOrganizationsInfo.builder().name("XXXX").slug("XXXX").build())); + when(buildKiteFeignClient.getPipelineInfo("Bearer test_token", "XXXX", "1", "100", "startTime", "endTime")) + .thenReturn(pipelineDTOS); + + BuildKiteResponseDTO buildKiteResponseDTO = buildKiteService.getBuildKiteInfo(pipelineParam); + + assertThat(buildKiteResponseDTO.getPipelineList().size()).isEqualTo(1); + Pipeline pipeline = buildKiteResponseDTO.getPipelineList().get(0); + assertThat(pipeline.getId()).isEqualTo("payment-selector-ui"); + assertThat(pipeline.getName()).isEqualTo("payment-selector-ui"); + assertThat(pipeline.getOrgId()).isEqualTo("XXXX"); + assertThat(pipeline.getOrgName()).isEqualTo("XXXX"); + assertThat(pipeline.getRepository()) + .isEqualTo("https://github.com/XXXX-fs/fs-platform-payment-selector-ui.git"); + assertThat(pipeline.getSteps().size()).isEqualTo(1); + } + + @Test + void shouldThrowInternalServerErrorExceptionWhenGetBuildKiteInfo500Exception() { + PipelineParam pipelineParam = PipelineParam.builder() + .token("test_token") + .startTime("startTime") + .endTime("endTime") + .build(); + + when(buildKiteFeignClient.getBuildKiteOrganizationsInfo(any())).thenReturn(null); + when(buildKiteFeignClient.getPipelineInfo("Bearer test_token", "XXXX", "1", "100", "startTime", "endTime")) + .thenReturn(null); + + assertThatThrownBy(() -> buildKiteService.getBuildKiteInfo(pipelineParam)) + .isInstanceOf(InternalServerErrorException.class) + .hasMessageContaining("Failed to call BuildKite, cause is"); + } + + @Test + void shouldThrowUnauthorizedExceptionWhenGetBuildKiteInfoAndTokenIsIncorrect() { + PipelineParam pipelineParam = PipelineParam.builder() + .token("test_token") + .startTime("startTime") + .endTime("endTime") + .build(); + + when(buildKiteFeignClient.getBuildKiteOrganizationsInfo(any())) + .thenThrow(new UnauthorizedException("unauthorized")); + + Assertions.assertThatThrownBy(() -> buildKiteService.getBuildKiteInfo(pipelineParam)) + .isInstanceOf(UnauthorizedException.class) + .hasMessageContaining("unauthorized"); + } + } From e7efec8c846c02f84ab7bd4d62364cd9180ed398 Mon Sep 17 00:00:00 2001 From: Andrea Date: Fri, 5 Jan 2024 15:51:05 +0800 Subject: [PATCH 03/20] ADM-650:[docs]fix: fix docs --- .../docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/src/content/docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx b/docs/src/content/docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx index cf8fb48c82..a135c03032 100644 --- a/docs/src/content/docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx +++ b/docs/src/content/docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx @@ -93,7 +93,7 @@ request: { token: "your_token" } responses: - Status Code: 200 + Status Code: 204 ``` - Exception Handler @@ -189,6 +189,7 @@ responses:{ ] }] } +当 pipelineList 为空时, responses 204 ``` - Exception Handler
From 41f013dd018982c0889b085ae31e9e65c3985ca5 Mon Sep 17 00:00:00 2001 From: Andrea Date: Mon, 8 Jan 2024 10:15:00 +0800 Subject: [PATCH 04/20] ADM-709:[backend]fix: fix verify buildKite method --- .../heartbeat/controller/pipeline/PipelineController.java | 8 ++------ .../service/pipeline/buildkite/BuildKiteService.java | 8 ++++---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java index 59fadf6956..1eabdb0bdb 100644 --- a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java +++ b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java @@ -41,12 +41,8 @@ public BuildKiteResponseDTO getBuildKiteInfo(@PathVariable String pipelineType, @PostMapping("/{pipelineType}/verify") public ResponseEntity verifyBuildKiteToken(@PathVariable String pipelineType, @Valid @RequestBody TokenParam tokenParam) { - if (buildKiteService.getBuildKiteVerify(tokenParam.getToken())) { - return ResponseEntity.noContent().build(); - } - else { - return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); - } + buildKiteService.getBuildKiteVerify(tokenParam.getToken()); + return ResponseEntity.noContent().build(); } @PostMapping("/{pipelineType}/info") diff --git a/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java b/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java index 446995bee9..b63e11c15f 100644 --- a/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java +++ b/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java @@ -17,7 +17,7 @@ import heartbeat.exception.BaseException; import heartbeat.exception.InternalServerErrorException; import heartbeat.exception.NotFoundException; -import heartbeat.exception.PermissionDenyException; +import heartbeat.exception.UnauthorizedException; import heartbeat.util.TimeUtil; import jakarta.annotation.PreDestroy; import lombok.RequiredArgsConstructor; @@ -103,7 +103,8 @@ private void verifyToken(BuildKiteTokenInfo buildKiteTokenInfo) { if (!buildKiteTokenInfo.getScopes().contains(permission)) { log.error("Failed to call BuildKite, because of insufficient permission, current permissions: {}", buildKiteTokenInfo.getScopes()); - throw new PermissionDenyException("Failed to call BuildKite, because of insufficient permission!"); + throw new UnauthorizedException("Failed to call BuildKite, because of insufficient permission!");// todo + // 401 } } } @@ -300,7 +301,7 @@ private List getBuildsByState(List buildInfos, .toList(); } - public boolean getBuildKiteVerify(String token) { + public void getBuildKiteVerify(String token) { try { String buildKiteToken = "Bearer " + token; log.info("Start to query token permissions by token"); @@ -308,7 +309,6 @@ public boolean getBuildKiteVerify(String token) { log.info("Successfully query token permissions by token, token info scopes: {}", buildKiteTokenInfo.getScopes()); verifyToken(buildKiteTokenInfo); - return true; } catch (RuntimeException e) { Throwable cause = Optional.ofNullable(e.getCause()).orElse(e); From 5cf2d137db45c1ad6299cc2930ff2649729049dc Mon Sep 17 00:00:00 2001 From: Andrea Date: Mon, 8 Jan 2024 10:22:44 +0800 Subject: [PATCH 05/20] ADM-709:[backend]fix: fix test --- .../service/pipeline/buildkite/BuildKiteServiceTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java b/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java index e56d1921f4..0e18721bf4 100644 --- a/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java +++ b/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java @@ -9,10 +9,10 @@ import static org.hibernate.validator.internal.util.Contracts.assertNotNull; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -171,7 +171,7 @@ void shouldThrowNoPermissionExceptionWhenTokenPermissionDeny() { BuildKiteTokenInfo buildKiteTokenInfo = BuildKiteTokenInfo.builder().scopes(List.of("mock")).build(); when(buildKiteFeignClient.getTokenInfo(any())).thenReturn(buildKiteTokenInfo); - assertThrows(PermissionDenyException.class, () -> buildKiteService.fetchPipelineInfo( + assertThrows(UnauthorizedException.class, () -> buildKiteService.fetchPipelineInfo( PipelineParam.builder().token("test_token").startTime("startTime").endTime("endTime").build())); } @@ -559,9 +559,9 @@ void shouldReturnTrueWhenTokenIsCorrect() { .build(); when(buildKiteFeignClient.getTokenInfo(any())).thenReturn(buildKiteTokenInfo); + buildKiteService.getBuildKiteVerify("mock_token"); - boolean result = buildKiteService.getBuildKiteVerify("test_token"); - assertTrue(result); + verify(buildKiteFeignClient, times(1)).getTokenInfo(anyString()); } @Test From 417165e8e5e94ea464326b6f5baba2299be02d5b Mon Sep 17 00:00:00 2001 From: Andrea Date: Mon, 8 Jan 2024 10:51:01 +0800 Subject: [PATCH 06/20] ADM-709:[backend]fix: fix error --- .../service/pipeline/buildkite/BuildKiteService.java | 12 +++++------- .../pipeline/buildkite/BuildKiteServiceTest.java | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java b/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java index b63e11c15f..35d32588d8 100644 --- a/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java +++ b/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java @@ -17,7 +17,7 @@ import heartbeat.exception.BaseException; import heartbeat.exception.InternalServerErrorException; import heartbeat.exception.NotFoundException; -import heartbeat.exception.UnauthorizedException; +import heartbeat.exception.PermissionDenyException; import heartbeat.util.TimeUtil; import jakarta.annotation.PreDestroy; import lombok.RequiredArgsConstructor; @@ -38,7 +38,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.IntStream; -import java.net.URLEncoder; @Service @RequiredArgsConstructor @@ -65,7 +64,7 @@ public BuildKiteResponseDTO fetchPipelineInfo(PipelineParam pipelineParam) { BuildKiteTokenInfo buildKiteTokenInfo = buildKiteFeignClient.getTokenInfo(buildKiteToken); log.info("Successfully query token permissions by token, token info scopes: {}", buildKiteTokenInfo.getScopes()); - verifyToken(buildKiteTokenInfo); + verifyTokenScopes(buildKiteTokenInfo); log.info("Start to query BuildKite organizations by token"); List buildKiteOrganizationsInfo = buildKiteFeignClient .getBuildKiteOrganizationsInfo(buildKiteToken); @@ -98,13 +97,12 @@ public BuildKiteResponseDTO fetchPipelineInfo(PipelineParam pipelineParam) { } } - private void verifyToken(BuildKiteTokenInfo buildKiteTokenInfo) { + private void verifyTokenScopes(BuildKiteTokenInfo buildKiteTokenInfo) { for (String permission : permissions) { if (!buildKiteTokenInfo.getScopes().contains(permission)) { log.error("Failed to call BuildKite, because of insufficient permission, current permissions: {}", buildKiteTokenInfo.getScopes()); - throw new UnauthorizedException("Failed to call BuildKite, because of insufficient permission!");// todo - // 401 + throw new PermissionDenyException("Failed to call BuildKite, because of insufficient permission!"); } } } @@ -308,7 +306,7 @@ public void getBuildKiteVerify(String token) { BuildKiteTokenInfo buildKiteTokenInfo = buildKiteFeignClient.getTokenInfo(buildKiteToken); log.info("Successfully query token permissions by token, token info scopes: {}", buildKiteTokenInfo.getScopes()); - verifyToken(buildKiteTokenInfo); + verifyTokenScopes(buildKiteTokenInfo); } catch (RuntimeException e) { Throwable cause = Optional.ofNullable(e.getCause()).orElse(e); diff --git a/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java b/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java index 0e18721bf4..2f45dda56d 100644 --- a/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java +++ b/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java @@ -171,7 +171,7 @@ void shouldThrowNoPermissionExceptionWhenTokenPermissionDeny() { BuildKiteTokenInfo buildKiteTokenInfo = BuildKiteTokenInfo.builder().scopes(List.of("mock")).build(); when(buildKiteFeignClient.getTokenInfo(any())).thenReturn(buildKiteTokenInfo); - assertThrows(UnauthorizedException.class, () -> buildKiteService.fetchPipelineInfo( + assertThrows(PermissionDenyException.class, () -> buildKiteService.fetchPipelineInfo( PipelineParam.builder().token("test_token").startTime("startTime").endTime("endTime").build())); } From 4b48e0bb85cd6f7a5632f67ca1d2642d3d6bef7b Mon Sep 17 00:00:00 2001 From: Andrea Date: Mon, 8 Jan 2024 14:48:17 +0800 Subject: [PATCH 07/20] ADM-709:[backend]fix: add BuildKiteController Test --- .../pipeline/BuildKiteControllerTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java b/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java index 62efa0527c..f12bd2ab65 100644 --- a/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java +++ b/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -12,6 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.jayway.jsonpath.JsonPath; import heartbeat.controller.pipeline.dto.request.PipelineParam; +import heartbeat.controller.pipeline.dto.request.TokenParam; import heartbeat.controller.pipeline.dto.response.BuildKiteResponseDTO; import heartbeat.controller.pipeline.dto.response.Pipeline; import heartbeat.controller.pipeline.dto.response.PipelineStepsDTO; @@ -19,6 +21,7 @@ import java.io.File; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import lombok.val; @@ -116,4 +119,70 @@ void shouldReturnNoContentIfNoStepsWhenCallBuildKite() throws Exception { assertThat(response.getContentAsString()).isEqualTo(""); } + @Test + void shouldReturnNoContentIfTokenCorrectWhenCallBuildKiteMockServer() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + TokenParam tokenParam = TokenParam.builder().token("test_token").build(); + doNothing().when(buildKiteService).getBuildKiteVerify(any()); + + MockHttpServletResponse response = mockMvc + .perform(post("/pipelines/buildKite/verify").contentType(MediaType.APPLICATION_JSON) + .content(mapper.writeValueAsString(tokenParam))) + .andExpect(status().isNoContent()) + .andReturn() + .getResponse(); + + assertThat(response.getContentAsString()).isEqualTo(""); + } + + @Test + void shouldReturnPipelineInfoIfTokenCorrectWhenCallBuildKiteMockServer() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + List pipelines = mapper.readValue( + new File("src/test/java/heartbeat/controller/pipeline/pipelineInfoData.json"), new TypeReference<>() { + }); + BuildKiteResponseDTO buildKiteResponseDTO = BuildKiteResponseDTO.builder().pipelineList(pipelines).build(); + PipelineParam pipelineParam = PipelineParam.builder() + .token("test_token") + .startTime("16737733") + .endTime("17657557") + .build(); + + when(buildKiteService.getBuildKiteInfo(any())).thenReturn(buildKiteResponseDTO); + + MockHttpServletResponse response = mockMvc + .perform(post("/pipelines/buildKite/info").contentType(MediaType.APPLICATION_JSON) + .content(mapper.writeValueAsString(pipelineParam))) + .andExpect(status().isOk()) + .andReturn() + .getResponse(); + final var resultId = JsonPath.parse(response.getContentAsString()).read("$.pipelineList[0].id").toString(); + assertThat(resultId).contains("payment-selector-ui"); + final var resultName = JsonPath.parse(response.getContentAsString()).read("$.pipelineList[0].name").toString(); + assertThat(resultName).contains("payment-selector-ui"); + } + + @Test + void shouldReturnNoContentIfPipelineInfoIsNullWhenCallBuildKiteMockServer() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + BuildKiteResponseDTO buildKiteResponseDTO = BuildKiteResponseDTO.builder() + .pipelineList(Collections.emptyList()) + .build(); + PipelineParam pipelineParam = PipelineParam.builder() + .token("test_token") + .startTime("16737733") + .endTime("17657557") + .build(); + + when(buildKiteService.getBuildKiteInfo(any())).thenReturn(buildKiteResponseDTO); + + MockHttpServletResponse response = mockMvc + .perform(post("/pipelines/buildKite/info").contentType(MediaType.APPLICATION_JSON) + .content(mapper.writeValueAsString(pipelineParam))) + .andExpect(status().isNoContent()) + .andReturn() + .getResponse(); + assertThat(response.getContentAsString()).isEqualTo(""); + } + } From 3c94296737f8c6b77420d7b1fa9ce79b9e69e317 Mon Sep 17 00:00:00 2001 From: Andrea Date: Mon, 8 Jan 2024 15:04:05 +0800 Subject: [PATCH 08/20] ADM-709:[docs]fix: fix error message --- .../tech-spikes-split-verify-of-buildkite.mdx | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/docs/src/content/docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx b/docs/src/content/docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx index a135c03032..5bada19509 100644 --- a/docs/src/content/docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx +++ b/docs/src/content/docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx @@ -103,18 +103,19 @@ responses: - + - - - - + + + + + @@ -199,12 +200,9 @@ responses:{ - + - - - - + @@ -217,6 +215,11 @@ responses:{ + + + + + From 110cb36aaa8587cb4d525a9912296c639cc2d946 Mon Sep 17 00:00:00 2001 From: Andrea Date: Mon, 8 Jan 2024 17:44:01 +0800 Subject: [PATCH 09/20] ADM-709:[backend]fix: fix assert --- .../java/heartbeat/controller/pipeline/PipelineController.java | 2 -- .../service/pipeline/buildkite/BuildKiteServiceTest.java | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java index 1eabdb0bdb..90a5b6d32d 100644 --- a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java +++ b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java @@ -5,13 +5,11 @@ import heartbeat.controller.pipeline.dto.request.PipelineStepsParam; import heartbeat.controller.pipeline.dto.response.BuildKiteResponseDTO; import heartbeat.controller.pipeline.dto.response.PipelineStepsDTO; -import heartbeat.exception.PermissionDenyException; import heartbeat.service.pipeline.buildkite.BuildKiteService; import jakarta.validation.Valid; import jakarta.validation.constraints.NotBlank; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; diff --git a/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java b/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java index 2f45dda56d..e502ba46bf 100644 --- a/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java +++ b/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java @@ -644,7 +644,7 @@ void shouldThrowUnauthorizedExceptionWhenGetBuildKiteInfoAndTokenIsIncorrect() { when(buildKiteFeignClient.getBuildKiteOrganizationsInfo(any())) .thenThrow(new UnauthorizedException("unauthorized")); - Assertions.assertThatThrownBy(() -> buildKiteService.getBuildKiteInfo(pipelineParam)) + assertThatThrownBy(() -> buildKiteService.getBuildKiteInfo(pipelineParam)) .isInstanceOf(UnauthorizedException.class) .hasMessageContaining("unauthorized"); } From f646eeaf11b1082e9f9e85e43ac1acd7c025cafc Mon Sep 17 00:00:00 2001 From: Andrea Date: Tue, 9 Jan 2024 14:13:23 +0800 Subject: [PATCH 10/20] ADM-709:[backend]feat: add PipelineTypeEnum and rename --- .../pipeline/PipelineController.java | 7 ++++--- .../dto/request/PipelineTypeEnum.java | 20 +++++++++++++++++++ .../pipeline/buildkite/BuildKiteService.java | 10 +++++----- 3 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineTypeEnum.java diff --git a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java index 90a5b6d32d..d41fb2c034 100644 --- a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java +++ b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java @@ -1,5 +1,6 @@ package heartbeat.controller.pipeline; +import heartbeat.controller.pipeline.dto.request.PipelineTypeEnum; import heartbeat.controller.pipeline.dto.request.TokenParam; import heartbeat.controller.pipeline.dto.request.PipelineParam; import heartbeat.controller.pipeline.dto.request.PipelineStepsParam; @@ -37,14 +38,14 @@ public BuildKiteResponseDTO getBuildKiteInfo(@PathVariable String pipelineType, } @PostMapping("/{pipelineType}/verify") - public ResponseEntity verifyBuildKiteToken(@PathVariable String pipelineType, + public ResponseEntity verifyBuildKiteToken(@PathVariable PipelineTypeEnum pipelineType, @Valid @RequestBody TokenParam tokenParam) { - buildKiteService.getBuildKiteVerify(tokenParam.getToken()); + buildKiteService.verifyToken(tokenParam.getToken()); return ResponseEntity.noContent().build(); } @PostMapping("/{pipelineType}/info") - public ResponseEntity fetchBuildKiteInfo(@PathVariable String pipelineType, + public ResponseEntity fetchBuildKiteInfo(@PathVariable PipelineTypeEnum pipelineType, @Valid @RequestBody PipelineParam pipelineParam) { BuildKiteResponseDTO buildKiteResponse = buildKiteService.getBuildKiteInfo(pipelineParam); diff --git a/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineTypeEnum.java b/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineTypeEnum.java new file mode 100644 index 0000000000..4db36d6dd3 --- /dev/null +++ b/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineTypeEnum.java @@ -0,0 +1,20 @@ +package heartbeat.controller.pipeline.dto.request; + +public enum PipelineTypeEnum { + + BuildKite("BuildKite"); + + public final String pipelineType; + + PipelineTypeEnum(String pipelineType) { + this.pipelineType = pipelineType; + } + + public static PipelineTypeEnum fromValue(String type) { + return switch (type) { + case "BuildKite" -> BuildKite; + default -> throw new IllegalArgumentException("Pipeline type does not find!"); + }; + } + +} diff --git a/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java b/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java index 35d32588d8..ecff106684 100644 --- a/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java +++ b/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java @@ -70,7 +70,7 @@ public BuildKiteResponseDTO fetchPipelineInfo(PipelineParam pipelineParam) { .getBuildKiteOrganizationsInfo(buildKiteToken); log.info("Successfully query BuildKite organizations by token, slug: {}", buildKiteOrganizationsInfo); - log.info("Start to query buildKite pipelineInfo by organizations slug: {}", buildKiteOrganizationsInfo); + log.info("Start to query BuildKite pipelineInfo by organizations slug: {}", buildKiteOrganizationsInfo); List buildKiteInfoList = buildKiteOrganizationsInfo.stream() .flatMap(org -> buildKiteFeignClient .getPipelineInfo(buildKiteToken, org.getSlug(), "1", "100", pipelineParam.getStartTime(), @@ -79,7 +79,7 @@ public BuildKiteResponseDTO fetchPipelineInfo(PipelineParam pipelineParam) { .map(pipeline -> PipelineTransformer.fromBuildKitePipelineDto(pipeline, org.getSlug(), org.getName()))) .collect(Collectors.toList()); - log.info("Successfully get buildKite pipelineInfo, slug:{}, pipelineInfoList size:{}", + log.info("Successfully get BuildKite pipelineInfo, slug:{}, pipelineInfoList size:{}", buildKiteOrganizationsInfo, buildKiteInfoList.size()); return BuildKiteResponseDTO.builder().pipelineList(buildKiteInfoList).build(); @@ -299,7 +299,7 @@ private List getBuildsByState(List buildInfos, .toList(); } - public void getBuildKiteVerify(String token) { + public void verifyToken(String token) { try { String buildKiteToken = "Bearer " + token; log.info("Start to query token permissions by token"); @@ -327,7 +327,7 @@ public BuildKiteResponseDTO getBuildKiteInfo(PipelineParam pipelineParam) { .getBuildKiteOrganizationsInfo(buildKiteToken); log.info("Successfully query BuildKite organizations by token, slug: {}", buildKiteOrganizationsInfo); - log.info("Start to query buildKite pipelineInfo by organizations slug: {}", buildKiteOrganizationsInfo); + log.info("Start to query BuildKite pipelineInfo by organizations slug: {}", buildKiteOrganizationsInfo); List buildKiteInfoList = buildKiteOrganizationsInfo.stream() .flatMap(org -> buildKiteFeignClient .getPipelineInfo(buildKiteToken, org.getSlug(), "1", "100", pipelineParam.getStartTime(), @@ -336,7 +336,7 @@ public BuildKiteResponseDTO getBuildKiteInfo(PipelineParam pipelineParam) { .map(pipeline -> PipelineTransformer.fromBuildKitePipelineDto(pipeline, org.getSlug(), org.getName()))) .collect(Collectors.toList()); - log.info("Successfully get buildKite pipelineInfo, slug:{}, pipelineInfoList size:{}", + log.info("Successfully get BuildKite pipelineInfo, slug:{}, pipelineInfoList size:{}", buildKiteOrganizationsInfo, buildKiteInfoList.size()); return BuildKiteResponseDTO.builder().pipelineList(buildKiteInfoList).build(); From 4fa91642ec0c89e2760d280664b7fdd16950848e Mon Sep 17 00:00:00 2001 From: Andrea Date: Tue, 9 Jan 2024 14:20:09 +0800 Subject: [PATCH 11/20] ADM-709:[docs]fix: fix docs --- .../docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/content/docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx b/docs/src/content/docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx index 5bada19509..f182d29427 100644 --- a/docs/src/content/docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx +++ b/docs/src/content/docs/en/spikes/tech-spikes-split-verify-of-buildkite.mdx @@ -190,7 +190,7 @@ responses:{ ] }] } -当 pipelineList 为空时, responses 204 +When pipelineList is empty, responses 204 ``` - Exception Handler
hintInfo
400400 Token cannot be empty.
Token's pattern is incorrect.
401 Failed to get BuildKite info_status: 401 UNAUTHORIZED, reason: xxx Token is incorrect
403Failed to call BuildKite, because of insufficient permission!Permission deny
500 hintInfo
400400 Token cannot be empty.
Token's pattern is incorrect.
StartTime cannot be empty.Failed to get BuildKite info_status: 401 UNAUTHORIZED, reason: xxx Token is incorrect
403Failed to call BuildKite, because of insufficient permission!Permission deny
500
From cb961430d5fd92183a8e784ccbe0f2a21facc1fe Mon Sep 17 00:00:00 2001 From: Andrea Date: Tue, 9 Jan 2024 14:50:05 +0800 Subject: [PATCH 12/20] ADM-709:[backend]fix: fix tests --- .../pipeline/BuildKiteControllerTest.java | 45 ++-- .../buildkite/BuildKiteServiceTest.java | 213 +++++++++--------- 2 files changed, 131 insertions(+), 127 deletions(-) diff --git a/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java b/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java index f12bd2ab65..9611b9b3d7 100644 --- a/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java +++ b/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java @@ -41,6 +41,10 @@ @AutoConfigureJsonTesters public class BuildKiteControllerTest { + public static final String TEST_TOKEN = "test_token"; + public static final String TEST_START_TIME = "16737733"; + public static final String TEST_END_TIME = "17657557"; + public static final String BUILD_KITE = "BuildKite"; @MockBean private BuildKiteService buildKiteService; @@ -56,16 +60,18 @@ void shouldReturnCorrectPipelineInfoWhenCallBuildKiteMockServer() throws Excepti BuildKiteResponseDTO buildKiteResponseDTO = BuildKiteResponseDTO.builder().pipelineList(pipelines).build(); when(buildKiteService.fetchPipelineInfo(any())).thenReturn(buildKiteResponseDTO); PipelineParam pipelineParam = PipelineParam.builder() - .token("test_token") - .startTime("16737733") - .endTime("17657557") + .token(TEST_TOKEN) + .startTime(TEST_START_TIME) + .endTime(TEST_END_TIME) .build(); + MockHttpServletResponse response = mockMvc .perform(post("/pipelines/buildKite").contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(pipelineParam))) .andExpect(status().isOk()) .andReturn() .getResponse(); + final var resultId = JsonPath.parse(response.getContentAsString()).read("$.pipelineList[0].id").toString(); assertThat(resultId).contains("payment-selector-ui"); final var resultName = JsonPath.parse(response.getContentAsString()).read("$.pipelineList[0].name").toString(); @@ -99,7 +105,6 @@ void shouldReturnCorrectPipelineStepsWhenCalBuildKiteMockServer() throws Excepti void shouldReturnNoContentIfNoStepsWhenCallBuildKite() throws Exception { List steps = new ArrayList<>(); PipelineStepsDTO emptyPipelineSteps = PipelineStepsDTO.builder().steps(steps).build(); - when(buildKiteService.fetchPipelineSteps(anyString(), anyString(), anyString(), any())) .thenReturn(emptyPipelineSteps); @@ -120,13 +125,13 @@ void shouldReturnNoContentIfNoStepsWhenCallBuildKite() throws Exception { } @Test - void shouldReturnNoContentIfTokenCorrectWhenCallBuildKiteMockServer() throws Exception { + void shouldReturnNoContentWhenCorrectTokenCallBuildKite() throws Exception { ObjectMapper mapper = new ObjectMapper(); - TokenParam tokenParam = TokenParam.builder().token("test_token").build(); - doNothing().when(buildKiteService).getBuildKiteVerify(any()); + TokenParam tokenParam = TokenParam.builder().token(TEST_TOKEN).build(); + doNothing().when(buildKiteService).verifyToken(any()); MockHttpServletResponse response = mockMvc - .perform(post("/pipelines/buildKite/verify").contentType(MediaType.APPLICATION_JSON) + .perform(post("/pipelines/{pipelineType}/verify", BUILD_KITE).contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(tokenParam))) .andExpect(status().isNoContent()) .andReturn() @@ -136,26 +141,26 @@ void shouldReturnNoContentIfTokenCorrectWhenCallBuildKiteMockServer() throws Exc } @Test - void shouldReturnPipelineInfoIfTokenCorrectWhenCallBuildKiteMockServer() throws Exception { + void shouldReturnPipelineInfoWhenCorrectTokenWhenCallBuildKite() throws Exception { ObjectMapper mapper = new ObjectMapper(); List pipelines = mapper.readValue( new File("src/test/java/heartbeat/controller/pipeline/pipelineInfoData.json"), new TypeReference<>() { }); BuildKiteResponseDTO buildKiteResponseDTO = BuildKiteResponseDTO.builder().pipelineList(pipelines).build(); PipelineParam pipelineParam = PipelineParam.builder() - .token("test_token") - .startTime("16737733") - .endTime("17657557") + .token(TEST_TOKEN) + .startTime(TEST_START_TIME) + .endTime(TEST_END_TIME) .build(); - when(buildKiteService.getBuildKiteInfo(any())).thenReturn(buildKiteResponseDTO); MockHttpServletResponse response = mockMvc - .perform(post("/pipelines/buildKite/info").contentType(MediaType.APPLICATION_JSON) + .perform(post("/pipelines/{pipelineType}/info", BUILD_KITE).contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(pipelineParam))) .andExpect(status().isOk()) .andReturn() .getResponse(); + final var resultId = JsonPath.parse(response.getContentAsString()).read("$.pipelineList[0].id").toString(); assertThat(resultId).contains("payment-selector-ui"); final var resultName = JsonPath.parse(response.getContentAsString()).read("$.pipelineList[0].name").toString(); @@ -163,25 +168,25 @@ void shouldReturnPipelineInfoIfTokenCorrectWhenCallBuildKiteMockServer() throws } @Test - void shouldReturnNoContentIfPipelineInfoIsNullWhenCallBuildKiteMockServer() throws Exception { + void shouldReturnNoContentGivenPipelineInfoIsNullWhenCallingBuildKite() throws Exception { ObjectMapper mapper = new ObjectMapper(); BuildKiteResponseDTO buildKiteResponseDTO = BuildKiteResponseDTO.builder() .pipelineList(Collections.emptyList()) .build(); PipelineParam pipelineParam = PipelineParam.builder() - .token("test_token") - .startTime("16737733") - .endTime("17657557") + .token(TEST_TOKEN) + .startTime(TEST_START_TIME) + .endTime(TEST_END_TIME) .build(); - when(buildKiteService.getBuildKiteInfo(any())).thenReturn(buildKiteResponseDTO); MockHttpServletResponse response = mockMvc - .perform(post("/pipelines/buildKite/info").contentType(MediaType.APPLICATION_JSON) + .perform(post("/pipelines/{pipelineType}/info", BUILD_KITE).contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(pipelineParam))) .andExpect(status().isNoContent()) .andReturn() .getResponse(); + assertThat(response.getContentAsString()).isEqualTo(""); } diff --git a/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java b/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java index e502ba46bf..e1154a6f4f 100644 --- a/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java +++ b/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java @@ -4,6 +4,7 @@ import heartbeat.exception.InternalServerErrorException; import heartbeat.exception.ServiceUnavailableException; import heartbeat.exception.UnauthorizedException; + import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.hibernate.validator.internal.util.Contracts.assertNotNull; @@ -79,13 +80,27 @@ class BuildKiteServiceTest { ; rel="next" """; + public static final String MOCK_TOKEN = "mock_token"; + + public static final String TEST_ORG_ID = "test_org_id"; + + public static final String TEST_ORG_NAME = "test_org_name"; + + public static final String TEST_PIPELINE_ID = "test_pipeline_id"; + private static final String PASSED_STATE = "passed"; private static final String FAILED_STATE = "failed"; - private static final String mockStartTime = "1661702400000"; + private static final String MOCK_START_TIME = "1661702400000"; - private static final String mockEndTime = "1662739199000"; + private static final String MOCK_END_TIME = "1662739199000"; + + public static final List PERMISSION_SCOPES = List.of("read_builds", "read_organizations", "read_pipelines"); + + public static final String TEST_JOB_NAME = "testJob"; + + public static final String UNAUTHORIZED_MSG = "unauthorized"; @Mock BuildKiteFeignClient buildKiteFeignClient; @@ -122,17 +137,16 @@ void shouldReturnBuildKiteResponseWhenCallBuildKiteApi() throws IOException { new File("src/test/java/heartbeat/controller/pipeline/buildKitePipelineInfoData.json"), new TypeReference<>() { }); - BuildKiteTokenInfo buildKiteTokenInfo = BuildKiteTokenInfo.builder() - .scopes(List.of("read_builds", "read_organizations", "read_pipelines")) - .build(); + BuildKiteTokenInfo buildKiteTokenInfo = BuildKiteTokenInfo.builder().scopes(PERMISSION_SCOPES).build(); PipelineParam pipelineParam = PipelineParam.builder() - .token("test_token") - .startTime("startTime") - .endTime("endTime") + .token(MOCK_TOKEN) + .startTime(MOCK_START_TIME) + .endTime(MOCK_END_TIME) .build(); when(buildKiteFeignClient.getBuildKiteOrganizationsInfo(any())) - .thenReturn(List.of(BuildKiteOrganizationsInfo.builder().name("XXXX").slug("XXXX").build())); - when(buildKiteFeignClient.getPipelineInfo("Bearer test_token", "XXXX", "1", "100", "startTime", "endTime")) + .thenReturn(List.of(BuildKiteOrganizationsInfo.builder().name(TEST_ORG_NAME).slug(TEST_ORG_ID).build())); + when(buildKiteFeignClient.getPipelineInfo("Bearer mock_token", TEST_ORG_ID, "1", "100", MOCK_START_TIME, + MOCK_END_TIME)) .thenReturn(pipelineDTOS); when(buildKiteFeignClient.getTokenInfo(any())).thenReturn(buildKiteTokenInfo); @@ -142,8 +156,8 @@ void shouldReturnBuildKiteResponseWhenCallBuildKiteApi() throws IOException { Pipeline pipeline = buildKiteResponseDTO.getPipelineList().get(0); assertThat(pipeline.getId()).isEqualTo("payment-selector-ui"); assertThat(pipeline.getName()).isEqualTo("payment-selector-ui"); - assertThat(pipeline.getOrgId()).isEqualTo("XXXX"); - assertThat(pipeline.getOrgName()).isEqualTo("XXXX"); + assertThat(pipeline.getOrgId()).isEqualTo(TEST_ORG_ID); + assertThat(pipeline.getOrgName()).isEqualTo(TEST_ORG_NAME); assertThat(pipeline.getRepository()) .isEqualTo("https://github.com/XXXX-fs/fs-platform-payment-selector-ui.git"); assertThat(pipeline.getSteps().size()).isEqualTo(1); @@ -151,15 +165,13 @@ void shouldReturnBuildKiteResponseWhenCallBuildKiteApi() throws IOException { @Test void shouldThrowRequestFailedExceptionWhenFeignClientCallFailed() { - BuildKiteTokenInfo buildKiteTokenInfo = BuildKiteTokenInfo.builder() - .scopes(List.of("read_builds", "read_organizations", "read_pipelines")) - .build(); + BuildKiteTokenInfo buildKiteTokenInfo = BuildKiteTokenInfo.builder().scopes(PERMISSION_SCOPES).build(); when(buildKiteFeignClient.getBuildKiteOrganizationsInfo(any())) .thenThrow(new CustomFeignClientException(401, "Bad credentials")); when(buildKiteFeignClient.getTokenInfo(any())).thenReturn(buildKiteTokenInfo); assertThatThrownBy(() -> buildKiteService.fetchPipelineInfo( - PipelineParam.builder().token("test_token").startTime("startTime").endTime("endTime").build())) + PipelineParam.builder().token(MOCK_TOKEN).startTime(MOCK_START_TIME).endTime(MOCK_END_TIME).build())) .isInstanceOf(Exception.class) .hasMessageContaining("Bad credentials"); @@ -172,18 +184,15 @@ void shouldThrowNoPermissionExceptionWhenTokenPermissionDeny() { when(buildKiteFeignClient.getTokenInfo(any())).thenReturn(buildKiteTokenInfo); assertThrows(PermissionDenyException.class, () -> buildKiteService.fetchPipelineInfo( - PipelineParam.builder().token("test_token").startTime("startTime").endTime("endTime").build())); + PipelineParam.builder().token(MOCK_TOKEN).startTime(MOCK_START_TIME).endTime(MOCK_END_TIME).build())); } @Test public void shouldReturnResponseWhenFetchPipelineStepsSuccess() { - String token = "test_token"; - String organizationId = "test_org_id"; - String pipelineId = "test_pipeline_id"; PipelineStepsParam stepsParam = new PipelineStepsParam(); - stepsParam.setStartTime(mockStartTime); - stepsParam.setEndTime(mockEndTime); - BuildKiteJob testJob = BuildKiteJob.builder().name("testJob").build(); + stepsParam.setStartTime(MOCK_START_TIME); + stepsParam.setEndTime(MOCK_END_TIME); + BuildKiteJob testJob = BuildKiteJob.builder().name(TEST_JOB_NAME).build(); List buildKiteBuildInfoList = new ArrayList<>(); buildKiteBuildInfoList.add(BuildKiteBuildInfo.builder() .jobs(List.of(testJob)) @@ -195,11 +204,11 @@ public void shouldReturnResponseWhenFetchPipelineStepsSuccess() { anyString(), anyString(), any())) .thenReturn(responseEntity); - PipelineStepsDTO pipelineStepsDTO = buildKiteService.fetchPipelineSteps(token, organizationId, pipelineId, - stepsParam); + PipelineStepsDTO pipelineStepsDTO = buildKiteService.fetchPipelineSteps(MOCK_TOKEN, TEST_ORG_ID, + TEST_PIPELINE_ID, stepsParam); assertNotNull(pipelineStepsDTO); - assertThat(pipelineStepsDTO.getSteps().get(0)).isEqualTo("testJob"); + assertThat(pipelineStepsDTO.getSteps().get(0)).isEqualTo(TEST_JOB_NAME); } @Test @@ -212,22 +221,22 @@ public void shouldThrowRequestFailedExceptionWhenFetchPipelineStepsWithException .thenThrow(mockException); assertThrows(RequestFailedException.class, - () -> buildKiteService.fetchPipelineSteps("test_token", "test_org_id", "test_pipeline_id", - PipelineStepsParam.builder().startTime(mockStartTime).endTime(mockEndTime).build()), + () -> buildKiteService.fetchPipelineSteps(MOCK_TOKEN, TEST_ORG_ID, TEST_PIPELINE_ID, + PipelineStepsParam.builder().startTime(MOCK_START_TIME).endTime(MOCK_END_TIME).build()), "Request failed with status code 500, error: exception"); } @Test public void shouldReturnMoreThanOnePageStepsWhenPageFetchPipelineSteps() { PipelineStepsParam stepsParam = new PipelineStepsParam(); - stepsParam.setStartTime(mockStartTime); - stepsParam.setEndTime(mockEndTime); + stepsParam.setStartTime(MOCK_START_TIME); + stepsParam.setEndTime(MOCK_END_TIME); List linkHeader = new ArrayList<>(); linkHeader.add(TOTAL_PAGE_HEADER); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.addAll(HttpHeaders.LINK, linkHeader); List buildKiteBuildInfoList = new ArrayList<>(); - BuildKiteJob testJob = BuildKiteJob.builder().name("testJob").build(); + BuildKiteJob testJob = BuildKiteJob.builder().name(TEST_JOB_NAME).build(); BuildKiteJob testJob2 = BuildKiteJob.builder().name("testJob2").build(); buildKiteBuildInfoList.add(BuildKiteBuildInfo.builder().jobs(List.of(testJob, testJob2)).build()); ResponseEntity> responseEntity = new ResponseEntity<>(buildKiteBuildInfoList, @@ -243,12 +252,12 @@ public void shouldReturnMoreThanOnePageStepsWhenPageFetchPipelineSteps() { anyString(), anyString(), any())) .thenReturn(buildKiteBuildInfoList2); - PipelineStepsDTO pipelineStepsDTO = buildKiteService.fetchPipelineSteps("test_token", "test_org_id", - "test_pipeline_id", stepsParam); + PipelineStepsDTO pipelineStepsDTO = buildKiteService.fetchPipelineSteps(MOCK_TOKEN, TEST_ORG_ID, + TEST_PIPELINE_ID, stepsParam); assertNotNull(pipelineStepsDTO); assertThat(pipelineStepsDTO.getSteps().size()).isEqualTo(3); - assertThat(pipelineStepsDTO.getSteps().get(0)).isEqualTo("testJob"); + assertThat(pipelineStepsDTO.getSteps().get(0)).isEqualTo(TEST_JOB_NAME); assertThat(pipelineStepsDTO.getSteps().get(1)).isEqualTo("testJob2"); assertThat(pipelineStepsDTO.getSteps().get(2)).isEqualTo("testJob3"); } @@ -260,7 +269,7 @@ public void shouldRThrowServerErrorWhenPageFetchPipelineStepsAndFetchNextPage404 HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.addAll(HttpHeaders.LINK, linkHeader); List buildKiteBuildInfoList = new ArrayList<>(); - BuildKiteJob testJob = BuildKiteJob.builder().name("testJob").build(); + BuildKiteJob testJob = BuildKiteJob.builder().name(TEST_JOB_NAME).build(); buildKiteBuildInfoList.add(BuildKiteBuildInfo.builder().jobs(List.of(testJob)).build()); ResponseEntity> responseEntity = new ResponseEntity<>(buildKiteBuildInfoList, httpHeaders, HttpStatus.OK); @@ -271,8 +280,8 @@ public void shouldRThrowServerErrorWhenPageFetchPipelineStepsAndFetchNextPage404 any(), any(), any())) .thenThrow(new CompletionException(new NotFoundException("Client Error"))); - assertThatThrownBy(() -> buildKiteService.fetchPipelineSteps("test_token", "test_org_id", "test_pipeline_id", - PipelineStepsParam.builder().startTime(mockStartTime).endTime(mockEndTime).build())) + assertThatThrownBy(() -> buildKiteService.fetchPipelineSteps(MOCK_TOKEN, TEST_ORG_ID, TEST_PIPELINE_ID, + PipelineStepsParam.builder().startTime(MOCK_START_TIME).endTime(MOCK_END_TIME).build())) .isInstanceOf(NotFoundException.class) .hasMessageContaining("Client Error"); } @@ -284,7 +293,7 @@ public void shouldRThrowTimeoutExceptionWhenPageFetchPipelineStepsAndFetchNextPa HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.addAll(HttpHeaders.LINK, linkHeader); List buildKiteBuildInfoList = new ArrayList<>(); - BuildKiteJob testJob = BuildKiteJob.builder().name("testJob").build(); + BuildKiteJob testJob = BuildKiteJob.builder().name(TEST_JOB_NAME).build(); buildKiteBuildInfoList.add(BuildKiteBuildInfo.builder().jobs(List.of(testJob)).build()); ResponseEntity> responseEntity = new ResponseEntity<>(buildKiteBuildInfoList, httpHeaders, HttpStatus.OK); @@ -295,8 +304,8 @@ public void shouldRThrowTimeoutExceptionWhenPageFetchPipelineStepsAndFetchNextPa any(), any(), any())) .thenThrow(new CompletionException(new ServiceUnavailableException("Service Unavailable"))); - assertThatThrownBy(() -> buildKiteService.fetchPipelineSteps("test_token", "test_org_id", "test_pipeline_id", - PipelineStepsParam.builder().startTime(mockStartTime).endTime(mockEndTime).build())) + assertThatThrownBy(() -> buildKiteService.fetchPipelineSteps(MOCK_TOKEN, TEST_ORG_ID, TEST_PIPELINE_ID, + PipelineStepsParam.builder().startTime(MOCK_START_TIME).endTime(MOCK_END_TIME).build())) .isInstanceOf(ServiceUnavailableException.class) .hasMessageContaining("Service Unavailable"); } @@ -308,7 +317,7 @@ public void shouldThrowServerErrorWhenPageFetchPipelineStepsAndFetchNextPage5xxE HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.addAll(HttpHeaders.LINK, linkHeader); List buildKiteBuildInfoList = new ArrayList<>(); - BuildKiteJob testJob = BuildKiteJob.builder().name("testJob").build(); + BuildKiteJob testJob = BuildKiteJob.builder().name(TEST_JOB_NAME).build(); buildKiteBuildInfoList.add(BuildKiteBuildInfo.builder().jobs(List.of(testJob)).build()); ResponseEntity> responseEntity = new ResponseEntity<>(buildKiteBuildInfoList, httpHeaders, HttpStatus.OK); @@ -319,8 +328,8 @@ public void shouldThrowServerErrorWhenPageFetchPipelineStepsAndFetchNextPage5xxE any(), any(), any())) .thenThrow(new RequestFailedException(504, "Server Error")); - assertThatThrownBy(() -> buildKiteService.fetchPipelineSteps("test_token", "test_org_id", "test_pipeline_id", - PipelineStepsParam.builder().startTime(mockStartTime).endTime(mockEndTime).build())) + assertThatThrownBy(() -> buildKiteService.fetchPipelineSteps(MOCK_TOKEN, TEST_ORG_ID, TEST_PIPELINE_ID, + PipelineStepsParam.builder().startTime(MOCK_START_TIME).endTime(MOCK_END_TIME).build())) .isInstanceOf(RequestFailedException.class) .hasMessageContaining("Server Error"); } @@ -332,11 +341,10 @@ public void shouldThrowInternalServerErrorExceptionWhenPageFetchPipelineStepsAnd HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.addAll(HttpHeaders.LINK, linkHeader); List buildKiteBuildInfoList = new ArrayList<>(); - BuildKiteJob testJob = BuildKiteJob.builder().name("testJob").build(); + BuildKiteJob testJob = BuildKiteJob.builder().name(TEST_JOB_NAME).build(); buildKiteBuildInfoList.add(BuildKiteBuildInfo.builder().jobs(List.of(testJob)).build()); ResponseEntity> responseEntity = new ResponseEntity<>(buildKiteBuildInfoList, httpHeaders, HttpStatus.OK); - when(buildKiteFeignClient.getPipelineSteps(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), any())) .thenReturn(responseEntity); @@ -344,7 +352,7 @@ public void shouldThrowInternalServerErrorExceptionWhenPageFetchPipelineStepsAnd any(), any(), any())) .thenReturn(buildKiteBuildInfoList); - assertThatThrownBy(() -> buildKiteService.fetchPipelineSteps("test_token", "test_org_id", "test_pipeline_id", + assertThatThrownBy(() -> buildKiteService.fetchPipelineSteps(MOCK_TOKEN, TEST_ORG_ID, TEST_PIPELINE_ID, PipelineStepsParam.builder().build())) .isInstanceOf(InternalServerErrorException.class) .hasMessageContaining("Failed to get pipeline steps"); @@ -354,14 +362,14 @@ public void shouldThrowInternalServerErrorExceptionWhenPageFetchPipelineStepsAnd @Test public void shouldReturnOnePageStepsWhenPageFetchPipelineStepsAndHeaderParseOnePage() { PipelineStepsParam stepsParam = new PipelineStepsParam(); - stepsParam.setStartTime(mockStartTime); - stepsParam.setEndTime(mockEndTime); + stepsParam.setStartTime(MOCK_START_TIME); + stepsParam.setEndTime(MOCK_END_TIME); List linkHeader = new ArrayList<>(); linkHeader.add(NONE_TOTAL_PAGE_HEADER); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.addAll(HttpHeaders.LINK, linkHeader); List buildKiteBuildInfoList = new ArrayList<>(); - BuildKiteJob testJob = BuildKiteJob.builder().name("testJob").build(); + BuildKiteJob testJob = BuildKiteJob.builder().name(TEST_JOB_NAME).build(); buildKiteBuildInfoList.add(BuildKiteBuildInfo.builder().jobs(List.of(testJob)).build()); ResponseEntity> responseEntity = new ResponseEntity<>(buildKiteBuildInfoList, httpHeaders, HttpStatus.OK); @@ -369,19 +377,19 @@ public void shouldReturnOnePageStepsWhenPageFetchPipelineStepsAndHeaderParseOneP anyString(), anyString(), any())) .thenReturn(responseEntity); - PipelineStepsDTO pipelineStepsDTO = buildKiteService.fetchPipelineSteps("test_token", "test_org_id", - "test_pipeline_id", stepsParam); + PipelineStepsDTO pipelineStepsDTO = buildKiteService.fetchPipelineSteps(MOCK_TOKEN, TEST_ORG_ID, + TEST_PIPELINE_ID, stepsParam); assertNotNull(pipelineStepsDTO); assertThat(pipelineStepsDTO.getSteps().size()).isEqualTo(1); - assertThat(pipelineStepsDTO.getSteps().get(0)).isEqualTo("testJob"); + assertThat(pipelineStepsDTO.getSteps().get(0)).isEqualTo(TEST_JOB_NAME); } @Test public void shouldReturnOnePageStepsWhenPageFetchPipelineStep() { PipelineStepsParam stepsParam = new PipelineStepsParam(); - stepsParam.setStartTime(mockStartTime); - stepsParam.setEndTime(mockEndTime); + stepsParam.setStartTime(MOCK_START_TIME); + stepsParam.setEndTime(MOCK_END_TIME); List linkHeader = new ArrayList<>(); linkHeader.add(NONE_TOTAL_PAGE_HEADER); HttpHeaders httpHeaders = new HttpHeaders(); @@ -392,8 +400,8 @@ public void shouldReturnOnePageStepsWhenPageFetchPipelineStep() { anyString(), anyString(), any())) .thenReturn(responseEntity); - PipelineStepsDTO pipelineStepsDTO = buildKiteService.fetchPipelineSteps("test_token", "test_org_id", - "test_pipeline_id", stepsParam); + PipelineStepsDTO pipelineStepsDTO = buildKiteService.fetchPipelineSteps(MOCK_TOKEN, TEST_ORG_ID, + TEST_PIPELINE_ID, stepsParam); assertNotNull(pipelineStepsDTO); assertThat(pipelineStepsDTO.getSteps().size()).isEqualTo(0); @@ -401,7 +409,6 @@ public void shouldReturnOnePageStepsWhenPageFetchPipelineStep() { @Test public void shouldReturnBuildKiteBuildInfoWhenFetchPipelineBuilds() { - String mockToken = "xxxxxxxxxx"; DeploymentEnvironment mockDeployment = DeploymentEnvironmentBuilder.withDefault().build(); List linkHeader = new ArrayList<>(); linkHeader.add(NONE_TOTAL_PAGE_HEADER); @@ -413,8 +420,8 @@ public void shouldReturnBuildKiteBuildInfoWhenFetchPipelineBuilds() { when(buildKiteFeignClient.getPipelineSteps(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), any())) .thenReturn(responseEntity); - List pipelineBuilds = buildKiteService.fetchPipelineBuilds(mockToken, mockDeployment, - mockStartTime, mockEndTime); + List pipelineBuilds = buildKiteService.fetchPipelineBuilds(MOCK_TOKEN, mockDeployment, + MOCK_START_TIME, MOCK_END_TIME); assertNotNull(pipelineBuilds); assertThat(pipelineBuilds.size()).isEqualTo(0); @@ -422,23 +429,20 @@ public void shouldReturnBuildKiteBuildInfoWhenFetchPipelineBuilds() { @Test public void shouldThrowUnauthorizedExceptionWhenFetchPipelineBuilds401Exception() { - String mockToken = "xxxxxxxxxx"; DeploymentEnvironment mockDeployment = DeploymentEnvironmentBuilder.withDefault().build(); when(buildKiteFeignClient.getPipelineSteps(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), any())) - .thenThrow(new UnauthorizedException("unauthorized")); + .thenThrow(new UnauthorizedException(UNAUTHORIZED_MSG)); - Assertions - .assertThatThrownBy( - () -> buildKiteService.fetchPipelineBuilds(mockToken, mockDeployment, mockStartTime, mockEndTime)) + assertThatThrownBy( + () -> buildKiteService.fetchPipelineBuilds(MOCK_TOKEN, mockDeployment, MOCK_START_TIME, MOCK_END_TIME)) .isInstanceOf(UnauthorizedException.class) - .hasMessageContaining("unauthorized"); + .hasMessageContaining(UNAUTHORIZED_MSG); } @Test public void shouldThrowInternalServerErrorExceptionWhenFetchPipelineBuilds500Exception() { - String mockToken = "xxxxxxxxxx"; DeploymentEnvironment mockDeployment = DeploymentEnvironmentBuilder.withDefault().build(); List linkHeader = new ArrayList<>(); linkHeader.add(NONE_TOTAL_PAGE_HEADER); @@ -451,7 +455,7 @@ public void shouldThrowInternalServerErrorExceptionWhenFetchPipelineBuilds500Exc anyString(), anyString(), any())) .thenReturn(responseEntity); - assertThatThrownBy(() -> buildKiteService.fetchPipelineBuilds(mockToken, mockDeployment, null, mockEndTime)) + assertThatThrownBy(() -> buildKiteService.fetchPipelineBuilds(MOCK_TOKEN, mockDeployment, null, MOCK_END_TIME)) .isInstanceOf(InternalServerErrorException.class) .hasMessageContaining("Failed to get pipeline builds_param"); } @@ -469,7 +473,7 @@ public void shouldReturnDeployTimesWhenCountDeployTimes() { DeployTimes expectedDeployTimes = DeployTimesBuilder.withDefault().build(); DeployTimes deployTimes = buildKiteService.countDeployTimes(mockDeployment, mockBuildKiteBuildInfos, - mockStartTime, mockEndTime); + MOCK_START_TIME, MOCK_END_TIME); assertThat(expectedDeployTimes).isEqualTo(deployTimes); } @@ -480,7 +484,7 @@ public void shouldReturnDeployInfoWhenMappedDeployInfoIsNull() { List mockBuildKiteBuildInfos = List.of(BuildKiteBuildInfoBuilder.withDefault().build()); DeployTimes deployTimes = buildKiteService.countDeployTimes(mockDeployment, mockBuildKiteBuildInfos, - mockStartTime, mockEndTime); + MOCK_START_TIME, MOCK_END_TIME); assertThat(0).isEqualTo(deployTimes.getPassed().size()); assertThat(1).isEqualTo(deployTimes.getFailed().size()); @@ -493,7 +497,7 @@ public void shouldThrowErrorWhenCountDeployTimesGivenOrgIdIsNull() { Assertions .assertThatThrownBy(() -> buildKiteService.countDeployTimes(mockDeployment, mockBuildKiteBuildInfos, - mockStartTime, mockEndTime)) + MOCK_START_TIME, MOCK_END_TIME)) .isInstanceOf(NotFoundException.class) .hasMessageContaining("miss orgId argument"); } @@ -534,53 +538,49 @@ public void shouldReturnDeployTimesWhenCountDeployTimesAtFixedTimeIntervals() { .build(); DeployTimes deployTimes = buildKiteService.countDeployTimes(mockDeployment, mockBuildKiteBuildInfos, - mockStartTime, mockEndTime); + MOCK_START_TIME, MOCK_END_TIME); assertThat(expectedDeployTimes).isEqualTo(deployTimes); } @Test void shouldReturnStepBeforeEndStepsGivenStepsArray() { - String targetStep = "Deploy qa"; - List stepArray = Arrays.asList("Test", "Build", targetStep, "Deploy prod"); + List stepArray = Arrays.asList("Test", "Build", "Deploy qa", "Deploy prod"); - List result = buildKiteService.getStepsBeforeEndStep(targetStep, stepArray); + List result = buildKiteService.getStepsBeforeEndStep("Deploy qa", stepArray); assertEquals(3, result.size()); assertEquals("Test", result.get(0)); assertEquals("Build", result.get(1)); - assertEquals(targetStep, result.get(2)); + assertEquals("Deploy qa", result.get(2)); } @Test void shouldReturnTrueWhenTokenIsCorrect() { - BuildKiteTokenInfo buildKiteTokenInfo = BuildKiteTokenInfo.builder() - .scopes(List.of("read_builds", "read_organizations", "read_pipelines")) - .build(); - + BuildKiteTokenInfo buildKiteTokenInfo = BuildKiteTokenInfo.builder().scopes(PERMISSION_SCOPES).build(); when(buildKiteFeignClient.getTokenInfo(any())).thenReturn(buildKiteTokenInfo); - buildKiteService.getBuildKiteVerify("mock_token"); + + buildKiteService.verifyToken(MOCK_TOKEN); verify(buildKiteFeignClient, times(1)).getTokenInfo(anyString()); } @Test - void shouldThrowUnauthorizedExceptionWhenTokenIsIncorrect(){ + void shouldThrowUnauthorizedExceptionWhenTokenIsIncorrect() { when(buildKiteFeignClient.getTokenInfo(any())) - .thenThrow(new UnauthorizedException("unauthorized")); + .thenThrow(new UnauthorizedException(UNAUTHORIZED_MSG)); - Assertions - .assertThatThrownBy( - () -> buildKiteService.getBuildKiteVerify("test_token")) + assertThatThrownBy( + () -> buildKiteService.verifyToken(MOCK_TOKEN)) .isInstanceOf(UnauthorizedException.class) - .hasMessageContaining("unauthorized"); + .hasMessageContaining(UNAUTHORIZED_MSG); } @Test - void shouldThrowInternalServerErrorExceptionWhenGetBuildKiteVerify500Exception(){ + void shouldThrowInternalServerErrorExceptionWhenGetBuildKiteVerify500Exception() { when(buildKiteFeignClient.getTokenInfo(any())).thenReturn(null); - assertThatThrownBy(() -> buildKiteService.getBuildKiteVerify("test_token")) + assertThatThrownBy(() -> buildKiteService.verifyToken(MOCK_TOKEN)) .isInstanceOf(InternalServerErrorException.class) .hasMessageContaining("Failed to call BuildKite, cause is"); } @@ -588,19 +588,19 @@ void shouldThrowInternalServerErrorExceptionWhenGetBuildKiteVerify500Exception() @Test void shouldReturnBuildKiteResponseWhenGetBuildKiteInfo() throws IOException { PipelineParam pipelineParam = PipelineParam.builder() - .token("test_token") - .startTime("startTime") - .endTime("endTime") + .token(MOCK_TOKEN) + .startTime(MOCK_START_TIME) + .endTime(MOCK_END_TIME) .build(); ObjectMapper mapper = new ObjectMapper(); List pipelineDTOS = mapper.readValue( new File("src/test/java/heartbeat/controller/pipeline/buildKitePipelineInfoData.json"), new TypeReference<>() { }); - when(buildKiteFeignClient.getBuildKiteOrganizationsInfo(any())) - .thenReturn(List.of(BuildKiteOrganizationsInfo.builder().name("XXXX").slug("XXXX").build())); - when(buildKiteFeignClient.getPipelineInfo("Bearer test_token", "XXXX", "1", "100", "startTime", "endTime")) + .thenReturn(List.of(BuildKiteOrganizationsInfo.builder().name(TEST_ORG_NAME).slug(TEST_ORG_ID).build())); + when(buildKiteFeignClient.getPipelineInfo("Bearer mock_token", TEST_ORG_ID, "1", "100", MOCK_START_TIME, + MOCK_END_TIME)) .thenReturn(pipelineDTOS); BuildKiteResponseDTO buildKiteResponseDTO = buildKiteService.getBuildKiteInfo(pipelineParam); @@ -609,8 +609,8 @@ void shouldReturnBuildKiteResponseWhenGetBuildKiteInfo() throws IOException { Pipeline pipeline = buildKiteResponseDTO.getPipelineList().get(0); assertThat(pipeline.getId()).isEqualTo("payment-selector-ui"); assertThat(pipeline.getName()).isEqualTo("payment-selector-ui"); - assertThat(pipeline.getOrgId()).isEqualTo("XXXX"); - assertThat(pipeline.getOrgName()).isEqualTo("XXXX"); + assertThat(pipeline.getOrgId()).isEqualTo(TEST_ORG_ID); + assertThat(pipeline.getOrgName()).isEqualTo(TEST_ORG_NAME); assertThat(pipeline.getRepository()) .isEqualTo("https://github.com/XXXX-fs/fs-platform-payment-selector-ui.git"); assertThat(pipeline.getSteps().size()).isEqualTo(1); @@ -619,13 +619,13 @@ void shouldReturnBuildKiteResponseWhenGetBuildKiteInfo() throws IOException { @Test void shouldThrowInternalServerErrorExceptionWhenGetBuildKiteInfo500Exception() { PipelineParam pipelineParam = PipelineParam.builder() - .token("test_token") - .startTime("startTime") - .endTime("endTime") + .token(MOCK_TOKEN) + .startTime(MOCK_START_TIME) + .endTime(MOCK_END_TIME) .build(); - when(buildKiteFeignClient.getBuildKiteOrganizationsInfo(any())).thenReturn(null); - when(buildKiteFeignClient.getPipelineInfo("Bearer test_token", "XXXX", "1", "100", "startTime", "endTime")) + when(buildKiteFeignClient.getPipelineInfo("Bearer mock_token", TEST_ORG_ID, "1", "100", MOCK_START_TIME, + MOCK_END_TIME)) .thenReturn(null); assertThatThrownBy(() -> buildKiteService.getBuildKiteInfo(pipelineParam)) @@ -636,17 +636,16 @@ void shouldThrowInternalServerErrorExceptionWhenGetBuildKiteInfo500Exception() { @Test void shouldThrowUnauthorizedExceptionWhenGetBuildKiteInfoAndTokenIsIncorrect() { PipelineParam pipelineParam = PipelineParam.builder() - .token("test_token") - .startTime("startTime") - .endTime("endTime") + .token(MOCK_TOKEN) + .startTime(MOCK_START_TIME) + .endTime(MOCK_END_TIME) .build(); - when(buildKiteFeignClient.getBuildKiteOrganizationsInfo(any())) - .thenThrow(new UnauthorizedException("unauthorized")); + .thenThrow(new UnauthorizedException(UNAUTHORIZED_MSG)); assertThatThrownBy(() -> buildKiteService.getBuildKiteInfo(pipelineParam)) .isInstanceOf(UnauthorizedException.class) - .hasMessageContaining("unauthorized"); + .hasMessageContaining(UNAUTHORIZED_MSG); } } From cff18490d40680d6a5c2489ca3cb5f16b9b6b075 Mon Sep 17 00:00:00 2001 From: Andrea Date: Tue, 9 Jan 2024 15:11:18 +0800 Subject: [PATCH 13/20] ADM-709:[backend]fix: format --- .../controller/pipeline/BuildKiteControllerTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java b/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java index 9611b9b3d7..518b6cde13 100644 --- a/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java +++ b/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java @@ -42,9 +42,13 @@ public class BuildKiteControllerTest { public static final String TEST_TOKEN = "test_token"; + public static final String TEST_START_TIME = "16737733"; + public static final String TEST_END_TIME = "17657557"; + public static final String BUILD_KITE = "BuildKite"; + @MockBean private BuildKiteService buildKiteService; From 53e4916b06b02789384e28d242bf965cd4482b46 Mon Sep 17 00:00:00 2001 From: Andrea Date: Tue, 9 Jan 2024 15:21:54 +0800 Subject: [PATCH 14/20] ADM-709:[backend]fix: rename PipelineType and add test --- .../pipeline/PipelineController.java | 6 ++--- ...ipelineTypeEnum.java => PipelineType.java} | 6 ++--- .../dto/response/PipelineTypeTest.java | 23 +++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) rename backend/src/main/java/heartbeat/controller/pipeline/dto/request/{PipelineTypeEnum.java => PipelineType.java} (70%) create mode 100644 backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java diff --git a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java index d41fb2c034..7315849ca5 100644 --- a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java +++ b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java @@ -1,6 +1,6 @@ package heartbeat.controller.pipeline; -import heartbeat.controller.pipeline.dto.request.PipelineTypeEnum; +import heartbeat.controller.pipeline.dto.request.PipelineType; import heartbeat.controller.pipeline.dto.request.TokenParam; import heartbeat.controller.pipeline.dto.request.PipelineParam; import heartbeat.controller.pipeline.dto.request.PipelineStepsParam; @@ -38,14 +38,14 @@ public BuildKiteResponseDTO getBuildKiteInfo(@PathVariable String pipelineType, } @PostMapping("/{pipelineType}/verify") - public ResponseEntity verifyBuildKiteToken(@PathVariable PipelineTypeEnum pipelineType, + public ResponseEntity verifyBuildKiteToken(@PathVariable PipelineType pipelineType, @Valid @RequestBody TokenParam tokenParam) { buildKiteService.verifyToken(tokenParam.getToken()); return ResponseEntity.noContent().build(); } @PostMapping("/{pipelineType}/info") - public ResponseEntity fetchBuildKiteInfo(@PathVariable PipelineTypeEnum pipelineType, + public ResponseEntity fetchBuildKiteInfo(@PathVariable PipelineType pipelineType, @Valid @RequestBody PipelineParam pipelineParam) { BuildKiteResponseDTO buildKiteResponse = buildKiteService.getBuildKiteInfo(pipelineParam); diff --git a/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineTypeEnum.java b/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java similarity index 70% rename from backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineTypeEnum.java rename to backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java index 4db36d6dd3..afb71225f9 100644 --- a/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineTypeEnum.java +++ b/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java @@ -1,16 +1,16 @@ package heartbeat.controller.pipeline.dto.request; -public enum PipelineTypeEnum { +public enum PipelineType { BuildKite("BuildKite"); public final String pipelineType; - PipelineTypeEnum(String pipelineType) { + PipelineType(String pipelineType) { this.pipelineType = pipelineType; } - public static PipelineTypeEnum fromValue(String type) { + public static PipelineType fromValue(String type) { return switch (type) { case "BuildKite" -> BuildKite; default -> throw new IllegalArgumentException("Pipeline type does not find!"); diff --git a/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java b/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java new file mode 100644 index 0000000000..2321ec4ea0 --- /dev/null +++ b/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java @@ -0,0 +1,23 @@ +package heartbeat.controller.pipeline.dto.response; + +import heartbeat.controller.pipeline.dto.request.PipelineType; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class PipelineTypeTest { + + @Test + public void shouldConvertValueToType() { + PipelineType buildKiteType = PipelineType.fromValue("BuildKite"); + assertEquals(buildKiteType, PipelineType.BuildKite); + } + + @Test + public void shouldThrowExceptionWhenDateTypeNotSupported() { + assertThatThrownBy(() -> PipelineType.fromValue("unknown")).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Pipeline type does not find!"); + } + +} From ebc3e8158ce4b72a02942b547ea90d1ae4dcf893 Mon Sep 17 00:00:00 2001 From: Andrea Date: Tue, 9 Jan 2024 16:28:12 +0800 Subject: [PATCH 15/20] ADM-709:[backend]fix: fix test --- .../controller/pipeline/dto/response/PipelineTypeTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java b/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java index 2321ec4ea0..f51895783c 100644 --- a/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java +++ b/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java @@ -11,6 +11,7 @@ public class PipelineTypeTest { @Test public void shouldConvertValueToType() { PipelineType buildKiteType = PipelineType.fromValue("BuildKite"); + assertEquals(buildKiteType, PipelineType.BuildKite); } From f81b9ea90a0664bc13c06a907a26bc1802de58dc Mon Sep 17 00:00:00 2001 From: Andrea Date: Tue, 9 Jan 2024 16:30:36 +0800 Subject: [PATCH 16/20] ADM-709:[backend]fix: fix test --- .../heartbeat/controller/pipeline/BuildKiteControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java b/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java index 518b6cde13..d102f556ba 100644 --- a/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java +++ b/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java @@ -145,7 +145,7 @@ void shouldReturnNoContentWhenCorrectTokenCallBuildKite() throws Exception { } @Test - void shouldReturnPipelineInfoWhenCorrectTokenWhenCallBuildKite() throws Exception { + void shouldReturnPipelineInfoWhenCorrectTokenCallBuildKite() throws Exception { ObjectMapper mapper = new ObjectMapper(); List pipelines = mapper.readValue( new File("src/test/java/heartbeat/controller/pipeline/pipelineInfoData.json"), new TypeReference<>() { From 36802714f0c33354504024e71344b4bcab9fca2e Mon Sep 17 00:00:00 2001 From: Andrea Date: Wed, 10 Jan 2024 17:41:43 +0800 Subject: [PATCH 17/20] ADM-709:[backend]fix: fix buildKite type --- .../heartbeat/controller/pipeline/PipelineController.java | 4 ++-- .../controller/pipeline/dto/request/PipelineType.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java index 7315849ca5..5465f66cd6 100644 --- a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java +++ b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java @@ -38,14 +38,14 @@ public BuildKiteResponseDTO getBuildKiteInfo(@PathVariable String pipelineType, } @PostMapping("/{pipelineType}/verify") - public ResponseEntity verifyBuildKiteToken(@PathVariable PipelineType pipelineType, + public ResponseEntity verifyBuildKiteToken(@PathVariable @NotBlank PipelineType pipelineType, @Valid @RequestBody TokenParam tokenParam) { buildKiteService.verifyToken(tokenParam.getToken()); return ResponseEntity.noContent().build(); } @PostMapping("/{pipelineType}/info") - public ResponseEntity fetchBuildKiteInfo(@PathVariable PipelineType pipelineType, + public ResponseEntity fetchBuildKiteInfo(@PathVariable @NotBlank PipelineType pipelineType, @Valid @RequestBody PipelineParam pipelineParam) { BuildKiteResponseDTO buildKiteResponse = buildKiteService.getBuildKiteInfo(pipelineParam); diff --git a/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java b/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java index afb71225f9..4c6aaf41c0 100644 --- a/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java +++ b/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java @@ -2,7 +2,7 @@ public enum PipelineType { - BuildKite("BuildKite"); + BuildKite("buildKite"); public final String pipelineType; @@ -12,7 +12,7 @@ public enum PipelineType { public static PipelineType fromValue(String type) { return switch (type) { - case "BuildKite" -> BuildKite; + case "buildKite" -> BuildKite; default -> throw new IllegalArgumentException("Pipeline type does not find!"); }; } From 354de5381d8250f028cc255432f187dcf2964126 Mon Sep 17 00:00:00 2001 From: Andrea Date: Thu, 11 Jan 2024 09:58:21 +0800 Subject: [PATCH 18/20] ADM-709:[backend]fix: fix test --- .../controller/pipeline/BuildKiteControllerTest.java | 1 + .../service/pipeline/buildkite/BuildKiteServiceTest.java | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java b/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java index d102f556ba..28c66eb930 100644 --- a/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java +++ b/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java @@ -101,6 +101,7 @@ void shouldReturnCorrectPipelineStepsWhenCalBuildKiteMockServer() throws Excepti .andExpect(status().isOk()) .andReturn() .getResponse(); + val resultStep = JsonPath.parse(response.getContentAsString()).read("$.steps[0]"); assertThat(resultStep).isEqualTo(":docker: publish image to cloudsmith"); } diff --git a/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java b/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java index e1154a6f4f..43f3214d93 100644 --- a/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java +++ b/backend/src/test/java/heartbeat/service/pipeline/buildkite/BuildKiteServiceTest.java @@ -416,10 +416,10 @@ public void shouldReturnBuildKiteBuildInfoWhenFetchPipelineBuilds() { httpHeaders.addAll(HttpHeaders.LINK, linkHeader); ResponseEntity> responseEntity = new ResponseEntity<>(null, httpHeaders, HttpStatus.OK); - when(buildKiteFeignClient.getPipelineSteps(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), any())) .thenReturn(responseEntity); + List pipelineBuilds = buildKiteService.fetchPipelineBuilds(MOCK_TOKEN, mockDeployment, MOCK_START_TIME, MOCK_END_TIME); @@ -430,7 +430,6 @@ public void shouldReturnBuildKiteBuildInfoWhenFetchPipelineBuilds() { @Test public void shouldThrowUnauthorizedExceptionWhenFetchPipelineBuilds401Exception() { DeploymentEnvironment mockDeployment = DeploymentEnvironmentBuilder.withDefault().build(); - when(buildKiteFeignClient.getPipelineSteps(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), any())) .thenThrow(new UnauthorizedException(UNAUTHORIZED_MSG)); @@ -450,7 +449,6 @@ public void shouldThrowInternalServerErrorExceptionWhenFetchPipelineBuilds500Exc httpHeaders.addAll(HttpHeaders.LINK, linkHeader); ResponseEntity> responseEntity = new ResponseEntity<>(null, httpHeaders, HttpStatus.OK); - when(buildKiteFeignClient.getPipelineSteps(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), any())) .thenReturn(responseEntity); From 29695c7b36af7f3239d372b700cdb66568ece960 Mon Sep 17 00:00:00 2001 From: Andrea Date: Thu, 11 Jan 2024 10:35:06 +0800 Subject: [PATCH 19/20] ADM-709:[backend]fix: fix buildkite type --- .../heartbeat/controller/pipeline/PipelineController.java | 8 +++++--- .../controller/pipeline/dto/request/PipelineType.java | 4 ++-- .../service/pipeline/buildkite/BuildKiteService.java | 1 + .../controller/pipeline/BuildKiteControllerTest.java | 2 +- .../pipeline/dto/response/PipelineTypeTest.java | 2 +- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java index 5465f66cd6..af80edf7a0 100644 --- a/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java +++ b/backend/src/main/java/heartbeat/controller/pipeline/PipelineController.java @@ -31,6 +31,7 @@ public class PipelineController { private final BuildKiteService buildKiteService; + @Deprecated @PostMapping("/{pipelineType}") public BuildKiteResponseDTO getBuildKiteInfo(@PathVariable String pipelineType, @Valid @RequestBody PipelineParam pipelineParam) { @@ -38,17 +39,18 @@ public BuildKiteResponseDTO getBuildKiteInfo(@PathVariable String pipelineType, } @PostMapping("/{pipelineType}/verify") - public ResponseEntity verifyBuildKiteToken(@PathVariable @NotBlank PipelineType pipelineType, + public ResponseEntity verifyBuildKiteToken(@PathVariable @NotBlank String pipelineType, @Valid @RequestBody TokenParam tokenParam) { + PipelineType.fromValue(pipelineType); buildKiteService.verifyToken(tokenParam.getToken()); return ResponseEntity.noContent().build(); } @PostMapping("/{pipelineType}/info") - public ResponseEntity fetchBuildKiteInfo(@PathVariable @NotBlank PipelineType pipelineType, + public ResponseEntity fetchBuildKiteInfo(@PathVariable @NotBlank String pipelineType, @Valid @RequestBody PipelineParam pipelineParam) { + PipelineType.fromValue(pipelineType); BuildKiteResponseDTO buildKiteResponse = buildKiteService.getBuildKiteInfo(pipelineParam); - if (buildKiteResponse.getPipelineList().isEmpty()) { return ResponseEntity.noContent().build(); } diff --git a/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java b/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java index 4c6aaf41c0..41a8a95abe 100644 --- a/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java +++ b/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java @@ -2,7 +2,7 @@ public enum PipelineType { - BuildKite("buildKite"); + BuildKite("buildkite"); public final String pipelineType; @@ -12,7 +12,7 @@ public enum PipelineType { public static PipelineType fromValue(String type) { return switch (type) { - case "buildKite" -> BuildKite; + case "buildkite" -> BuildKite; default -> throw new IllegalArgumentException("Pipeline type does not find!"); }; } diff --git a/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java b/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java index ecff106684..08799f70f2 100644 --- a/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java +++ b/backend/src/main/java/heartbeat/service/pipeline/buildkite/BuildKiteService.java @@ -57,6 +57,7 @@ public void shutdownExecutor() { customTaskExecutor.shutdown(); } + @Deprecated public BuildKiteResponseDTO fetchPipelineInfo(PipelineParam pipelineParam) { try { String buildKiteToken = "Bearer " + pipelineParam.getToken(); diff --git a/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java b/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java index 28c66eb930..bdefefdf47 100644 --- a/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java +++ b/backend/src/test/java/heartbeat/controller/pipeline/BuildKiteControllerTest.java @@ -47,7 +47,7 @@ public class BuildKiteControllerTest { public static final String TEST_END_TIME = "17657557"; - public static final String BUILD_KITE = "BuildKite"; + public static final String BUILD_KITE = "buildkite"; @MockBean private BuildKiteService buildKiteService; diff --git a/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java b/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java index f51895783c..e9c1f3bea5 100644 --- a/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java +++ b/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java @@ -10,7 +10,7 @@ public class PipelineTypeTest { @Test public void shouldConvertValueToType() { - PipelineType buildKiteType = PipelineType.fromValue("BuildKite"); + PipelineType buildKiteType = PipelineType.fromValue("buildkite"); assertEquals(buildKiteType, PipelineType.BuildKite); } From c1126fcff19e0a4ab4914d506b0a525436f474d5 Mon Sep 17 00:00:00 2001 From: Andrea Date: Thu, 11 Jan 2024 11:16:18 +0800 Subject: [PATCH 20/20] ADM-709:[backend]fix: fix buildkite type --- .../pipeline/dto/request/PipelineType.java | 12 +++++++++--- .../pipeline/dto/response/PipelineTypeTest.java | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java b/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java index 41a8a95abe..ba42169952 100644 --- a/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java +++ b/backend/src/main/java/heartbeat/controller/pipeline/dto/request/PipelineType.java @@ -1,8 +1,11 @@ package heartbeat.controller.pipeline.dto.request; +import lombok.extern.log4j.Log4j2; + +@Log4j2 public enum PipelineType { - BuildKite("buildkite"); + BUILDKITE("buildkite"); public final String pipelineType; @@ -12,8 +15,11 @@ public enum PipelineType { public static PipelineType fromValue(String type) { return switch (type) { - case "buildkite" -> BuildKite; - default -> throw new IllegalArgumentException("Pipeline type does not find!"); + case "buildkite" -> BUILDKITE; + default -> { + log.error("Failed to match Pipeline type: {} ", type); + throw new IllegalArgumentException("Pipeline type does not find!"); + } }; } diff --git a/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java b/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java index e9c1f3bea5..66e1a5957f 100644 --- a/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java +++ b/backend/src/test/java/heartbeat/controller/pipeline/dto/response/PipelineTypeTest.java @@ -12,7 +12,7 @@ public class PipelineTypeTest { public void shouldConvertValueToType() { PipelineType buildKiteType = PipelineType.fromValue("buildkite"); - assertEquals(buildKiteType, PipelineType.BuildKite); + assertEquals(buildKiteType, PipelineType.BUILDKITE); } @Test