Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Liquibase changelog version #2155

Merged
merged 4 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void appendContext(JHipsterModuleBuilder builder, JHipsterModuleProperti
.packageName(properties.basePackage())
.put("applicationName", properties.projectBaseName().capitalized())
.put("srcMainDocker", "src/main/docker") // Used in mariadb.md
.put("dockerImageWithVersion", dockerImage.fullName()); // Used in mariadb.yml
.put("mariaDBDockerImageWithVersion", dockerImage.fullName()); // Used in mariadb.yml
}

private void appendDocumentation(JHipsterModuleBuilder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.time.Clock;
import tech.jhipster.lite.common.domain.TimeUtils;
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;
Expand Down Expand Up @@ -51,7 +52,20 @@ public void init(Project project) {

@Override
public void addLiquibase(Project project) {
Dependency liquibaseDependency = Dependency.builder().groupId("org.liquibase").artifactId("liquibase-core").build();
buildToolService
.getVersion(project, "liquibase")
.ifPresentOrElse(
version -> buildToolService.addProperty(project, "liquibase.version", version),
() -> {
throw new GeneratorException("Version not found: liquibase");
}
);
Dependency liquibaseDependency = Dependency
.builder()
.groupId("org.liquibase")
.artifactId("liquibase-core")
.version("\\${liquibase.version}")
.build();
buildToolService.addDependency(project, liquibaseDependency);

Dependency h2dependency = Dependency.builder().groupId("com.h2database").artifactId("h2").scope("test").build();
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/generator/dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<cucumber.version>7.3.4</cucumber.version>
<testng.version>7.6.0</testng.version>
<pulsar.version>2.10.0</pulsar.version>
<liquibase.version>4.12.0</liquibase.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -154,6 +155,11 @@
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>${liquibase.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.5.xsd"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"
>
<property name="now" value="now()" dbms="h2" />
<property name="now" value="current_timestamp" dbms="postgresql" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.5.xsd
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd"
>
<changeSet id="create_table_jhi_authority" author="jhipster">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.5.xsd
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd"
>
<changeSet id="create_sequence_jhi_user" author="jhipster">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.5.xsd
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd"
>
<changeSet id="create_table_jhi_user" author="jhipster">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.5.xsd
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd"
>
<changeSet id="create_table_jhi_user" author="jhipster">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,7 @@ void shouldInit() {

liquibaseApplicationService.init(project);

assertFileContent(
project,
POM_XML,
List.of("<dependency>", "<groupId>org.liquibase</groupId>", "<artifactId>liquibase-core</artifactId>", "</dependency>")
);
assertFileContent(
project,
POM_XML,
List.of("<dependency>", "<groupId>com.h2database</groupId>", "<artifactId>h2</artifactId>", "<scope>test</scope>", "</dependency>")
);
assertDependencies(project);
assertFilesLiquibaseChangelogMasterXml(project);
assertFilesLiquibaseJava(project);
assertLoggerInConfig(project);
Expand All @@ -97,11 +88,7 @@ void shouldAddLiquibase() {

liquibaseApplicationService.addLiquibase(project);

assertFileContent(
project,
POM_XML,
List.of("<dependency>", "<groupId>org.liquibase</groupId>", "<artifactId>liquibase-core</artifactId>", "</dependency>")
);
assertDependencies(project);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
import static tech.jhipster.lite.TestUtils.assertFileContent;
import static tech.jhipster.lite.TestUtils.assertFileExist;
import static tech.jhipster.lite.common.domain.FileUtils.getPath;
import static tech.jhipster.lite.generator.project.domain.Constants.MAIN_JAVA;
import static tech.jhipster.lite.generator.project.domain.Constants.MAIN_RESOURCES;
import static tech.jhipster.lite.generator.project.domain.Constants.TEST_JAVA;
import static tech.jhipster.lite.generator.project.domain.Constants.TEST_RESOURCES;
import static tech.jhipster.lite.generator.project.domain.Constants.*;
import static tech.jhipster.lite.generator.server.springboot.core.domain.SpringBoot.LOGGING_CONFIGURATION;
import static tech.jhipster.lite.generator.server.springboot.core.domain.SpringBoot.LOGGING_TEST_CONFIGURATION;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.util.List;
import tech.jhipster.lite.TestUtils;
import tech.jhipster.lite.generator.project.domain.DefaultConfig;
import tech.jhipster.lite.generator.project.domain.Project;

Expand All @@ -23,6 +21,27 @@ public class LiquibaseAssertFiles {
public static final String CURRENT_DATE = "2022-01-28T17:30:26.0Z";
public static final ZoneId DEFAULT_TIMEZONE = ZoneId.of("UTC");

public static void assertDependencies(Project project) {
TestUtils.assertFileContent(project, POM_XML, "<liquibase.version>");
TestUtils.assertFileContent(project, POM_XML, "</liquibase.version>");
assertFileContent(
project,
POM_XML,
List.of(
"<dependency>",
"<groupId>org.liquibase</groupId>",
"<artifactId>liquibase-core</artifactId>",
"<version>${liquibase.version}</version>",
"</dependency>"
)
);
assertFileContent(
project,
POM_XML,
List.of("<dependency>", "<groupId>com.h2database</groupId>", "<artifactId>h2</artifactId>", "<scope>test</scope>", "</dependency>")
);
}

public static void assertFilesLiquibaseJava(Project project) {
String liquibasePackage =
project.getPackageName().orElse(DefaultConfig.DEFAULT_PACKAGE_NAME) + ".technical.infrastructure.secondary.liquibase";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.domain;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static tech.jhipster.lite.TestUtils.*;
import static tech.jhipster.lite.common.domain.FileUtils.getPath;
import static tech.jhipster.lite.generator.project.domain.Constants.*;
import static tech.jhipster.lite.generator.project.domain.ProjectFilesAsserter.*;
import static tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.domain.LiquibaseDomainService.CONFIG_LIQUIBASE;
import static tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.domain.LiquibaseDomainService.LIQUIBASE_PATH;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.util.List;
import java.util.Optional;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.*;
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.FilePath;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectFile;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;
import tech.jhipster.lite.generator.server.springboot.common.domain.Level;
import tech.jhipster.lite.generator.server.springboot.common.domain.SpringBootCommonService;

@UnitTest
@ExtendWith(MockitoExtension.class)
class LiquibaseDomainServiceTest {

@Mock
ProjectRepository projectRepository;

@Mock
BuildToolService buildToolService;

@Mock
SpringBootCommonService springBootCommonService;

@Spy
Clock clock = Clock.fixed(Instant.parse("2022-01-22T14:01:54.954396664Z"), ZoneId.of("Europe/Paris"));

@InjectMocks
LiquibaseDomainService liquibaseDomainService;

@Test
void shouldInit() {
// Given
Project project = tmpProject();

when(buildToolService.getVersion(any(Project.class), anyString())).thenReturn(Optional.of("0.0.0"));

// When
liquibaseDomainService.init(project);

// Then
ArgumentCaptor<Dependency> dependencyArgCaptor = ArgumentCaptor.forClass(Dependency.class);
verify(buildToolService, times(2)).addDependency(eq(project), dependencyArgCaptor.capture());
List<Dependency> dependencies = dependencyArgCaptor.getAllValues();

Dependency expectedDependency = Dependency
.builder()
.groupId("org.liquibase")
.artifactId("liquibase-core")
.version("\\${liquibase.version}")
.build();
assertThat(dependencies.get(0)).usingRecursiveComparison().isEqualTo(expectedDependency);

expectedDependency = Dependency.builder().groupId("com.h2database").artifactId("h2").scope("test").build();
assertThat(dependencies.get(1)).usingRecursiveComparison().isEqualTo(expectedDependency);

verify(projectRepository)
.add(
projectFileArgument(
project,
new FilePath("server/springboot/dbmigration/liquibase/resources", LIQUIBASE_MASTER_XML),
new FilePath("src/main/resources/" + CONFIG_LIQUIBASE, LIQUIBASE_MASTER_XML)
)
);

ArgumentCaptor<ProjectFile> projectFileArgCaptor = ArgumentCaptor.forClass(ProjectFile.class);
verify(projectRepository, times(6)).template(projectFileArgCaptor.capture());
List<ProjectFile> projectFiles = projectFileArgCaptor.getAllValues();

ProjectFile projectFile = ProjectFile
.forProject(project)
.withSource("server/springboot/dbmigration/liquibase/src", "AsyncSpringLiquibase.java")
.withDestinationFolder(MAIN_JAVA + "/com/mycompany/myapp/" + LIQUIBASE_PATH);
assertThat(projectFiles.get(0)).usingRecursiveComparison().isEqualTo(projectFile);
projectFile =
ProjectFile
.forProject(project)
.withSource("server/springboot/dbmigration/liquibase/src", "LiquibaseConfiguration.java")
.withDestinationFolder(MAIN_JAVA + "/com/mycompany/myapp/" + LIQUIBASE_PATH);
assertThat(projectFiles.get(1)).usingRecursiveComparison().isEqualTo(projectFile);
projectFile =
ProjectFile
.forProject(project)
.withSource("server/springboot/dbmigration/liquibase/src", "SpringLiquibaseUtil.java")
.withDestinationFolder(MAIN_JAVA + "/com/mycompany/myapp/" + LIQUIBASE_PATH);
assertThat(projectFiles.get(2)).usingRecursiveComparison().isEqualTo(projectFile);
projectFile =
ProjectFile
.forProject(project)
.withSource("server/springboot/dbmigration/liquibase/test", "AsyncSpringLiquibaseTest.java")
.withDestinationFolder(TEST_JAVA + "/com/mycompany/myapp/" + LIQUIBASE_PATH);
assertThat(projectFiles.get(3)).usingRecursiveComparison().isEqualTo(projectFile);
projectFile =
ProjectFile
.forProject(project)
.withSource("server/springboot/dbmigration/liquibase/test", "LiquibaseConfigurationIT.java")
.withDestinationFolder(TEST_JAVA + "/com/mycompany/myapp/" + LIQUIBASE_PATH);
assertThat(projectFiles.get(4)).usingRecursiveComparison().isEqualTo(projectFile);
projectFile =
ProjectFile
.forProject(project)
.withSource("server/springboot/dbmigration/liquibase/test", "SpringLiquibaseUtilTest.java")
.withDestinationFolder(TEST_JAVA + "/com/mycompany/myapp/" + LIQUIBASE_PATH);
assertThat(projectFiles.get(5)).usingRecursiveComparison().isEqualTo(projectFile);

verify(springBootCommonService).addTestLogbackRecorder(any(Project.class));
verify(springBootCommonService, times(3)).addLogger(any(Project.class), anyString(), any(Level.class));
verify(springBootCommonService, times(3)).addLoggerTest(any(Project.class), anyString(), any(Level.class));
}

@Test
void shouldNotAddLiquibaseDependency() {
Project project = tmpProject();

Assertions.assertThatThrownBy(() -> liquibaseDomainService.addLiquibase(project)).isExactlyInstanceOf(GeneratorException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static tech.jhipster.lite.TestUtils.assertFileContent;
import static tech.jhipster.lite.generator.project.domain.Constants.POM_XML;
import static tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.application.LiquibaseAssertFiles.assertFilesLiquibaseChangelogMasterXml;
import static tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.application.LiquibaseAssertFiles.assertFilesLiquibaseJava;
import static tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.application.LiquibaseAssertFiles.assertFilesLiquibaseSqlUser;
import static tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.application.LiquibaseAssertFiles.assertFilesLiquibaseSqlUserAuthority;
import static tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.application.LiquibaseAssertFiles.initClock;
import static tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.application.LiquibaseAssertFiles.*;

import java.time.Clock;
import java.util.List;
Expand Down Expand Up @@ -90,16 +84,7 @@ void shouldInit() throws Exception {
)
.andExpect(status().isOk());

assertFileContent(
project,
POM_XML,
List.of("<dependency>", "<groupId>org.liquibase</groupId>", "<artifactId>liquibase-core</artifactId>", "</dependency>")
);
assertFileContent(
project,
POM_XML,
List.of("<dependency>", "<groupId>com.h2database</groupId>", "<artifactId>h2</artifactId>", "<scope>test</scope>", "</dependency>")
);
assertDependencies(project);
assertFilesLiquibaseChangelogMasterXml(project);
assertFilesLiquibaseJava(project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.5.xsd"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"
>
<property name="now" value="now()" dbms="h2" />
<property name="now" value="current_timestamp" dbms="postgresql" />
Expand Down