Skip to content

Commit

Permalink
Add test for same results with different message
Browse files Browse the repository at this point in the history
  • Loading branch information
jckoenen committed Dec 8, 2023
1 parent 0662825 commit e86d929
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.jetbrains.qodana.sarif

import com.jetbrains.qodana.sarif.baseline.BaselineCalculation
import com.jetbrains.qodana.sarif.model.Result.BaselineState
import com.jetbrains.qodana.sarif.model.SarifReport
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.function.Executable
import java.nio.file.Paths
import kotlin.streams.asStream

class BaselineFingerprintTest {
private fun read(path: String) = SarifUtil.readReport(Paths.get(path))

// The findings in both reports are the same except for the human-readable message
private fun readBaseline(fingerprintVersion: String) =
read("src/test/resources/testData/fingerprinting/before.sarif_$fingerprintVersion.json")

private fun readReport(fingerprintVersion: String) =
read("src/test/resources/testData/fingerprinting/after.sarif_$fingerprintVersion.json")

private fun test(
report: SarifReport,
baseline: SarifReport,
expect: Map<BaselineState, Int>,
) {
val res = BaselineCalculation.compare(report, baseline, BaselineCalculation.Options(true))

val byState = report.runs.orEmpty().asSequence()
.flatMap { it.results.orEmpty() }
.groupingBy { it.baselineState }
.eachCount()

sequenceOf(BaselineState.UNCHANGED, BaselineState.ABSENT, BaselineState.NEW)
.flatMap {
val inReport = byState[it] ?: 0
val inResult = when (it) {
BaselineState.NEW -> res.newResults
BaselineState.UNCHANGED -> res.unchangedResults
BaselineState.ABSENT -> res.absentResults
BaselineState.UPDATED -> error("Not in source")
}
val expected = expect[it] ?: 0
sequenceOf(
Executable { Assertions.assertEquals(expected, inReport, "[$it] in report") },
Executable { Assertions.assertEquals(expected, inResult, "[$it] in result") },
)
}
.asStream()
.let(Assertions::assertAll)
}

private val expectBaselineMismatch = mapOf(
BaselineState.UNCHANGED to 1,
BaselineState.ABSENT to 17,
BaselineState.NEW to 17,
)

private val expectBaselineMatch = mapOf(BaselineState.UNCHANGED to 18)

@Test
fun `both are v1`() {
// V1 fingerprint checks message => results are all absent / new
test(readReport("v1"), readBaseline("v1"), expectBaselineMismatch)
}

@Test
fun `both are v2`() {
// V2 fingerprint ignores message => all results are unchanged
test(readReport("v2"), readBaseline("v2"), expectBaselineMatch)
}

@Test
fun `backward compat -- report v1_v2 - baseline v1`() {
// we can only compare v1 because baseline doesn't have v2
test(readReport("v1_v2"), readBaseline("v1"), expectBaselineMismatch)
}

@Test
fun `forward compat -- report v1 - baseline v1_v2`() {
// we can only compare v1 because report doesn't have v2
test(readReport("v1"), readBaseline("v1_v2"), expectBaselineMismatch)
}

@Test
fun `v1_v2 to v1_v2`() {
// while V1 differs, V2 is the same, so expect match
test(readReport("v1_v2"), readBaseline("v1_v2"), expectBaselineMatch)
}
}
17 changes: 9 additions & 8 deletions sarif/src/test/java/com/jetbrains/qodana/sarif/BaselineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class BaselineTest {
private static final String QODANA_REPORT_JSON = "src/test/resources/testData/readWriteTest/qodanaReport.json";
private static final String QODANA_REPORT_JSON_2 = "src/test/resources/testData/readWriteTest/qodanaReport2.json";

private static final BaselineCalculation.Options INCLUDE_ABSENT = new BaselineCalculation.Options(true);
@Test
public void testSameReport() throws IOException {
SarifReport report = readReport();
Expand Down Expand Up @@ -59,7 +60,7 @@ public void testCompareWithOneAbsent() throws IOException {
Result newResult = new Result(new Message().withText("new result"));
baseline.getRuns().get(0).getResults().add(newResult);

doTest(report, baseline, problemsCount(report), 1, 0, new BaselineCalculation.Options(true));
doTest(report, baseline, problemsCount(report), 1, 0, INCLUDE_ABSENT);
assertEquals(Result.BaselineState.ABSENT, newResult.getBaselineState());
}

Expand All @@ -70,11 +71,11 @@ public void testCompareWithOneAbsentInBaseline() throws IOException {
Result newResult = new Result(new Message().withText("new result"));
baseline.getRuns().get(0).getResults().add(newResult);

BaselineCalculation.compare(report, baseline, new BaselineCalculation.Options(true));
BaselineCalculation.compare(report, baseline, INCLUDE_ABSENT);

SarifReport newReport = readReport();

doTest(newReport, report, problemsCount(newReport), 0, 0, new BaselineCalculation.Options(true));
doTest(newReport, report, problemsCount(newReport), 0, 0, INCLUDE_ABSENT);
}

@Test
Expand All @@ -86,11 +87,11 @@ public void testCompareWithOneAbsentInReport() throws IOException {
baseline.getRuns().get(0).getResults().add(newResult);


doTest(report, baseline, 0, problemsCount(baseline), 0, new BaselineCalculation.Options(true));
doTest(report, baseline, 0, problemsCount(baseline), 0, INCLUDE_ABSENT);

SarifReport newBaseline = SarifUtil.emptyReport(toolName);

doTest(report, newBaseline, 0, 0, 0, new BaselineCalculation.Options(true));
doTest(report, newBaseline, 0, 0, 0, INCLUDE_ABSENT);
}

@Test
Expand Down Expand Up @@ -170,7 +171,7 @@ public void testDifferentToolName() throws IOException {
SarifReport report = readReport();
SarifReport baseline = readReport(QODANA_REPORT_JSON_2);
int problemsCount = problemsCount(report);
doTest(report, baseline, problemsCount, 1, 0, new BaselineCalculation.Options(true));
doTest(report, baseline, problemsCount, 1, 0, INCLUDE_ABSENT);
//assertEquals(Result.BaselineState.NEW, newResult.getBaselineState());
}

Expand All @@ -179,7 +180,7 @@ public void testAbsentResultWithChangedIdAndSameVersion() throws IOException {
SarifReport report = readReport("src/test/resources/testData/AbsentBaselineTest/report.json");
SarifReport baseline = readReport("src/test/resources/testData/AbsentBaselineTest/baseline.json");

doTest(report, baseline, 1, 17, 18, new BaselineCalculation.Options(true));
doTest(report, baseline, 1, 17, 18, INCLUDE_ABSENT);

Set<String> knownDescriptorIds = RuleUtil.allRules(report)
.map(ReportingDescriptor::getId)
Expand All @@ -200,7 +201,7 @@ public void testAbsentResultWithChangedIdAndOldVersion() throws IOException {
SarifReport report = readReport("src/test/resources/testData/AbsentBaselineTest/report.json");
SarifReport baseline = readReport("src/test/resources/testData/AbsentBaselineTest/baseline_old.json");

doTest(report, baseline, 0, 18, 19, new BaselineCalculation.Options(true));
doTest(report, baseline, 0, 18, 19, INCLUDE_ABSENT);

Set<String> knownDescriptorIds = RuleUtil.allRules(report)
.map(ReportingDescriptor::getId)
Expand Down
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown

0 comments on commit e86d929

Please sign in to comment.