Skip to content

Commit

Permalink
Fix missing job uuid in report, closes #864
Browse files Browse the repository at this point in the history
- changed sereco product result transformer - does now
  set job uuid into reporting model (was missing)
- changed existing pds integration test and check now for job uuid
  inside
- added unit tests to check fallback handling
- implemented fallback handling when no Job UUID in report JSON result
  (so old reports having no report job uuid inside will be
  repaired automatically)
  • Loading branch information
de-jcup committed Oct 29, 2021
1 parent f20bd6b commit 5810a49
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,15 @@ private List<SecHubFinding> assertFindings(SecHubReportData report) {
assertNotNull(findings);
return findings;
}

public AssertReport hasJobUUID(String uuidAsString) {
assertEquals(UUID.fromString(uuidAsString), report.getJobUUID());
public AssertReport hasJobUUID(UUID uuid) {
assertEquals(uuid, report.getJobUUID());
return this;
}

public AssertReport hasJobUUID(String uuidAsString) {
return hasJobUUID(UUID.fromString(uuidAsString));
}

public AssertReport dump() {
LOG.info("-----------------------------------------------------------");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void a_user_can_start_a_pds_sarif_scan_and_get_the_sarif_results_transfor
assertReport(report).
hasStatus(SecHubStatus.SUCCESS).
hasMessages(0).
hasJobUUID(jobUUID).
hasTrafficLight(RED).
finding(0).
hasSeverity(Severity.HIGH).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public ReportTransformationResult transform(ProductResult serecoProductResult) t

ReportTransformationResult transformerResult = new ReportTransformationResult();
transformerResult.setReportVersion(SecHubReportVersion.VERSION_1_0.getVersionAsString());
transformerResult.setJobUUID(sechubJobUUID);

List<SecHubFinding> findings = transformerResult.getResult().getFindings();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public ScanSecHubReport(ScanReport report) {
if (ScanReportResultType.MODEL.equals(resultType)) {
try {
model = SecHubReportModel.fromJSONString(report.getResult());
if (model.getJobUUID() == null) {
// Fallback for problems when model did not contain job uuid - see https://github.com/Daimler/sechub/issues/864
LOG.warn("Job uuid not found inside report result JSON, will set Job UUID from entity data");
model.setJobUUID(report.getSecHubJobUUID());
}

} catch (JSONConverterException e) {
LOG.error("FATAL PROBLEM! Failed to create sechub result by model for job:{}", report.getSecHubJobUUID(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.File;
import java.util.List;
import java.util.UUID;

import org.junit.jupiter.api.Test;

Expand All @@ -18,6 +19,8 @@
import com.daimler.sechub.commons.model.TrafficLight;
import com.daimler.sechub.domain.scan.ScanDomainTestFileSupport;

import static org.mockito.Mockito.*;

class ScanSecHubReportTest {

@Test
Expand Down Expand Up @@ -115,6 +118,52 @@ void scanreport_result_by_report_model_does_not_recalculates_traffic_light_but_u
assertEquals(TrafficLight.GREEN, reportToTest.getTrafficLight());
}

@Test
void report_by_model_sets_jobUUID_from_scanreport_when_not_inside_model() {

/* prepare */
UUID uuid = UUID.randomUUID();

ScanReport report = mock(ScanReport.class);
when(report.getResultType()).thenReturn(ScanReportResultType.MODEL);
when(report.getSecHubJobUUID()).thenReturn(uuid);

SecHubReportModel model = new SecHubReportModel();

String jsonResult = model.toJSON();
when(report.getResult()).thenReturn(jsonResult);

/* execute */
ScanSecHubReport createdReport = new ScanSecHubReport(report);

/* test */
assertEquals(uuid, createdReport.getJobUUID());
}

@Test
void report_by_model_has_jobUUID_from_model_when_there_not_null() {

/* prepare */
UUID uuid1 = UUID.randomUUID();
UUID uuid2 = UUID.randomUUID();

ScanReport report = mock(ScanReport.class);
when(report.getResultType()).thenReturn(ScanReportResultType.MODEL);
when(report.getSecHubJobUUID()).thenReturn(uuid1);

SecHubReportModel model = new SecHubReportModel();
model.setJobUUID(uuid2);

String jsonResult = model.toJSON();
when(report.getResult()).thenReturn(jsonResult);

/* execute */
ScanSecHubReport createdReport = new ScanSecHubReport(report);

/* test */
assertEquals(uuid2, createdReport.getJobUUID());
}

@Test
void report_by_model_sets_version_to_version_from_model() {

Expand Down

0 comments on commit 5810a49

Please sign in to comment.