From 10220df92eafdbcbc9bf78b7377292ba4c73b3a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Sat, 25 Jan 2025 22:45:15 +0100 Subject: [PATCH] refactor(jpa): jpa modules now depends on datasource modules This allow to highly simplify jpa module responsibilities --- .../application/JpaApplicationService.java | 5 +- .../database/jpa/domain/DatabaseType.java | 18 -- .../database/jpa/domain/JpaModuleFactory.java | 180 ++++--------- .../jpa/domain/SQLCommonModuleBuilder.java | 136 ---------- .../primary/JpaModuleConfiguration.java | 17 +- .../jpa/DatabaseConfiguration.java.mustache | 2 +- .../server/springboot/database/jpa.feature | 44 ++-- .../jpa/domain/JpaModuleFactoryTest.java | 237 ++++-------------- tests-ci/generate.sh | 18 +- 9 files changed, 139 insertions(+), 518 deletions(-) delete mode 100644 src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/DatabaseType.java delete mode 100644 src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/SQLCommonModuleBuilder.java diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/application/JpaApplicationService.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/application/JpaApplicationService.java index 8a9c8601a17..3bf50ea9f4a 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/application/JpaApplicationService.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/application/JpaApplicationService.java @@ -3,7 +3,6 @@ import org.springframework.stereotype.Service; import tech.jhipster.lite.generator.server.springboot.database.jpa.domain.JpaModuleFactory; import tech.jhipster.lite.module.domain.JHipsterModule; -import tech.jhipster.lite.module.domain.docker.DockerImages; import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; @Service @@ -11,8 +10,8 @@ public class JpaApplicationService { private final JpaModuleFactory factory; - public JpaApplicationService(DockerImages dockerImages) { - factory = new JpaModuleFactory(dockerImages); + public JpaApplicationService() { + factory = new JpaModuleFactory(); } public JHipsterModule buildPostgresql(JHipsterModuleProperties properties) { diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/DatabaseType.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/DatabaseType.java deleted file mode 100644 index 37ce0b56769..00000000000 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/DatabaseType.java +++ /dev/null @@ -1,18 +0,0 @@ -package tech.jhipster.lite.generator.server.springboot.database.jpa.domain; - -enum DatabaseType { - POSTGRESQL("postgresql"), - MYSQL("mysql"), - MARIADB("mariadb"), - MSSQL("mssql"); - - private final String id; - - DatabaseType(String id) { - this.id = id; - } - - public String id() { - return id; - } -} diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/JpaModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/JpaModuleFactory.java index ced259dbc53..a5307661ddd 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/JpaModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/JpaModuleFactory.java @@ -1,174 +1,96 @@ package tech.jhipster.lite.generator.server.springboot.database.jpa.domain; -import static tech.jhipster.lite.generator.server.springboot.database.jpa.domain.SQLCommonModuleBuilder.sqlCommonModuleBuilder; import static tech.jhipster.lite.module.domain.JHipsterModule.artifactId; -import static tech.jhipster.lite.module.domain.JHipsterModule.documentationTitle; import static tech.jhipster.lite.module.domain.JHipsterModule.from; import static tech.jhipster.lite.module.domain.JHipsterModule.groupId; -import static tech.jhipster.lite.module.domain.JHipsterModule.javaDependency; -import static tech.jhipster.lite.module.domain.JHipsterModule.lineBeforeText; -import static tech.jhipster.lite.module.domain.JHipsterModule.path; +import static tech.jhipster.lite.module.domain.JHipsterModule.moduleBuilder; import static tech.jhipster.lite.module.domain.JHipsterModule.propertyKey; import static tech.jhipster.lite.module.domain.JHipsterModule.propertyValue; -import static tech.jhipster.lite.module.domain.JHipsterModule.to; -import static tech.jhipster.lite.module.domain.JHipsterModule.toSrcTestJava; +import static tech.jhipster.lite.module.domain.JHipsterModule.toSrcMainJava; import tech.jhipster.lite.module.domain.JHipsterModule; import tech.jhipster.lite.module.domain.LogLevel; -import tech.jhipster.lite.module.domain.docker.DockerImageVersion; -import tech.jhipster.lite.module.domain.docker.DockerImages; +import tech.jhipster.lite.module.domain.file.JHipsterDestination; import tech.jhipster.lite.module.domain.file.JHipsterSource; -import tech.jhipster.lite.module.domain.javadependency.JavaDependency; -import tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope; +import tech.jhipster.lite.module.domain.javaproperties.PropertyValue; import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; +import tech.jhipster.lite.shared.error.domain.Assert; public class JpaModuleFactory { - public static final String ORG_POSTGRESQL = "org.postgresql"; - private static final String MYSQL = "mysql"; - private static final String MYSQL_GROUP_ID = "com.mysql"; - private static final String MYSQL_ARTIFACT_ID = "mysql-connector-j"; - - private static final String SPRING_DATASOURCE_URL = "spring.datasource.url"; - private static final String SPRING_DATASOURCE_USERNAME = "spring.datasource.username"; - private static final String SPRING_DATASOURCE_DRIVER_CLASS_NAME = "spring.datasource.driver-class-name"; - - private final DockerImages dockerImages; - - public JpaModuleFactory(DockerImages dockerImages) { - this.dockerImages = dockerImages; - } + private static final String ORG_HIBERNATE = "org.hibernate.orm"; + private static final PropertyValue FALSE = propertyValue(false); + private static final PropertyValue TRUE = propertyValue(true); public JHipsterModule buildPostgresql(JHipsterModuleProperties properties) { - DockerImageVersion dockerImage = dockerImages.get("postgres"); - - //@formatter:off - return sqlCommonModuleBuilder( - properties, - DatabaseType.POSTGRESQL, - dockerImage, - documentationTitle("Postgresql"), - artifactId("postgresql") - ) - .javaDependencies() - .addDependency( - JavaDependency.builder() - .groupId(groupId(ORG_POSTGRESQL)) - .artifactId(artifactId("postgresql")) - .scope(JavaDependencyScope.RUNTIME) - .build() - ) - .and() - .springMainProperties() - .set(propertyKey(SPRING_DATASOURCE_URL), propertyValue("jdbc:postgresql://localhost:5432/" + properties.projectBaseName().name())) - .set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue(properties.projectBaseName().name())) - .set(propertyKey(SPRING_DATASOURCE_DRIVER_CLASS_NAME), propertyValue("org.postgresql.Driver")) - .and() - .springTestProperties() - .set( - propertyKey(SPRING_DATASOURCE_URL), - propertyValue( - "jdbc:tc:postgresql:" + dockerImage.version().get() + ":///" + properties.projectBaseName().name() + "?TC_TMPFS=/testtmpfs:rw" - ) - ) - .and() - .springMainLogger(ORG_POSTGRESQL, LogLevel.WARN) - .springTestLogger(ORG_POSTGRESQL, LogLevel.WARN) - .springTestLogger("org.jboss.logging", LogLevel.WARN) - .build(); - //@formatter:on + return sqlCommonModuleBuilder(properties).build(); } public JHipsterModule buildMariaDB(JHipsterModuleProperties properties) { - //@formatter:off - return sqlCommonModuleBuilder( - properties, - DatabaseType.MARIADB, - dockerImages.get("mariadb"), - documentationTitle("MariaDB"), - artifactId("mariadb") - ) - .javaDependencies() - .addDependency( - javaDependency().groupId("org.mariadb.jdbc").artifactId("mariadb-java-client").scope(JavaDependencyScope.RUNTIME).build() - ) - .and() - .springMainProperties() - .set(propertyKey(SPRING_DATASOURCE_URL), propertyValue("jdbc:mariadb://localhost:3306/" + properties.projectBaseName().name())) - .set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue("root")) - .set(propertyKey(SPRING_DATASOURCE_DRIVER_CLASS_NAME), propertyValue("org.mariadb.jdbc.Driver")) - .and() - .build(); - //@formatter:on + return sqlCommonModuleBuilder(properties).build(); } public JHipsterModule buildMySQL(JHipsterModuleProperties properties) { + return sqlCommonModuleBuilder(properties).build(); + } + + public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) { //@formatter:off - return sqlCommonModuleBuilder(properties, DatabaseType.MYSQL, dockerImages.get(MYSQL), documentationTitle("MySQL"), artifactId(MYSQL)) - .javaDependencies() - .addDependency(javaDependency().groupId(MYSQL_GROUP_ID).artifactId(MYSQL_ARTIFACT_ID).scope(JavaDependencyScope.RUNTIME).build()) - .and() + return sqlCommonModuleBuilder(properties) .springMainProperties() - .set(propertyKey(SPRING_DATASOURCE_URL), propertyValue("jdbc:mysql://localhost:3306/" + properties.projectBaseName().name())) - .set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue("root")) - .set(propertyKey(SPRING_DATASOURCE_DRIVER_CLASS_NAME), propertyValue("com.mysql.cj.jdbc.Driver")) + .set(propertyKey("spring.jpa.hibernate.ddl-auto"), propertyValue("update")) + .set(propertyKey("spring.jpa.properties.hibernate.criteria.literal_handling_mode"), propertyValue("BIND")) + .set(propertyKey("spring.jpa.properties.hibernate.dialect"), propertyValue("org.hibernate.dialect.SQLServer2012Dialect")) + .set(propertyKey("spring.jpa.properties.hibernate.format_sql"), propertyValue(true)) + .set(propertyKey("spring.jpa.properties.hibernate.jdbc.fetch_size"), propertyValue(150)) .and() .build(); //@formatter:on } - public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) { - DockerImageVersion dockerImage = dockerImages.get("mcr.microsoft.com/mssql/server"); - JHipsterSource source = from("server/springboot/database/common"); + public static JHipsterModule.JHipsterModuleBuilder sqlCommonModuleBuilder(JHipsterModuleProperties properties) { + Assert.notNull("properties", properties); + + JHipsterSource jpaSource = from("server/springboot/database/jpa"); + JHipsterDestination mainDestination = toSrcMainJava() + .append(properties.packagePath()) + .append("wire/database/infrastructure/secondary/"); //@formatter:off - return sqlCommonModuleBuilder(properties, DatabaseType.MSSQL, dockerImage, documentationTitle("MsSQL"), artifactId("mssqlserver")) + return moduleBuilder(properties) .files() - .add(source.append("docker").template("container-license-acceptance.txt"), to("src/test/resources/container-license-acceptance.txt")) - .add( - source.template("MsSQLTestContainerExtension.java"), - toSrcTestJava().append(properties.basePackage().path()).append("MsSQLTestContainerExtension.java") - ) + .add(jpaSource.template("DatabaseConfiguration.java"), mainDestination.append("DatabaseConfiguration.java")) .and() .javaDependencies() - .addDependency(javaDependency().groupId("com.microsoft.sqlserver").artifactId("mssql-jdbc").scope(JavaDependencyScope.RUNTIME).build()) + .addDependency(groupId("org.springframework.boot"), artifactId("spring-boot-starter-data-jpa")) + .addDependency(groupId(ORG_HIBERNATE), artifactId("hibernate-core")) .and() .springMainProperties() + .set(propertyKey("spring.data.jpa.repositories.bootstrap-mode"), propertyValue("deferred")) + .set(propertyKey("spring.jpa.hibernate.ddl-auto"), propertyValue("none")) .set( - propertyKey(SPRING_DATASOURCE_URL), - propertyValue("jdbc:sqlserver://localhost:1433;database=" + properties.projectBaseName().name() + ";trustServerCertificate=true") + propertyKey("spring.jpa.hibernate.naming.implicit-strategy"), + propertyValue("org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy") ) - .set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue("SA")) - .set(propertyKey("spring.datasource.password"), propertyValue("yourStrong(!)Password")) - .set(propertyKey(SPRING_DATASOURCE_DRIVER_CLASS_NAME), propertyValue("com.microsoft.sqlserver.jdbc.SQLServerDriver")) - .set(propertyKey("spring.jpa.hibernate.ddl-auto"), propertyValue("update")) - .set(propertyKey("spring.jpa.properties.hibernate.criteria.literal_handling_mode"), propertyValue("BIND")) - .set(propertyKey("spring.jpa.properties.hibernate.dialect"), propertyValue("org.hibernate.dialect.SQLServer2012Dialect")) - .set(propertyKey("spring.jpa.properties.hibernate.format_sql"), propertyValue(true)) - .set(propertyKey("spring.jpa.properties.hibernate.jdbc.fetch_size"), propertyValue(150)) - .and() - .springTestProperties() .set( - propertyKey(SPRING_DATASOURCE_URL), - propertyValue( - "jdbc:tc:sqlserver:///;database=" + properties.projectBaseName().name() + ";trustServerCertificate=true?TC_TMPFS=/testtmpfs:rw" - ) + propertyKey("spring.jpa.hibernate.naming.physical-strategy"), + propertyValue("org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy") ) - .set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue("SA")) - .set(propertyKey("spring.datasource.password"), propertyValue("yourStrong(!)Password")) + .set(propertyKey("spring.jpa.open-in-view"), FALSE) + .set(propertyKey("spring.jpa.properties.hibernate.connection.provider_disables_autocommit"), TRUE) + .set(propertyKey("spring.jpa.properties.hibernate.generate_statistics"), FALSE) + .set(propertyKey("spring.jpa.properties.hibernate.jdbc.batch_size"), propertyValue(25)) + .set(propertyKey("spring.jpa.properties.hibernate.jdbc.time_zone"), propertyValue("UTC")) + .set(propertyKey("spring.jpa.properties.hibernate.order_inserts"), TRUE) + .set(propertyKey("spring.jpa.properties.hibernate.order_updates"), TRUE) + .set(propertyKey("spring.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch"), TRUE) + .set(propertyKey("spring.jpa.properties.hibernate.query.in_clause_parameter_padding"), TRUE) .and() - .mandatoryReplacements() - .in(path("src/test/java").append(properties.basePackage().path()).append("IntegrationTest.java")) - .add( - lineBeforeText("import org.springframework.boot.test.context.SpringBootTest;"), - "import org.junit.jupiter.api.extension.ExtendWith;" - ) - .add(lineBeforeText("public @interface"), "@ExtendWith(MsSQLTestContainerExtension.class)") - .and() - .and() - .springMainLogger("com.microsoft.sqlserver", LogLevel.WARN) - .springMainLogger("org.reflections", LogLevel.WARN) - .build(); + .springMainLogger(ORG_HIBERNATE, LogLevel.WARN) + .springMainLogger("org.hibernate.ejb.HibernatePersistence", LogLevel.OFF) + .springTestLogger("org.hibernate.validator", LogLevel.WARN) + .springTestLogger(ORG_HIBERNATE, LogLevel.WARN) + .springTestLogger("org.hibernate.ejb.HibernatePersistence", LogLevel.OFF); //@formatter:on } } diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/SQLCommonModuleBuilder.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/SQLCommonModuleBuilder.java deleted file mode 100644 index 057f43a319b..00000000000 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/SQLCommonModuleBuilder.java +++ /dev/null @@ -1,136 +0,0 @@ -package tech.jhipster.lite.generator.server.springboot.database.jpa.domain; - -import static tech.jhipster.lite.module.domain.JHipsterModule.JHipsterModuleBuilder; -import static tech.jhipster.lite.module.domain.JHipsterModule.artifactId; -import static tech.jhipster.lite.module.domain.JHipsterModule.from; -import static tech.jhipster.lite.module.domain.JHipsterModule.groupId; -import static tech.jhipster.lite.module.domain.JHipsterModule.javaDependency; -import static tech.jhipster.lite.module.domain.JHipsterModule.moduleBuilder; -import static tech.jhipster.lite.module.domain.JHipsterModule.propertyKey; -import static tech.jhipster.lite.module.domain.JHipsterModule.propertyValue; -import static tech.jhipster.lite.module.domain.JHipsterModule.toSrcMainDocker; -import static tech.jhipster.lite.module.domain.JHipsterModule.toSrcMainJava; - -import tech.jhipster.lite.module.domain.DocumentationTitle; -import tech.jhipster.lite.module.domain.LogLevel; -import tech.jhipster.lite.module.domain.docker.DockerImageVersion; -import tech.jhipster.lite.module.domain.file.JHipsterDestination; -import tech.jhipster.lite.module.domain.file.JHipsterSource; -import tech.jhipster.lite.module.domain.javabuild.ArtifactId; -import tech.jhipster.lite.module.domain.javadependency.JavaDependency; -import tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope; -import tech.jhipster.lite.module.domain.javaproperties.PropertyValue; -import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; -import tech.jhipster.lite.shared.error.domain.Assert; - -final class SQLCommonModuleBuilder { - - private static final String ORG_HIBERNATE = "org.hibernate.orm"; - private static final PropertyValue FALSE = propertyValue(false); - private static final PropertyValue TRUE = propertyValue(true); - - private SQLCommonModuleBuilder() {} - - public static JHipsterModuleBuilder sqlCommonModuleBuilder( - JHipsterModuleProperties properties, - DatabaseType databaseType, - DockerImageVersion dockerImage, - DocumentationTitle documentationTitle, - ArtifactId testContainerArtifactId - ) { - Assert.notNull("properties", properties); - Assert.notNull("databaseType", databaseType); - Assert.notNull("dockerImage", dockerImage); - Assert.notNull("documentationTitle", documentationTitle); - Assert.notNull("testContainerArtifactId", testContainerArtifactId); - - String databaseId = databaseType.id(); - JHipsterSource commonSource = from("server/springboot/database/common"); - JHipsterSource jpaSource = from("server/springboot/database/jpa"); - JHipsterDestination mainDestination = toSrcMainJava() - .append(properties.packagePath()) - .append("wire") - .append(databaseId) - .append("infrastructure/secondary/"); - - //@formatter:off - return moduleBuilder(properties) - .context() - .put("srcMainDocker", "src/main/docker") // To be used in >.md file - .put("databaseType", databaseId) - .put(databaseId + "DockerImageWithVersion", dockerImage.fullName()) // To be used in .yml docker-compose file - .and() - .documentation(documentationTitle, commonSource.template("databaseType.md")) - .startupCommands() - .dockerCompose(startupCommand(databaseId)) - .and() - .files() - .add(jpaSource.template("DatabaseConfiguration.java"), mainDestination.append("DatabaseConfiguration.java")) - .add(commonSource.append("docker").template(databaseId + ".yml"), toSrcMainDocker().append(databaseId + ".yml")) - .and() - .javaDependencies() - .addDependency(groupId("org.springframework.boot"), artifactId("spring-boot-starter-data-jpa")) - .addDependency(groupId("com.zaxxer"), artifactId("HikariCP")) - .addDependency(groupId(ORG_HIBERNATE), artifactId("hibernate-core")) - .addDependency(testContainer(testContainerArtifactId)) - .and() - .springMainProperties() - .set(propertyKey("spring.datasource.password"), propertyValue("")) - .set(propertyKey("spring.datasource.type"), propertyValue("com.zaxxer.hikari.HikariDataSource")) - .set(propertyKey("spring.datasource.hikari.poolName"), propertyValue("Hikari")) - .set(propertyKey("spring.datasource.hikari.auto-commit"), FALSE) - .set(propertyKey("spring.data.jpa.repositories.bootstrap-mode"), propertyValue("deferred")) - .set(propertyKey("spring.jpa.hibernate.ddl-auto"), propertyValue("none")) - .set( - propertyKey("spring.jpa.hibernate.naming.implicit-strategy"), - propertyValue("org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy") - ) - .set( - propertyKey("spring.jpa.hibernate.naming.physical-strategy"), - propertyValue("org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy") - ) - .set(propertyKey("spring.jpa.open-in-view"), FALSE) - .set(propertyKey("spring.jpa.properties.hibernate.connection.provider_disables_autocommit"), TRUE) - .set(propertyKey("spring.jpa.properties.hibernate.generate_statistics"), FALSE) - .set(propertyKey("spring.jpa.properties.hibernate.jdbc.batch_size"), propertyValue(25)) - .set(propertyKey("spring.jpa.properties.hibernate.jdbc.time_zone"), propertyValue("UTC")) - .set(propertyKey("spring.jpa.properties.hibernate.order_inserts"), TRUE) - .set(propertyKey("spring.jpa.properties.hibernate.order_updates"), TRUE) - .set(propertyKey("spring.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch"), TRUE) - .set(propertyKey("spring.jpa.properties.hibernate.query.in_clause_parameter_padding"), TRUE) - .and() - .springTestProperties() - .set( - propertyKey("spring.datasource.url"), - propertyValue("jdbc:tc:" + dockerImage.fullName() + ":///" + properties.projectBaseName().name()) - ) - .set(propertyKey("spring.datasource.username"), propertyValue(properties.projectBaseName().name())) - .set(propertyKey("spring.datasource.password"), propertyValue("")) - .set(propertyKey("spring.datasource.driver-class-name"), propertyValue("org.testcontainers.jdbc.ContainerDatabaseDriver")) - .set(propertyKey("spring.datasource.hikari.maximum-pool-size"), propertyValue(2)) - .and() - .springMainLogger("org.hibernate.validator", LogLevel.WARN) - .springMainLogger(ORG_HIBERNATE, LogLevel.WARN) - .springMainLogger("org.hibernate.ejb.HibernatePersistence", LogLevel.OFF) - .springTestLogger("org.hibernate.validator", LogLevel.WARN) - .springTestLogger(ORG_HIBERNATE, LogLevel.WARN) - .springTestLogger("org.hibernate.ejb.HibernatePersistence", LogLevel.OFF) - .springTestLogger("com.github.dockerjava", LogLevel.WARN) - .springTestLogger("org.testcontainers", LogLevel.WARN); - //@formatter:on - } - - private static JavaDependency testContainer(ArtifactId testContainerArtifactId) { - return javaDependency() - .groupId("org.testcontainers") - .artifactId(testContainerArtifactId) - .dependencySlug("%s-%s".formatted("testcontainers", testContainerArtifactId)) - .versionSlug("testcontainers") - .scope(JavaDependencyScope.TEST) - .build(); - } - - private static String startupCommand(String databaseId) { - return "src/main/docker/" + databaseId + ".yml"; - } -} diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/infrastructure/primary/JpaModuleConfiguration.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/infrastructure/primary/JpaModuleConfiguration.java index ebaf035fd82..2ad722f1b32 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/infrastructure/primary/JpaModuleConfiguration.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jpa/infrastructure/primary/JpaModuleConfiguration.java @@ -1,11 +1,14 @@ package tech.jhipster.lite.generator.server.springboot.database.jpa.infrastructure.primary; import static tech.jhipster.lite.shared.slug.domain.JHLiteFeatureSlug.JPA_PERSISTENCE; +import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.DATASOURCE_MARIADB; +import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.DATASOURCE_MSSQL; +import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.DATASOURCE_MYSQL; +import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.DATASOURCE_POSTGRESQL; import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.JPA_MARIADB; import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.JPA_MSSQL; import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.JPA_MYSQL; import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.JPA_POSTGRESQL; -import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.SPRING_BOOT; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -25,7 +28,7 @@ JHipsterModuleResource jpaPostgresqlModule(JpaApplicationService postgresql) { .slug(JPA_POSTGRESQL) .propertiesDefinition(properties()) .apiDoc(API_DOC_GROUP, "Add JPA with Postgresql to project") - .organization(organization()) + .organization(JHipsterModuleOrganization.builder().feature(JPA_PERSISTENCE).addDependency(DATASOURCE_POSTGRESQL).build()) .tags(tags()) .factory(postgresql::buildPostgresql); } @@ -36,7 +39,7 @@ JHipsterModuleResource jpaMariaDBModule(JpaApplicationService applicationService .slug(JPA_MARIADB) .propertiesDefinition(properties()) .apiDoc(API_DOC_GROUP, "Add JPA with MariaDB to project") - .organization(organization()) + .organization(JHipsterModuleOrganization.builder().feature(JPA_PERSISTENCE).addDependency(DATASOURCE_MARIADB).build()) .tags(tags()) .factory(applicationService::buildMariaDB); } @@ -47,7 +50,7 @@ JHipsterModuleResource jpaMmySQLModule(JpaApplicationService applicationService) .slug(JPA_MYSQL) .propertiesDefinition(properties()) .apiDoc(API_DOC_GROUP, "Add JPA with MySQL to project") - .organization(organization()) + .organization(JHipsterModuleOrganization.builder().feature(JPA_PERSISTENCE).addDependency(DATASOURCE_MYSQL).build()) .tags(tags()) .factory(applicationService::buildMySQL); } @@ -58,7 +61,7 @@ JHipsterModuleResource jpaMsSQLModule(JpaApplicationService applicationService) .slug(JPA_MSSQL) .propertiesDefinition(properties()) .apiDoc(API_DOC_GROUP, "Add JPA with MsSQL to project") - .organization(organization()) + .organization(JHipsterModuleOrganization.builder().feature(JPA_PERSISTENCE).addDependency(DATASOURCE_MSSQL).build()) .tags(tags()) .factory(applicationService::buildMsSQL); } @@ -72,10 +75,6 @@ private static JHipsterModulePropertiesDefinition properties() { .build(); } - private static JHipsterModuleOrganization organization() { - return JHipsterModuleOrganization.builder().feature(JPA_PERSISTENCE).addDependency(SPRING_BOOT).build(); - } - private static String[] tags() { return new String[] { "server", "spring", "spring-boot", "database" }; } diff --git a/src/main/resources/generator/server/springboot/database/jpa/DatabaseConfiguration.java.mustache b/src/main/resources/generator/server/springboot/database/jpa/DatabaseConfiguration.java.mustache index 584a526d028..f294bfeb266 100644 --- a/src/main/resources/generator/server/springboot/database/jpa/DatabaseConfiguration.java.mustache +++ b/src/main/resources/generator/server/springboot/database/jpa/DatabaseConfiguration.java.mustache @@ -1,4 +1,4 @@ -package {{packageName}}.wire.{{databaseType}}.infrastructure.secondary; +package {{packageName}}.wire.database.infrastructure.secondary; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; diff --git a/src/test/features/server/springboot/database/jpa.feature b/src/test/features/server/springboot/database/jpa.feature index c1fae6afdfa..d9fbd6b4cd3 100644 --- a/src/test/features/server/springboot/database/jpa.feature +++ b/src/test/features/server/springboot/database/jpa.feature @@ -2,21 +2,20 @@ Feature: JPA modules Scenario: Should add PostgresSQL elements using legacy url When I apply modules to default project - | maven-java | - | spring-boot | - | jpa-postgresql | + | maven-java | + | spring-boot | + | datasource-postgresql | + | jpa-postgresql | Then I should have files in "" | pom.xml | And I should have files in "documentation" | postgresql.md | And I should have files in "src/main/docker" | postgresql.yml | - And I should have files in "src/main/java/tech/jhipster/chips/wire/postgresql/infrastructure/secondary" + And I should have files in "src/main/java/tech/jhipster/chips/wire/database/infrastructure/secondary" | DatabaseConfiguration.java | And I should have files in "src/main/resources/config" | application.yml | - And I should have files in "src/test/resources/config" - | application-test.yml | Scenario: Should get PostgresSQL module properties definition When I get module "jpa-postgresql" properties definition @@ -29,21 +28,20 @@ Feature: JPA modules Scenario: Should add MariaDB elements using legacy url When I apply modules to default project - | maven-java | - | spring-boot | - | jpa-mariadb | + | maven-java | + | spring-boot | + | datasource-mariadb | + | jpa-mariadb | Then I should have files in "" | pom.xml | And I should have files in "documentation" | mariadb.md | And I should have files in "src/main/docker" | mariadb.yml | - And I should have files in "src/main/java/tech/jhipster/chips/wire/mariadb/infrastructure/secondary" + And I should have files in "src/main/java/tech/jhipster/chips/wire/database/infrastructure/secondary" | DatabaseConfiguration.java | And I should have files in "src/main/resources/config" | application.yml | - And I should have files in "src/test/resources/config" - | application-test.yml | Scenario: Should get MariaDB module properties definition When I get module "jpa-mariadb" properties definition @@ -56,21 +54,20 @@ Feature: JPA modules Scenario: Should add MySQL elements using legacy url When I apply modules to default project - | maven-java | - | spring-boot | - | jpa-mysql | + | maven-java | + | spring-boot | + | datasource-mysql | + | jpa-mysql | Then I should have files in "" | pom.xml | And I should have files in "documentation" | mysql.md | And I should have files in "src/main/docker" | mysql.yml | - And I should have files in "src/main/java/tech/jhipster/chips/wire/mysql/infrastructure/secondary" + And I should have files in "src/main/java/tech/jhipster/chips/wire/database/infrastructure/secondary" | DatabaseConfiguration.java | And I should have files in "src/main/resources/config" | application.yml | - And I should have files in "src/test/resources/config" - | application-test.yml | Scenario: Should get MySQL module properties definition When I get module "jpa-mysql" properties definition @@ -83,16 +80,17 @@ Feature: JPA modules Scenario: Should add MsSQL elements using legacy url When I apply modules to default project - | maven-java | - | spring-boot | - | jpa-mssql | + | maven-java | + | spring-boot | + | datasource-mssql | + | jpa-mssql | Then I should have files in "" | pom.xml | And I should have files in "documentation" | mssql.md | And I should have files in "src/main/docker" | mssql.yml | - And I should have files in "src/main/java/tech/jhipster/chips/wire/mssql/infrastructure/secondary" + And I should have files in "src/main/java/tech/jhipster/chips/wire/database/infrastructure/secondary" | DatabaseConfiguration.java | And I should have files in "src/test/java/tech/jhipster/chips" | MsSQLTestContainerExtension.java | @@ -100,8 +98,6 @@ Feature: JPA modules | container-license-acceptance.txt | And I should have files in "src/main/resources/config" | application.yml | - And I should have files in "src/test/resources/config" - | application-test.yml | Scenario: Should get MsSQL module properties definition When I get module "jpa-mssql" properties definition diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/JpaModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/JpaModuleFactoryTest.java index e4a2128b9c9..36f267861a3 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/JpaModuleFactoryTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/database/jpa/domain/JpaModuleFactoryTest.java @@ -1,33 +1,19 @@ package tech.jhipster.lite.generator.server.springboot.database.jpa.domain; -import static org.mockito.Mockito.when; import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.assertThatModuleWithFiles; -import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.file; import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.pomFile; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import tech.jhipster.lite.TestFileUtils; import tech.jhipster.lite.UnitTest; import tech.jhipster.lite.module.domain.JHipsterModule; import tech.jhipster.lite.module.domain.JHipsterModulesFixture; -import tech.jhipster.lite.module.domain.docker.DockerImageVersion; -import tech.jhipster.lite.module.domain.docker.DockerImages; import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; -import tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions; @UnitTest -@ExtendWith(MockitoExtension.class) class JpaModuleFactoryTest { - @Mock - private DockerImages dockerImages; - - @InjectMocks - private JpaModuleFactory factory; + private final JpaModuleFactory factory = new JpaModuleFactory(); @Test void shouldCreatePostgresqlModule() { @@ -36,32 +22,25 @@ void shouldCreatePostgresqlModule() { .projectBaseName("myapp") .build(); - when(dockerImages.get("postgres")).thenReturn(new DockerImageVersion("postgres", "0.0.0")); - JHipsterModule module = factory.buildPostgresql(properties); assertThatModuleWithFiles(module, pomFile()) - .hasFile("src/main/java/tech/jhipster/jhlitest/wire/postgresql/infrastructure/secondary/DatabaseConfiguration.java") - .containing("package tech.jhipster.jhlitest.wire.postgresql.infrastructure.secondary;") - .and() - .hasFile("documentation/postgresql.md") - .containing("docker compose -f src/main/docker/postgresql.yml up -d") + .hasFile("src/main/java/tech/jhipster/jhlitest/wire/database/infrastructure/secondary/DatabaseConfiguration.java") + .containing("package tech.jhipster.jhlitest.wire.database.infrastructure.secondary;") .and() .hasFile("pom.xml") .containing( """ - - org.postgresql - postgresql - runtime - + org.springframework.boot + spring-boot-starter-data-jpa\ + """ + ) + .containing( + """ + org.hibernate.orm + hibernate-core\ """ ) - .containing("com.zaxxer") - .containing("HikariCP") - .containing("org.hibernate.orm") - .containing("hibernate-core") - .containing("org.testcontainers") .and() .hasFile("src/main/resources/config/application.yml") .containing( @@ -71,15 +50,6 @@ void shouldCreatePostgresqlModule() { jpa: repositories: bootstrap-mode: deferred - datasource: - driver-class-name: org.postgresql.Driver - hikari: - auto-commit: false - poolName: Hikari - password: '' - type: com.zaxxer.hikari.HikariDataSource - url: jdbc:postgresql://localhost:5432/myapp - username: myapp jpa: hibernate: ddl-auto: none @@ -101,20 +71,6 @@ void shouldCreatePostgresqlModule() { fail_on_pagination_over_collection_fetch: true in_clause_parameter_padding: true """ - ) - .and() - .hasFile("src/test/resources/config/application-test.yml") - .containing( - """ - spring: - datasource: - driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver - hikari: - maximum-pool-size: 2 - password: '' - url: jdbc:tc:postgresql:0.0.0:///myapp?TC_TMPFS=/testtmpfs:rw - username: myapp - """ ); } @@ -125,36 +81,25 @@ void shouldCreateMariadbModule() { .projectBaseName("myapp") .build(); - when(dockerImages.get("mariadb")).thenReturn(new DockerImageVersion("mariadb", "0.0.0")); - JHipsterModule module = factory.buildMariaDB(properties); assertThatModuleWithFiles(module, pomFile()) - .hasFile("documentation/mariadb.md") - .containing("docker compose -f src/main/docker/mariadb.yml up -d") + .hasFile("src/main/java/tech/jhipster/jhlitest/wire/database/infrastructure/secondary/DatabaseConfiguration.java") + .containing("package tech.jhipster.jhlitest.wire.database.infrastructure.secondary;") .and() - .hasFile("src/main/java/tech/jhipster/jhlitest/wire/mariadb/infrastructure/secondary/DatabaseConfiguration.java") - .containing("package tech.jhipster.jhlitest.wire.mariadb.infrastructure.secondary;") - .and() - .hasPrefixedFiles("src/main/docker", "mariadb.yml") .hasFile("pom.xml") - .containing("org.springframework.boot") - .containing("spring-boot-starter-data-jpa") .containing( """ - - org.mariadb.jdbc - mariadb-java-client - runtime - + org.springframework.boot + spring-boot-starter-data-jpa\ + """ + ) + .containing( + """ + org.hibernate.orm + hibernate-core\ """ ) - .containing("com.zaxxer") - .containing("HikariCP") - .containing("org.hibernate.orm") - .containing("hibernate-core") - .containing("org.testcontainers") - .containing("mariadb") .and() .hasFile("src/main/resources/config/application.yml") .containing( @@ -164,15 +109,6 @@ void shouldCreateMariadbModule() { jpa: repositories: bootstrap-mode: deferred - datasource: - driver-class-name: org.mariadb.jdbc.Driver - hikari: - auto-commit: false - poolName: Hikari - password: '' - type: com.zaxxer.hikari.HikariDataSource - url: jdbc:mariadb://localhost:3306/myapp - username: root jpa: hibernate: ddl-auto: none @@ -194,20 +130,6 @@ void shouldCreateMariadbModule() { fail_on_pagination_over_collection_fetch: true in_clause_parameter_padding: true """ - ) - .and() - .hasFile("src/test/resources/config/application-test.yml") - .containing( - """ - spring: - datasource: - driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver - hikari: - maximum-pool-size: 2 - password: '' - url: jdbc:tc:mariadb:0.0.0:///myapp - username: myapp - """ ); } @@ -218,36 +140,25 @@ void shouldCreateMysqlModule() { .projectBaseName("myapp") .build(); - when(dockerImages.get("mysql")).thenReturn(new DockerImageVersion("mysql", "0.0.0")); - JHipsterModule module = factory.buildMySQL(properties); assertThatModuleWithFiles(module, pomFile()) - .hasFile("documentation/mysql.md") - .containing("docker compose -f src/main/docker/mysql.yml up -d") - .and() - .hasFile("src/main/java/tech/jhipster/jhlitest/wire/mysql/infrastructure/secondary/DatabaseConfiguration.java") - .containing("package tech.jhipster.jhlitest.wire.mysql.infrastructure.secondary;") + .hasFile("src/main/java/tech/jhipster/jhlitest/wire/database/infrastructure/secondary/DatabaseConfiguration.java") + .containing("package tech.jhipster.jhlitest.wire.database.infrastructure.secondary;") .and() - .hasPrefixedFiles("src/main/docker", "mysql.yml") .hasFile("pom.xml") - .containing("org.springframework.boot") - .containing("spring-boot-starter-data-jpa") .containing( """ - - com.mysql - mysql-connector-j - runtime - + org.springframework.boot + spring-boot-starter-data-jpa\ + """ + ) + .containing( + """ + org.hibernate.orm + hibernate-core\ """ ) - .containing("com.zaxxer") - .containing("HikariCP") - .containing("org.hibernate.orm") - .containing("hibernate-core") - .containing("org.testcontainers") - .containing("mysql") .and() .hasFile("src/main/resources/config/application.yml") .containing( @@ -258,15 +169,6 @@ void shouldCreateMysqlModule() { jpa: repositories: bootstrap-mode: deferred - datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - hikari: - auto-commit: false - poolName: Hikari - password: '' - type: com.zaxxer.hikari.HikariDataSource - url: jdbc:mysql://localhost:3306/myapp - username: root jpa: hibernate: ddl-auto: none @@ -288,21 +190,6 @@ void shouldCreateMysqlModule() { fail_on_pagination_over_collection_fetch: true in_clause_parameter_padding: true """ - ) - .and() - .hasFile("src/test/resources/config/application-test.yml") - .containing( - // language=yaml - """ - spring: - datasource: - driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver - hikari: - maximum-pool-size: 2 - password: '' - url: jdbc:tc:mysql:0.0.0:///myapp - username: myapp - """ ); } @@ -313,37 +200,25 @@ void shouldCreateMssqlModule() { .projectBaseName("myapp") .build(); - when(dockerImages.get("mcr.microsoft.com/mssql/server")).thenReturn(new DockerImageVersion("mcr.microsoft.com/mssql/server", "0.0.0")); - JHipsterModule module = factory.buildMsSQL(properties); - assertThatModuleWithFiles(module, pomFile(), integrationTestAnnotation()) - .hasFile("src/main/java/tech/jhipster/jhlitest/wire/mssql/infrastructure/secondary/DatabaseConfiguration.java") - .containing("package tech.jhipster.jhlitest.wire.mssql.infrastructure.secondary;") - .and() - .hasFile("documentation/mssql.md") - .containing("docker compose -f src/main/docker/mssql.yml up -d") - .and() - .hasFile("src/test/java/tech/jhipster/jhlitest/MsSQLTestContainerExtension.java") - .and() - .hasFile("src/test/resources/container-license-acceptance.txt") + assertThatModuleWithFiles(module, pomFile()) + .hasFile("src/main/java/tech/jhipster/jhlitest/wire/database/infrastructure/secondary/DatabaseConfiguration.java") + .containing("package tech.jhipster.jhlitest.wire.database.infrastructure.secondary;") .and() .hasFile("pom.xml") .containing( """ - - com.microsoft.sqlserver - mssql-jdbc - runtime - + org.springframework.boot + spring-boot-starter-data-jpa\ + """ + ) + .containing( + """ + org.hibernate.orm + hibernate-core\ """ ) - .containing("com.zaxxer") - .containing("HikariCP") - .containing("org.hibernate.orm") - .containing("hibernate-core") - .containing("org.testcontainers") - .containing("mssqlserver") .and() .hasFile("src/main/resources/config/application.yml") .containing( @@ -354,15 +229,6 @@ void shouldCreateMssqlModule() { jpa: repositories: bootstrap-mode: deferred - datasource: - driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver - hikari: - auto-commit: false - poolName: Hikari - password: yourStrong(!)Password - type: com.zaxxer.hikari.HikariDataSource - url: jdbc:sqlserver://localhost:1433;database=myapp;trustServerCertificate=true - username: SA jpa: hibernate: ddl-auto: update @@ -389,25 +255,6 @@ void shouldCreateMssqlModule() { fail_on_pagination_over_collection_fetch: true in_clause_parameter_padding: true """ - ) - .and() - .hasFile("src/test/resources/config/application-test.yml") - .containing( - // language=yaml - """ - spring: - datasource: - driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver - hikari: - maximum-pool-size: 2 - password: yourStrong(!)Password - url: jdbc:tc:sqlserver:///;database=myapp;trustServerCertificate=true?TC_TMPFS=/testtmpfs:rw - username: SA - """ ); } - - private JHipsterModulesAssertions.ModuleFile integrationTestAnnotation() { - return file("src/test/resources/projects/files/IntegrationTest.java", "src/test/java/tech/jhipster/jhlitest/IntegrationTest.java"); - } } diff --git a/tests-ci/generate.sh b/tests-ci/generate.sh index 7b96735dfbc..4fb7847cfee 100755 --- a/tests-ci/generate.sh +++ b/tests-ci/generate.sh @@ -167,6 +167,7 @@ elif [[ $application == fullapp* ]]; then apply_modules "spring-boot-cucumber-jpa-reset" apply_modules \ + "datasource-postgresql" \ "jpa-postgresql" \ "liquibase" \ "liquibase-linter" \ @@ -220,6 +221,7 @@ elif [[ $application == 'mysqlapp' ]]; then sonar_back apply_modules \ + "datasource-mysql" \ "jpa-mysql" \ "liquibase" @@ -242,7 +244,10 @@ elif [[ $application == 'mariadbapp' ]]; then sonar_back apply_modules "springdoc-mvc-openapi" - apply_modules "jpa-mariadb" "liquibase" + apply_modules \ + "datasource-mariadb" \ + "jpa-mariadb" \ + "liquibase" apply_modules "ehcache-xml-config" elif [[ $application == 'mssqlapp' ]]; then @@ -251,14 +256,20 @@ elif [[ $application == 'mssqlapp' ]]; then sonar_back apply_modules "springdoc-mvc-openapi" - apply_modules "jpa-mssql" + apply_modules \ + "datasource-mssql" \ + "jpa-mssql" elif [[ $application == 'flywayapp' ]]; then init_server spring_boot_mvc sonar_back - apply_modules "jpa-postgresql" "flyway" "flyway-postgresql" + apply_modules \ + "datasource-postgresql" \ + "jpa-postgresql" \ + "flyway" \ + "flyway-postgresql" cucumber_with_jwt apply_modules "spring-boot-cucumber-jpa-reset" @@ -276,6 +287,7 @@ elif [[ $application == 'undertowapp' ]]; then sonar_back apply_modules \ + "datasource-mysql" \ "jpa-mysql" \ "flyway" \ "flyway-mysql"