From efd4a3f6eca465a7163204c1f1138e2ee7beb042 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen <119989010+ndkhanh-axonivy@users.noreply.github.com> Date: Thu, 19 Dec 2024 09:12:16 +0700 Subject: [PATCH] MARP-1294 Enhance performance for central monitoring reporting (#259) --- .../service/impl/GitHubServiceImpl.java | 13 ++++++------ .../service/impl/GitHubServiceImplTest.java | 21 +++++++++++++++---- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/marketplace-service/src/main/java/com/axonivy/market/github/service/impl/GitHubServiceImpl.java b/marketplace-service/src/main/java/com/axonivy/market/github/service/impl/GitHubServiceImpl.java index d8cf56e7..6030dae0 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/github/service/impl/GitHubServiceImpl.java +++ b/marketplace-service/src/main/java/com/axonivy/market/github/service/impl/GitHubServiceImpl.java @@ -42,7 +42,6 @@ import java.util.Map; import java.util.Optional; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutorService; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -173,11 +172,13 @@ public List getSecurityDetailsForAllProducts(String accessT GitHub gitHub = getGitHub(accessToken); GHOrganization organization = gitHub.getOrganization(orgName); - return organization.listRepositories().toList().stream() - .map(repo -> CompletableFuture.supplyAsync(() -> fetchSecurityInfoSafe(repo, organization, accessToken), taskScheduler.getScheduledExecutor())) - .map(CompletableFuture::join) - .sorted(Comparator.comparing(ProductSecurityInfo::getRepoName)) - .collect(Collectors.toList()); + List> futures = organization.listRepositories().toList().stream() + .map(repo -> CompletableFuture.supplyAsync(() -> fetchSecurityInfoSafe(repo, organization, accessToken), + taskScheduler.getScheduledExecutor())).toList(); + + return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) + .thenApply(v -> futures.stream().map(CompletableFuture::join).sorted( + Comparator.comparing(ProductSecurityInfo::getRepoName)).collect(Collectors.toList())).join(); } catch (IOException e) { log.error(e.getStackTrace()); return Collections.emptyList(); 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 97d210d6..3da6c0ed 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 @@ -58,7 +58,7 @@ class GitHubServiceImplTest { private GitHubAccessTokenResponse gitHubAccessTokenResponse; @Mock - private GHTeam team1; + private GHTeam ghTeam; @Spy @InjectMocks @@ -281,7 +281,7 @@ void testIsUserInOrganizationAndTeam_TeamNotFound() throws IOException { String organization = "my-org"; String teamName = "my-team"; Set teams = new HashSet<>(); - teams.add(team1); + teams.add(ghTeam); Map> hashMapTeams = new HashMap<>(); hashMapTeams.put(organization, teams); when(gitHub.getMyTeams()).thenReturn(hashMapTeams); @@ -296,8 +296,8 @@ void testIsUserInOrganizationAndTeam_TeamFound() throws IOException { String organization = "my-org"; String teamName = "my-team"; Set teams = new HashSet<>(); - when(team1.getName()).thenReturn(teamName); - teams.add(team1); + when(ghTeam.getName()).thenReturn(teamName); + teams.add(ghTeam); Map> hashMapTeams = new HashMap<>(); hashMapTeams.put(organization, teams); when(gitHub.getMyTeams()).thenReturn(hashMapTeams); @@ -511,4 +511,17 @@ void testGetDependabotAlerts_Disabled() { Dependabot result = gitHubService.getDependabotAlerts(ghRepository, ghOrganization, accessToken); assertEquals(AccessLevel.DISABLED, result.getStatus()); } + + @Test + void testGetSecurityDetailsForAllProducts() throws Exception { + String accessToken = "mockAccessToken"; + String orgName = "mockOrganization"; + GHOrganization ghOrganization = mock(GHOrganization.class); + when(gitHubService.getGitHub(accessToken)).thenReturn(gitHub); + when(gitHub.getOrganization(orgName)).thenReturn(ghOrganization); + PagedIterable mockPagedIterable = mock(PagedIterable.class); + when(ghOrganization.listRepositories()).thenReturn(mockPagedIterable); + List result = gitHubService.getSecurityDetailsForAllProducts(accessToken, orgName); + assertEquals(0, result.size()); + } }