Skip to content

Commit

Permalink
Extract project db knowledge logic into springboot common service
Browse files Browse the repository at this point in the history
  • Loading branch information
seraphinandrieux committed Mar 8, 2022
1 parent 179ae42 commit c113daa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -172,6 +169,20 @@ public Optional<String> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public interface SpringBootCommonService {
void addLoggerTest(Project project, String packageName, Level level);

Optional<String> getProperty(Project project, String key);
boolean isSetWithMySQLOrMariaDBDatabase(Project project);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -235,4 +231,11 @@ void shouldNotGetPropertyForBlankKey() {
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("key");
}

@Test
void shouldNotCheckIfProjectIsSetWithMariaDbOrMySqlDatabaseWithoutProject() {
assertThatThrownBy(() -> springBootCommonDomainService.isSetWithMySQLOrMariaDBDatabase(null))
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("project");
}
}

0 comments on commit c113daa

Please sign in to comment.