Browse Source

SONAR-16129 Handle Owasp Top 10 2021 facet ordering

tags/9.4.0.54424
Matteo Mara 2 years ago
parent
commit
f6a29b92ca

+ 24
- 3
server/sonar-server-common/src/main/java/org/sonar/server/es/Facets.java View File

@@ -20,6 +20,7 @@
package org.sonar.server.es;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
@@ -47,7 +48,9 @@ public class Facets {

public static final String SELECTED_SUB_AGG_NAME_SUFFIX = "_selected";
public static final String TOTAL = "total";
private static final java.lang.String NO_DATA_PREFIX = "no_data_";
private static final String NO_DATA_PREFIX = "no_data_";
private static final String FILTER_SUFFIX = "_filter";
private static final String FILTER_BY_RULE_PREFIX = "filter_by_rule_types_";

private final LinkedHashMap<String, LinkedHashMap<String, Long>> facetsByName;
private final ZoneId timeZone;
@@ -122,16 +125,34 @@ public class Facets {
if (Filter.class.isAssignableFrom(aggregation.getClass())) {
Filter filter = (Filter) aggregation;
if (filter.getName().startsWith(NO_DATA_PREFIX)) {
LinkedHashMap<String, Long> facet = getOrCreateFacet(filter.getName().replaceFirst(NO_DATA_PREFIX,""));
LinkedHashMap<String, Long> facet = getOrCreateFacet(filter.getName().replaceFirst(NO_DATA_PREFIX, ""));
facet.put("NO_DATA", ((Filter) aggregation).getDocCount());
}
}

for (Aggregation sub : aggregation.getAggregations()) {
for (Aggregation sub : getOrderedAggregations(aggregation)) {
processAggregation(sub);
}
}

private static List<Aggregation> getOrderedAggregations(HasAggregations topAggregation) {
String topAggregationName = ((Aggregation) topAggregation).getName();
List<Aggregation> orderedAggregations = new ArrayList<>();
for (Aggregation aggregation : topAggregation.getAggregations()) {
if (isNameMatchingTopAggregation(topAggregationName, aggregation.getName())) {
orderedAggregations.add(0, aggregation);
} else {
orderedAggregations.add(aggregation);
}
}
return orderedAggregations;
}

private static boolean isNameMatchingTopAggregation(String topAggregationName, String aggregationName) {
return aggregationName.equals(topAggregationName) ||
aggregationName.equals(FILTER_BY_RULE_PREFIX + topAggregationName.replace(FILTER_SUFFIX, ""));
}

private void processDateHistogram(Histogram aggregation) {
LinkedHashMap<String, Long> facet = getOrCreateFacet(aggregation.getName());
for (Histogram.Bucket value : aggregation.getBuckets()) {

+ 36
- 1
server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueIndexFacetsTest.java View File

@@ -20,6 +20,7 @@
package org.sonar.server.issue.index;

import java.time.ZoneId;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;
@@ -64,6 +65,8 @@ import static org.sonar.api.rule.Severity.CRITICAL;
import static org.sonar.api.rule.Severity.INFO;
import static org.sonar.api.rule.Severity.MAJOR;
import static org.sonar.api.rule.Severity.MINOR;
import static org.sonar.api.server.rule.RulesDefinition.OwaspTop10Version.Y2017;
import static org.sonar.api.server.rule.RulesDefinition.OwaspTop10Version.Y2021;
import static org.sonar.api.utils.DateUtils.parseDateTime;
import static org.sonar.db.component.ComponentTesting.newDirectory;
import static org.sonar.db.component.ComponentTesting.newFileDto;
@@ -194,7 +197,39 @@ public class IssueIndexFacetsTest {
newDoc("I2", file).setType(RuleType.VULNERABILITY).setOwaspTop10(singletonList("a3")),
newDoc("I3", file));

assertThatFacetHasOnly(IssueQuery.builder(), "owaspTop10",
assertThatFacetHasOnly(IssueQuery.builder(), Y2017.prefix(),
entry("a1", 1L),
entry("a2", 1L),
entry("a3", 1L));
}

@Test
public void facets_on_owaspTop10_2021() {
ComponentDto project = newPrivateProjectDto();
ComponentDto file = newFileDto(project, null);

indexIssues(
newDoc("I1", file).setType(RuleType.VULNERABILITY).setOwaspTop10For2021(asList("a1", "a2")),
newDoc("I2", file).setType(RuleType.VULNERABILITY).setOwaspTop10For2021(singletonList("a3")),
newDoc("I3", file));

assertThatFacetHasExactly(IssueQuery.builder(), Y2021.prefix(),
entry("a1", 1L),
entry("a2", 1L),
entry("a3", 1L));
}

@Test
public void facets_on_owaspTop10_2021_stay_ordered() {
ComponentDto project = newPrivateProjectDto();
ComponentDto file = newFileDto(project, null);

indexIssues(
newDoc("I1", file).setType(RuleType.VULNERABILITY).setOwaspTop10For2021(asList("a1", "a2")),
newDoc("I2", file).setType(RuleType.VULNERABILITY).setOwaspTop10For2021(singletonList("a3")),
newDoc("I3", file));

assertThatFacetHasExactly(IssueQuery.builder().owaspTop10For2021(Collections.singletonList("a3")), Y2021.prefix(),
entry("a1", 1L),
entry("a2", 1L),
entry("a3", 1L));

Loading…
Cancel
Save