From f0f0caf2f11eac319854f02bffa397c0e0e4e297 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Mon, 16 Dec 2024 10:33:23 +0700 Subject: [PATCH] Fix sonar issues --- .../service/impl/GitHubServiceImplTest.java | 15 +++---- .../security-monitor.component.ts | 45 +++++++------------ 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/impl/GitHubServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/impl/GitHubServiceImplTest.java index 6e5da0eb..97d210d6 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/impl/GitHubServiceImplTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/impl/GitHubServiceImplTest.java @@ -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 @@ -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"; @@ -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"; @@ -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); @@ -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); } diff --git a/marketplace-ui/src/app/modules/security-monitor/security-monitor.component.ts b/marketplace-ui/src/app/modules/security-monitor/security-monitor.component.ts index 2805004a..a8aa6418 100644 --- a/marketplace-ui/src/app/modules/security-monitor/security-monitor.component.ts +++ b/marketplace-ui/src/app/modules/security-monitor/security-monitor.component.ts @@ -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` + ); } }