Skip to content

Commit

Permalink
PostgreSQL without Docker
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Aug 26, 2024
1 parent ae0bd38 commit f82f937
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 190 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/fit_Tomcat_PostgreSQL_JSON.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License
name: "FIT Tomcat H2 JSON"
name: "FIT Tomcat PostgreSQL JSON"

on:
push:
Expand All @@ -26,7 +26,7 @@ on:
- cron: '0 13 * * 4'

jobs:
fit_Tomcat_H2_JSON:
fit_Tomcat_PostgreSQL_JSON:
runs-on: ubuntu-latest

steps:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/fit_Tomcat_PostgreSQL_XML.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License
name: "FIT Tomcat H2 XML"
name: "FIT Tomcat PostgreSQL XML"

on:
push:
Expand All @@ -26,7 +26,7 @@ on:
- cron: '0 13 * * 4'

jobs:
fit_Tomcat_H2_XML:
fit_Tomcat_PostgreSQL_XML:
runs-on: ubuntu-latest

steps:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/fit_Tomcat_PostgreSQL_YAML.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License
name: "FIT Tomcat H2 YAML"
name: "FIT Tomcat PostgreSQL YAML"

on:
push:
Expand All @@ -26,7 +26,7 @@ on:
- cron: '0 13 * * 4'

jobs:
fit_Tomcat_H2_YAML:
fit_Tomcat_PostgreSQL_YAML:
runs-on: ubuntu-latest

steps:
Expand Down
4 changes: 2 additions & 2 deletions core/idm/logic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ under the License.
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<groupId>io.zonky.test</groupId>
<artifactId>embedded-postgres</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,49 @@
*/
package org.apache.syncope.core.logic;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

import io.zonky.test.db.postgres.embedded.EmbeddedPostgres;
import jakarta.persistence.EntityManager;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.function.Supplier;
import org.apache.syncope.common.lib.types.EntitlementsHolder;
import org.apache.syncope.common.lib.types.IdMEntitlement;
import org.apache.syncope.common.lib.types.IdRepoEntitlement;
import org.apache.syncope.core.persistence.jpa.MasterDomain;
import org.junit.jupiter.api.BeforeAll;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.testcontainers.containers.PostgreSQLContainer;

@SpringJUnitConfig(classes = { MasterDomain.class, IdMLogicTestContext.class })
public abstract class AbstractTest {

private static final Logger LOG = LoggerFactory.getLogger(AbstractTest.class);
private static Supplier<Object> JDBC_URL_SUPPLIER;

private static final PostgreSQLContainer<?> MASTER_DOMAIN;
private static final Supplier<Object> DB_CRED_SUPPLIER = () -> "syncope";

static {
String dockerPostgreSQLVersion = null;
try (InputStream propStream = AbstractTest.class.getResourceAsStream("/test.properties")) {
Properties props = new Properties();
props.load(propStream);
try {
EmbeddedPostgres pg = EmbeddedPostgres.builder().start();
JdbcTemplate jdbcTemplate = new JdbcTemplate(pg.getPostgresDatabase());
jdbcTemplate.execute("CREATE DATABASE syncope");

dockerPostgreSQLVersion = props.getProperty("docker.postgresql.version");
jdbcTemplate.execute("CREATE USER syncope WITH PASSWORD 'syncope'");
jdbcTemplate.execute("ALTER DATABASE syncope OWNER TO syncope");

JDBC_URL_SUPPLIER = () -> pg.getJdbcUrl("syncope", "syncope") + "&stringtype=unspecified";
} catch (Exception e) {
LOG.error("Could not load /test.properties", e);
fail("Could not setup PostgreSQL database", e);
}
assertNotNull(dockerPostgreSQLVersion);

MASTER_DOMAIN = new PostgreSQLContainer<>("postgres:" + dockerPostgreSQLVersion).
withTmpFs(Map.of("/var/lib/postgresql/data", "rw")).
withDatabaseName("syncope").withPassword("syncope").withUsername("syncope").
withUrlParam("stringtype", "unspecified").
withReuse(true);
MASTER_DOMAIN.start();
}

@DynamicPropertySource
static void configureProperties(final DynamicPropertyRegistry registry) {
registry.add("DB_URL", MASTER_DOMAIN::getJdbcUrl);
registry.add("DB_USER", MASTER_DOMAIN::getUsername);
registry.add("DB_PASSWORD", MASTER_DOMAIN::getPassword);
registry.add("DB_URL", JDBC_URL_SUPPLIER);
registry.add("DB_USER", DB_CRED_SUPPLIER);
registry.add("DB_PASSWORD", DB_CRED_SUPPLIER);
}

@BeforeAll
Expand Down
4 changes: 2 additions & 2 deletions core/idrepo/logic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ under the License.
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<groupId>io.zonky.test</groupId>
<artifactId>embedded-postgres</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,48 @@
*/
package org.apache.syncope.core.logic;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

import io.zonky.test.db.postgres.embedded.EmbeddedPostgres;
import jakarta.persistence.EntityManager;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.function.Supplier;
import org.apache.syncope.common.lib.types.EntitlementsHolder;
import org.apache.syncope.common.lib.types.IdRepoEntitlement;
import org.apache.syncope.core.persistence.jpa.MasterDomain;
import org.junit.jupiter.api.BeforeAll;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.testcontainers.containers.PostgreSQLContainer;

@SpringJUnitConfig(classes = { MasterDomain.class, IdRepoLogicTestContext.class })
public abstract class AbstractTest {

private static final Logger LOG = LoggerFactory.getLogger(AbstractTest.class);
private static Supplier<Object> JDBC_URL_SUPPLIER;

private static final PostgreSQLContainer<?> MASTER_DOMAIN;
private static final Supplier<Object> DB_CRED_SUPPLIER = () -> "syncope";

static {
String dockerPostgreSQLVersion = null;
try (InputStream propStream = AbstractTest.class.getResourceAsStream("/test.properties")) {
Properties props = new Properties();
props.load(propStream);
try {
EmbeddedPostgres pg = EmbeddedPostgres.builder().start();
JdbcTemplate jdbcTemplate = new JdbcTemplate(pg.getPostgresDatabase());
jdbcTemplate.execute("CREATE DATABASE syncope");

dockerPostgreSQLVersion = props.getProperty("docker.postgresql.version");
jdbcTemplate.execute("CREATE USER syncope WITH PASSWORD 'syncope'");
jdbcTemplate.execute("ALTER DATABASE syncope OWNER TO syncope");

JDBC_URL_SUPPLIER = () -> pg.getJdbcUrl("syncope", "syncope") + "&stringtype=unspecified";
} catch (Exception e) {
LOG.error("Could not load /test.properties", e);
fail("Could not setup PostgreSQL database", e);
}
assertNotNull(dockerPostgreSQLVersion);

MASTER_DOMAIN = new PostgreSQLContainer<>("postgres:" + dockerPostgreSQLVersion).
withTmpFs(Map.of("/var/lib/postgresql/data", "rw")).
withDatabaseName("syncope").withPassword("syncope").withUsername("syncope").
withUrlParam("stringtype", "unspecified").
withReuse(true);
MASTER_DOMAIN.start();
}

@DynamicPropertySource
static void configureProperties(final DynamicPropertyRegistry registry) {
registry.add("DB_URL", MASTER_DOMAIN::getJdbcUrl);
registry.add("DB_USER", MASTER_DOMAIN::getUsername);
registry.add("DB_PASSWORD", MASTER_DOMAIN::getPassword);
registry.add("DB_URL", JDBC_URL_SUPPLIER);
registry.add("DB_USER", DB_CRED_SUPPLIER);
registry.add("DB_PASSWORD", DB_CRED_SUPPLIER);
}

@BeforeAll
Expand Down
4 changes: 2 additions & 2 deletions core/persistence-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ under the License.
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<groupId>io.zonky.test</groupId>
<artifactId>embedded-postgres</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Loading

0 comments on commit f82f937

Please sign in to comment.