From cad10d47adbc276fcfa9293feaa2ddcc52138067 Mon Sep 17 00:00:00 2001 From: Colin DAMON Date: Sun, 10 Jul 2022 16:16:54 +0200 Subject: [PATCH] Migrate jacoco coverrage to module --- .../project/domain/GeneratorAction.java | 2 - .../application/JacocoApplicationService.java | 15 ++- .../jacoco/domain/JacocoDomainService.java | 25 ---- .../javatool/jacoco/domain/JacocoService.java | 7 - .../domain/JacocoThresholdModuleFactory.java | 54 ++++++++ .../config/JacocoBeanConfiguration.java | 22 ---- .../JacocoThresholdModuleConfiguration.java | 22 ++++ .../primary/rest/JacocoResource.java | 35 ----- .../lite/module/domain/JHipsterModule.java | 5 + .../replacement/RegexNeedleAfterReplacer.java | 42 ++++++ .../RegexNeedleBeforeReplacer.java | 2 +- .../jacoco/jacoco-check-coverage.patch | 37 ------ src/test/features/aop-logging.feature | 2 +- src/test/features/jacoco-threshold.feature | 5 + .../jhipster/lite/generator/ModulesSteps.java | 11 +- .../JacocoApplicationServiceIT.java | 30 ----- .../domain/JacocoDomainServiceTest.java | 38 ------ .../JacocoThresholdModuleFactoryTest.java | 120 ++++++++++++++++++ .../config/JacocoBeanConfigurationIT.java | 21 --- .../primary/rest/JacocoResourceIT.java | 45 ------- .../RegexNeedleAfterReplacerTest.java | 92 ++++++++++++++ 21 files changed, 358 insertions(+), 274 deletions(-) delete mode 100644 src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoDomainService.java delete mode 100644 src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoService.java create mode 100644 src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoThresholdModuleFactory.java delete mode 100644 src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/config/JacocoBeanConfiguration.java create mode 100644 src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/primary/JacocoThresholdModuleConfiguration.java delete mode 100644 src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/primary/rest/JacocoResource.java create mode 100644 src/main/java/tech/jhipster/lite/module/domain/replacement/RegexNeedleAfterReplacer.java delete mode 100644 src/main/resources/generator/server/javatool/jacoco/jacoco-check-coverage.patch create mode 100644 src/test/features/jacoco-threshold.feature delete mode 100644 src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/application/JacocoApplicationServiceIT.java delete mode 100644 src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoDomainServiceTest.java create mode 100644 src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoThresholdModuleFactoryTest.java delete mode 100644 src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/config/JacocoBeanConfigurationIT.java delete mode 100644 src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/primary/rest/JacocoResourceIT.java create mode 100644 src/test/java/tech/jhipster/lite/module/domain/replacement/RegexNeedleAfterReplacerTest.java diff --git a/src/main/java/tech/jhipster/lite/generator/project/domain/GeneratorAction.java b/src/main/java/tech/jhipster/lite/generator/project/domain/GeneratorAction.java index 2e68548b1aa..cdedcef768e 100644 --- a/src/main/java/tech/jhipster/lite/generator/project/domain/GeneratorAction.java +++ b/src/main/java/tech/jhipster/lite/generator/project/domain/GeneratorAction.java @@ -58,8 +58,6 @@ private GeneratorAction() {} public static final String SPRINGBOOT_UNDERTOW = "springboot-undertow"; public static final String SPRINGBOOT_ACTUATOR = "springboot-actuator"; - public static final String JACOCO_CHECK_MIN_COVERAGE = "jacoco-check-min-coverage"; - public static final String REACT_CYPRESS = "react-cypress"; public static final String SPRINGBOOT_KAFKA = "springboot-kafka"; diff --git a/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/application/JacocoApplicationService.java b/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/application/JacocoApplicationService.java index 0724a2a1b24..60db2d995fa 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/application/JacocoApplicationService.java +++ b/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/application/JacocoApplicationService.java @@ -1,19 +1,20 @@ package tech.jhipster.lite.generator.server.javatool.jacoco.application; import org.springframework.stereotype.Component; -import tech.jhipster.lite.generator.project.domain.Project; -import tech.jhipster.lite.generator.server.javatool.jacoco.domain.JacocoService; +import tech.jhipster.lite.generator.server.javatool.jacoco.domain.JacocoThresholdModuleFactory; +import tech.jhipster.lite.module.domain.JHipsterModule; +import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; @Component public class JacocoApplicationService { - private final JacocoService jacocoService; + private final JacocoThresholdModuleFactory factory; - public JacocoApplicationService(JacocoService jacocoService) { - this.jacocoService = jacocoService; + public JacocoApplicationService() { + factory = new JacocoThresholdModuleFactory(); } - public void addCheckMinimumCoverage(Project project) { - jacocoService.addCheckMinimumCoverage(project); + public JHipsterModule buildModule(JHipsterModuleProperties properties) { + return factory.buildModule(properties); } } diff --git a/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoDomainService.java b/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoDomainService.java deleted file mode 100644 index c9b11427664..00000000000 --- a/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoDomainService.java +++ /dev/null @@ -1,25 +0,0 @@ -package tech.jhipster.lite.generator.server.javatool.jacoco.domain; - -import tech.jhipster.lite.common.domain.FileUtils; -import tech.jhipster.lite.generator.project.domain.Project; -import tech.jhipster.lite.generator.project.domain.ProjectFile; -import tech.jhipster.lite.generator.project.domain.ProjectRepository; - -public class JacocoDomainService implements JacocoService { - - public static final String SOURCE = "server/javatool/jacoco"; - public static final String PATCH = "jacoco-check-coverage.patch"; - - private final ProjectRepository projectRepository; - - public JacocoDomainService(ProjectRepository projectRepository) { - this.projectRepository = projectRepository; - } - - @Override - public void addCheckMinimumCoverage(Project project) { - projectRepository.gitInit(project); - projectRepository.add(ProjectFile.forProject(project).withSource(SOURCE, PATCH).withDestinationFolder(".jhipster")); - projectRepository.gitApplyPatch(project, FileUtils.getPath(project.getFolder(), ".jhipster", PATCH)); - } -} diff --git a/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoService.java b/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoService.java deleted file mode 100644 index a048f1bb135..00000000000 --- a/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoService.java +++ /dev/null @@ -1,7 +0,0 @@ -package tech.jhipster.lite.generator.server.javatool.jacoco.domain; - -import tech.jhipster.lite.generator.project.domain.Project; - -public interface JacocoService { - void addCheckMinimumCoverage(Project project); -} diff --git a/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoThresholdModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoThresholdModuleFactory.java new file mode 100644 index 00000000000..5500a67d3de --- /dev/null +++ b/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoThresholdModuleFactory.java @@ -0,0 +1,54 @@ +package tech.jhipster.lite.generator.server.javatool.jacoco.domain; + +import static tech.jhipster.lite.module.domain.JHipsterModule.*; + +import tech.jhipster.lite.error.domain.Assert; +import tech.jhipster.lite.module.domain.JHipsterModule; +import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; + +public class JacocoThresholdModuleFactory { + + public JHipsterModule buildModule(JHipsterModuleProperties properties) { + Assert.notNull("properties", properties); + + return moduleBuilder(properties) + .mandatoryReplacements() + .in("pom.xml") + .add( + lineAfterRegex( + "target\\/jacoco\\/<\\/outputDirectory>[\n\r]*\\s*<\\/configuration>[\n\r]*\\s*<\\/execution>\\s*$" + ), + """ + + check + + check + + + target/jacoco/allTest.exec + + + CLASS + + + LINE + COVEREDRATIO + 1.00 + + + BRANCH + COVEREDRATIO + 1.00 + + + + + + \ + """ + ) + .and() + .and() + .build(); + } +} diff --git a/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/config/JacocoBeanConfiguration.java b/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/config/JacocoBeanConfiguration.java deleted file mode 100644 index 864a964a07f..00000000000 --- a/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/config/JacocoBeanConfiguration.java +++ /dev/null @@ -1,22 +0,0 @@ -package tech.jhipster.lite.generator.server.javatool.jacoco.infrastructure.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import tech.jhipster.lite.generator.project.domain.ProjectRepository; -import tech.jhipster.lite.generator.server.javatool.jacoco.domain.JacocoDomainService; -import tech.jhipster.lite.generator.server.javatool.jacoco.domain.JacocoService; - -@Configuration -public class JacocoBeanConfiguration { - - private final ProjectRepository projectRepository; - - public JacocoBeanConfiguration(ProjectRepository projectRepository) { - this.projectRepository = projectRepository; - } - - @Bean - public JacocoService jacocoService() { - return new JacocoDomainService(projectRepository); - } -} diff --git a/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/primary/JacocoThresholdModuleConfiguration.java b/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/primary/JacocoThresholdModuleConfiguration.java new file mode 100644 index 00000000000..d1b9d8723bb --- /dev/null +++ b/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/primary/JacocoThresholdModuleConfiguration.java @@ -0,0 +1,22 @@ +package tech.jhipster.lite.generator.server.javatool.jacoco.infrastructure.primary; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import tech.jhipster.lite.generator.server.javatool.jacoco.application.JacocoApplicationService; +import tech.jhipster.lite.module.infrastructure.primary.JHipsterModuleApiDoc; +import tech.jhipster.lite.module.infrastructure.primary.JHipsterModuleResource; + +@Configuration +class JacocoThresholdModuleConfiguration { + + @Bean + JHipsterModuleResource jacocoModuleThreshold(JacocoApplicationService jacoco) { + return JHipsterModuleResource + .builder() + .legacyUrl("/api/servers/java/jacoco-minimum-coverage") + .slug("jacoco-check-min-coverage") + .withoutProperties() + .apiDoc(new JHipsterModuleApiDoc("Java", "Add JaCoCo configuration to check minimum coverage")) + .factory(jacoco::buildModule); + } +} diff --git a/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/primary/rest/JacocoResource.java b/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/primary/rest/JacocoResource.java deleted file mode 100644 index 70c3ba05a6c..00000000000 --- a/src/main/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/primary/rest/JacocoResource.java +++ /dev/null @@ -1,35 +0,0 @@ -package tech.jhipster.lite.generator.server.javatool.jacoco.infrastructure.primary.rest; - -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.responses.ApiResponse; -import io.swagger.v3.oas.annotations.tags.Tag; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import tech.jhipster.lite.generator.project.domain.GeneratorAction; -import tech.jhipster.lite.generator.project.domain.Project; -import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO; -import tech.jhipster.lite.generator.server.javatool.jacoco.application.JacocoApplicationService; -import tech.jhipster.lite.technical.infrastructure.primary.annotation.GeneratorStep; - -@RestController -@RequestMapping("/api/servers/java/jacoco-minimum-coverage") -@Tag(name = "Java") -class JacocoResource { - - private final JacocoApplicationService jacocoApplicationService; - - public JacocoResource(JacocoApplicationService jacocoApplicationService) { - this.jacocoApplicationService = jacocoApplicationService; - } - - @Operation(summary = "Add JaCoCo configuration to check minimum coverage") - @ApiResponse(responseCode = "500", description = "An error occurred while adding JaCoco configuration") - @PostMapping - @GeneratorStep(id = GeneratorAction.JACOCO_CHECK_MIN_COVERAGE) - public void addCheckMinimumCoverage(@RequestBody ProjectDTO projectDTO) { - Project project = ProjectDTO.toProject(projectDTO); - jacocoApplicationService.addCheckMinimumCoverage(project); - } -} diff --git a/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java b/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java index 04bec86c3c0..d4e3aacda07 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java +++ b/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java @@ -47,6 +47,7 @@ import tech.jhipster.lite.module.domain.replacement.JHipsterModuleMandatoryReplacements.JHipsterModuleMandatoryReplacementsBuilder; import tech.jhipster.lite.module.domain.replacement.JHipsterModuleOptionalReplacements; import tech.jhipster.lite.module.domain.replacement.JHipsterModuleOptionalReplacements.JHipsterModuleOptionalReplacementsBuilder; +import tech.jhipster.lite.module.domain.replacement.RegexNeedleAfterReplacer; import tech.jhipster.lite.module.domain.replacement.RegexNeedleBeforeReplacer; import tech.jhipster.lite.module.domain.replacement.RegexReplacer; import tech.jhipster.lite.module.domain.replacement.TextNeedleBeforeReplacer; @@ -175,6 +176,10 @@ public static RegexNeedleBeforeReplacer lineBeforeRegex(String regex) { return new RegexNeedleBeforeReplacer(Pattern.compile(regex, Pattern.MULTILINE)); } + public static RegexNeedleAfterReplacer lineAfterRegex(String regex) { + return new RegexNeedleAfterReplacer(Pattern.compile(regex, Pattern.MULTILINE)); + } + public static PropertyKey propertyKey(String key) { return new PropertyKey(key); } diff --git a/src/main/java/tech/jhipster/lite/module/domain/replacement/RegexNeedleAfterReplacer.java b/src/main/java/tech/jhipster/lite/module/domain/replacement/RegexNeedleAfterReplacer.java new file mode 100644 index 00000000000..54f3b351a3a --- /dev/null +++ b/src/main/java/tech/jhipster/lite/module/domain/replacement/RegexNeedleAfterReplacer.java @@ -0,0 +1,42 @@ +package tech.jhipster.lite.module.domain.replacement; + +import java.util.function.BiFunction; +import java.util.regex.Pattern; +import tech.jhipster.lite.error.domain.Assert; +import tech.jhipster.lite.module.domain.JHipsterModule; + +public record RegexNeedleAfterReplacer(Pattern pattern) implements ElementReplacer { + public RegexNeedleAfterReplacer { + Assert.notNull("pattern", pattern); + } + + @Override + public boolean notMatchIn(String content) { + return !pattern().matcher(content).find(); + } + + @Override + public BiFunction replacer() { + return (content, replacement) -> + linePattern().matcher(content).replaceAll(result -> result.group() + JHipsterModule.LINE_BREAK + replacement); + } + + private Pattern linePattern() { + String stringPattern = searchMatcher(); + + if (isLinePattern(stringPattern)) { + return pattern(); + } + + return Pattern.compile(stringPattern + ".*$", pattern().flags()); + } + + private boolean isLinePattern(String stringPattern) { + return stringPattern.endsWith("$"); + } + + @Override + public String searchMatcher() { + return pattern().pattern(); + } +} diff --git a/src/main/java/tech/jhipster/lite/module/domain/replacement/RegexNeedleBeforeReplacer.java b/src/main/java/tech/jhipster/lite/module/domain/replacement/RegexNeedleBeforeReplacer.java index 603075828f6..ac1a8234ee1 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/replacement/RegexNeedleBeforeReplacer.java +++ b/src/main/java/tech/jhipster/lite/module/domain/replacement/RegexNeedleBeforeReplacer.java @@ -28,7 +28,7 @@ private Pattern linePattern() { return pattern(); } - return Pattern.compile("^.*" + stringPattern, Pattern.MULTILINE); + return Pattern.compile("^.*" + stringPattern, pattern().flags()); } private boolean isLinePattern(String stringPattern) { diff --git a/src/main/resources/generator/server/javatool/jacoco/jacoco-check-coverage.patch b/src/main/resources/generator/server/javatool/jacoco/jacoco-check-coverage.patch deleted file mode 100644 index eabc180a7dd..00000000000 --- a/src/main/resources/generator/server/javatool/jacoco/jacoco-check-coverage.patch +++ /dev/null @@ -1,37 +0,0 @@ -diff --git a/pom.xml b/pom.xml -index c8d36a9..3a6a27d 100644 ---- a/pom.xml -+++ b/pom.xml -@@ -307,6 +307,32 @@ - target/jacoco/ - - -+ -+ check -+ -+ check -+ -+ -+ target/jacoco/allTest.exec -+ -+ -+ CLASS -+ -+ -+ LINE -+ COVEREDRATIO -+ 1.00 -+ -+ -+ BRANCH -+ COVEREDRATIO -+ 1.00 -+ -+ -+ -+ -+ -+ - - - diff --git a/src/test/features/aop-logging.feature b/src/test/features/aop-logging.feature index f5bbeaad8d7..41620901cf5 100644 --- a/src/test/features/aop-logging.feature +++ b/src/test/features/aop-logging.feature @@ -1,7 +1,7 @@ Feature: AOP logging Scenario: Should apply AOP logging module - When I apply "aop-logging" module to default project with maven file + When I apply "aop-logging" module to default project with maven file | packageName | tech.jhipster.chips | | baseName | jhipster | Then I should have files in "src/main/java/tech/jhipster/chips/technical/infrastructure/secondary/aop/logging" diff --git a/src/test/features/jacoco-threshold.feature b/src/test/features/jacoco-threshold.feature new file mode 100644 index 00000000000..f1be6b36e26 --- /dev/null +++ b/src/test/features/jacoco-threshold.feature @@ -0,0 +1,5 @@ +Feature: Jacoco threshold module + + Scenario: Should apply jacoco threshol module + When I apply "jacoco-check-min-coverage" module to default project with maven file without properties + Then I should have "COVEREDRATIO" in "pom.xml" diff --git a/src/test/java/tech/jhipster/lite/generator/ModulesSteps.java b/src/test/java/tech/jhipster/lite/generator/ModulesSteps.java index 8820fef0792..fe76d11aa8f 100644 --- a/src/test/java/tech/jhipster/lite/generator/ModulesSteps.java +++ b/src/test/java/tech/jhipster/lite/generator/ModulesSteps.java @@ -83,6 +83,11 @@ public void applyModuleForLastProject(String moduleSlug) { post(applyModuleUrl(moduleSlug), buildModuleQuery(lastProjectFolder(), null)); } + @When("I apply {string} module to default project with maven file without properties") + public void applyModuleForDefaultProjectWithMavenFileWithoutProperties(String moduleSlug) { + applyModuleForDefaultProjectWithMavenFile(moduleSlug, null); + } + @When("I apply {string} module to default project with maven file") public void applyModuleForDefaultProjectWithMavenFile(String moduleSlug, Map properties) { String projectFolder = newTestFolder(); @@ -151,7 +156,7 @@ private static void addPackageJsonToProject(String folder) { } private static void addPomToProject(String folder) { - addFileToProject(folder, "src/test/resources/projects/maven/pom.xml", "pom.xml"); + addFileToProject(folder, "src/test/resources/projects/init-maven/pom.xml", "pom.xml"); } private static void addFileToProject(String folder, String source, String destination) { @@ -162,9 +167,9 @@ private static void addFileToProject(String folder, String source, String destin throw new AssertionError(e); } - Path pomPath = folderPath.resolve(destination); + Path filePath = folderPath.resolve(destination); try { - Files.copy(Paths.get(source), pomPath); + Files.copy(Paths.get(source), filePath); } catch (IOException e) { throw new AssertionError(e); } diff --git a/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/application/JacocoApplicationServiceIT.java b/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/application/JacocoApplicationServiceIT.java deleted file mode 100644 index 9aa3d70da46..00000000000 --- a/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/application/JacocoApplicationServiceIT.java +++ /dev/null @@ -1,30 +0,0 @@ -package tech.jhipster.lite.generator.server.javatool.jacoco.application; - -import static tech.jhipster.lite.generator.project.domain.Constants.POM_XML; - -import java.util.List; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import tech.jhipster.lite.IntegrationTest; -import tech.jhipster.lite.TestUtils; -import tech.jhipster.lite.generator.project.domain.Project; - -@IntegrationTest -class JacocoApplicationServiceIT { - - @Autowired - JacocoApplicationService jacocoApplicationService; - - @Test - void shouldAddCheckMinimumCoverage() { - Project project = TestUtils.tmpProjectWithPomXml(); - - jacocoApplicationService.addCheckMinimumCoverage(project); - - TestUtils.assertFileContent( - project, - POM_XML, - List.of("LINE", "COVEREDRATIO", "1.00") - ); - } -} diff --git a/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoDomainServiceTest.java b/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoDomainServiceTest.java deleted file mode 100644 index a43a5cf06fe..00000000000 --- a/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoDomainServiceTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package tech.jhipster.lite.generator.server.javatool.jacoco.domain; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.verify; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; -import tech.jhipster.lite.TestUtils; -import tech.jhipster.lite.UnitTest; -import tech.jhipster.lite.generator.project.domain.Project; -import tech.jhipster.lite.generator.project.domain.ProjectFile; -import tech.jhipster.lite.generator.project.domain.ProjectRepository; - -@UnitTest -@ExtendWith(MockitoExtension.class) -class JacocoDomainServiceTest { - - @Mock - ProjectRepository projectRepository; - - @InjectMocks - JacocoDomainService jacocoDomainService; - - @Test - void shouldAddCheckMinimumCoverage() { - Project project = TestUtils.tmpProjectWithPomXml(); - - jacocoDomainService.addCheckMinimumCoverage(project); - - verify(projectRepository).gitInit(any(Project.class)); - verify(projectRepository).add(any(ProjectFile.class)); - verify(projectRepository).gitApplyPatch(any(Project.class), anyString()); - } -} diff --git a/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoThresholdModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoThresholdModuleFactoryTest.java new file mode 100644 index 00000000000..237c7698c2f --- /dev/null +++ b/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/domain/JacocoThresholdModuleFactoryTest.java @@ -0,0 +1,120 @@ +package tech.jhipster.lite.generator.server.javatool.jacoco.domain; + +import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.*; + +import org.junit.jupiter.api.Test; +import tech.jhipster.lite.TestFileUtils; +import tech.jhipster.lite.UnitTest; +import tech.jhipster.lite.module.domain.JHipsterModule; +import tech.jhipster.lite.module.domain.JHipsterModulesFixture; +import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; + +@UnitTest +class JacocoThresholdModuleFactoryTest { + + private static final JacocoThresholdModuleFactory factory = new JacocoThresholdModuleFactory(); + + @Test + void shouldBuildModule() { + JHipsterModuleProperties properties = JHipsterModulesFixture.propertiesBuilder(TestFileUtils.tmpDirForTest()).build(); + + JHipsterModule module = factory.buildModule(properties); + + assertThatModuleWithFiles(module, pomFile()) + .createFile("pom.xml") + .containing( + """ + + org.jacoco + jacoco-maven-plugin + ${jacoco.version} + + + pre-unit-tests + + prepare-agent + + + + + post-unit-test + test + + report + + + + pre-integration-tests + + prepare-agent-integration + + + + + post-integration-tests + post-integration-test + + report-integration + + + + merge + verify + + merge + + + + + ${project.basedir} + + **/*.exec + + + + target/jacoco/allTest.exec + + + + post-merge-report + verify + + report + + + target/jacoco/allTest.exec + target/jacoco/ + + + + check + + check + + + target/jacoco/allTest.exec + + + CLASS + + + LINE + COVEREDRATIO + 1.00 + + + BRANCH + COVEREDRATIO + 1.00 + + + + + + + + + """ + ); + } +} diff --git a/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/config/JacocoBeanConfigurationIT.java b/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/config/JacocoBeanConfigurationIT.java deleted file mode 100644 index feebb2fc7cb..00000000000 --- a/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/config/JacocoBeanConfigurationIT.java +++ /dev/null @@ -1,21 +0,0 @@ -package tech.jhipster.lite.generator.server.javatool.jacoco.infrastructure.config; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import tech.jhipster.lite.IntegrationTest; -import tech.jhipster.lite.generator.server.javatool.jacoco.domain.JacocoDomainService; - -@IntegrationTest -class JacocoBeanConfigurationIT { - - @Autowired - ApplicationContext applicationContext; - - @Test - void shouldGetBean() { - assertThat(applicationContext.getBean("jacocoService")).isNotNull().isInstanceOf(JacocoDomainService.class); - } -} diff --git a/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/primary/rest/JacocoResourceIT.java b/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/primary/rest/JacocoResourceIT.java deleted file mode 100644 index d78087128bb..00000000000 --- a/src/test/java/tech/jhipster/lite/generator/server/javatool/jacoco/infrastructure/primary/rest/JacocoResourceIT.java +++ /dev/null @@ -1,45 +0,0 @@ -package tech.jhipster.lite.generator.server.javatool.jacoco.infrastructure.primary.rest; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import static tech.jhipster.lite.TestUtils.*; -import static tech.jhipster.lite.generator.project.domain.Constants.*; - -import java.util.List; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; -import tech.jhipster.lite.IntegrationTest; -import tech.jhipster.lite.TestFileUtils; -import tech.jhipster.lite.TestUtils; -import tech.jhipster.lite.generator.project.domain.Project; -import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO; -import tech.jhipster.lite.module.infrastructure.secondary.TestJHipsterModules; - -@IntegrationTest -@AutoConfigureMockMvc -class JacocoResourceIT { - - @Autowired - private MockMvc mockMvc; - - @Test - void shouldAddCheckMinimumCoverage() throws Exception { - ProjectDTO projectDTO = TestUtils.readFileToObject("json/chips.json", ProjectDTO.class).folder(TestFileUtils.tmpDirForTest()); - Project project = ProjectDTO.toProject(projectDTO); - TestJHipsterModules.applyInit(project); - TestJHipsterModules.applyMaven(project); - - mockMvc - .perform( - post("/api/servers/java/jacoco-minimum-coverage") - .contentType(MediaType.APPLICATION_JSON) - .content(TestUtils.convertObjectToJsonBytes(projectDTO)) - ) - .andExpect(status().isOk()); - - assertFileContent(project, POM_XML, List.of("LINE", "COVEREDRATIO", "1.00")); - } -} diff --git a/src/test/java/tech/jhipster/lite/module/domain/replacement/RegexNeedleAfterReplacerTest.java b/src/test/java/tech/jhipster/lite/module/domain/replacement/RegexNeedleAfterReplacerTest.java new file mode 100644 index 00000000000..a2b8714070c --- /dev/null +++ b/src/test/java/tech/jhipster/lite/module/domain/replacement/RegexNeedleAfterReplacerTest.java @@ -0,0 +1,92 @@ +package tech.jhipster.lite.module.domain.replacement; + +import static org.assertj.core.api.Assertions.*; + +import java.util.regex.Pattern; +import org.junit.jupiter.api.Test; +import tech.jhipster.lite.UnitTest; + +@UnitTest +class RegexNeedleAfterReplacerTest { + + @Test + void shouldNotMatchNotMatchingRegex() { + RegexNeedleAfterReplacer replacer = new RegexNeedleAfterReplacer(Pattern.compile("pattern")); + + assertThat(replacer.notMatchIn("content")).isTrue(); + } + + @Test + void shouldMatchMatchingRegex() { + RegexNeedleAfterReplacer replacer = new RegexNeedleAfterReplacer(Pattern.compile("cont[en]{2}t")); + + assertThat(replacer.notMatchIn("content")).isFalse(); + } + + @Test + void shouldNotReplaceNotMatchingNeedle() { + RegexNeedleAfterReplacer replacer = new RegexNeedleAfterReplacer(Pattern.compile("ne{1,2}dle")); + + String updatedContent = replacer.replacer().apply("content", "replacement"); + + assertThat(updatedContent).isEqualTo("content"); + } + + @Test + void shouldReplaceLineEndNeedle() { + RegexNeedleAfterReplacer replacer = new RegexNeedleAfterReplacer(Pattern.compile("ne{1,2}dle !-->$", Pattern.MULTILINE)); + + String updatedContent = replacer + .replacer() + .apply(""" + + + + """, ""); + + assertThat(updatedContent).isEqualTo(""" + + + + + """); + } + + @Test + void shouldReplaceLinePartNeedle() { + RegexNeedleAfterReplacer replacer = new RegexNeedleAfterReplacer(Pattern.compile(" + + + + """, + "" + ); + + assertThat(updatedContent) + .isEqualTo( + """ + + + + + + + + """ + ); + } + + @Test + void shouldGetPatternAsSearchMatcher() { + RegexNeedleAfterReplacer replacer = new RegexNeedleAfterReplacer(Pattern.compile("cont[en]{2}t")); + + assertThat(replacer.searchMatcher()).isEqualTo("cont[en]{2}t"); + } +}