Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magaupp committed Oct 22, 2024
1 parent fbb6b63 commit 4644cdb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 157 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.tum.cit.aet.artemis.programming.service.localci.scaparser;

import de.tum.cit.aet.artemis.programming.dto.StaticCodeAnalysisReportDTO;
import de.tum.cit.aet.artemis.programming.service.localci.scaparser.exception.UnsupportedToolException;
import de.tum.cit.aet.artemis.programming.service.localci.scaparser.strategy.ParserPolicy;
import de.tum.cit.aet.artemis.programming.service.localci.scaparser.strategy.ParserStrategy;

Expand All @@ -9,6 +10,13 @@
*/
public class ReportParser {

/**
* Builds the document using the provided string and parses it to a Report object.
*
* @param reportContent String containing the static code analysis report
* @return Report containing the static code analysis issues
* @throws UnsupportedToolException if the static code analysis tool which created the report is not supported
*/
public static StaticCodeAnalysisReportDTO getReport(String reportContent, String fileName) {
ParserPolicy parserPolicy = new ParserPolicy();
ParserStrategy parserStrategy = parserPolicy.configure(fileName);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand All @@ -15,8 +14,8 @@
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;
import de.tum.cit.aet.artemis.programming.service.localci.scaparser.ReportParser;
import de.tum.cit.aet.artemis.programming.service.localci.scaparser.exception.UnsupportedToolException;

/**
* Tests each parser with an example file
Expand All @@ -29,95 +28,64 @@ class StaticCodeAnalysisParserUnitTest {

private final ObjectMapper mapper = new ObjectMapper();

private void testParserWithFile(String toolGeneratedReportFileName, String expectedJSONReportFileName) throws IOException {
testParserWithFileNamed(toolGeneratedReportFileName, toolGeneratedReportFileName, expectedJSONReportFileName);
}

/**
* Compares the parsed JSON report with the expected JSON report
*
* @param toolGeneratedReportFileName The name of the file contains the report as generated by the different tools
* @param expectedJSONReportFileName The name of the file that contains the parsed report
* @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 actualReportFile = REPORTS_FOLDER_PATH.resolve(toolGeneratedReportFileName).toFile();
File expectedReportFile = EXPECTED_FOLDER_PATH.resolve(expectedJSONReportFileName).toFile();
private void testParserWithFileNamed(String toolGeneratedReportFileName, String fileName, String expectedJSONReportFileName) throws IOException {
Path actualReportPath = REPORTS_FOLDER_PATH.resolve(toolGeneratedReportFileName);
File expectedJSONReportFile = EXPECTED_FOLDER_PATH.resolve(expectedJSONReportFileName).toFile();

StaticCodeAnalysisReportDTO actualReport = ReportParserHelper.transformToReport(actualReportFile);
StaticCodeAnalysisReportDTO expectedReport = mapper.readValue(expectedReportFile, StaticCodeAnalysisReportDTO.class);

assertThat(actualReport).isEqualTo(expectedReport);
String actualReportContent = Files.readString(actualReportPath);
testParserWithContent(actualReportContent, fileName, expectedJSONReportFile);
}

private void testParserWithNullValue() throws ParserException {
ReportParserHelper parser = new ReportParserHelper();
parser.transformToJSONReport(null);
private void testParserWithContent(String actualReportContent, String actualReportFilename, File expectedJSONReportFile) throws IOException {
StaticCodeAnalysisReportDTO actualReport = ReportParser.getReport(actualReportContent, actualReportFilename);

StaticCodeAnalysisReportDTO expectedReport = mapper.readValue(expectedJSONReportFile, StaticCodeAnalysisReportDTO.class);

assertThat(actualReport).isEqualTo(expectedReport);
}

@Test
void testCheckstyleParser() throws IOException {
try {
testParserWithFile("checkstyle-result.xml", "checkstyle.txt");
}
catch (ParserException e) {
fail("Checkstyle parser failed with exception: " + e.getMessage());
}
testParserWithFile("checkstyle-result.xml", "checkstyle.txt");
}

@Test
void testPMDCPDParser() throws IOException {
try {
testParserWithFile("cpd.xml", "pmd_cpd.txt");
}
catch (ParserException e) {
fail("PMD-CPD parser failed with exception: " + e.getMessage());
}
testParserWithFile("cpd.xml", "pmd_cpd.txt");
}

@Test
void testPMDParser() throws IOException {
try {
testParserWithFile("pmd.xml", "pmd.txt");
}
catch (ParserException e) {
fail("PMD parser failed with exception: " + e.getMessage());
}
testParserWithFile("pmd.xml", "pmd.txt");
}

@Test
void testSpotbugsParser() throws IOException {
try {
testParserWithFile("spotbugsXml.xml", "spotbugs.txt");
}
catch (ParserException e) {
fail("Spotbugs parser failed with exception: " + e.getMessage());
}
testParserWithFile("spotbugsXml.xml", "spotbugs.txt");
}

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

@Test
void testParseInvalidFilename() {
assertThatCode(() -> testParserWithFile("cpd_invalid.txt", "invalid_filename.txt")).isInstanceOf(ParserException.class);
}

@Test
void testParseInvalidXML() throws Exception {
testParserWithFile("invalid_xml.xml", "invalid_xml.txt");
void testParseInvalidXML() throws IOException {
assertThatCode(() -> testParserWithFileNamed("invalid_xml.xml", "pmd.xml", "invalid_xml.txt")).isInstanceOf(RuntimeException.class);
}

@Test
void testInvalidName() throws IOException {
try {
testParserWithFile("invalid_name.xml", "invalid_name.txt");
}
catch (ParserException e) {
fail("Parser failed with exception: " + e.getMessage());
}
}

@Test
void testThrowsParserException() {
assertThatExceptionOfType(ParserException.class).isThrownBy(this::testParserWithNullValue);
assertThatCode(() -> testParserWithFile("invalid_name.xml", "invalid_name.txt")).isInstanceOf(UnsupportedToolException.class);
}
}

This file was deleted.

0 comments on commit 4644cdb

Please sign in to comment.