From d164fd0288895767e42afc4b4fe29e621272efd0 Mon Sep 17 00:00:00 2001 From: Daniel Franco Date: Thu, 3 Feb 2022 17:59:34 +0000 Subject: [PATCH 1/3] Delete constant version --- .../server/springboot/springcloud/consul/domain/Consul.java | 5 ----- .../springcloud/consul/application/ConsulAssert.java | 3 +-- 2 files changed, 1 insertion(+), 7 deletions(-) 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/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, From 2dd1ed4d97922cfcc30779526c51b07b8306ec92 Mon Sep 17 00:00:00 2001 From: Daniel Franco Date: Thu, 3 Feb 2022 18:00:21 +0000 Subject: [PATCH 2/3] Use getVersion for spring-cloud consul --- .../consul/domain/ConsulDomainService.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) 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 From 5c304b986a3da8cab9f49f75f2b2c54a62b90166 Mon Sep 17 00:00:00 2001 From: Daniel Franco Date: Thu, 3 Feb 2022 18:00:55 +0000 Subject: [PATCH 3/3] Add new tests for ConsulDomainService --- .../domain/ConsulDomainServiceTest.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/ConsulDomainServiceTest.java 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); + } +}