From 310431bf27ebb389c139c4d10a26ce99dd3303dc Mon Sep 17 00:00:00 2001 From: Prudhvi Godithi Date: Mon, 26 Aug 2024 15:42:59 -0700 Subject: [PATCH] Support component name for general release metrics Signed-off-by: Prudhvi Godithi --- .../metrics/MetricsCalculation.java | 37 ++--- .../metrics/release/ReleaseMetrics.java | 4 +- .../metrics/release/ReleaseRepoFetcher.java | 54 +++---- .../model/release/ReleaseMetricsData.java | 4 + .../metrics/MetricsCalculationTest.java | 144 ++++++++++++++++++ .../metrics/release/ReleaseMetricsTest.java | 7 +- .../release/ReleaseRepoFetcherTest.java | 44 +++--- .../model/release/ReleaseMetricsDataTest.java | 10 ++ 8 files changed, 233 insertions(+), 71 deletions(-) create mode 100644 src/test/java/org/opensearchmetrics/metrics/MetricsCalculationTest.java diff --git a/src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java b/src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java index 4c1f44a..d0942ae 100644 --- a/src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java +++ b/src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java @@ -126,19 +126,19 @@ public void generateLabelMetrics(List repositories) { throw new RuntimeException(e); } return labelInfo.entrySet().stream().flatMap(entry -> { - String labelname = entry.getKey(); + String labelName = entry.getKey(); List values = entry.getValue(); LabelData labelData = new LabelData(); try { labelData.setId(String.valueOf(UUID.nameUUIDFromBytes(MessageDigest.getInstance("SHA-1") - .digest(("label-metrics-" + labelname + "-" + currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "-" + repo) + .digest(("label-metrics-" + labelName + "-" + currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "-" + repo) .getBytes())))); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } labelData.setRepository(repo); labelData.setCurrentDate(currentDate.toString()); - labelData.setLabelName(labelname); + labelData.setLabelName(labelName); labelData.setLabelPullCount(values.get(1)); labelData.setLabelIssueCount(values.get(0)); return Stream.of(labelData); @@ -155,14 +155,17 @@ public void generateReleaseMetrics() { Map metricFinalData = Arrays.stream(releaseInputs) .filter(ReleaseInputs::getTrack) - .flatMap(releaseInput -> releaseMetrics.getReleaseRepos(releaseInput.getVersion()).stream() - .flatMap(repo -> { + .flatMap(releaseInput -> releaseMetrics.getReleaseRepos(releaseInput.getVersion()).entrySet().stream() + .flatMap(entry -> { + String repoName = entry.getKey(); + String componentName = entry.getValue(); ReleaseMetricsData releaseMetricsData = new ReleaseMetricsData(); - releaseMetricsData.setRepository(repo); + releaseMetricsData.setRepository(repoName); + releaseMetricsData.setComponent(componentName); releaseMetricsData.setCurrentDate(currentDate.toString()); try { releaseMetricsData.setId(String.valueOf(UUID.nameUUIDFromBytes(MessageDigest.getInstance("SHA-1") - .digest(("release-metrics-" + releaseInput.getVersion() + "-" + currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "-" + repo) + .digest(("release-metrics-" + releaseInput.getVersion() + "-" + currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "-" + repoName) .getBytes())))); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); @@ -170,20 +173,20 @@ public void generateReleaseMetrics() { releaseMetricsData.setReleaseVersion(releaseInput.getVersion()); releaseMetricsData.setVersion(releaseInput.getVersion()); releaseMetricsData.setReleaseState(releaseInput.getState()); - releaseMetricsData.setIssuesOpen(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repo, "open", false)); - releaseMetricsData.setAutocutIssuesOpen(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repo, "open", true)); - releaseMetricsData.setIssuesClosed(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repo, "closed", false)); - releaseMetricsData.setPullsOpen(releaseMetrics.getReleaseLabelPulls(releaseInput.getVersion(), repo, "open")); - releaseMetricsData.setPullsClosed(releaseMetrics.getReleaseLabelPulls(releaseInput.getVersion(), repo, "closed")); - releaseMetricsData.setVersionIncrement(releaseMetrics.getReleaseVersionIncrement(releaseInput.getVersion(), repo, releaseInput.getBranch())); - releaseMetricsData.setReleaseNotes(releaseMetrics.getReleaseNotes(releaseInput.getVersion(), repo, releaseInput.getBranch())); - releaseMetricsData.setReleaseBranch(releaseMetrics.getReleaseBranch(releaseInput.getVersion(), repo)); - String[] releaseOwners = releaseMetrics.getReleaseOwners(releaseInput.getVersion(), repo); + releaseMetricsData.setIssuesOpen(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repoName, "open", false)); + releaseMetricsData.setAutocutIssuesOpen(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repoName, "open", true)); + releaseMetricsData.setIssuesClosed(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repoName, "closed", false)); + releaseMetricsData.setPullsOpen(releaseMetrics.getReleaseLabelPulls(releaseInput.getVersion(), repoName, "open")); + releaseMetricsData.setPullsClosed(releaseMetrics.getReleaseLabelPulls(releaseInput.getVersion(), repoName, "closed")); + releaseMetricsData.setVersionIncrement(releaseMetrics.getReleaseVersionIncrement(releaseInput.getVersion(), repoName, releaseInput.getBranch())); + releaseMetricsData.setReleaseNotes(releaseMetrics.getReleaseNotes(releaseInput.getVersion(), repoName, releaseInput.getBranch())); + releaseMetricsData.setReleaseBranch(releaseMetrics.getReleaseBranch(releaseInput.getVersion(), repoName)); + String[] releaseOwners = releaseMetrics.getReleaseOwners(releaseInput.getVersion(), repoName); releaseMetricsData.setReleaseOwners(releaseOwners); releaseMetricsData.setReleaseOwnerExists(Optional.ofNullable(releaseOwners) .map(owners -> owners.length > 0) .orElse(false)); - String releaseIssue = releaseMetrics.getReleaseIssue(releaseInput.getVersion(), repo); + String releaseIssue = releaseMetrics.getReleaseIssue(releaseInput.getVersion(), repoName); releaseMetricsData.setReleaseIssue(releaseIssue); releaseMetricsData.setReleaseIssueExists(Optional.ofNullable(releaseIssue) .map(str -> !str.isEmpty()) diff --git a/src/main/java/org/opensearchmetrics/metrics/release/ReleaseMetrics.java b/src/main/java/org/opensearchmetrics/metrics/release/ReleaseMetrics.java index 52ea0f2..16d16c0 100644 --- a/src/main/java/org/opensearchmetrics/metrics/release/ReleaseMetrics.java +++ b/src/main/java/org/opensearchmetrics/metrics/release/ReleaseMetrics.java @@ -5,6 +5,7 @@ import javax.inject.Inject; import java.util.List; +import java.util.Map; public class ReleaseMetrics { @@ -39,10 +40,11 @@ public ReleaseMetrics(OpenSearchUtil openSearchUtil, ObjectMapper objectMapper, this.releaseIssueChecker = releaseIssueChecker; } - public List getReleaseRepos(String releaseVersion) { + public Map getReleaseRepos(String releaseVersion) { return releaseRepoFetcher.getReleaseRepos(releaseVersion); } + public Long getReleaseLabelIssues(String releaseVersion, String repo, String issueState, boolean autoCut) { return releaseLabelIssuesFetcher.releaseLabelIssues(releaseVersion, repo, issueState, autoCut, openSearchUtil); } diff --git a/src/main/java/org/opensearchmetrics/metrics/release/ReleaseRepoFetcher.java b/src/main/java/org/opensearchmetrics/metrics/release/ReleaseRepoFetcher.java index 677d979..5c18b28 100644 --- a/src/main/java/org/opensearchmetrics/metrics/release/ReleaseRepoFetcher.java +++ b/src/main/java/org/opensearchmetrics/metrics/release/ReleaseRepoFetcher.java @@ -8,10 +8,9 @@ import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; -import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; public class ReleaseRepoFetcher { @@ -20,32 +19,28 @@ public class ReleaseRepoFetcher { public ReleaseRepoFetcher() { } - public List getReleaseRepos(String releaseVersion) { - List repoNames = new ArrayList<>(); - repoNames.add("opensearch-build"); - repoNames.add("performance-analyzer-rca"); - repoNames.add("project-website"); + public Map getReleaseRepos(String releaseVersion) { + Map repoMap = new HashMap<>(); String[] urls = { String.format("https://raw.githubusercontent.com/opensearch-project/opensearch-build/main/manifests/%s/opensearch-%s.yml", releaseVersion, releaseVersion), String.format("https://raw.githubusercontent.com/opensearch-project/opensearch-build/main/manifests/%s/opensearch-dashboards-%s.yml", releaseVersion, releaseVersion) }; for (String url : urls) { String responseBody = readUrl(url); - parseYaml(responseBody, repoNames); + parseYaml(responseBody, repoMap); } - repoNames.addAll(releaseRepoExceptionList()); - return repoNames.stream() - .distinct() - .collect(Collectors.toList()); + repoMap.putAll(releaseRepoExceptionMap()); + return repoMap; } - public List releaseRepoExceptionList() { - List repoExceptionList = new ArrayList<>(); - repoExceptionList.add("opensearch-build"); - repoExceptionList.add("performance-analyzer-rca"); - repoExceptionList.add("project-website"); - repoExceptionList.add("documentation-website"); - return repoExceptionList; + + public Map releaseRepoExceptionMap() { + Map repoExceptionMap = new HashMap<>(); + repoExceptionMap.put("opensearch-build", "opensearch-build"); + repoExceptionMap.put("performance-analyzer-rca", "performance-analyzer-rca"); + repoExceptionMap.put("project-website", "project-website"); + repoExceptionMap.put("documentation-website", "documentation-website"); + return repoExceptionMap; }; public String readUrl(String url) { @@ -70,7 +65,7 @@ public URL createURL(String url){ } - public void parseYaml(String responseBody, List repoNames) { + public void parseYaml(String responseBody, Map repoMap) { new Yaml().loadAll(responseBody).forEach(document -> { if (document instanceof Map) { Map map = (Map) document; @@ -79,21 +74,16 @@ public void parseYaml(String responseBody, List repoNames) { .flatMap(value -> ((List) value).stream()) .filter(component -> component instanceof Map) .map(component -> (Map) component) - .filter(componentMap -> { - Object repository = componentMap.get("repository"); - return repository != null && repository.toString().contains("github.com"); - }) - .map(componentMap -> { + .forEach(componentMap -> { String repoUrl = componentMap.get("repository").toString(); + String componentName = componentMap.get("name").toString(); + int startIndex = repoUrl.lastIndexOf('/') + 1; int endIndex = repoUrl.lastIndexOf(".git"); - if (repoUrl != null && endIndex != -1) { - return repoUrl.substring(startIndex, endIndex); - } else { - return repoUrl.substring(startIndex); - } - }) - .forEach(repoNames::add); + String repoName = (endIndex != -1) ? repoUrl.substring(startIndex, endIndex) : repoUrl.substring(startIndex); + + repoMap.put(repoName, componentName); + }); } }); } diff --git a/src/main/java/org/opensearchmetrics/model/release/ReleaseMetricsData.java b/src/main/java/org/opensearchmetrics/model/release/ReleaseMetricsData.java index b19f55f..6113173 100644 --- a/src/main/java/org/opensearchmetrics/model/release/ReleaseMetricsData.java +++ b/src/main/java/org/opensearchmetrics/model/release/ReleaseMetricsData.java @@ -21,6 +21,9 @@ public class ReleaseMetricsData { @JsonProperty("repository") private String repository; + @JsonProperty("component") + private String component; + @JsonProperty("release_version") private String releaseVersion; @@ -70,6 +73,7 @@ public String toJson(ObjectMapper mapper) throws JsonProcessingException { data.put("id", id); data.put("current_date", currentDate); data.put("repository", repository); + data.put("component", component); data.put("release_version", releaseVersion); data.put("version", version); data.put("release_state", releaseState); diff --git a/src/test/java/org/opensearchmetrics/metrics/MetricsCalculationTest.java b/src/test/java/org/opensearchmetrics/metrics/MetricsCalculationTest.java new file mode 100644 index 0000000..edaf286 --- /dev/null +++ b/src/test/java/org/opensearchmetrics/metrics/MetricsCalculationTest.java @@ -0,0 +1,144 @@ +package org.opensearchmetrics.metrics; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.opensearch.action.search.SearchRequest; +import org.opensearch.index.query.BoolQueryBuilder; +import org.opensearchmetrics.metrics.general.*; +import org.opensearchmetrics.metrics.label.LabelMetrics; +import org.opensearchmetrics.metrics.release.ReleaseInputs; +import org.opensearchmetrics.metrics.release.ReleaseMetrics; +import org.opensearchmetrics.model.label.LabelData; +import org.opensearchmetrics.model.general.MetricsData; +import org.opensearchmetrics.model.release.ReleaseMetricsData; +import org.opensearchmetrics.util.OpenSearchUtil; + +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.*; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +public class MetricsCalculationTest { + @Mock + private OpenSearchUtil openSearchUtil; + + @Mock + private ObjectMapper objectMapper; + + @Mock + private UntriagedIssues untriagedIssues; + @Mock + private UncommentedPullRequests uncommentedPullRequests; + @Mock + private UnlabelledPullRequests unlabelledPullRequests; + @Mock + private UnlabelledIssues unlabelledIssues; + @Mock + private MergedPullRequests mergedPullRequests; + @Mock + private OpenPullRequests openPullRequests; + @Mock + private OpenIssues openIssues; + @Mock + private ClosedIssues closedIssues; + @Mock + private CreatedIssues createdIssues; + @Mock + private IssueComments issueComments; + @Mock + private PullComments pullComments; + @Mock + private IssuePositiveReactions issuePositiveReactions; + @Mock + private IssueNegativeReactions issueNegativeReactions; + @Mock + private LabelMetrics labelMetrics; + @Mock + private ReleaseMetrics releaseMetrics; + + @InjectMocks + private MetricsCalculation metricsCalculation; + + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + metricsCalculation = new MetricsCalculation(openSearchUtil, objectMapper, + untriagedIssues, uncommentedPullRequests, unlabelledPullRequests, unlabelledIssues, + mergedPullRequests, openPullRequests, openIssues, closedIssues, createdIssues, + issueComments, pullComments, issuePositiveReactions, issueNegativeReactions, + labelMetrics, releaseMetrics); + } + + @Test + void testGenerateGeneralMetrics() throws IOException { + List repositories = Arrays.asList("repo1", "repo2"); + MetricsData metricsData = new MetricsData(); + metricsData.setId("id"); + metricsData.setRepository("repo1"); + metricsData.setCurrentDate(LocalDateTime.now(ZoneId.of("UTC")).toString()); + metricsData.setMetricName("metric"); + metricsData.setMetricCount(10L); + when(untriagedIssues.getBoolQueryBuilder(any())).thenReturn(new BoolQueryBuilder()); + when(untriagedIssues.createSearchRequest(any(), any())).thenReturn(new SearchRequest()); + when(untriagedIssues.performSearch(any(), any())).thenReturn(10L); + when(objectMapper.writeValueAsString(any())).thenReturn("json"); + metricsCalculation.generateGeneralMetrics(repositories); + verify(openSearchUtil).createIndexIfNotExists("opensearch_general_metrics"); + verify(openSearchUtil).bulkIndex(eq("opensearch_general_metrics"), any(Map.class)); + } + + @Test + void testGenerateLabelMetrics() throws IOException { + List repositories = Arrays.asList("repo1", "repo2"); + Map> labelInfo = new HashMap<>(); + labelInfo.put("label1", Arrays.asList(5L, 10L)); + LabelData labelData = new LabelData(); + labelData.setId("id"); + labelData.setRepository("repo1"); + labelData.setCurrentDate(LocalDateTime.now(ZoneId.of("UTC")).toString()); + labelData.setLabelName("label1"); + labelData.setLabelPullCount(10L); + labelData.setLabelIssueCount(5L); + when(labelMetrics.getLabelInfo(any(), any())).thenReturn(labelInfo); + when(objectMapper.writeValueAsString(any())).thenReturn("json"); + metricsCalculation.generateLabelMetrics(repositories); + verify(openSearchUtil).createIndexIfNotExists("opensearch_label_metrics"); + verify(openSearchUtil).bulkIndex(eq("opensearch_label_metrics"), any(Map.class)); + } + + @Test + void testGenerateReleaseMetrics() { + Map releaseRepos = new HashMap<>(); + releaseRepos.put("repo1", "component1"); + releaseRepos.put("repo2", "component2"); + when(releaseMetrics.getReleaseRepos("2.13.0")).thenReturn(releaseRepos); + when(releaseMetrics.getReleaseLabelIssues(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "open", false)).thenReturn(10L); + when(releaseMetrics.getReleaseLabelIssues(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "open", true)).thenReturn(5L); + when(releaseMetrics.getReleaseLabelIssues(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "closed", false)).thenReturn(20L); + when(releaseMetrics.getReleaseLabelPulls(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "open")).thenReturn(3L); + when(releaseMetrics.getReleaseLabelPulls(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "closed")).thenReturn(8L); + when(releaseMetrics.getReleaseVersionIncrement(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "main")).thenReturn(true); + when(releaseMetrics.getReleaseNotes(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "main")).thenReturn(true); + when(releaseMetrics.getReleaseBranch(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1")).thenReturn(true); + when(releaseMetrics.getReleaseOwners(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1")).thenReturn(new String[]{"owner1", "owner2"}); + when(releaseMetrics.getReleaseIssue(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1")).thenReturn("release-123"); + metricsCalculation.generateReleaseMetrics(); + verify(openSearchUtil).createIndexIfNotExists("opensearch_release_metrics"); + verify(openSearchUtil).bulkIndex(eq("opensearch_release_metrics"), ArgumentMatchers.anyMap()); + verify(openSearchUtil, times(1)).createIndexIfNotExists("opensearch_release_metrics"); + } +} diff --git a/src/test/java/org/opensearchmetrics/metrics/release/ReleaseMetricsTest.java b/src/test/java/org/opensearchmetrics/metrics/release/ReleaseMetricsTest.java index 79af90d..b82d646 100644 --- a/src/test/java/org/opensearchmetrics/metrics/release/ReleaseMetricsTest.java +++ b/src/test/java/org/opensearchmetrics/metrics/release/ReleaseMetricsTest.java @@ -8,6 +8,7 @@ import java.util.Collections; import java.util.List; +import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; @@ -47,10 +48,10 @@ public class ReleaseMetricsTest { @Test public void testGetReleaseRepos() { MockitoAnnotations.openMocks(this); - List repos = Collections.singletonList("testRepo"); + Map repos = Collections.singletonMap("testRepo", "testComponent"); when(releaseRepoFetcher.getReleaseRepos(anyString())).thenReturn(repos); - List result = releaseRepoFetcher.getReleaseRepos("1.0.0"); - assertSame(repos, result); + Map result = releaseRepoFetcher.getReleaseRepos("1.0.0"); + assertEquals(repos, result); } @Test diff --git a/src/test/java/org/opensearchmetrics/metrics/release/ReleaseRepoFetcherTest.java b/src/test/java/org/opensearchmetrics/metrics/release/ReleaseRepoFetcherTest.java index b22b602..f9e7ff1 100644 --- a/src/test/java/org/opensearchmetrics/metrics/release/ReleaseRepoFetcherTest.java +++ b/src/test/java/org/opensearchmetrics/metrics/release/ReleaseRepoFetcherTest.java @@ -9,7 +9,9 @@ import java.net.URL; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -36,15 +38,15 @@ public void testReleaseRepoExceptionList() { ReleaseRepoFetcher fetcher = new ReleaseRepoFetcher(); // Call the method under test - List result = fetcher.releaseRepoExceptionList(); - List expectedList = new ArrayList<>(); - expectedList.add("opensearch-build"); - expectedList.add("performance-analyzer-rca"); - expectedList.add("project-website"); - expectedList.add("documentation-website"); + Map result = fetcher.releaseRepoExceptionMap(); + Map expectedMap = new HashMap<>(); + expectedMap.put("opensearch-build", "opensearch-build"); + expectedMap.put("performance-analyzer-rca", "performance-analyzer-rca"); + expectedMap.put("project-website", "project-website"); + expectedMap.put("documentation-website", "documentation-website"); // Assert that the result matches the expected list - assertEquals(expectedList, result); + assertEquals(expectedMap, result); } @Test public void testCreateURL() { @@ -72,7 +74,7 @@ public void testParseYaml() { " checks:\n" + " - gradle:publish\n" + " - gradle:properties:version\n" + - " - name: common-utils\n" + + " - name: commonUtils\n" + " repository: https://github.com/opensearch-project/common-utils.git\n" + " ref: tags/1.3.15.0\n" + " checks:\n" + @@ -81,18 +83,22 @@ public void testParseYaml() { " platforms:\n" + " - linux\n" + " - windows\n"; - List repoNames = new ArrayList<>(); + Map repoNames = new HashMap<>(); ReleaseRepoFetcher fetcher = new ReleaseRepoFetcher(); fetcher.parseYaml(responseBody, repoNames); assertEquals(2, repoNames.size()); - assertEquals(new ArrayList<>(Arrays.asList("OpenSearch", "common-utils")), repoNames); + Map expectedRepoNames = new HashMap<>(); + expectedRepoNames.put("OpenSearch", "OpenSearch"); + expectedRepoNames.put("common-utils", "commonUtils"); + assertEquals(expectedRepoNames, repoNames); + // assertEquals(new ArrayList<>(Arrays.asList("OpenSearch", "common-utils")), repoNames); } @Test public void testGetReleaseRepos() { ReleaseRepoFetcher fetcher = Mockito.spy(new ReleaseRepoFetcher()); Mockito.doReturn("Test content").when(fetcher).readUrl(Mockito.anyString()); - List repos = fetcher.getReleaseRepos("1.0.0"); + Map repos = fetcher.getReleaseRepos("1.0.0"); // Default will always have 4 repos part of exception list assertEquals(4, repos.size()); } @@ -101,18 +107,20 @@ public void testGetReleaseRepos() { public void testGetReleaseRepos_withData() { ReleaseRepoFetcher fetcher = Mockito.spy(new ReleaseRepoFetcher()); Mockito.doReturn("Test content").when(fetcher).readUrl(Mockito.anyString()); - List repoNames = new ArrayList<>(); - repoNames.add("repo1"); + Map repoNames = new HashMap<>(); + repoNames.put("repoName", "componentName"); Mockito.doAnswer(invocation -> { Object[] args = invocation.getArguments(); - List list = (List) args[1]; - list.addAll(repoNames); + String responseBody = (String) args[0]; + Map map = (Map) args[1]; + // Add all entries from repoNames to the provided map + map.putAll(repoNames); return null; - }).when(fetcher).parseYaml(Mockito.anyString(), Mockito.anyList()); - List repos = fetcher.getReleaseRepos("1.0.0"); + }).when(fetcher).parseYaml(Mockito.anyString(), Mockito.anyMap()); + Map repos = fetcher.getReleaseRepos("1.0.0"); // Default will always have 4 repos part of exception list assertEquals(5, repos.size()); - assertTrue(repos.contains("repo1")); + assertTrue(repos.containsKey("repoName")); } } \ No newline at end of file diff --git a/src/test/java/org/opensearchmetrics/model/release/ReleaseMetricsDataTest.java b/src/test/java/org/opensearchmetrics/model/release/ReleaseMetricsDataTest.java index c88a0e0..9aa4648 100644 --- a/src/test/java/org/opensearchmetrics/model/release/ReleaseMetricsDataTest.java +++ b/src/test/java/org/opensearchmetrics/model/release/ReleaseMetricsDataTest.java @@ -47,6 +47,12 @@ public void testRepository() { assertEquals("exampleRepo", releaseMetricsData.getRepository()); } + @Test + public void testCOmponent() { + releaseMetricsData.setComponent("exampleComponent"); + assertEquals("exampleComponent", releaseMetricsData.getComponent()); + } + @Test public void testReleaseVersion() { releaseMetricsData.setReleaseVersion("1.0"); @@ -144,6 +150,7 @@ void toJson() throws JsonProcessingException { releaseMetricsData.setId("1"); releaseMetricsData.setCurrentDate("2024-03-15"); releaseMetricsData.setRepository("test-repo"); + releaseMetricsData.setComponent("test-component"); releaseMetricsData.setReleaseVersion("1.0.0"); releaseMetricsData.setVersion("1.0.0"); releaseMetricsData.setReleaseIssue("https://sample-release-issue/100"); @@ -164,6 +171,7 @@ void toJson() throws JsonProcessingException { expectedData.put("id", "1"); expectedData.put("current_date", "2024-03-15"); expectedData.put("repository", "test-repo"); + expectedData.put("component", "test-component"); expectedData.put("release_version", "1.0.0"); expectedData.put("version", "1.0.0"); expectedData.put("release_state", "stable"); @@ -196,6 +204,7 @@ void getJson() throws JsonProcessingException { releaseMetricsData.setId("1"); releaseMetricsData.setCurrentDate("2024-03-15"); releaseMetricsData.setRepository("test-repo"); + releaseMetricsData.setComponent("test-component"); releaseMetricsData.setReleaseVersion("1.0.0"); releaseMetricsData.setVersion("1.0.0"); releaseMetricsData.setReleaseState("stable"); @@ -228,6 +237,7 @@ void getJson_WithJsonProcessingException() throws JsonProcessingException { releaseMetricsData.setId("1"); releaseMetricsData.setCurrentDate("2024-03-15"); releaseMetricsData.setRepository("test-repo"); + releaseMetricsData.setComponent("test-component"); releaseMetricsData.setReleaseVersion("1.0.0"); releaseMetricsData.setVersion("1.0.0"); releaseMetricsData.setReleaseState("stable");