Skip to content

Commit 5933dfa

Browse files
committed
clean up redundant elements of test
1 parent 3c36768 commit 5933dfa

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

plugins/codemodder-plugin-appscan/src/test/java/io/codemodder/providers/sarif/appscan/AppScanModuleTest.java

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,22 @@
44
import static org.hamcrest.Matchers.is;
55
import static org.hamcrest.Matchers.notNullValue;
66

7-
import com.contrastsecurity.sarif.Region;
7+
import com.contrastsecurity.sarif.SarifSchema210;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
89
import com.google.inject.Guice;
910
import com.google.inject.Injector;
1011
import io.codemodder.*;
1112
import io.codemodder.codetf.CodeTFReference;
1213
import java.io.IOException;
13-
import java.nio.file.Files;
1414
import java.nio.file.Path;
15-
import java.nio.file.StandardOpenOption;
1615
import java.util.List;
16+
import java.util.Optional;
1717
import javax.inject.Inject;
18-
import org.junit.jupiter.api.BeforeEach;
1918
import org.junit.jupiter.api.Test;
2019
import org.junit.jupiter.api.io.TempDir;
2120

2221
final class AppScanModuleTest {
2322

24-
private Path repoDir;
25-
2623
@Codemod(
2724
id = "appscan-test:java/finds-stuff",
2825
importance = Importance.LOW,
@@ -56,30 +53,45 @@ public String getIndividualChangeDescription(Path filePath, CodemodChange change
5653
}
5754
}
5855

59-
@BeforeEach
60-
void setup(@TempDir final Path tmpDir) {
61-
AppScanRuleSarifFactory factory = new AppScanRuleSarifFactory();
62-
factory.build("appscan", "SA2813462719", null, null);
63-
this.repoDir = tmpDir;
64-
}
56+
private static final String emptySarif =
57+
"""
58+
{
59+
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json",
60+
"version": "2.1.0",
61+
"runs": [
62+
{
63+
"tool": {
64+
"driver": {
65+
"name": "HCL AppScan Static Analyzer"
66+
}
67+
},
68+
"artifacts": [
69+
{
70+
"location": {
71+
"uri": "file:///com/acme/MyVulnerableType.java"
72+
}
73+
}
74+
],
75+
"results": []
76+
}
77+
]
78+
}
79+
""";
6580

81+
/** This only tests that the module binds the rule sarif to the codemod. */
6682
@Test
67-
void it_works_with_appscan_sarif() throws IOException {
68-
String javaCode = "class Foo { \n Object a = new Thing(); \n }";
69-
70-
Path javaFile = Files.createTempFile(repoDir, "HasThing", ".java");
71-
Files.writeString(javaFile, javaCode, StandardOpenOption.TRUNCATE_EXISTING);
72-
AppScanModule module = createModule(List.of(AppScanSarifTestCodemod.class));
83+
void it_works_with_appscan_sarif(@TempDir final Path repoDir) throws IOException {
84+
SarifSchema210 rawSarif =
85+
new ObjectMapper().readValue(AppScanModuleTest.emptySarif, SarifSchema210.class);
86+
AppScanRuleSarifFactory ruleSarifFactory = new AppScanRuleSarifFactory();
87+
Optional<RuleSarif> ruleSarif =
88+
ruleSarifFactory.build("HCL AppScan Static Analyzer", "SA2813462719", rawSarif, repoDir);
89+
assertThat(ruleSarif.isPresent(), is(true));
90+
AppScanModule module =
91+
new AppScanModule(List.of(AppScanSarifTestCodemod.class), List.of(ruleSarif.get()));
7392
Injector injector = Guice.createInjector(module);
7493
AppScanSarifTestCodemod instance = injector.getInstance(AppScanSarifTestCodemod.class);
75-
76-
RuleSarif ruleSarif = instance.ruleSarif;
77-
assertThat(ruleSarif, is(notNullValue()));
78-
List<Region> regions = ruleSarif.getRegionsFromResultsByRule(javaFile);
79-
assertThat(regions.size(), is(1));
80-
}
81-
82-
private AppScanModule createModule(final List<Class<? extends CodeChanger>> codemodTypes) {
83-
return new AppScanModule(codemodTypes, List.of());
94+
assertThat(instance, is(notNullValue()));
95+
assertThat(instance.ruleSarif, is(notNullValue()));
8496
}
8597
}

0 commit comments

Comments
 (0)