Skip to content

Commit

Permalink
MARP-1294 Enhance performance for central monitoring reporting (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkhanh-axonivy authored Dec 19, 2024
1 parent e1a0d85 commit efd4a3f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -173,11 +172,13 @@ public List<ProductSecurityInfo> 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<CompletableFuture<ProductSecurityInfo>> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class GitHubServiceImplTest {
private GitHubAccessTokenResponse gitHubAccessTokenResponse;

@Mock
private GHTeam team1;
private GHTeam ghTeam;

@Spy
@InjectMocks
Expand Down Expand Up @@ -281,7 +281,7 @@ void testIsUserInOrganizationAndTeam_TeamNotFound() throws IOException {
String organization = "my-org";
String teamName = "my-team";
Set<GHTeam> teams = new HashSet<>();
teams.add(team1);
teams.add(ghTeam);
Map<String, Set<GHTeam>> hashMapTeams = new HashMap<>();
hashMapTeams.put(organization, teams);
when(gitHub.getMyTeams()).thenReturn(hashMapTeams);
Expand All @@ -296,8 +296,8 @@ void testIsUserInOrganizationAndTeam_TeamFound() throws IOException {
String organization = "my-org";
String teamName = "my-team";
Set<GHTeam> teams = new HashSet<>();
when(team1.getName()).thenReturn(teamName);
teams.add(team1);
when(ghTeam.getName()).thenReturn(teamName);
teams.add(ghTeam);
Map<String, Set<GHTeam>> hashMapTeams = new HashMap<>();
hashMapTeams.put(organization, teams);
when(gitHub.getMyTeams()).thenReturn(hashMapTeams);
Expand Down Expand Up @@ -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<GHRepository> mockPagedIterable = mock(PagedIterable.class);
when(ghOrganization.listRepositories()).thenReturn(mockPagedIterable);
List<ProductSecurityInfo> result = gitHubService.getSecurityDetailsForAllProducts(accessToken, orgName);
assertEquals(0, result.size());
}
}

0 comments on commit efd4a3f

Please sign in to comment.