diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/springdoc/domain/SpringDocDomainService.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/springdoc/domain/SpringDocDomainService.java index d8456eb0384..ab94fdec490 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/springdoc/domain/SpringDocDomainService.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/springdoc/domain/SpringDocDomainService.java @@ -9,6 +9,7 @@ import java.util.HashMap; import java.util.Map; import java.util.TreeMap; +import tech.jhipster.lite.error.domain.GeneratorException; import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService; import tech.jhipster.lite.generator.project.domain.Project; import tech.jhipster.lite.generator.project.domain.ProjectRepository; @@ -49,8 +50,16 @@ public void initWithSecurityJWT(Project project) { @Override public void addSpringDocDependency(Project project) { - buildToolService.addProperty(project, "springdoc-openapi-ui.version", SpringDoc.springDocVersion()); - buildToolService.addDependency(project, SpringDoc.springDocDependency()); + this.buildToolService.getVersion(project, "springdoc-openapi") + .ifPresentOrElse( + version -> { + buildToolService.addProperty(project, "springdoc-openapi-ui.version", version); + buildToolService.addDependency(project, SpringDoc.springDocDependency()); + }, + () -> { + throw new GeneratorException("Springdoc Openapi version not found"); + } + ); } @Override diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/springdoc/domain/SpringDocDomainServiceTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/springdoc/domain/SpringDocDomainServiceTest.java index d8a3301a37e..5d58d08f3c5 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/springdoc/domain/SpringDocDomainServiceTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/springdoc/domain/SpringDocDomainServiceTest.java @@ -1,14 +1,15 @@ package tech.jhipster.lite.generator.server.springboot.mvc.springdoc.domain; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.*; import static tech.jhipster.lite.TestUtils.tmpProject; import static tech.jhipster.lite.generator.server.springboot.mvc.springdoc.domain.SpringDocConstants.*; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; @@ -16,6 +17,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import tech.jhipster.lite.UnitTest; +import tech.jhipster.lite.error.domain.GeneratorException; import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService; import tech.jhipster.lite.generator.buildtool.generic.domain.Dependency; import tech.jhipster.lite.generator.project.domain.Project; @@ -44,10 +46,11 @@ void shouldInitWithDefaultValues() { Project project = tmpProject(); // When + when(buildToolService.getVersion(project, "springdoc-openapi")).thenReturn(Optional.of("0.0.0")); springDocDomainService.init(project); // Then - verify(buildToolService).addProperty(project, "springdoc-openapi-ui.version", "1.6.4"); + verify(buildToolService).addProperty(project, "springdoc-openapi-ui.version", "0.0.0"); ArgumentCaptor dependencyArgCaptor = ArgumentCaptor.forClass(Dependency.class); verify(buildToolService).addDependency(eq(project), dependencyArgCaptor.capture()); assertThat(dependencyArgCaptor.getValue()).usingRecursiveComparison().isEqualTo(getExpectedDependency()); @@ -89,10 +92,11 @@ void shouldInitWithAdditionalConfigFromProject() { project.getConfig().putAll(projectConfig); // When + when(buildToolService.getVersion(project, "springdoc-openapi")).thenReturn(Optional.of("0.0.0")); springDocDomainService.init(project); // Then - verify(buildToolService).addProperty(project, "springdoc-openapi-ui.version", "1.6.4"); + verify(buildToolService).addProperty(project, "springdoc-openapi-ui.version", "0.0.0"); ArgumentCaptor dependencyArgCaptor = ArgumentCaptor.forClass(Dependency.class); verify(buildToolService).addDependency(eq(project), dependencyArgCaptor.capture()); assertThat(dependencyArgCaptor.getValue()).usingRecursiveComparison().isEqualTo(getExpectedDependency()); @@ -113,6 +117,13 @@ void shouldInitWithAdditionalConfigFromProject() { assertThat(project.getConfig()).containsAllEntriesOf(projectConfig); } + @Test + void shouldNotAddSpringDocDependency() { + Project project = tmpProject(); + + assertThatThrownBy(() -> springDocDomainService.addSpringDocDependency(project)).isExactlyInstanceOf(GeneratorException.class); + } + private static Dependency getExpectedDependency() { return Dependency .builder()