Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reports: use own stats key if no resource message #6084

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions addOns/reports/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Update automation job help.
- Fields with default or missing values are omitted for the `report` job in saved Automation Framework plans.

### Fixed
- Do not log an error when the statistics do not have a resource message (Issue 8788).

## [0.34.0] - 2024-10-07
### Changed
- Checkmarx rebrand.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

public class ReportHelper {

private static final String STATS_RESOURCE_PREFIX = ExtensionReports.PREFIX + ".report.";

public static String getRiskString(int risk) {
return Constant.messages.getString(ExtensionReports.PREFIX + ".report.risk." + risk);
}
Expand All @@ -48,7 +50,11 @@ public static String getConfidenceString(int confidence) {
}

public static String getStatisticsString(String statsKey) {
return Constant.messages.getString(ExtensionReports.PREFIX + ".report." + statsKey);
String resourceKey = STATS_RESOURCE_PREFIX + statsKey;
if (Constant.messages.containsKey(resourceKey)) {
return Constant.messages.getString(resourceKey);
}
return statsKey;
}

public static String getHostForSite(String site) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
package org.zaproxy.addon.reports;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.core.scanner.Alert;
import org.zaproxy.zap.extension.alert.AlertNode;
import org.zaproxy.zap.utils.I18N;

class ReportHelperUnitTest {

Expand Down Expand Up @@ -165,6 +170,33 @@ void shouldGetAlertInstancesForSite() {
assertThat(ex3sql2Alerts.size(), is(1));
}

@Test
void shouldGetStatisticsString() throws Exception {
// Given
Constant.messages = mock(I18N.class);
String statsKey = "some.stats.key";
String resourceKey = "reports.report." + statsKey;
given(Constant.messages.containsKey(resourceKey)).willReturn(true);
String expectedString = "Statistic Xyz";
given(Constant.messages.getString(resourceKey)).willReturn(expectedString);
// When
String string = ReportHelper.getStatisticsString(statsKey);
// Then
assertThat(string, is(equalTo(expectedString)));
}

@Test
void shouldGetStatsKeyForMissingStatisticsString() throws Exception {
// Given
Constant.messages = mock(I18N.class);
String statsKey = "some.stats.key";
given(Constant.messages.containsKey("reports.report." + statsKey)).willReturn(false);
// When
String string = ReportHelper.getStatisticsString(statsKey);
// Then
assertThat(string, is(equalTo(statsKey)));
}

AlertNode newAlertNode(int risk, String name, String url) {
AlertNode node = new AlertNode(risk, name);
Alert alert = new Alert(1);
Expand Down
Loading