Skip to content

Commit

Permalink
Add test (#1147)[backend]
Browse files Browse the repository at this point in the history
* pref(sonar): refactor backend test case

* pref(sonar): move slack notification after e2e job step

* fix(infra): given specific branch name for github OIDC
  • Loading branch information
guzhongren authored and WSSsssss33 committed Mar 18, 2024
1 parent 2f93fca commit bdd4cea
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 145 deletions.
24 changes: 8 additions & 16 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,14 @@ jobs:
name: playwright-report
path: frontend/e2e/reports/
retention-days: 30
- name: Slack Notification
uses: rtCamp/action-slack-notify@v2
if: always()
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_ICON_EMOJI: ":heart-beat:"
SLACK_COLOR: ${{ job.status }}
SLACK_USERNAME: "Heartbeat E2E Status"

deploy:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -517,19 +525,3 @@ jobs:
# docker pull ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_HOST }}/heartbeat_backend:$IMAGE_TAG
# docker pull ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_HOST }}/heartbeat_frontend:$IMAGE_TAG
# docker-compose up -d frontend

slack-notification:
name: Slack Notification
if: github.ref == 'refs/heads/main' && always()
needs:
- e2e
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Slack Notification
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_ICON_EMOJI: ":heart-beat:"
SLACK_COLOR: ${{ job.status }}
SLACK_USERNAME: "Heartbeat E2E Status"
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;

Expand All @@ -28,7 +30,7 @@ void setup() {
}

@Test
void testDecode_UnauthorizedException() {
void testDecodeUnauthorizedException() {
int statusCode = HttpStatus.UNAUTHORIZED.value();
Response response = responseMock.getMockResponse(statusCode);

Expand All @@ -38,7 +40,7 @@ void testDecode_UnauthorizedException() {
}

@Test
void testDecode_NotFoundException() {
void testDecodeNotFoundException() {
int statusCode = HttpStatus.NOT_FOUND.value();

Exception exception = decoder.decode("methodKey", responseMock.getMockResponse(statusCode));
Expand All @@ -47,7 +49,7 @@ void testDecode_NotFoundException() {
}

@Test
void testDecode_HBTimeoutException() {
void testDecodeTimeoutException() {
int statusCode = HttpStatus.SERVICE_UNAVAILABLE.value();

Exception exception = decoder.decode("methodKey", responseMock.getMockResponse(statusCode));
Expand All @@ -56,7 +58,7 @@ void testDecode_HBTimeoutException() {
}

@Test
void testDecode_4xxRequestFailedException() {
void testDecode4xxRequestFailedException() {
int statusCode = HttpStatus.METHOD_NOT_ALLOWED.value();

Exception exception = decoder.decode("methodKey", responseMock.getMockResponse(statusCode));
Expand All @@ -66,7 +68,7 @@ void testDecode_4xxRequestFailedException() {
}

@Test
void testDecode_5xxRequestFailedException() {
void testDecode5xxRequestFailedException() {
int statusCode = HttpStatus.BAD_GATEWAY.value();

Exception exception = decoder.decode("methodKey", responseMock.getMockResponse(statusCode));
Expand All @@ -76,7 +78,7 @@ void testDecode_5xxRequestFailedException() {
}

@Test
void testDecode_UnKnownException() {
void testDecodeUnKnownException() {
int statusCode = HttpStatus.SEE_OTHER.value();

Exception exception = decoder.decode("methodKey", responseMock.getMockResponse(statusCode));
Expand All @@ -85,49 +87,19 @@ void testDecode_UnKnownException() {
assertTrue(exception.getMessage().contains("UnKnown Error"));
}

@Test
void shouldDecodeExceptionErrorMessageWhenCallGetTokenInfo() {
int statusCode = HttpStatus.NOT_FOUND.value();

Exception exception = decoder.decode("getTokenInfo", responseMock.getMockResponse(statusCode));

assertEquals("Failed to get token info", exception.getMessage());
}

@Test
void shouldDecodeExceptionErrorMessageWhenCallGetBuildKiteOrganizationsInfo() {
int statusCode = HttpStatus.NOT_FOUND.value();

Exception exception = decoder.decode("getBuildKiteOrganizationsInfo", responseMock.getMockResponse(statusCode));

assertEquals("Failed to get BuildKite OrganizationsInfo info", exception.getMessage());
}

@Test
void shouldDecodeExceptionErrorMessageWhenCallGetPipelineInfo() {
int statusCode = HttpStatus.NOT_FOUND.value();

Exception exception = decoder.decode("getPipelineInfo", responseMock.getMockResponse(statusCode));

assertEquals("Failed to get pipeline info", exception.getMessage());
}

@Test
void shouldDecodeExceptionErrorMessageWhenCallGetPipelineSteps() {
int statusCode = HttpStatus.NOT_FOUND.value();

Exception exception = decoder.decode("getPipelineSteps", responseMock.getMockResponse(statusCode));
@ParameterizedTest
@CsvSource({ "getTokenInfo,Failed to get token info",
"getBuildKiteOrganizationsInfo,Failed to get BuildKite OrganizationsInfo info",
"getPipelineInfo,Failed to get pipeline info", "getPipelineSteps,Failed to get pipeline steps",
"getPipelineStepsInfo,Failed to get pipeline steps info"

assertEquals("Failed to get pipeline steps", exception.getMessage());
}

@Test
void shouldDecodeExceptionErrorMessageWhenCallGetPipelineStepsInfo() {
})
void shouldDecodeExceptionErrorMessage(String methodKey, String expectedMsg) {
int statusCode = HttpStatus.NOT_FOUND.value();

Exception exception = decoder.decode("getPipelineStepsInfo", responseMock.getMockResponse(statusCode));
Exception exception = decoder.decode(methodKey, responseMock.getMockResponse(statusCode));

assertEquals("Failed to get pipeline steps info", exception.getMessage());
assertEquals(expectedMsg, exception.getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;

Expand All @@ -29,7 +31,7 @@ void setup() {
}

@Test
void testDecode_UnauthorizedException() {
void testDecodeUnauthorizedException() {
int statusCode = HttpStatus.UNAUTHORIZED.value();
Response response = responseMock.getMockResponse(statusCode);

Expand All @@ -39,7 +41,7 @@ void testDecode_UnauthorizedException() {
}

@Test
void testDecode_NotFoundException() {
void testDecodeNotFoundException() {
int statusCode = HttpStatus.NOT_FOUND.value();

Exception exception = decoder.decode("methodKey", responseMock.getMockResponse(statusCode));
Expand All @@ -48,7 +50,7 @@ void testDecode_NotFoundException() {
}

@Test
void testDecode_4xxRequestFailedException() {
void testDecode4xxRequestFailedException() {
int statusCode = HttpStatus.METHOD_NOT_ALLOWED.value();

Exception exception = decoder.decode("methodKey", responseMock.getMockResponse(statusCode));
Expand All @@ -58,7 +60,7 @@ void testDecode_4xxRequestFailedException() {
}

@Test
void testDecode_5xxRequestFailedException() {
void testDecode5xxRequestFailedException() {
int statusCode = HttpStatus.BAD_GATEWAY.value();

Exception exception = decoder.decode("methodKey", responseMock.getMockResponse(statusCode));
Expand All @@ -68,7 +70,7 @@ void testDecode_5xxRequestFailedException() {
}

@Test
void testDecode_UnKnownException() {
void testDecodeUnKnownException() {
int statusCode = HttpStatus.SEE_OTHER.value();

Exception exception = decoder.decode("methodKey", responseMock.getMockResponse(statusCode));
Expand All @@ -78,7 +80,7 @@ void testDecode_UnKnownException() {
}

@Test
void testDecode_HBTimeoutException() {
void testDecodeTimeoutException() {
int statusCode = HttpStatus.SERVICE_UNAVAILABLE.value();

Exception exception = decoder.decode("methodKey", responseMock.getMockResponse(statusCode));
Expand All @@ -87,57 +89,28 @@ void testDecode_HBTimeoutException() {
}

@Test
void testDecode_PermissionDenyException() {
void testDecodePermissionDenyException() {
int statusCode = HttpStatus.FORBIDDEN.value();

Exception exception = decoder.decode("methodKey", responseMock.getMockResponse(statusCode));

assertEquals(PermissionDenyException.class, exception.getClass());
}

@Test
void shouldDecodeExceptionErrorMessageWhenCallVerifyToken() {
int statusCode = HttpStatus.NOT_FOUND.value();

Exception exception = decoder.decode("verifyToken", responseMock.getMockResponse(statusCode));

assertEquals("Failed to verify token", exception.getMessage());
}

@Test
void shouldDecodeExceptionErrorMessageWhenCallVerifyCanReadTargetBranch() {
int statusCode = HttpStatus.NOT_FOUND.value();

Exception exception = decoder.decode("verifyCanReadTargetBranch", responseMock.getMockResponse(statusCode));

assertEquals("Failed to verify canRead target branch", exception.getMessage());
}

@Test
void shouldDecodeExceptionErrorMessageWhenCallGetCommitInfo() {
int statusCode = HttpStatus.NOT_FOUND.value();

Exception exception = decoder.decode("getCommitInfo", responseMock.getMockResponse(statusCode));

assertEquals("Failed to get commit info", exception.getMessage());
}

@Test
void shouldDecodeExceptionErrorMessageWhenCallGetPullRequestCommitInfo() {
int statusCode = HttpStatus.NOT_FOUND.value();

Exception exception = decoder.decode("getPullRequestCommitInfo", responseMock.getMockResponse(statusCode));
@ParameterizedTest
@CsvSource({ "verifyToken,Failed to verify token",
"verifyCanReadTargetBranch,Failed to verify canRead target branch",
"getCommitInfo,Failed to get commit info",
"getPullRequestCommitInfo,Failed to get pull request commit info",
"getPullRequestListInfo,Failed to get pull request list info"

assertEquals("Failed to get pull request commit info", exception.getMessage());
}

@Test
void shouldDecodeExceptionErrorMessageWhenCallGetPullRequestListInfo() {
})
void shouldDecodeExceptionErrorMessage(String methodKey, String expectMsg) {
int statusCode = HttpStatus.NOT_FOUND.value();

Exception exception = decoder.decode("getPullRequestListInfo", responseMock.getMockResponse(statusCode));
Exception exception = decoder.decode(methodKey, responseMock.getMockResponse(statusCode));

assertEquals("Failed to get pull request list info", exception.getMessage());
assertEquals(expectMsg, exception.getMessage());
}

}
Loading

0 comments on commit bdd4cea

Please sign in to comment.