diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/Consul.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/Consul.java
index 6efe4e3f93a..0df251bdcce 100644
--- a/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/Consul.java
+++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/Consul.java
@@ -10,7 +10,6 @@ public class Consul {
private static final String DOCKER_CONSUL_IMAGE = "consul:1.11.1";
private static final String DOCKER_CONSUL_CONFIG_LOADER_IMAGE = "jhipster/consul-config-loader:v0.4.1";
private static final String SPRING_CLOUD = "org.springframework.cloud";
- private static final String SPRING_CLOUD_VERSION = "2021.0.0";
private Consul() {}
@@ -18,10 +17,6 @@ public static String getDockerConsulImage() {
return DOCKER_CONSUL_IMAGE;
}
- public static String getSpringCloudVersion() {
- return SPRING_CLOUD_VERSION;
- }
-
public static String getDockerConsulConfigLoaderImage() {
return DOCKER_CONSUL_CONFIG_LOADER_IMAGE;
}
diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/ConsulDomainService.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/ConsulDomainService.java
index f6d685f9f28..391bf4bc1cc 100644
--- a/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/ConsulDomainService.java
+++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/ConsulDomainService.java
@@ -6,6 +6,7 @@
import static tech.jhipster.lite.generator.server.springboot.springcloud.consul.domain.Consul.*;
import tech.jhipster.lite.common.domain.Base64Utils;
+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;
@@ -31,11 +32,19 @@ public void init(Project project) {
@Override
public void addDependencies(Project project) {
- buildToolService.addProperty(project, "spring-cloud.version", getSpringCloudVersion());
- buildToolService.addDependencyManagement(project, springCloudDependencyManagement());
- buildToolService.addDependency(project, springCloudBootstrapDependency());
- buildToolService.addDependency(project, springCloudConsulConfigDependency());
- buildToolService.addDependency(project, springCloudConsulDiscoveryDependency());
+ this.buildToolService.getVersion(project, "spring-cloud")
+ .ifPresentOrElse(
+ version -> {
+ buildToolService.addProperty(project, "spring-cloud.version", version);
+ buildToolService.addDependencyManagement(project, springCloudDependencyManagement());
+ buildToolService.addDependency(project, springCloudBootstrapDependency());
+ buildToolService.addDependency(project, springCloudConsulConfigDependency());
+ buildToolService.addDependency(project, springCloudConsulDiscoveryDependency());
+ },
+ () -> {
+ throw new GeneratorException("Spring Cloud version not found");
+ }
+ );
}
@Override
diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/application/ConsulAssert.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/application/ConsulAssert.java
index f77cc011f40..abd4598ac81 100644
--- a/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/application/ConsulAssert.java
+++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/application/ConsulAssert.java
@@ -6,7 +6,6 @@
import static tech.jhipster.lite.generator.project.domain.Constants.MAIN_RESOURCES;
import static tech.jhipster.lite.generator.project.domain.Constants.TEST_RESOURCES;
import static tech.jhipster.lite.generator.server.springboot.springcloud.consul.domain.Consul.getDockerConsulImage;
-import static tech.jhipster.lite.generator.server.springboot.springcloud.consul.domain.Consul.getSpringCloudVersion;
import java.util.List;
import tech.jhipster.lite.generator.project.domain.Project;
@@ -14,7 +13,7 @@
public class ConsulAssert {
public static void assertDependencies(Project project) {
- assertFileContent(project, "pom.xml", "" + getSpringCloudVersion() + "");
+ assertFileContent(project, "pom.xml", "");
assertFileContent(
project,
diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/ConsulDomainServiceTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/ConsulDomainServiceTest.java
new file mode 100644
index 00000000000..51e75ec26c2
--- /dev/null
+++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/ConsulDomainServiceTest.java
@@ -0,0 +1,56 @@
+package tech.jhipster.lite.generator.server.springboot.springcloud.consul.domain;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.*;
+import static tech.jhipster.lite.TestUtils.tmpProjectWithPomXml;
+
+import java.util.Optional;
+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.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;
+import tech.jhipster.lite.generator.project.domain.ProjectRepository;
+
+@UnitTest
+@ExtendWith(MockitoExtension.class)
+class ConsulDomainServiceTest {
+
+ @Mock
+ ProjectRepository projectRepository;
+
+ @Mock
+ BuildToolService buildToolService;
+
+ @InjectMocks
+ ConsulDomainService consulDomainService;
+
+ @Test
+ void shouldInit() {
+ Project project = tmpProjectWithPomXml();
+ when(buildToolService.getVersion(project, "spring-cloud")).thenReturn(Optional.of("0.0.0"));
+
+ consulDomainService.init(project);
+
+ verify(buildToolService).addProperty(any(Project.class), anyString(), anyString());
+ verify(buildToolService).addDependencyManagement(any(Project.class), any(Dependency.class));
+ verify(buildToolService, times(3)).addDependency(any(Project.class), any(Dependency.class));
+
+ verify(projectRepository, times(3)).template(any(Project.class), anyString(), anyString(), anyString());
+ verify(projectRepository, times(2)).template(any(Project.class), anyString(), anyString(), anyString(), anyString());
+ }
+
+ @Test
+ void shouldNotAddDependencies() {
+ Project project = tmpProjectWithPomXml();
+
+ assertThatThrownBy(() -> consulDomainService.addDependencies(project)).isExactlyInstanceOf(GeneratorException.class);
+ }
+}