Skip to content

Commit

Permalink
Merge pull request #2183 from antarus/fix/postgresql-error
Browse files Browse the repository at this point in the history
fix missing common properties in postgresql module
  • Loading branch information
DanielFran authored Jun 20, 2022
2 parents ef26edd + e98c22d commit dea52e7
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class PostgresqlModuleFactory {
public static final String MAIN_RESOURCES = "src/main/resources";
public static final String LOGGING_TEST_CONFIGURATION = "logback.xml";
public static final String ORG_POSTGRESQL = "org.postgresql";
public static final String ORG_HIBERNATE = "org.hibernate";
public static final String FALSE = "false";
public static final String TRUE = "true";

private final DockerImages dockerImages;

Expand All @@ -35,62 +38,117 @@ public PostgresqlModuleFactory(DockerImages dockerImages) {

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);

String packagePath = properties.basePackage().path();
String applicationName = properties.projectBaseName().capitalized();
JHipsterSource source = from(SOURCE);
DockerImage postgresqlDockerImage = dockerImages.get(POSTGRESQL_DOCKER_IMAGE_NAME);
JHipsterDestination databasePath = toSrcMainJava().append(packagePath).append(DEST_SECONDARY);

//@formatter:off
return moduleBuilder(properties)
JHipsterModuleBuilder builder = moduleBuilder(properties);
appendContext(builder, properties, postgresqlDockerImage);
appendDocumentation(builder);
appendFiles(builder, properties);
appendJavaDependencies(builder);
appendSpringProperties(builder, properties, postgresqlDockerImage);
appendReplacements(builder);

return builder.build();
}

private void appendContext(JHipsterModuleBuilder builder, JHipsterModuleProperties properties, DockerImage dockerImage) {
builder
.context()
.packageName(properties.basePackage())
.put("applicationName", applicationName)
.put("srcDocker", SRC_MAIN_DOCKER)
.put("postgresqlDockerImageWithVersion", postgresqlDockerImage.fullName())
.and()
.documentation(documentationTitle("Postgresql"), from("server/springboot/database/postgresql/postgresql.md.mustache"))
.put("applicationName", properties.projectBaseName().capitalized())
.put("srcDocker", SRC_MAIN_DOCKER)
.put("postgresqlDockerImageWithVersion", dockerImage.fullName());
}

private void appendDocumentation(JHipsterModuleBuilder builder) {
builder.documentation(documentationTitle("Postgresql"), from("server/springboot/database/postgresql/postgresql.md.mustache"));
}

private void appendFiles(JHipsterModuleBuilder builder, JHipsterModuleProperties properties) {
JHipsterSource source = from(SOURCE);
JHipsterDestination databasePath = toSrcMainJava().append(properties.basePackage().path()).append(DEST_SECONDARY);
builder
.files()
.add(source.template("DatabaseConfiguration.java"), databasePath.append("DatabaseConfiguration.java"))
.add(source.template("FixedPostgreSQL10Dialect.java"), databasePath.append("FixedPostgreSQL10Dialect.java"))
.add(source.template("FixedPostgreSQL10DialectTest.java"), toSrcTestJava().append(packagePath).append(DEST_SECONDARY).append("FixedPostgreSQL10DialectTest.java"))
.add(source.template("postgresql.yml"), toSrcMainDocker().append("postgresql.yml"))
.and()
.javaDependencies()
.add(springDataJpa())
.add(psqlDriver())
.add(psqlHikari())
.add(psqlHibernateCore())
.add(testContainer())
.and()
.add(source.template("DatabaseConfiguration.java"), databasePath.append("DatabaseConfiguration.java"))
.add(source.template("FixedPostgreSQL10Dialect.java"), databasePath.append("FixedPostgreSQL10Dialect.java"))
.add(
source.template("FixedPostgreSQL10DialectTest.java"),
toSrcTestJava().append(properties.basePackage().path()).append(DEST_SECONDARY).append("FixedPostgreSQL10DialectTest.java")
)
.add(source.template("postgresql.yml"), toSrcMainDocker().append("postgresql.yml"));
}

private void appendJavaDependencies(JHipsterModuleBuilder builder) {
builder.javaDependencies().add(springDataJpa()).add(psqlDriver()).add(psqlHikari()).add(psqlHibernateCore()).add(testContainer());
}

private void appendSpringProperties(JHipsterModuleBuilder builder, JHipsterModuleProperties properties, DockerImage dockerImage) {
builder
.springMainProperties()
.set(springDatasourceUrl(), propertyValue("jdbc:postgresql://localhost:5432/" + properties.projectBaseName().name()))
.set(springDatasourceUsername(), propertyValue(properties.projectBaseName().name()))
.set(springDatasourcePassword(), propertyValue(""))
.set(springDatasourceDriverClassName(), propertyValue("org.postgresql.Driver"))
.set(propertyKey("spring.jpa.database-platform"), propertyValue(properties.basePackage().basePackage() + ".technical.infrastructure.secondary.postgresql.FixedPostgreSQL10Dialect"))
.and()
.set(springDatasourceUrl(), propertyValue("jdbc:postgresql://localhost:5432/" + properties.projectBaseName().name()))
.set(springDatasourceUsername(), propertyValue(properties.projectBaseName().name()))
.set(springDatasourcePassword(), propertyValue(""))
.set(springDatasourceDriverClassName(), propertyValue("org.postgresql.Driver"))
.set(
propertyKey("spring.jpa.database-platform"),
propertyValue(properties.basePackage().basePackage() + ".technical.infrastructure.secondary.postgresql.FixedPostgreSQL10Dialect")
)
.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"), propertyValue(FALSE))
.set(propertyKey("spring.data.jpa.repositories.bootstrap-mode"), propertyValue("deferred"))
.set(propertyKey("spring.jpa.properties.hibernate.jdbc.time_zone"), propertyValue("UTC"))
.set(propertyKey("spring.jpa.open-in-view"), propertyValue(FALSE))
.set(propertyKey("spring.jpa.properties.hibernate.id.new_generator_mappings"), propertyValue(TRUE))
.set(propertyKey("spring.jpa.properties.hibernate.connection.provider_disables_autocommit"), propertyValue(TRUE))
.set(propertyKey("spring.jpa.properties.hibernate.generate_statistics"), propertyValue(FALSE))
.set(propertyKey("spring.jpa.properties.hibernate.jdbc.batch_size"), propertyValue("25"))
.set(propertyKey("spring.jpa.properties.hibernate.order_inserts"), propertyValue(TRUE))
.set(propertyKey("spring.jpa.properties.hibernate.order_updates"), propertyValue(TRUE))
.set(propertyKey("spring.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch"), propertyValue(TRUE))
.set(propertyKey("spring.jpa.properties.hibernate.query.in_clause_parameter_padding"), propertyValue(TRUE))
.set(propertyKey("spring.jpa.hibernate.ddl-auto"), propertyValue("none"))
.set(
propertyKey("spring.jpa.hibernate.naming.physical-strategy"),
propertyValue("org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy")
)
.set(
propertyKey("spring.jpa.hibernate.naming.implicit-strategy"),
propertyValue("org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy")
);

builder
.springTestProperties()
.set(springDatasourceDriverClassName(), propertyValue("org.testcontainers.jdbc.ContainerDatabaseDriver"))
.set(springDatasourceUrl(), propertyValue("jdbc:tc:postgresql:" + postgresqlDockerImage.version()+ ":///" + properties.projectBaseName().name() + "?TC_TMPFS=/testtmpfs:rw"))
.set(springDatasourceUsername(), propertyValue(properties.projectBaseName().name()))
.set(springDatasourcePassword(), propertyValue(""))
.set(propertyKey("spring.datasource.hikari.maximum-pool-size"), propertyValue("2"))
.and()
.set(springDatasourceDriverClassName(), propertyValue("org.testcontainers.jdbc.ContainerDatabaseDriver"))
.set(
springDatasourceUrl(),
propertyValue(
"jdbc:tc:postgresql:" + dockerImage.version() + ":///" + properties.projectBaseName().name() + "?TC_TMPFS=/testtmpfs:rw"
)
)
.set(springDatasourceUsername(), propertyValue(properties.projectBaseName().name()))
.set(springDatasourcePassword(), propertyValue(""))
.set(propertyKey("spring.datasource.hikari.maximum-pool-size"), propertyValue("2"));
}

private void appendReplacements(JHipsterModuleBuilder builder) {
builder
.optionalReplacements()
.in("src/main/resources/logback-spring.xml")
.add(text(NEEDLE_LOGBACK_LOGGER), logger(ORG_POSTGRESQL, Level.WARN))
.and()
.in("src/test/resources/logback.xml")
.add(text(NEEDLE_LOGBACK_LOGGER), logger(ORG_POSTGRESQL, Level.WARN))
.add(text(NEEDLE_LOGBACK_LOGGER), logger("com.github.dockerjava", Level.WARN))
.add(text(NEEDLE_LOGBACK_LOGGER), logger("org.testcontainers", Level.WARN))
.add(text(NEEDLE_LOGBACK_LOGGER), logger("org.jboss.logging", Level.WARN))
.and()
.and()
.build();
//@formatter:on
.in("src/main/resources/logback-spring.xml")
.add(text(NEEDLE_LOGBACK_LOGGER), logger(ORG_POSTGRESQL, Level.WARN))
.add(text(NEEDLE_LOGBACK_LOGGER), logger("org.hibernate.validator", Level.WARN))
.add(text(NEEDLE_LOGBACK_LOGGER), logger(ORG_HIBERNATE, Level.WARN))
.add(text(NEEDLE_LOGBACK_LOGGER), logger("org.hibernate.ejb.HibernatePersistence", Level.OFF))
.and()
.in("src/test/resources/logback.xml")
.add(text(NEEDLE_LOGBACK_LOGGER), logger(ORG_POSTGRESQL, Level.WARN))
.add(text(NEEDLE_LOGBACK_LOGGER), logger("org.hibernate.validator", Level.WARN))
.add(text(NEEDLE_LOGBACK_LOGGER), logger(ORG_HIBERNATE, Level.WARN))
.add(text(NEEDLE_LOGBACK_LOGGER), logger("org.hibernate.ejb.HibernatePersistence", Level.OFF))
.add(text(NEEDLE_LOGBACK_LOGGER), logger("com.github.dockerjava", Level.WARN))
.add(text(NEEDLE_LOGBACK_LOGGER), logger("org.testcontainers", Level.WARN))
.add(text(NEEDLE_LOGBACK_LOGGER), logger("org.jboss.logging", Level.WARN))
.and();
}

private PropertyKey springDatasourceUrl() {
Expand Down Expand Up @@ -126,7 +184,7 @@ private JavaDependency psqlHikari() {
}

private JavaDependency psqlHibernateCore() {
return javaDependency().groupId("org.hibernate").artifactId("hibernate-core").build();
return javaDependency().groupId(ORG_HIBERNATE).artifactId("hibernate-core").build();
}

private JavaDependency testContainer() {
Expand Down
52 changes: 52 additions & 0 deletions src/test/features/postgresql.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Feature: postgreSQL module

Scenario: Should add postgreSQL elements using legacy url
When I apply legacy modules to default project
| /api/build-tools/maven |
| /api/servers/spring-boot |
| /api/servers/spring-boot/databases/postgresql |
Then I should have files in ""
| pom.xml |
And I should have history entry for "postgresql"
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/technical/infrastructure/secondary/postgresql"
| DatabaseConfiguration.java |
| FixedPostgreSQL10Dialect.java |
And I should have files in "src/test/java/tech/jhipster/chips/technical/infrastructure/secondary/postgresql"
| FixedPostgreSQL10DialectTest.java |
And I should have files in "src/main/resources/config"
| application.properties |
And I should have files in "src/test/resources/config"
| application.properties |

Scenario: Should get postgreSQL module properties definition
When I get module "postgresql" properties definition
Then I should have properties definitions
| Key | Type | Mandatory |
| packageName | STRING | true |
| baseName | STRING | true |
| prettierDefaultIndent | INTEGER | false |

Scenario: Should add postgreSQL elements using module url
When I apply "postgresql" module to default project with maven file
| packageName | tech.jhipster.chips |
| baseName | jhipster |
Then I should have files in ""
| pom.xml |
And I should have history entry for "postgresql"
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/technical/infrastructure/secondary/postgresql"
| DatabaseConfiguration.java |
| FixedPostgreSQL10Dialect.java |
And I should have files in "src/test/java/tech/jhipster/chips/technical/infrastructure/secondary/postgresql"
| FixedPostgreSQL10DialectTest.java |
And I should have files in "src/main/resources/config"
| application.properties |
And I should have files in "src/test/resources/config"
| application.properties |
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tech.jhipster.lite.generator.server.springboot.database.postgresql.domain;

import static org.mockito.Mockito.when;
import static tech.jhipster.lite.generator.module.infrastructure.secondary.JHipsterModulesAssertions.*;
import static tech.jhipster.lite.generator.module.infrastructure.secondary.JHipsterModulesAssertions.assertThatModuleOnProjectWithDefaultPom;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -53,6 +53,39 @@ void shouldCreateModule() {
.containing("<artifactId>postgresql</artifactId>")
.containing("<groupId>com.zaxxer</groupId>")
.containing("<artifactId>HikariCP</artifactId>")
.containing("<groupId>org.hibernate</groupId>");
.containing("<groupId>org.hibernate</groupId>")
.containing("<artifactId>hibernate-core</artifactId>")
.containing("<groupId>org.testcontainers</groupId>")
.containing("<artifactId>postgresql</artifactId>")
.and()
.createFile("src/main/resources/config/application.properties")
.containing("spring.datasource.url=jdbc:postgresql://localhost:5432/myapp")
.containing("spring.datasource.username=myapp")
.containing("spring.datasource.password=")
.containing("spring.datasource.driver-class-name=org.postgresql.Driver")
.containing("spring.datasource.type=com.zaxxer.hikari.HikariDataSource")
.containing("spring.datasource.hikari.poolName=Hikari")
.containing("spring.datasource.hikari.auto-commit=false")
.containing("spring.data.jpa.repositories.bootstrap-mode=deferred")
.containing("spring.jpa.hibernate.ddl-auto=none")
.containing("spring.jpa.hibernate.naming.implicit-strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy")
.containing("spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy")
.containing("spring.jpa.open-in-view=false")
.containing("spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true")
.containing("spring.jpa.properties.hibernate.generate_statistics=false")
.containing("spring.jpa.properties.hibernate.id.new_generator_mappings=true")
.containing("spring.jpa.properties.hibernate.jdbc.batch_size=25")
.containing("spring.jpa.properties.hibernate.jdbc.time_zone=UTC")
.containing("spring.jpa.properties.hibernate.order_inserts=true")
.containing("spring.jpa.properties.hibernate.order_updates=true")
.containing("spring.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch=true")
.containing("spring.jpa.properties.hibernate.query.in_clause_parameter_padding=true")
.and()
.createFile("src/test/resources/config/application.properties")
.containing("spring.datasource.url=jdbc:tc:postgresql")
.containing("spring.datasource.username=myapp")
.containing("spring.datasource.password=")
.containing("spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver")
.containing("spring.datasource.hikari.maximum-pool-size=2");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import tech.jhipster.lite.generator.init.application.InitApplicationService;
import tech.jhipster.lite.generator.module.domain.JHipsterModulesFixture;
import tech.jhipster.lite.generator.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.generator.module.infrastructure.secondary.TestJHipsterModules;
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;
Expand Down Expand Up @@ -74,7 +75,7 @@ void shouldInit() throws Exception {
initApplicationService.init(project);
mavenApplicationService.init(project);
springBootApplicationService.init(project);
postgresqlApplicationService.build(properties);
TestJHipsterModules.applyer().module(postgresqlApplicationService.build(properties)).properties(properties).slug("postgresql").apply();

mockMvc
.perform(
Expand Down Expand Up @@ -107,7 +108,7 @@ void shouldAddUserPostgresql() throws Exception {
initApplicationService.init(project);
mavenApplicationService.init(project);
springBootApplicationService.init(project);
postgresqlApplicationService.build(properties);
TestJHipsterModules.applyer().module(postgresqlApplicationService.build(properties)).properties(properties).slug("postgresql").apply();
liquibaseApplicationService.init(project);

mockMvc
Expand Down

0 comments on commit dea52e7

Please sign in to comment.