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

Add user context for mariadb and liquibase #918

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
@@ -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
@@ -1,12 +1,10 @@
package tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.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.WordUtils.indent;
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.PACKAGE_NAME;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.PRETTIER_DEFAULT_INDENT;
import static tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.domain.Liquibase.NEEDLE_LIQUIBASE;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.*;
import static tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.domain.Liquibase.*;

import java.time.Clock;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -127,7 +125,7 @@ public void addUserAuthorityChangelog(Project project) {
}

private void addSqlSequenceUserChangelog(Project project) {
if (!isMySQLDatabase(project)) {
if (isDatabaseWhichNeedsSequenceStrategy(project)) {
String sequenceUserChangelog = getTimestamp() + "_added_sequence_User.xml";
addChangelogXml(project, "", sequenceUserChangelog);
projectRepository.template(
Expand All @@ -144,7 +142,7 @@ private void addSqlUserChangelog(Project project) {
String userChangelog = getTimestamp() + "_added_entity_User.xml";
addChangelogXml(project, "", userChangelog);
String userXmlFile = "user.xml";
if (isMySQLDatabase(project)) {
if (springBootCommonService.isSetWithMySQLOrMariaDBDatabase(project)) {
userXmlFile = "user_with_autoincrement.xml";
}
projectRepository.template(project, getUserResourcePath(), userXmlFile, getPath(MAIN_RESOURCES, CHANGELOG), userChangelog);
Expand All @@ -171,7 +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 isDatabaseWhichNeedsSequenceStrategy(Project project) {
return !springBootCommonService.isSetWithMySQLOrMariaDBDatabase(project);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package tech.jhipster.lite.generator.server.springboot.user.domain;

import static tech.jhipster.lite.common.domain.FileUtils.getPath;
import static tech.jhipster.lite.common.domain.FileUtils.*;
import static tech.jhipster.lite.generator.project.domain.Constants.*;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.PACKAGE_NAME;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.PACKAGE_PATH;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.*;

import tech.jhipster.lite.generator.project.domain.DatabaseType;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;
import tech.jhipster.lite.generator.server.springboot.common.domain.SpringBootCommonService;

public class SpringBootUserDomainService implements SpringBootUserService {

Expand All @@ -18,11 +16,9 @@ public class SpringBootUserDomainService implements SpringBootUserService {
private static final String USER_DATABASE_KEY = "sqlDatabaseName";

private final ProjectRepository projectRepository;
private final SpringBootCommonService springBootCommonService;

public SpringBootUserDomainService(ProjectRepository projectRepository, SpringBootCommonService springBootCommonService) {
public SpringBootUserDomainService(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
this.springBootCommonService = springBootCommonService;
}

@Override
Expand All @@ -37,7 +33,7 @@ private void addUserEntity(Project project, DatabaseType sqlDatabase) {
String packageNamePath = project.getPackageNamePath().orElse(getPath(PACKAGE_PATH));
project.addConfig(USER_DATABASE_KEY, sqlDatabase.id());

if (isMySQLDatabase(project)) {
if (isDatabaseWhichNoNeedsSequenceStrategy(sqlDatabase)) {
projectRepository.template(
project,
SOURCE,
Expand Down Expand Up @@ -86,7 +82,15 @@ private String getSqlJavaTestPath(String packageNamePath, DatabaseType sqlDataba
return getPath(TEST_JAVA, packageNamePath, TARGET_INFRA_SECOND_JAVA + "/" + sqlDatabase.id());
}

private boolean isMySQLDatabase(Project project) {
return springBootCommonService.getProperty(project, "spring.datasource.url").filter(value -> value.contains("mysql")).isPresent();
private boolean isMySQLDatabase(DatabaseType databaseType) {
return databaseType.equals(DatabaseType.MYSQL);
}

private boolean isMariaDBDatabase(DatabaseType databaseType) {
return databaseType.equals(DatabaseType.MARIADB);
}

private boolean isDatabaseWhichNoNeedsSequenceStrategy(DatabaseType databaseType) {
return isMySQLDatabase(databaseType) || isMariaDBDatabase(databaseType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;
import tech.jhipster.lite.generator.server.springboot.common.domain.SpringBootCommonService;
import tech.jhipster.lite.generator.server.springboot.user.domain.SpringBootUserDomainService;
import tech.jhipster.lite.generator.server.springboot.user.domain.SpringBootUserService;

@Configuration
public class SpringBootUserBeanConfiguration {

private final ProjectRepository projectRepository;
private final SpringBootCommonService springBootCommonService;

public SpringBootUserBeanConfiguration(ProjectRepository projectRepository, SpringBootCommonService springBootCommonService) {
public SpringBootUserBeanConfiguration(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
this.springBootCommonService = springBootCommonService;
}

@Bean
public SpringBootUserService springBootUserService() {
return new SpringBootUserDomainService(projectRepository, springBootCommonService);
return new SpringBootUserDomainService(projectRepository);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tech.jhipster.lite.generator.server.springboot.user.infrastructure.primary.rest;

import static tech.jhipster.lite.generator.project.domain.DatabaseType.MYSQL;
import static tech.jhipster.lite.generator.project.domain.DatabaseType.POSTGRESQL;
import static tech.jhipster.lite.generator.project.domain.DatabaseType.*;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand Down Expand Up @@ -48,6 +47,18 @@ public void addUserAndAuthorityEntitiesForMySQL(@RequestBody ProjectDTO projectD
addUserAndAuthorityEntities(project, MYSQL);
}

@Operation(summary = "Add UserEntity, AuthorityEntity and JpaRepository for MariaDB")
@PostMapping("/mariadb")
@ApiResponse(
responseCode = "500",
description = "An error occurred while adding UserEntity, AuthorityEntity and JpaRepository for MariaDB"
)
@GeneratorStep(id = "user-and-authority-entities-mariadb")
public void addUserAndAuthorityEntitiesForMariaDB(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
addUserAndAuthorityEntities(project, MARIADB);
}

private void addUserAndAuthorityEntities(Project project, DatabaseType sqlDatabase) {
springBootUserApplicationService.addUserAndAuthorityEntities(project, sqlDatabase);
}
Expand Down
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");
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
package tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.application;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.*;
import static tech.jhipster.lite.TestUtils.*;
import static tech.jhipster.lite.common.domain.FileUtils.getPath;
import static tech.jhipster.lite.common.domain.FileUtils.*;
import static tech.jhipster.lite.generator.project.domain.Constants.*;
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.dbmigration.liquibase.application.LiquibaseAssertFiles.*;
import static tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.domain.Liquibase.NEEDLE_LIQUIBASE;
import static tech.jhipster.lite.generator.server.springboot.dbmigration.liquibase.domain.Liquibase.*;

import java.time.Clock;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.SpyBean;
import tech.jhipster.lite.IntegrationTest;
import tech.jhipster.lite.error.domain.GeneratorException;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.project.domain.BuildToolType;
import tech.jhipster.lite.generator.project.domain.DatabaseType;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.server.springboot.core.domain.SpringBootService;
import tech.jhipster.lite.generator.server.springboot.database.mariadb.domain.MariaDBService;
import tech.jhipster.lite.generator.server.springboot.database.mysql.domain.MySQLService;
import tech.jhipster.lite.generator.server.springboot.database.postgresql.domain.PostgresqlService;

Expand All @@ -39,6 +43,9 @@ class LiquibaseApplicationServiceIT {
@Autowired
MySQLService mySQLService;

@Autowired
MariaDBService mariaDBService;

@Autowired
LiquibaseApplicationService liquibaseApplicationService;

Expand Down Expand Up @@ -178,13 +185,18 @@ void shouldAddUserAuthorityChangelogForMyPostgreSQL() {
assertFileContent(project, getPath(MAIN_RESOURCES, "config/liquibase/master.xml"), "20220128173026_added_sequence_User.xml");
}

@Test
@DisplayName("should add user and authority changelog for MySQL")
void shouldAddUserAuthorityChangelogForMySQL() {
@ParameterizedTest
@EnumSource(value = DatabaseType.class, names = { "MYSQL", "MARIADB" })
@DisplayName("should add user and authority changelog for MySQL or MariaDB")
void shouldAddUserAuthorityChangelogForMySQLorMariaDB(DatabaseType databaseType) {
Project project = tmpProject();
buildToolService.init(project, BuildToolType.MAVEN);
springBootService.init(project);
mySQLService.init(project);
if (databaseType.equals(DatabaseType.MYSQL)) {
mySQLService.init(project);
} else {
mariaDBService.init(project);
}
liquibaseApplicationService.init(project);

liquibaseApplicationService.addUserAuthorityChangelog(project);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package tech.jhipster.lite.generator.server.springboot.user.application;

import static tech.jhipster.lite.TestUtils.*;
import static tech.jhipster.lite.generator.project.domain.DatabaseType.MYSQL;
import static tech.jhipster.lite.generator.project.domain.DatabaseType.POSTGRESQL;
import static tech.jhipster.lite.generator.project.domain.DatabaseType.*;
import static tech.jhipster.lite.generator.server.springboot.user.application.SpringBootUserAssertFiles.*;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.springframework.beans.factory.annotation.Autowired;
import tech.jhipster.lite.IntegrationTest;
import tech.jhipster.lite.generator.buildtool.maven.application.MavenApplicationService;
import tech.jhipster.lite.generator.project.domain.DatabaseType;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.server.springboot.core.application.SpringBootApplicationService;
import tech.jhipster.lite.generator.server.springboot.database.mariadb.application.MariaDBApplicationService;
import tech.jhipster.lite.generator.server.springboot.database.mysql.application.MySQLApplicationService;

@IntegrationTest
Expand All @@ -25,6 +28,9 @@ class SpringBootUserApplicationServiceIT {
@Autowired
MySQLApplicationService mySQLApplicationService;

@Autowired
MariaDBApplicationService mariaDBApplicationService;

@Autowired
SpringBootUserApplicationService springBootUserApplicationService;

Expand All @@ -41,19 +47,24 @@ void shouldAddUserAndAuthorityEntitiesForPostgresql() {
checkSequence(project, POSTGRESQL);
}

@Test
void shouldAddUserAndAuthorityEntitiesForMysql() {
@ParameterizedTest
@EnumSource(value = DatabaseType.class, names = { "MYSQL", "MARIADB" })
void shouldAddUserAndAuthorityEntitiesForMysqlOrMariaDB(DatabaseType databaseType) {
Project project = tmpProject();
mavenApplicationService.init(project);
springBootApplicationService.init(project);
mySQLApplicationService.init(project);
if (databaseType.equals(MYSQL)) {
mySQLApplicationService.init(project);
} else {
mariaDBApplicationService.init(project);
}

springBootUserApplicationService.addUserAndAuthorityEntities(project, MYSQL);
springBootUserApplicationService.addUserAndAuthorityEntities(project, databaseType);

assertFilesSqlJavaUser(project, MYSQL);
assertFilesSqlJavaUserAuthority(project, MYSQL);
assertFilesSqlJavaAuditEntity(project, MYSQL);
assertFilesSqlJavaUser(project, databaseType);
assertFilesSqlJavaUserAuthority(project, databaseType);
assertFilesSqlJavaAuditEntity(project, databaseType);

checkSequence(project, MYSQL);
checkSequence(project, databaseType);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package tech.jhipster.lite.generator.server.springboot.user.application;

import static tech.jhipster.lite.TestUtils.*;
import static tech.jhipster.lite.common.domain.FileUtils.getPath;
import static tech.jhipster.lite.common.domain.FileUtils.*;
import static tech.jhipster.lite.generator.project.domain.Constants.*;
import static tech.jhipster.lite.generator.project.domain.DatabaseType.MYSQL;
import static tech.jhipster.lite.generator.project.domain.DatabaseType.*;

import java.util.List;
import tech.jhipster.lite.generator.project.domain.DatabaseType;
import tech.jhipster.lite.generator.project.domain.Project;

Expand Down Expand Up @@ -33,7 +34,7 @@ public static void assertFilesSqlJavaAuditEntity(Project project, DatabaseType d
public static void checkSequence(Project project, DatabaseType databaseType) {
String userPath = getUserPath(project, databaseType);

if (databaseType == MYSQL) {
if (List.of(MYSQL, MARIADB).contains(databaseType)) {
assertFileContent(project, getPath(userPath, "UserEntity.java"), "@GeneratedValue(strategy = GenerationType.IDENTITY)");
assertFileNoContent(project, getPath(userPath, "UserEntity.java"), "@GeneratedValue(strategy = GenerationType.SEQUENCE");
} else {
Expand Down
Loading