Skip to content

Commit

Permalink
Merge pull request #642 from DanielFran/maven-version_consul
Browse files Browse the repository at this point in the history
Spring-cloud consul: use get version, managed by dependabot
  • Loading branch information
pascalgrimaud authored Feb 3, 2022
2 parents fac6918 + 5c304b9 commit 3bcb967
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@ 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() {}

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
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;

public class ConsulAssert {

public static void assertDependencies(Project project) {
assertFileContent(project, "pom.xml", "<spring-cloud.version>" + getSpringCloudVersion() + "</spring-cloud.version>");
assertFileContent(project, "pom.xml", "<spring-cloud.version>");

assertFileContent(
project,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 3bcb967

Please sign in to comment.