Skip to content

Commit

Permalink
Fix sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkhanh-axonivy committed Dec 16, 2024
1 parent 2e834b1 commit f0f0caf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ class GitHubServiceImplTest {
@Test
void testGetGitHub_WithValidToken() throws IOException {
when(gitHubProperty.getToken()).thenReturn("validToken");
GitHub gitHub = gitHubService.getGitHub();
assertNotNull(gitHub);
assertNotNull(gitHubService.getGitHub());
verify(gitHubProperty).getToken();
}

@Test
void testGetGitHub_WithNullToken() throws IOException {
when(gitHubProperty.getToken()).thenReturn(null);
GitHub gitHub = gitHubService.getGitHub();
assertNotNull(gitHub);
assertNotNull(gitHubService.getGitHub());
}

@Test
Expand Down Expand Up @@ -189,7 +187,7 @@ void testGetAccessToken_NullGitHubProperty() {
}

@Test
void testGetAccessToken_GitHubErrorResponse() throws Oauth2ExchangeCodeException, MissingHeaderException {
void testGetAccessToken_GitHubErrorResponse() throws Oauth2ExchangeCodeException {
String code = "validCode";
String clientId = "clientId";
String clientSecret = "clientSecret";
Expand Down Expand Up @@ -217,7 +215,7 @@ void testGetAccessToken_GitHubErrorResponse() throws Oauth2ExchangeCodeException
}

@Test
void testGetAccessToken_SuccessfulResponseWithError() throws Oauth2ExchangeCodeException, MissingHeaderException {
void testGetAccessToken_SuccessfulResponseWithError() throws Oauth2ExchangeCodeException {
String code = "validCode";
String clientId = "clientId";
String clientSecret = "clientSecret";
Expand Down Expand Up @@ -248,7 +246,6 @@ void testValidateUserInOrganizationAndTeam_Valid() throws UnauthorizedException,
String organization = "testOrg";
String team = "devTeam";

GitHub gitHub = mock(GitHub.class);
when(gitHubService.getGitHub(accessToken)).thenReturn(gitHub);

when(gitHubService.isUserInOrganizationAndTeam(gitHub, organization, team)).thenReturn(true);
Expand All @@ -258,11 +255,11 @@ void testValidateUserInOrganizationAndTeam_Valid() throws UnauthorizedException,

@Test
void testIsUserInOrganizationAndTeam_NullGitHub() throws IOException {
GitHub gitHub = null;
GitHub gitHubNullAble = null;
String organization = "my-org";
String teamName = "my-team";

boolean result = gitHubService.isUserInOrganizationAndTeam(gitHub, organization, teamName);
boolean result = gitHubService.isUserInOrganizationAndTeam(gitHubNullAble, organization, teamName);

assertFalse(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,37 +124,24 @@ export class SecurityMonitorComponent {
const targetDate = new Date(date).getTime();
const diffInSeconds = Math.floor((now - targetDate) / 1000);

if (diffInSeconds < 60) {
return 'just now';
}

const diffInMinutes = Math.floor(diffInSeconds / 60);
if (diffInMinutes < 60) {
return `${diffInMinutes} minute${diffInMinutes > 1 ? 's' : ''} ago`;
}

const diffInHours = Math.floor(diffInMinutes / 60);
if (diffInHours < 24) {
return `${diffInHours} hour${diffInHours > 1 ? 's' : ''} ago`;
}

const diffInDays = Math.floor(diffInHours / 24);
if (diffInDays < 7) {
return `${diffInDays} day${diffInDays > 1 ? 's' : ''} ago`;
}

const diffInWeeks = Math.floor(diffInDays / 7);
if (diffInWeeks < 4) {
return `${diffInWeeks} week${diffInWeeks > 1 ? 's' : ''} ago`;
}
const formatDuration = (diff: number, unit: number, singular: string, plural: string): string | null => {
const value = Math.floor(diff / unit);
if (value < unit) {
return `${value} ${value === 1 ? singular : plural} ago`;
}
return null;
};

const diffInMonths = Math.floor(diffInDays / 30);
if (diffInMonths < 12) {
return `${diffInMonths} month${diffInMonths > 1 ? 's' : ''} ago`;
}
if (diffInSeconds < 60) return 'just now';

const diffInYears = Math.floor(diffInMonths / 12);
return `${diffInYears} year${diffInYears > 1 ? 's' : ''} ago`;
return (
formatDuration(diffInSeconds, 60, 'minute', 'minutes') ??
formatDuration(diffInSeconds, 3600, 'hour', 'hours') ??
formatDuration(diffInSeconds, 86400, 'day', 'days') ??
formatDuration(diffInSeconds, 604800, 'week', 'weeks') ??
formatDuration(diffInSeconds, 2592000, 'month', 'months') ??
`${Math.floor(diffInSeconds / 31536000)} year${Math.floor(diffInSeconds / 31536000) === 1 ? '' : 's'} ago`
);
}
}

Expand Down

0 comments on commit f0f0caf

Please sign in to comment.