From c113daa10b2a56ad7a257d4004e5656653876d5e Mon Sep 17 00:00:00 2001 From: seraphinandrieux Date: Tue, 8 Mar 2022 08:25:42 +0100 Subject: [PATCH] Extract project db knowledge logic into springboot common service --- .../domain/SpringBootCommonDomainService.java | 23 ++++++++++++++----- .../domain/SpringBootCommonService.java | 1 + .../domain/LiquibaseDomainService.java | 16 ++----------- .../SpringBootCommonDomainServiceTest.java | 19 ++++++++------- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainService.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainService.java index e96451c9a39..f26e52819db 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainService.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainService.java @@ -1,13 +1,10 @@ package tech.jhipster.lite.generator.server.springboot.common.domain; -import static tech.jhipster.lite.common.domain.FileUtils.getPath; -import static tech.jhipster.lite.common.domain.WordUtils.LF; +import static tech.jhipster.lite.common.domain.FileUtils.*; +import static tech.jhipster.lite.common.domain.WordUtils.*; import static tech.jhipster.lite.generator.project.domain.Constants.*; -import static tech.jhipster.lite.generator.project.domain.DefaultConfig.BASE_NAME; -import static tech.jhipster.lite.generator.project.domain.DefaultConfig.PACKAGE_NAME; +import static tech.jhipster.lite.generator.project.domain.DefaultConfig.*; import static tech.jhipster.lite.generator.server.springboot.core.domain.SpringBoot.*; -import static tech.jhipster.lite.generator.server.springboot.core.domain.SpringBoot.NEEDLE_APPLICATION_TEST_LOGGING_PROPERTIES; -import static tech.jhipster.lite.generator.server.springboot.core.domain.SpringBoot.NEEDLE_APPLICATION_TEST_PROPERTIES; import java.util.Optional; import tech.jhipster.lite.common.domain.FileUtils; @@ -172,6 +169,20 @@ public Optional getProperty(Project project, String key) { }); } + @Override + public boolean isSetWithMySQLOrMariaDBDatabase(Project project) { + Assert.notNull("project", project); + return isMariaDBDatabase(project) || isMySQLDatabase(project); + } + + private boolean isMySQLDatabase(Project project) { + return getProperty(project, "spring.datasource.url").filter(value -> value.contains("mysql")).isPresent(); + } + + private boolean isMariaDBDatabase(Project project) { + return getProperty(project, "spring.datasource.url").filter(value -> value.contains("mariadb")).isPresent(); + } + private void addLoggerToConfiguration( Project project, String packageName, diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonService.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonService.java index 572d7efedd9..61bad047e14 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonService.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonService.java @@ -23,4 +23,5 @@ public interface SpringBootCommonService { void addLoggerTest(Project project, String packageName, Level level); Optional getProperty(Project project, String key); + boolean isSetWithMySQLOrMariaDBDatabase(Project project); } diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/dbmigration/liquibase/domain/LiquibaseDomainService.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/dbmigration/liquibase/domain/LiquibaseDomainService.java index 684332f23c5..c99f3d5602e 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/dbmigration/liquibase/domain/LiquibaseDomainService.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/dbmigration/liquibase/domain/LiquibaseDomainService.java @@ -142,7 +142,7 @@ private void addSqlUserChangelog(Project project) { String userChangelog = getTimestamp() + "_added_entity_User.xml"; addChangelogXml(project, "", userChangelog); String userXmlFile = "user.xml"; - if (isMySQLOrMariaDBDatabase(project)) { + if (springBootCommonService.isSetWithMySQLOrMariaDBDatabase(project)) { userXmlFile = "user_with_autoincrement.xml"; } projectRepository.template(project, getUserResourcePath(), userXmlFile, getPath(MAIN_RESOURCES, CHANGELOG), userChangelog); @@ -169,19 +169,7 @@ private String getTimestamp() { return localDateTime.format(DATE_TIME_FORMATTER); } - private boolean isMySQLDatabase(Project project) { - return springBootCommonService.getProperty(project, "spring.datasource.url").filter(value -> value.contains("mysql")).isPresent(); - } - - private boolean isMariaDBDatabase(Project project) { - return springBootCommonService.getProperty(project, "spring.datasource.url").filter(value -> value.contains("mariadb")).isPresent(); - } - private boolean isDatabaseWhichNeedsSequenceStrategy(Project project) { - return !isMySQLOrMariaDBDatabase(project); - } - - private boolean isMySQLOrMariaDBDatabase(Project project) { - return isMySQLDatabase(project) || isMariaDBDatabase(project); + return !springBootCommonService.isSetWithMySQLOrMariaDBDatabase(project); } } diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainServiceTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainServiceTest.java index 7779ddde223..1a445824df1 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainServiceTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainServiceTest.java @@ -1,16 +1,12 @@ package tech.jhipster.lite.generator.server.springboot.common.domain; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.verify; +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.common.domain.FileUtils.getPathOf; -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.common.domain.FileUtils.*; +import static tech.jhipster.lite.generator.project.domain.Constants.*; import static tech.jhipster.lite.generator.server.springboot.core.domain.SpringBoot.*; -import static tech.jhipster.lite.generator.server.springboot.core.domain.SpringBoot.NEEDLE_LOGBACK_LOGGER; import java.nio.file.Files; import org.junit.jupiter.api.Test; @@ -235,4 +231,11 @@ void shouldNotGetPropertyForBlankKey() { .isExactlyInstanceOf(MissingMandatoryValueException.class) .hasMessageContaining("key"); } + + @Test + void shouldNotCheckIfProjectIsSetWithMariaDbOrMySqlDatabaseWithoutProject() { + assertThatThrownBy(() -> springBootCommonDomainService.isSetWithMySQLOrMariaDBDatabase(null)) + .isExactlyInstanceOf(MissingMandatoryValueException.class) + .hasMessageContaining("project"); + } }