Skip to content

Commit

Permalink
Sonar: add java backend and frontend sonar configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalgrimaud committed Feb 12, 2022
1 parent 9f6ce9a commit d7915f0
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public SonarApplicationService(SonarService sonarService) {
public void addSonarJavaBackend(Project project) {
this.sonarService.addSonarJavaBackend(project);
}

public void addSonarJavaBackendAndFrontend(Project project) {
this.sonarService.addSonarJavaBackendAndFrontend(project);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public void addSonarJavaBackend(Project project) {
addDockerCompose(project);
}

@Override
public void addSonarJavaBackendAndFrontend(Project project) {
addPropertiesPlugin(project);
addSonarScannerPluginManagement(project);
addFullstackPropertiesFile(project);
addDockerCompose(project);
}

private void addPropertiesPlugin(Project project) {
Plugin plugin = Plugin
.builder()
Expand Down Expand Up @@ -73,6 +81,12 @@ private void addPropertiesFile(Project project) {
projectRepository.template(project, SOURCE, "sonar-project.properties");
}

private void addFullstackPropertiesFile(Project project) {
project.addDefaultConfig(BASE_NAME);
project.addDefaultConfig(PROJECT_NAME);
projectRepository.template(project, SOURCE, "sonar-fullstack-project.properties", "", "sonar-project.properties");
}

private void addDockerCompose(Project project) {
project.addDefaultConfig(BASE_NAME);
project.addConfig("sonarqubeDockerImage", Sonar.getSonarqubeDockerImage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

public interface SonarService {
void addSonarJavaBackend(Project project);
void addSonarJavaBackendAndFrontend(Project project);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ public SonarResource(SonarApplicationService sonarApplicationService) {
this.sonarApplicationService = sonarApplicationService;
}

@Operation(summary = "Add Sonar configuration to inspect code quality")
@ApiResponse(responseCode = "500", description = "An error occurred while adding Sonar configuration")
@Operation(summary = "Add Sonar configuration for Java Backend to inspect code quality")
@ApiResponse(responseCode = "500", description = "An error occurred while adding Sonar configuration for Java Backend")
@PostMapping("/java-backend")
@GeneratorStep(id = "sonar-java-backend")
public void addSonarJavaBackend(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
sonarApplicationService.addSonarJavaBackend(project);
}

@Operation(summary = "Add Sonar configuration for Java Backend and Frontend to inspect code quality")
@ApiResponse(responseCode = "500", description = "An error occurred while adding Sonar configuration for Java Backend and Frontend")
@PostMapping("/java-backend-and-frontend")
@GeneratorStep(id = "sonar-java-backend-and-frontend")
public void addSonarJavaBackendAndFrontend(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
sonarApplicationService.addSonarJavaBackendAndFrontend(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
sonar.projectKey={{baseName}}
sonar.projectName={{projectName}}

sonar.sources=src/main/
sonar.tests=src/test/
sonar.host.url=http://localhost:9001

sonar.test.inclusions=src/test/**/*.*, src/main/webapp/app/**/*.spec.ts
sonar.coverage.jacoco.xmlReportPaths=target/jacoco/jacoco.xml
sonar.java.codeCoveragePlugin=jacoco
sonar.junit.reportPaths=target/surefire-reports,target/failsafe-reports

sonar.testExecutionReportPaths=target/test-results/jest/TESTS-results-sonar.xml
sonar.javascript.lcov.reportPaths=target/test-results/lcov.info

sonar.sourceEncoding=UTF-8
sonar.exclusions=src/main/webapp/app/main.ts, src/main/webapp/content/**/*.*, src/main/webapp/i18n/*.js, target/classes/static/**/*.*

sonar.issue.ignore.multicriteria=S3437,S4502,S4684,S4032,UndocumentedApi

# Rule https://rules.sonarsource.com/java/RSPEC-3437 is ignored, as a JPA-managed field cannot be transient
sonar.issue.ignore.multicriteria.S3437.resourceKey=src/main/java/**/*
sonar.issue.ignore.multicriteria.S3437.ruleKey=squid:S3437

# Rule https://rules.sonarsource.com/java/RSPEC-1176 is ignored, as we want to follow "clean code" guidelines and classes, methods and arguments names should be self-explanatory
sonar.issue.ignore.multicriteria.UndocumentedApi.resourceKey=src/main/java/**/*
sonar.issue.ignore.multicriteria.UndocumentedApi.ruleKey=squid:UndocumentedApi

# Rule https://rules.sonarsource.com/java/RSPEC-4502 is ignored, as for JWT tokens we are not subject to CSRF attack
sonar.issue.ignore.multicriteria.S4502.resourceKey=src/main/java/**/*
sonar.issue.ignore.multicriteria.S4502.ruleKey=java:S4502

# Rule https://rules.sonarsource.com/java/RSPEC-4684
sonar.issue.ignore.multicriteria.S4684.resourceKey=src/main/java/**/*
sonar.issue.ignore.multicriteria.S4684.ruleKey=java:S4684

# Rule: Packages containing only "package-info.java" should be removed
sonar.issue.ignore.multicriteria.S4032.resourceKey=src/main/java/**/*
sonar.issue.ignore.multicriteria.S4032.ruleKey=java:S4032
Original file line number Diff line number Diff line change
Expand Up @@ -31,50 +31,20 @@ void shouldAddSonarJavaBackend() {

sonarApplicationService.addSonarJavaBackend(project);

assertFileExist(project, "src/main/docker/sonar.yml");
assertFileExist(project, "sonar-project.properties");

assertFileContent(project, POM_XML, "<properties-maven-plugin.version>");
assertFileContent(project, POM_XML, "</properties-maven-plugin.version>");

assertFileContent(project, POM_XML, "<sonar-maven-plugin.version>");
assertFileContent(project, POM_XML, "</sonar-maven-plugin.version>");

assertFileContent(project, POM_XML, sonarSourcePlugin());

assertFileContent(project, POM_XML, propertiesPlugin());
SonarAssert.assertFiles(project);
SonarAssert.assertPomXml(project);
}

private List<String> propertiesPlugin() {
return List.of(
"<plugin>",
"<groupId>org.codehaus.mojo</groupId>",
"<artifactId>properties-maven-plugin</artifactId>",
"<version>${properties-maven-plugin.version}</version>",
"<executions>",
"<execution>",
"<phase>initialize</phase>",
"<goals>",
"<goal>read-project-properties</goal>",
"</goals>",
"<configuration>",
"<files>",
"<file>sonar-project.properties</file>",
"</files>",
"</configuration>",
"</execution>",
"</executions>",
"</plugin>"
);
}
@Test
void shouldAddSonarJavaBackendAndFrontend() {
Project project = tmpProject();
initApplicationService.init(project);
mavenApplicationService.addPomXml(project);

sonarApplicationService.addSonarJavaBackendAndFrontend(project);

private List<String> sonarSourcePlugin() {
return List.of(
"<plugin>",
"<groupId>org.sonarsource.scanner.maven</groupId>",
"<artifactId>sonar-maven-plugin</artifactId>",
"<version>${sonar-maven-plugin.version}</version>",
"</plugin>"
);
SonarAssert.assertFiles(project);
SonarAssert.assertFrontProperties(project);
SonarAssert.assertPomXml(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package tech.jhipster.lite.generator.server.sonar.application;

import static tech.jhipster.lite.TestUtils.assertFileContent;
import static tech.jhipster.lite.TestUtils.assertFileExist;
import static tech.jhipster.lite.generator.project.domain.Constants.POM_XML;

import java.util.List;
import tech.jhipster.lite.generator.project.domain.Project;

public class SonarAssert {

private SonarAssert() {}

public static void assertFiles(Project project) {
assertFileExist(project, "src/main/docker/sonar.yml");
assertFileExist(project, "sonar-project.properties");
}

public static void assertFrontProperties(Project project) {
assertFileContent(project, "sonar-project.properties", "sonar.testExecutionReportPaths");
assertFileContent(project, "sonar-project.properties", "sonar.javascript.lcov.reportPaths");
}

public static void assertPomXml(Project project) {
assertFileContent(project, POM_XML, "<properties-maven-plugin.version>");
assertFileContent(project, POM_XML, "</properties-maven-plugin.version>");
assertFileContent(project, POM_XML, "<sonar-maven-plugin.version>");
assertFileContent(project, POM_XML, "</sonar-maven-plugin.version>");

assertFileContent(project, POM_XML, sonarSourcePlugin());
assertFileContent(project, POM_XML, propertiesPlugin());
}

private static List<String> sonarSourcePlugin() {
return List.of(
"<plugin>",
"<groupId>org.sonarsource.scanner.maven</groupId>",
"<artifactId>sonar-maven-plugin</artifactId>",
"<version>${sonar-maven-plugin.version}</version>",
"</plugin>"
);
}

private static List<String> propertiesPlugin() {
return List.of(
"<plugin>",
"<groupId>org.codehaus.mojo</groupId>",
"<artifactId>properties-maven-plugin</artifactId>",
"<version>${properties-maven-plugin.version}</version>",
"<executions>",
"<execution>",
"<phase>initialize</phase>",
"<goals>",
"<goal>read-project-properties</goal>",
"</goals>",
"<configuration>",
"<files>",
"<file>sonar-project.properties</file>",
"</files>",
"</configuration>",
"</execution>",
"</executions>",
"</plugin>"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.generator.server.sonar.application.SonarApplicationService;
import tech.jhipster.lite.generator.server.sonar.application.SonarAssert;

@IntegrationTest
@AutoConfigureMockMvc
Expand All @@ -28,9 +29,6 @@ class SonarResourceIT {
@Autowired
MavenApplicationService mavenApplicationService;

@Autowired
SonarApplicationService sonarApplicationService;

@Autowired
MockMvc mockMvc;

Expand All @@ -40,7 +38,6 @@ void shouldAddSonarJavaBackend() throws Exception {
Project project = ProjectDTO.toProject(projectDTO);
initApplicationService.init(project);
mavenApplicationService.init(project);
sonarApplicationService.addSonarJavaBackend(project);

mockMvc
.perform(
Expand All @@ -50,9 +47,27 @@ void shouldAddSonarJavaBackend() throws Exception {
)
.andExpect(status().isOk());

String projectPath = projectDTO.getFolder();
SonarAssert.assertFiles(project);
SonarAssert.assertPomXml(project);
}

@Test
void shouldAddSonarJavaBackendAndFrontend() throws Exception {
ProjectDTO projectDTO = TestUtils.readFileToObject("json/chips.json", ProjectDTO.class).folder(FileUtils.tmpDirForTest());
Project project = ProjectDTO.toProject(projectDTO);
initApplicationService.init(project);
mavenApplicationService.init(project);

mockMvc
.perform(
post("/api/servers/sonar/java-backend-and-frontend")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtils.convertObjectToJsonBytes(projectDTO))
)
.andExpect(status().isOk());

assertFileExist(projectPath, "sonar-project.properties");
assertFileExist(projectPath, "src/main/docker/sonar.yml");
SonarAssert.assertFiles(project);
SonarAssert.assertFrontProperties(project);
SonarAssert.assertPomXml(project);
}
}

0 comments on commit d7915f0

Please sign in to comment.