Skip to content

Commit

Permalink
Add user context for mariadb and liquibase
Browse files Browse the repository at this point in the history
  • Loading branch information
seraphinandrieux committed Mar 7, 2022
1 parent 62d1343 commit 264561a
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 50 deletions.
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 (isMySQLOrMariaDBDatabase(project)) {
userXmlFile = "user_with_autoincrement.xml";
}
projectRepository.template(project, getUserResourcePath(), userXmlFile, getPath(MAIN_RESOURCES, CHANGELOG), userChangelog);
Expand Down Expand Up @@ -174,4 +172,16 @@ private String getTimestamp() {
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);
}
}
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,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
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package tech.jhipster.lite.generator.server.springboot.user.infrastructure.primary.rest;

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.generator.project.domain.DatabaseType.MYSQL;
import static tech.jhipster.lite.generator.project.domain.DatabaseType.POSTGRESQL;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
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.DisplayName;
Expand All @@ -19,6 +18,7 @@
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
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 @@ -34,6 +34,9 @@ class SpringBootUserResourceIT {
@Autowired
MySQLApplicationService mySQLApplicationService;

@Autowired
MariaDBApplicationService mariaDbApplicationService;

@Autowired
MockMvc mockMvc;

Expand Down Expand Up @@ -82,4 +85,29 @@ void shouldAddUserAndAuthorityEntitiesForMySQL() throws Exception {

checkSequence(project, MYSQL);
}

@Test
@DisplayName("should add user and authority entities for MariaDB")
void shouldAddUserAndAuthorityEntitiesForMariaDB() throws Exception {
ProjectDTO projectDTO = TestUtils.readFileToObject("json/chips.json", ProjectDTO.class).folder(FileUtils.tmpDirForTest());
Project project = ProjectDTO.toProject(projectDTO);

mavenApplicationService.init(project);
springBootApplicationService.init(project);
mariaDbApplicationService.init(project);

mockMvc
.perform(
post("/api/servers/spring-boot/user/mariadb")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtils.convertObjectToJsonBytes(projectDTO))
)
.andExpect(status().isOk());

assertFilesSqlJavaUser(project, MARIADB);
assertFilesSqlJavaUserAuthority(project, MARIADB);
assertFilesSqlJavaAuditEntity(project, MARIADB);

checkSequence(project, MARIADB);
}
}
2 changes: 2 additions & 0 deletions tests-ci/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ elif [[ $application == 'mariadbapp' ]]; then
callApi "/api/servers/spring-boot/mvc/dummy"

callApi "/api/servers/spring-boot/databases/mariadb"
callApi "/api/servers/spring-boot/user/mariadb"
callApi "/api/servers/spring-boot/databases/migration/liquibase/init"
callApi "/api/servers/spring-boot/databases/migration/liquibase/user"

callApi "/api/servers/spring-boot/cache/ehcache/xml-configuration"

Expand Down

0 comments on commit 264561a

Please sign in to comment.