Skip to content

Commit

Permalink
ADM-708:[backend]fix: board verify throw base exception (#947)
Browse files Browse the repository at this point in the history
* ADM-708:[backend]fix: check start and end time style

* ADM-708:[backend]fix: board verify throw base exception

* ADM-708:[backend]refactor: update site and board id exception message

* ADM-708: refactor [backend][docs]

* ADM-708: refactor [backend][docs]

---------

Co-authored-by: guzhongren <guzhongren@live.cn>
  • Loading branch information
weiraneve and guzhongren authored Jan 16, 2024
1 parent 8109623 commit 5fe0abb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ public BoardConfigDTO getInfo(@PathVariable @NotBlank BoardType boardType,
return jiraService.getInfo(boardType, boardRequestParam);
}

private void checkTime(String startTimeString, String endTimeString) {
long startTime = Long.parseLong(startTimeString);
long endTime = Long.parseLong(endTimeString);
if (startTime >= endTime) {
throw new BadRequestException("Time inputs wrong.");
private void checkTime(String startTime, String endTime) {
String errorMessage = "The startTime should be before the endTime.";
try {
long start = Long.parseLong(startTime);
long end = Long.parseLong(endTime);
if (start >= end) {
throw new BadRequestException(errorMessage);
}
}
catch (Exception e) {
throw new BadRequestException(errorMessage);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package heartbeat.controller.board.dto.request;

import heartbeat.exception.BadRequestException;

public enum BoardType {

JIRA("jira"), CLASSIC_JIRA("classic-jira");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,15 @@ public String verify(BoardType boardType, BoardVerifyRequestParam boardVerifyReq
}
catch (NotFoundException e) {
log.error("Failed to call Jira to verify board url, url: {}", baseUrl);
throw new NotFoundException("boardId not found");
throw new NotFoundException("boardId is incorrect");
}
catch (RuntimeException e) {
Throwable cause = Optional.ofNullable(e.getCause()).orElse(e);
log.error("Failed to call Jira to verify board, board id: {}, e: {}", boardVerifyRequestParam.getBoardId(),
cause.getMessage());
if (cause instanceof BaseException baseException) {
throw baseException;
}
throw new InternalServerErrorException(
String.format("Failed to call Jira to verify board, cause is %s", cause.getMessage()));
}
Expand Down Expand Up @@ -217,7 +220,7 @@ private void checkSite(URI baseUrl) {
}
catch (NotFoundException e) {
log.error("Failed to call Jira to verify board url, url: {}", baseUrl);
throw new NotFoundException("site not found");
throw new NotFoundException("site is incorrect");
}
}

Expand Down
23 changes: 19 additions & 4 deletions backend/src/test/java/heartbeat/service/jira/JiraServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import heartbeat.controller.report.dto.request.JiraBoardSetting;
import heartbeat.enums.AssigneeFilterMethod;
import heartbeat.exception.BadRequestException;
import heartbeat.exception.BaseException;
import heartbeat.exception.CustomFeignClientException;
import heartbeat.exception.InternalServerErrorException;
import heartbeat.exception.NoContentException;
import heartbeat.exception.NotFoundException;
import heartbeat.exception.PermissionDenyException;
import heartbeat.exception.UnauthorizedException;
import heartbeat.service.board.jira.JiraService;
import heartbeat.util.BoardUtil;
import heartbeat.util.SystemUtil;
Expand Down Expand Up @@ -480,10 +482,23 @@ void shouldCallJiraFeignClientSiteAndThrowNotFoundWhenVerifyJiraBoard() {
BoardVerifyRequestParam boardVerifyRequestParam = BOARD_VERIFY_REQUEST_BUILDER().build();

when(urlGenerator.getUri(any())).thenReturn(URI.create(SITE_ATLASSIAN_NET));
doThrow(new NotFoundException("site not found")).when(jiraFeignClient).getSite(baseUrl);
doThrow(new NotFoundException("site is incorrect")).when(jiraFeignClient).getSite(baseUrl);

Throwable thrown = catchThrowable(() -> jiraService.verify(boardTypeJira, boardVerifyRequestParam));
assertThat(thrown).isInstanceOf(RuntimeException.class).hasMessageContaining("site not found");
assertThat(thrown).isInstanceOf(RuntimeException.class).hasMessageContaining("site is incorrect");
}

@Test
void shouldCallJiraFeignClientAndThrowBaseExceptionWhenVerifyJiraBoard() {
URI baseUrl = URI.create(SITE_ATLASSIAN_NET);
BoardVerifyRequestParam boardVerifyRequestParam = BOARD_VERIFY_REQUEST_BUILDER().build();

when(urlGenerator.getUri(any())).thenReturn(URI.create(SITE_ATLASSIAN_NET));
doThrow(new UnauthorizedException("")).when(jiraFeignClient)
.getBoard(baseUrl, boardVerifyRequestParam.getBoardId(), boardVerifyRequestParam.getToken());

Throwable thrown = catchThrowable(() -> jiraService.verify(boardTypeJira, boardVerifyRequestParam));
assertThat(thrown).isInstanceOf(BaseException.class);
}

@Test
Expand All @@ -493,10 +508,10 @@ void shouldCallJiraFeignClientBoardAndThrowNotFoundWhenVerifyJiraBoard() {
BoardVerifyRequestParam boardVerifyRequestParam = BOARD_VERIFY_REQUEST_BUILDER().build();

when(urlGenerator.getUri(any())).thenReturn(URI.create(SITE_ATLASSIAN_NET));
doThrow(new NotFoundException("boardId not found")).when(jiraFeignClient).getBoard(baseUrl, BOARD_ID, token);
doThrow(new NotFoundException("boardId is incorrect")).when(jiraFeignClient).getBoard(baseUrl, BOARD_ID, token);

Throwable thrown = catchThrowable(() -> jiraService.verify(boardTypeJira, boardVerifyRequestParam));
assertThat(thrown).isInstanceOf(RuntimeException.class).hasMessageContaining("boardId not found");
assertThat(thrown).isInstanceOf(RuntimeException.class).hasMessageContaining("boardId is incorrect");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Example

| status | message | hintInfo |
|--------|---------------------------------------------------|------------------------|
| 400 | Time inputs wrong. | Please reconfirm the input |
| 400 | The startTime should be before the endTime. | Please reconfirm the input |
| 400 | Token cannot be empty. | Please reconfirm the input |
| 400 | Token's pattern is incorrect. | Please reconfirm the input |
| 400 | boardType param is not correct | Please reconfirm the input |
Expand Down

0 comments on commit 5fe0abb

Please sign in to comment.