Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
magaupp committed Oct 22, 2024
1 parent 41fcac0 commit fbb6b63
Show file tree
Hide file tree
Showing 4 changed files with 485 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

import de.tum.cit.aet.artemis.programming.dto.StaticCodeAnalysisReportDTO;
import de.tum.cit.aet.artemis.programming.service.localci.scaparser.ReportParserHelper;
import de.tum.cit.aet.artemis.programming.service.localci.scaparser.exception.ParserException;

Expand All @@ -27,6 +27,8 @@ class StaticCodeAnalysisParserUnitTest {

private static final Path REPORTS_FOLDER_PATH = Paths.get("src", "test", "resources", "test-data", "static-code-analysis", "reports");

private final ObjectMapper mapper = new ObjectMapper();

/**
* Compares the parsed JSON report with the expected JSON report
*
Expand All @@ -35,15 +37,13 @@ class StaticCodeAnalysisParserUnitTest {
* @throws ParserException If an exception occurs that is not already handled by the parser itself, e.g. caused by the json-parsing
*/
private void testParserWithFile(String toolGeneratedReportFileName, String expectedJSONReportFileName) throws ParserException, IOException {
File toolReport = REPORTS_FOLDER_PATH.resolve(toolGeneratedReportFileName).toFile();
File actualReportFile = REPORTS_FOLDER_PATH.resolve(toolGeneratedReportFileName).toFile();
File expectedReportFile = EXPECTED_FOLDER_PATH.resolve(expectedJSONReportFileName).toFile();

ReportParserHelper parser = new ReportParserHelper();
String actual = parser.transformToJSONReport(toolReport);
StaticCodeAnalysisReportDTO actualReport = ReportParserHelper.transformToReport(actualReportFile);
StaticCodeAnalysisReportDTO expectedReport = mapper.readValue(expectedReportFile, StaticCodeAnalysisReportDTO.class);

try (BufferedReader reader = Files.newBufferedReader(EXPECTED_FOLDER_PATH.resolve(expectedJSONReportFileName))) {
String expected = reader.lines().collect(Collectors.joining(System.lineSeparator()));
assertThat(actual).isEqualTo(expected);
}
assertThat(actualReport).isEqualTo(expectedReport);
}

private void testParserWithNullValue() throws ParserException {
Expand Down Expand Up @@ -91,6 +91,11 @@ void testSpotbugsParser() throws IOException {
}
}

@Test
void testRuffParser() throws IOException, ParserException {
testParserWithFile("ruff.sarif", "ruff.json");
}

@Test
void testParseInvalidFilename() {
assertThatCode(() -> testParserWithFile("cpd_invalid.txt", "invalid_filename.txt")).isInstanceOf(ParserException.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ public String transformToJSONReport(File file) throws ParserException {
* @param file Reference to the static code analysis report
* @return Static code analysis report represented as a plain Java object
*/
public StaticCodeAnalysisReportDTO transformToReport(File file) {
public static StaticCodeAnalysisReportDTO transformToReport(File file) {
if (file == null) {
throw new IllegalArgumentException("File must not be null");
}

// The static code analysis parser only supports xml files.
if (!FileUtils.getExtension(file).equals("xml")) {
throw new IllegalArgumentException("File must be xml format");
if (!FileUtils.getExtension(file).equals("xml") && !FileUtils.getExtension(file).equals("sarif")) {
throw new IllegalArgumentException("File must be xml or sarif format");
}
try {
// Reject any file larger than the given threshold
Expand Down
Loading

0 comments on commit fbb6b63

Please sign in to comment.