From 0809670d22f5bbe2e4bba5bec9ee379c17ad0226 Mon Sep 17 00:00:00 2001 From: "Michal J. Sladek" Date: Thu, 14 Nov 2024 22:45:58 +0100 Subject: [PATCH 1/5] Testcontainer PoC --- .oracle/oracle-init/fpsak.sql | 84 ------------ .../behandlingslager/EntityTest.java | 4 +- ...SjekkCollectionsOrderedIEntiteterTest.java | 7 +- .../datavarehus/domene/BehandlingDvh.java | 3 +- .../vedtak/innsyn/InnsynTjenesteTest.java | 9 +- migreringer/pom.xml | 33 ----- migreringer/src/main/resources/db/fpsak.sql | 89 ++++++++++++ .../db/validering/SjekkDbStrukturTest.java | 11 +- .../Databaseskjemainitialisering.java | 127 ------------------ .../dbstoette/JpaExtension.java | 29 ++-- .../dbstoette/TestDatabaseInit.java | 94 +++++++++++++ migreringer/src/test/resources/db/fpsak.sql | 89 ++++++++++++ pom.xml | 5 + ...apporterUnmappedKolonnerIDatabaseTest.java | 8 +- .../test/resources/application-vtp.properties | 2 + web/src/test/resources/fpsak.sql | 89 ++++++++++++ 16 files changed, 410 insertions(+), 273 deletions(-) create mode 100644 migreringer/src/main/resources/db/fpsak.sql delete mode 100644 migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/Databaseskjemainitialisering.java create mode 100644 migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/TestDatabaseInit.java create mode 100644 migreringer/src/test/resources/db/fpsak.sql create mode 100644 web/src/test/resources/fpsak.sql diff --git a/.oracle/oracle-init/fpsak.sql b/.oracle/oracle-init/fpsak.sql index 576855ee561..151bc1578c4 100644 --- a/.oracle/oracle-init/fpsak.sql +++ b/.oracle/oracle-init/fpsak.sql @@ -28,56 +28,6 @@ BEGIN END; / -DECLARE - userexists INTEGER; -BEGIN - SELECT count(*) - INTO userexists - FROM SYS.ALL_USERS - WHERE USERNAME = 'FPSAK_UNIT'; - IF (userexists = 0) - THEN - EXECUTE IMMEDIATE ('CREATE USER FPSAK_UNIT IDENTIFIED BY fpsak_unit PROFILE DEFAULT ACCOUNT UNLOCK'); - END IF; -END; -/ - -DECLARE - userexists INTEGER; -BEGIN - SELECT count(*) - INTO userexists - FROM SYS.ALL_USERS - WHERE USERNAME = 'FPSAK_HIST_UNIT'; - IF (userexists = 0) - THEN - EXECUTE IMMEDIATE ('CREATE USER FPSAK_HIST_UNIT IDENTIFIED BY fpsak_hist_unit PROFILE DEFAULT ACCOUNT UNLOCK'); - END IF; -END; -/ - -DECLARE - userexists INTEGER; -BEGIN - SELECT count(*) - INTO userexists - FROM SYS.ALL_USERS - WHERE USERNAME = 'VL_DBA'; - IF (userexists = 0) - THEN - EXECUTE IMMEDIATE ('CREATE USER VL_DBA IDENTIFIED BY vl_dba PROFILE DEFAULT ACCOUNT UNLOCK'); - END IF; -END; -/ - --- 1 Role for vl_dba -GRANT DBA TO VL_DBA; -ALTER USER VL_DBA DEFAULT ROLE ALL; - --- 2 System Privileges for vl_dba -GRANT CREATE SESSION TO VL_DBA; -GRANT UNLIMITED TABLESPACE TO VL_DBA; - GRANT CREATE SESSION, ALTER SESSION, @@ -108,36 +58,6 @@ GRANT SELECT ANY TABLE TO FPSAK_HIST; -GRANT - CREATE SESSION, - ALTER SESSION, - CONNECT, - RESOURCE, - CREATE MATERIALIZED VIEW, - CREATE JOB, - CREATE TABLE, - CREATE SYNONYM, - CREATE VIEW, - CREATE SEQUENCE, - UNLIMITED TABLESPACE, - SELECT ANY TABLE -TO FPSAK_UNIT; - -GRANT - CREATE SESSION, - ALTER SESSION, - CONNECT, - RESOURCE, - CREATE MATERIALIZED VIEW, - CREATE JOB, - CREATE TABLE, - CREATE SYNONYM, - CREATE VIEW, - CREATE SEQUENCE, - UNLIMITED TABLESPACE, - SELECT ANY TABLE -TO FPSAK_HIST_UNIT; - -- Ikke endre rollenavn, den er referert i migreringsskriptene og skal finnes i alle miljøer inkl prod DECLARE roleexists INTEGER; BEGIN @@ -167,10 +87,6 @@ END; / GRANT FPSAK_HIST_SKRIVE_ROLE TO FPSAK; -GRANT FPSAK_HIST_SKRIVE_ROLE TO FPSAK_UNIT; ALTER USER FPSAK QUOTA UNLIMITED ON SYSTEM; -ALTER USER FPSAK_UNIT QUOTA UNLIMITED ON SYSTEM; - ALTER USER FPSAK_HIST QUOTA UNLIMITED ON SYSTEM; -ALTER USER FPSAK_HIST_UNIT QUOTA UNLIMITED ON SYSTEM; diff --git a/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/EntityTest.java b/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/EntityTest.java index 2e736b70cc9..b3b13337d17 100644 --- a/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/EntityTest.java +++ b/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/EntityTest.java @@ -30,7 +30,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import no.nav.foreldrepenger.dbstoette.Databaseskjemainitialisering; +import no.nav.foreldrepenger.dbstoette.TestDatabaseInit; /** * Sjekker alle entiteter er mappet korrekt. Ligger i web slik at den fanger @@ -45,7 +45,7 @@ class EntityTest { // System.setProperty("hibernate.hbm2ddl.auto", "validate"); try { // trenger å konfigurere opp jndi etc. - Databaseskjemainitialisering.settJdniOppslag(); + //TestDatabaseInit.settJdniOppslag(); } catch (Exception e) { throw new ExceptionInInitializerError(e); } diff --git a/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/SjekkCollectionsOrderedIEntiteterTest.java b/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/SjekkCollectionsOrderedIEntiteterTest.java index 8f1e255f51d..df3a6242609 100644 --- a/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/SjekkCollectionsOrderedIEntiteterTest.java +++ b/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/SjekkCollectionsOrderedIEntiteterTest.java @@ -16,20 +16,23 @@ import jakarta.persistence.Persistence; import jakarta.persistence.metamodel.Type; +import no.nav.foreldrepenger.dbstoette.TestDatabaseInit; + +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import no.nav.foreldrepenger.behandlingslager.diff.IndexKey; -import no.nav.foreldrepenger.dbstoette.Databaseskjemainitialisering; /** Lagt til web for å sjekke orm filer fra alle moduler. */ +@Disabled class SjekkCollectionsOrderedIEntiteterTest { private static final EntityManagerFactory entityManagerFactory; static { try { - Databaseskjemainitialisering.settJdniOppslag(); + //TestDatabaseInit.settJdniOppslag(); } catch (Exception e) { throw new ExceptionInInitializerError(e); } diff --git a/domenetjenester/datavarehus/src/main/java/no/nav/foreldrepenger/datavarehus/domene/BehandlingDvh.java b/domenetjenester/datavarehus/src/main/java/no/nav/foreldrepenger/datavarehus/domene/BehandlingDvh.java index 80197456a95..34b2f903c39 100644 --- a/domenetjenester/datavarehus/src/main/java/no/nav/foreldrepenger/datavarehus/domene/BehandlingDvh.java +++ b/domenetjenester/datavarehus/src/main/java/no/nav/foreldrepenger/datavarehus/domene/BehandlingDvh.java @@ -12,12 +12,13 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.PrePersist; +import jakarta.persistence.SequenceGenerator; import jakarta.persistence.Table; import org.hibernate.annotations.NaturalId; @Entity(name = "BehandlingDvh") -@Table(name = "BEHANDLING_DVH") +@Table(name = "BEHANDLING_DVH", schema = "FPSAK_HIST") public class BehandlingDvh implements Serializable { private static final String PAPIR_SØKNAD = "1"; diff --git a/domenetjenester/vedtak/src/test/java/no/nav/foreldrepenger/domene/vedtak/innsyn/InnsynTjenesteTest.java b/domenetjenester/vedtak/src/test/java/no/nav/foreldrepenger/domene/vedtak/innsyn/InnsynTjenesteTest.java index 732d044bbc5..84a85ca288a 100644 --- a/domenetjenester/vedtak/src/test/java/no/nav/foreldrepenger/domene/vedtak/innsyn/InnsynTjenesteTest.java +++ b/domenetjenester/vedtak/src/test/java/no/nav/foreldrepenger/domene/vedtak/innsyn/InnsynTjenesteTest.java @@ -9,8 +9,13 @@ import jakarta.inject.Inject; +import no.nav.foreldrepenger.dbstoette.EntityManagerAwareTest; +import no.nav.foreldrepenger.dbstoette.JpaExtension; + import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import no.nav.foreldrepenger.behandlingskontroll.BehandlingskontrollTjeneste; @@ -33,8 +38,8 @@ import no.nav.foreldrepenger.produksjonsstyring.behandlingenhet.BehandlendeEnhetTjeneste; import no.nav.vedtak.felles.prosesstask.api.ProsessTaskTjeneste; -@CdiDbAwareTest -class InnsynTjenesteTest { +@Disabled +class InnsynTjenesteTest extends EntityManagerAwareTest { @Inject private BehandlingskontrollTjeneste behandlingKontrollTjeneste; diff --git a/migreringer/pom.xml b/migreringer/pom.xml index 73b7ef7b003..fc91411a0e1 100644 --- a/migreringer/pom.xml +++ b/migreringer/pom.xml @@ -61,37 +61,4 @@ - - - migrerDb - - - !skipTests - - - - - - org.codehaus.mojo - exec-maven-plugin - - - migrering - test-compile - - java - - - - - no.nav.foreldrepenger.dbstoette.Databaseskjemainitialisering - test - false - - - - - - - diff --git a/migreringer/src/main/resources/db/fpsak.sql b/migreringer/src/main/resources/db/fpsak.sql new file mode 100644 index 00000000000..b01cd6542b3 --- /dev/null +++ b/migreringer/src/main/resources/db/fpsak.sql @@ -0,0 +1,89 @@ +ALTER SESSION SET CONTAINER=FREEPDB1; + +DECLARE + userexists INTEGER; +BEGIN + SELECT count(*) + INTO userexists + FROM SYS.ALL_USERS + WHERE USERNAME = 'FPSAK'; + IF (userexists = 0) + THEN + EXECUTE IMMEDIATE ('CREATE USER FPSAK IDENTIFIED BY fpsak PROFILE DEFAULT ACCOUNT UNLOCK'); + END IF; +END; + + +DECLARE + userexists INTEGER; +BEGIN + SELECT count(*) + INTO userexists + FROM SYS.ALL_USERS + WHERE USERNAME = 'FPSAK_HIST'; + IF (userexists = 0) + THEN + EXECUTE IMMEDIATE ('CREATE USER FPSAK_HIST IDENTIFIED BY fpsak_hist PROFILE DEFAULT ACCOUNT UNLOCK'); + END IF; +END; + +GRANT + CREATE SESSION, + ALTER SESSION, + CONNECT, + RESOURCE, + CREATE MATERIALIZED VIEW, + CREATE JOB, + CREATE TABLE, + CREATE SYNONYM, + CREATE VIEW, + CREATE SEQUENCE, + UNLIMITED TABLESPACE, + SELECT ANY TABLE +TO FPSAK; + +GRANT + CREATE SESSION, + ALTER SESSION, + CONNECT, + RESOURCE, + CREATE MATERIALIZED VIEW, + CREATE JOB, + CREATE TABLE, + CREATE SYNONYM, + CREATE VIEW, + CREATE SEQUENCE, + UNLIMITED TABLESPACE, + SELECT ANY TABLE +TO FPSAK_HIST; + +-- Ikke endre rollenavn, den er referert i migreringsskriptene og skal finnes i alle miljøer inkl prod +DECLARE roleexists INTEGER; +BEGIN + SELECT count(*) + INTO roleexists + FROM SYS.DBA_ROLES + WHERE ROLE = 'FPSAK_HIST_SKRIVE_ROLE'; + IF (roleexists = 0) + THEN + EXECUTE IMMEDIATE ('CREATE ROLE FPSAK_HIST_SKRIVE_ROLE'); + END IF; +END; + +-- Ikke endre rollenavn, den er referert i migreringsskriptene og skal finnes i alle miljøer inkl prod +DECLARE roleexists INTEGER; +BEGIN + SELECT count(*) + INTO roleexists + FROM SYS.DBA_ROLES + WHERE ROLE = 'FPSAK_HIST_LESE_ROLE'; + IF (roleexists = 0) + THEN + EXECUTE IMMEDIATE ('CREATE ROLE FPSAK_HIST_LESE_ROLE'); + END IF; +END; + +GRANT FPSAK_HIST_SKRIVE_ROLE TO FPSAK; + +ALTER USER FPSAK QUOTA UNLIMITED ON SYSTEM; +ALTER USER FPSAK_HIST QUOTA UNLIMITED ON SYSTEM; diff --git a/migreringer/src/test/java/no/nav/foreldrepenger/db/validering/SjekkDbStrukturTest.java b/migreringer/src/test/java/no/nav/foreldrepenger/db/validering/SjekkDbStrukturTest.java index ff879a01a4d..115f13904ad 100644 --- a/migreringer/src/test/java/no/nav/foreldrepenger/db/validering/SjekkDbStrukturTest.java +++ b/migreringer/src/test/java/no/nav/foreldrepenger/db/validering/SjekkDbStrukturTest.java @@ -7,14 +7,15 @@ import javax.sql.DataSource; +import no.nav.foreldrepenger.dbstoette.TestDatabaseInit; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import no.nav.foreldrepenger.dbstoette.Databaseskjemainitialisering; - /** * Tester at alle migreringer følger standarder for navn og god praksis. */ +@Disabled class SjekkDbStrukturTest { private static final String HJELP = """ @@ -29,9 +30,9 @@ class SjekkDbStrukturTest { @BeforeAll public static void setup() { - Databaseskjemainitialisering.migrerUnittestSkjemaer(); - ds = Databaseskjemainitialisering.initUnitTestDataSource(); - schema = Databaseskjemainitialisering.DEFAULTDS_USER; + //TestDatabaseInit.migrerUnittestSkjemaer(); + //ds = TestDatabaseInit.initUnitTestDataSource(); + //schema = TestDatabaseInit.DEFAULTDS_USER; } @Test diff --git a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/Databaseskjemainitialisering.java b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/Databaseskjemainitialisering.java deleted file mode 100644 index 680b5f4e559..00000000000 --- a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/Databaseskjemainitialisering.java +++ /dev/null @@ -1,127 +0,0 @@ -package no.nav.foreldrepenger.dbstoette; - -import static java.lang.Runtime.getRuntime; - -import java.io.File; -import java.util.concurrent.atomic.AtomicBoolean; - -import javax.naming.NamingException; -import javax.sql.DataSource; - -import org.eclipse.jetty.plus.jndi.EnvEntry; -import org.flywaydb.core.Flyway; -import org.flywaydb.core.api.FlywayException; - -import com.zaxxer.hikari.HikariConfig; -import com.zaxxer.hikari.HikariDataSource; - -import no.nav.foreldrepenger.konfig.Environment; - -public final class Databaseskjemainitialisering { - - private static final AtomicBoolean GUARD_UNIT_TEST_SKJEMAER = new AtomicBoolean(); - - private static final Environment ENV = Environment.current(); - - private static final String DB_SCRIPT_LOCATION = "/db/migration/"; - - private static final DataSource DEFAULT_DS = settJdniOppslag(); - private static final String DEFAULTDS_SCHEMA = "defaultDS"; - public static final String DEFAULTDS_USER = "fpsak_unit"; - - public static void main(String[] args) { - //brukes i mvn clean install - migrerUnittestSkjemaer(); - } - - public static DataSource initUnitTestDataSource() { - if (DEFAULT_DS != null) { - return DEFAULT_DS; - } - settJdniOppslag(); - return DEFAULT_DS; - } - - public static void migrerUnittestSkjemaer() { - migrerUnittestSkjemaer(DEFAULTDS_SCHEMA, DEFAULTDS_USER); - migrerUnittestSkjemaer("dvhDS", "fpsak_hist"); - } - - private static void migrerUnittestSkjemaer(String schemaName, String user) { - if (GUARD_UNIT_TEST_SKJEMAER.compareAndSet(false, true)) { - var flyway = Flyway.configure() - .dataSource(createDs(user)) - .locations(getScriptLocation(schemaName)) - .table("schema_version") - .baselineOnMigrate(true) - .cleanDisabled(false) - .load(); - try { - if (!ENV.isLocal()) { - throw new IllegalStateException("Forventer at denne migreringen bare kjøres lokalt"); - } - flyway.migrate(); - } catch (FlywayException ignore) { - flyway.clean(); - flyway.migrate(); - } - } - GUARD_UNIT_TEST_SKJEMAER.compareAndSet(true, false); - } - - private static String getScriptLocation(String dsName) { - if (ENV.getProperty("maven.cmd.line.args") != null) { - return classpathScriptLocation(dsName); - } - return fileScriptLocation(dsName); - } - - private static String classpathScriptLocation(String dsName) { - return "classpath:" + DB_SCRIPT_LOCATION + dsName; - } - - private static String fileScriptLocation(String dsName) { - var relativePath = "migreringer/src/main/resources" + DB_SCRIPT_LOCATION + dsName; - var baseDir = new File(".").getAbsoluteFile(); - var location = new File(baseDir, relativePath); - while (!location.exists()) { - baseDir = baseDir.getParentFile(); - if (baseDir == null || !baseDir.isDirectory()) { - throw new IllegalArgumentException("Klarte ikke finne : " + baseDir); - } - location = new File(baseDir, relativePath); - } - return "filesystem:" + location.getPath(); - } - - public static synchronized DataSource settJdniOppslag() { - var ds = createDs(DEFAULTDS_USER); - try { - new EnvEntry("jdbc/defaultDS", ds); // NOSONAR - return ds; - } catch (NamingException e) { - throw new IllegalStateException("Feil under registrering av JDNI-entry for defaultDS", e); // NOSONAR - } - } - - private static DataSource createDs(String user) { - var cfg = new HikariConfig(); - cfg.setJdbcUrl(buildJdbcUrl()); - cfg.setUsername(ENV.getProperty("database.user", user)); - cfg.setPassword(ENV.getProperty("database.password", user)); - cfg.setConnectionTimeout(1500); - cfg.setValidationTimeout(120L * 1000L); - cfg.setMaximumPoolSize(4); - cfg.setAutoCommit(false); - var ds = new HikariDataSource(cfg); - getRuntime().addShutdownHook(new Thread(ds::close)); - return ds; - } - - private static String buildJdbcUrl() { - return String.format("jdbc:oracle:thin:@//%s:%s/%s", - ENV.getProperty("database.host", "localhost"), - ENV.getProperty("database.post", "1521"), - ENV.getProperty("database.service", "FREEPDB1")); - } -} diff --git a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/JpaExtension.java b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/JpaExtension.java index f97322d9cee..15b81ff56db 100644 --- a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/JpaExtension.java +++ b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/JpaExtension.java @@ -1,24 +1,25 @@ package no.nav.foreldrepenger.dbstoette; -import static no.nav.foreldrepenger.dbstoette.Databaseskjemainitialisering.initUnitTestDataSource; -import static no.nav.foreldrepenger.dbstoette.Databaseskjemainitialisering.migrerUnittestSkjemaer; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import no.nav.foreldrepenger.konfig.Environment; import no.nav.vedtak.felles.testutilities.db.EntityManagerAwareExtension; +import org.testcontainers.oracle.OracleContainer; +import org.testcontainers.utility.DockerImageName; +import org.testcontainers.utility.MountableFile; + public class JpaExtension extends EntityManagerAwareExtension { - private static final Logger LOG = LoggerFactory.getLogger(JpaExtension.class); + public static final String DEFAULT_TEST_DB_SCHEMA_NAME; + private static final String TEST_DB_CONTAINER = Environment.current().getProperty("testcontainer.test.db", String.class, "gvenzl/oracle-free:23-slim-faststart"); + private static final OracleContainer TEST_DATABASE; + static { - if (Environment.current().getProperty("maven.cmd.line.args") == null) { - LOG.info("Kjører IKKE under maven"); - // prøver alltid migrering hvis endring, ellers funker det dårlig i IDE. - migrerUnittestSkjemaer(); - } - initUnitTestDataSource(); + TEST_DATABASE = new OracleContainer(DockerImageName.parse(TEST_DB_CONTAINER)) + .withCopyFileToContainer(MountableFile.forHostPath("../../.oracle/oracle-init/fpsak.sql"), "/docker-entrypoint-initdb.d/init.sql") + .withReuse(true); + TEST_DATABASE.start(); + DEFAULT_TEST_DB_SCHEMA_NAME = TEST_DATABASE.getUsername(); + TestDatabaseInit.settOppDatasourceOgMigrer(TEST_DATABASE.getJdbcUrl(), "fpsak_hist", "fpsak_hist", "dvhDS"); + TestDatabaseInit.settOppDatasourceOgMigrer(TEST_DATABASE.getJdbcUrl(), "fpsak", "fpsak", "defaultDS"); } - } diff --git a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/TestDatabaseInit.java b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/TestDatabaseInit.java new file mode 100644 index 00000000000..e5dab7a151e --- /dev/null +++ b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/TestDatabaseInit.java @@ -0,0 +1,94 @@ +package no.nav.foreldrepenger.dbstoette; + +import static java.lang.Runtime.getRuntime; + +import java.io.File; +import java.util.concurrent.atomic.AtomicBoolean; + +import javax.naming.NamingException; +import javax.sql.DataSource; + +import org.eclipse.jetty.plus.jndi.EnvEntry; +import org.flywaydb.core.Flyway; +import org.flywaydb.core.api.FlywayException; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +import no.nav.foreldrepenger.konfig.Environment; + +public final class TestDatabaseInit { + + private static final AtomicBoolean GUARD_UNIT_TEST_SKJEMAER = new AtomicBoolean(); + + private static final Environment ENV = Environment.current(); + + private static final String DB_SCRIPT_LOCATION = "/db/migration/"; + + public static void settOppDatasourceOgMigrer(String jdbcUrl, String username, String password, String schema) { + var ds = createDatasource(jdbcUrl, username, password); + settJdniOppslag(ds); + if (GUARD_UNIT_TEST_SKJEMAER.compareAndSet(false, true)) { + var flyway = Flyway.configure() + .dataSource(ds) + .locations(getScriptLocation(schema)) + .baselineOnMigrate(true) + .cleanDisabled(false) + .load(); + try { + if (!ENV.isLocal()) { + throw new IllegalStateException("Forventer at denne migreringen bare kjøres lokalt"); + } + flyway.migrate(); + } catch (Exception ignore) { + try { + flyway.clean(); + flyway.migrate(); + } catch (FlywayException e) { + throw new RuntimeException("Flyway migrering feilet", e); + } + } + } + GUARD_UNIT_TEST_SKJEMAER.compareAndSet(true, false); + } + + private static String getScriptLocation(String schema) { + return fileScriptLocation(schema); + } + + private static String fileScriptLocation(String schema) { + var relativePath = "migreringer/src/main/resources" + DB_SCRIPT_LOCATION + schema; + var baseDir = new File(".").getAbsoluteFile(); + var location = new File(baseDir, relativePath); + while (!location.exists()) { + baseDir = baseDir.getParentFile(); + if (baseDir == null || !baseDir.isDirectory()) { + throw new IllegalArgumentException("Klarte ikke finne : " + baseDir); + } + location = new File(baseDir, relativePath); + } + return "filesystem:" + location.getPath(); + } + + private static void settJdniOppslag(DataSource dataSource) { + try { + new EnvEntry("jdbc/defaultDS", dataSource); // NOSONAR + } catch (NamingException e) { + throw new IllegalStateException("Feil under registrering av JDNI-entry for default datasource", e); // NOSONAR + } + } + + private static HikariDataSource createDatasource(String jdbcUrl, String username, String password) { + var cfg = new HikariConfig(); + cfg.setJdbcUrl(jdbcUrl); + cfg.setUsername(username); + cfg.setPassword(password); + cfg.setConnectionTimeout(1500); + cfg.setValidationTimeout(120L * 1000L); + cfg.setMaximumPoolSize(4); + cfg.setAutoCommit(false); + var ds = new HikariDataSource(cfg); + getRuntime().addShutdownHook(new Thread(ds::close)); + return ds; + } +} diff --git a/migreringer/src/test/resources/db/fpsak.sql b/migreringer/src/test/resources/db/fpsak.sql new file mode 100644 index 00000000000..b01cd6542b3 --- /dev/null +++ b/migreringer/src/test/resources/db/fpsak.sql @@ -0,0 +1,89 @@ +ALTER SESSION SET CONTAINER=FREEPDB1; + +DECLARE + userexists INTEGER; +BEGIN + SELECT count(*) + INTO userexists + FROM SYS.ALL_USERS + WHERE USERNAME = 'FPSAK'; + IF (userexists = 0) + THEN + EXECUTE IMMEDIATE ('CREATE USER FPSAK IDENTIFIED BY fpsak PROFILE DEFAULT ACCOUNT UNLOCK'); + END IF; +END; + + +DECLARE + userexists INTEGER; +BEGIN + SELECT count(*) + INTO userexists + FROM SYS.ALL_USERS + WHERE USERNAME = 'FPSAK_HIST'; + IF (userexists = 0) + THEN + EXECUTE IMMEDIATE ('CREATE USER FPSAK_HIST IDENTIFIED BY fpsak_hist PROFILE DEFAULT ACCOUNT UNLOCK'); + END IF; +END; + +GRANT + CREATE SESSION, + ALTER SESSION, + CONNECT, + RESOURCE, + CREATE MATERIALIZED VIEW, + CREATE JOB, + CREATE TABLE, + CREATE SYNONYM, + CREATE VIEW, + CREATE SEQUENCE, + UNLIMITED TABLESPACE, + SELECT ANY TABLE +TO FPSAK; + +GRANT + CREATE SESSION, + ALTER SESSION, + CONNECT, + RESOURCE, + CREATE MATERIALIZED VIEW, + CREATE JOB, + CREATE TABLE, + CREATE SYNONYM, + CREATE VIEW, + CREATE SEQUENCE, + UNLIMITED TABLESPACE, + SELECT ANY TABLE +TO FPSAK_HIST; + +-- Ikke endre rollenavn, den er referert i migreringsskriptene og skal finnes i alle miljøer inkl prod +DECLARE roleexists INTEGER; +BEGIN + SELECT count(*) + INTO roleexists + FROM SYS.DBA_ROLES + WHERE ROLE = 'FPSAK_HIST_SKRIVE_ROLE'; + IF (roleexists = 0) + THEN + EXECUTE IMMEDIATE ('CREATE ROLE FPSAK_HIST_SKRIVE_ROLE'); + END IF; +END; + +-- Ikke endre rollenavn, den er referert i migreringsskriptene og skal finnes i alle miljøer inkl prod +DECLARE roleexists INTEGER; +BEGIN + SELECT count(*) + INTO roleexists + FROM SYS.DBA_ROLES + WHERE ROLE = 'FPSAK_HIST_LESE_ROLE'; + IF (roleexists = 0) + THEN + EXECUTE IMMEDIATE ('CREATE ROLE FPSAK_HIST_LESE_ROLE'); + END IF; +END; + +GRANT FPSAK_HIST_SKRIVE_ROLE TO FPSAK; + +ALTER USER FPSAK QUOTA UNLIMITED ON SYSTEM; +ALTER USER FPSAK_HIST QUOTA UNLIMITED ON SYSTEM; diff --git a/pom.xml b/pom.xml index 2dd2ea72932..59a671b75aa 100644 --- a/pom.xml +++ b/pom.xml @@ -468,6 +468,11 @@ jetty-ee10-plus test + + org.testcontainers + oracle-free + test + diff --git a/web/src/test/java/no/nav/foreldrepenger/RapporterUnmappedKolonnerIDatabaseTest.java b/web/src/test/java/no/nav/foreldrepenger/RapporterUnmappedKolonnerIDatabaseTest.java index 5bb9d49dbe5..0b039785b1b 100644 --- a/web/src/test/java/no/nav/foreldrepenger/RapporterUnmappedKolonnerIDatabaseTest.java +++ b/web/src/test/java/no/nav/foreldrepenger/RapporterUnmappedKolonnerIDatabaseTest.java @@ -13,6 +13,8 @@ import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; +import no.nav.foreldrepenger.dbstoette.TestDatabaseInit; + import org.hibernate.boot.Metadata; import org.hibernate.boot.model.relational.Database; import org.hibernate.boot.model.relational.Namespace; @@ -22,18 +24,18 @@ import org.hibernate.service.spi.SessionFactoryServiceRegistry; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import no.nav.foreldrepenger.dbstoette.Databaseskjemainitialisering; - /** * Denne testen rapporterer kun tabeller og kolonner som ikke er mappet i * hibernate. Det kan være gyldige grunner til det (f.eks. dersom det kun * aksesseres gjennom native sql), men p.t. høyst sannsynlig ikke. Bør * gjennomgås jevnlig for å luke manglende contract av db skjema. */ +@Disabled class RapporterUnmappedKolonnerIDatabaseTest { private static final Logger LOG = LoggerFactory.getLogger(RapporterUnmappedKolonnerIDatabaseTest.class); @@ -55,7 +57,7 @@ public static void setup() { // fikses først // System.setProperty("hibernate.hbm2ddl.auto", "validate"); try { - Databaseskjemainitialisering.settJdniOppslag(); + //TestDatabaseInit.settJdniOppslag(); } catch (Exception e) { throw new ExceptionInInitializerError(e); } diff --git a/web/src/test/resources/application-vtp.properties b/web/src/test/resources/application-vtp.properties index b585e92074c..dba9abf1dcb 100644 --- a/web/src/test/resources/application-vtp.properties +++ b/web/src/test/resources/application-vtp.properties @@ -92,3 +92,5 @@ TOKEN_X_ISSUER=http://vtp:8060/rest/tokenx TOKEN_X_TOKEN_ENDPOINT=http://vtp:8060/rest/tokenx/token TOKEN_X_JWKS_URI=http://vtp:8060/rest/tokenx/jwks TOKEN_X_CLIENT_ID=lokal + +testcontainer.test.db=gvenzl/oracle-free:23-slim-faststart diff --git a/web/src/test/resources/fpsak.sql b/web/src/test/resources/fpsak.sql new file mode 100644 index 00000000000..b01cd6542b3 --- /dev/null +++ b/web/src/test/resources/fpsak.sql @@ -0,0 +1,89 @@ +ALTER SESSION SET CONTAINER=FREEPDB1; + +DECLARE + userexists INTEGER; +BEGIN + SELECT count(*) + INTO userexists + FROM SYS.ALL_USERS + WHERE USERNAME = 'FPSAK'; + IF (userexists = 0) + THEN + EXECUTE IMMEDIATE ('CREATE USER FPSAK IDENTIFIED BY fpsak PROFILE DEFAULT ACCOUNT UNLOCK'); + END IF; +END; + + +DECLARE + userexists INTEGER; +BEGIN + SELECT count(*) + INTO userexists + FROM SYS.ALL_USERS + WHERE USERNAME = 'FPSAK_HIST'; + IF (userexists = 0) + THEN + EXECUTE IMMEDIATE ('CREATE USER FPSAK_HIST IDENTIFIED BY fpsak_hist PROFILE DEFAULT ACCOUNT UNLOCK'); + END IF; +END; + +GRANT + CREATE SESSION, + ALTER SESSION, + CONNECT, + RESOURCE, + CREATE MATERIALIZED VIEW, + CREATE JOB, + CREATE TABLE, + CREATE SYNONYM, + CREATE VIEW, + CREATE SEQUENCE, + UNLIMITED TABLESPACE, + SELECT ANY TABLE +TO FPSAK; + +GRANT + CREATE SESSION, + ALTER SESSION, + CONNECT, + RESOURCE, + CREATE MATERIALIZED VIEW, + CREATE JOB, + CREATE TABLE, + CREATE SYNONYM, + CREATE VIEW, + CREATE SEQUENCE, + UNLIMITED TABLESPACE, + SELECT ANY TABLE +TO FPSAK_HIST; + +-- Ikke endre rollenavn, den er referert i migreringsskriptene og skal finnes i alle miljøer inkl prod +DECLARE roleexists INTEGER; +BEGIN + SELECT count(*) + INTO roleexists + FROM SYS.DBA_ROLES + WHERE ROLE = 'FPSAK_HIST_SKRIVE_ROLE'; + IF (roleexists = 0) + THEN + EXECUTE IMMEDIATE ('CREATE ROLE FPSAK_HIST_SKRIVE_ROLE'); + END IF; +END; + +-- Ikke endre rollenavn, den er referert i migreringsskriptene og skal finnes i alle miljøer inkl prod +DECLARE roleexists INTEGER; +BEGIN + SELECT count(*) + INTO roleexists + FROM SYS.DBA_ROLES + WHERE ROLE = 'FPSAK_HIST_LESE_ROLE'; + IF (roleexists = 0) + THEN + EXECUTE IMMEDIATE ('CREATE ROLE FPSAK_HIST_LESE_ROLE'); + END IF; +END; + +GRANT FPSAK_HIST_SKRIVE_ROLE TO FPSAK; + +ALTER USER FPSAK QUOTA UNLIMITED ON SYSTEM; +ALTER USER FPSAK_HIST QUOTA UNLIMITED ON SYSTEM; From 6abb6c1ce24b378d82a2a854d5b638a26427ea23 Mon Sep 17 00:00:00 2001 From: Jens-Otto Larsen Date: Thu, 9 Jan 2025 15:24:01 +0100 Subject: [PATCH 2/5] Fikse oppsett for hist og diabled tester --- .../behandlingslager/EntityTest.java | 9 ++++-- ...SjekkCollectionsOrderedIEntiteterTest.java | 12 ++----- .../vedtak/innsyn/InnsynTjenesteTest.java | 9 ++---- .../db/validering/SjekkDbStrukturTest.java | 32 ++++++++++++++----- .../dbstoette/JpaExtension.java | 16 +++++++--- .../dbstoette/TestDatabaseInit.java | 8 +++-- ...apporterUnmappedKolonnerIDatabaseTest.java | 16 +++------- 7 files changed, 56 insertions(+), 46 deletions(-) diff --git a/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/EntityTest.java b/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/EntityTest.java index b3b13337d17..d6e4c66569d 100644 --- a/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/EntityTest.java +++ b/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/EntityTest.java @@ -27,15 +27,17 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import no.nav.foreldrepenger.dbstoette.TestDatabaseInit; +import no.nav.foreldrepenger.dbstoette.JpaExtension; /** * Sjekker alle entiteter er mappet korrekt. Ligger i web slik at den fanger * alle orm filer lagt i ulike moduler. */ +@ExtendWith(JpaExtension.class) class EntityTest { private static final EntityManagerFactory entityManagerFactory; @@ -103,7 +105,7 @@ public void skal_ha_registrert_alle_entiteter_i_orm_xml(Class entityClass) { } } - @Disabled("Venter til etter migrering av aliased tables") + //@Disabled("Venter til etter migrering av aliased tables") @ParameterizedTest @MethodSource("parameters") public void sjekk_felt_mapping_primitive_felt_i_entiteter_må_ha_not_nullable_i_db(Class entityClass) throws Exception { @@ -134,6 +136,7 @@ public void skal_ha_registrert_alle_entiteter_i_orm_xml(Class entityClass) { } } + // TODO: Slå på og gjennomgå resultatene @Disabled("Venter til etter migrering av aliased tables") @ParameterizedTest @MethodSource("parameters") @@ -184,7 +187,7 @@ private String getNullability(String tableName, String columnName) { .setParameter("col", columnName) .getResultList(); - return result.isEmpty() ? null : result.get(0); + return result.isEmpty() ? null : String.valueOf(result.get(0)); } private String getTableName(Class entityClass, Field field) { diff --git a/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/SjekkCollectionsOrderedIEntiteterTest.java b/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/SjekkCollectionsOrderedIEntiteterTest.java index df3a6242609..18f09ded429 100644 --- a/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/SjekkCollectionsOrderedIEntiteterTest.java +++ b/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/SjekkCollectionsOrderedIEntiteterTest.java @@ -16,26 +16,20 @@ import jakarta.persistence.Persistence; import jakarta.persistence.metamodel.Type; -import no.nav.foreldrepenger.dbstoette.TestDatabaseInit; - -import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import no.nav.foreldrepenger.behandlingslager.diff.IndexKey; +import no.nav.foreldrepenger.dbstoette.JpaExtension; /** Lagt til web for å sjekke orm filer fra alle moduler. */ -@Disabled +@ExtendWith(JpaExtension.class) class SjekkCollectionsOrderedIEntiteterTest { private static final EntityManagerFactory entityManagerFactory; static { - try { - //TestDatabaseInit.settJdniOppslag(); - } catch (Exception e) { - throw new ExceptionInInitializerError(e); - } entityManagerFactory = Persistence.createEntityManagerFactory("pu-default"); } diff --git a/domenetjenester/vedtak/src/test/java/no/nav/foreldrepenger/domene/vedtak/innsyn/InnsynTjenesteTest.java b/domenetjenester/vedtak/src/test/java/no/nav/foreldrepenger/domene/vedtak/innsyn/InnsynTjenesteTest.java index 84a85ca288a..732d044bbc5 100644 --- a/domenetjenester/vedtak/src/test/java/no/nav/foreldrepenger/domene/vedtak/innsyn/InnsynTjenesteTest.java +++ b/domenetjenester/vedtak/src/test/java/no/nav/foreldrepenger/domene/vedtak/innsyn/InnsynTjenesteTest.java @@ -9,13 +9,8 @@ import jakarta.inject.Inject; -import no.nav.foreldrepenger.dbstoette.EntityManagerAwareTest; -import no.nav.foreldrepenger.dbstoette.JpaExtension; - import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import no.nav.foreldrepenger.behandlingskontroll.BehandlingskontrollTjeneste; @@ -38,8 +33,8 @@ import no.nav.foreldrepenger.produksjonsstyring.behandlingenhet.BehandlendeEnhetTjeneste; import no.nav.vedtak.felles.prosesstask.api.ProsessTaskTjeneste; -@Disabled -class InnsynTjenesteTest extends EntityManagerAwareTest { +@CdiDbAwareTest +class InnsynTjenesteTest { @Inject private BehandlingskontrollTjeneste behandlingKontrollTjeneste; diff --git a/migreringer/src/test/java/no/nav/foreldrepenger/db/validering/SjekkDbStrukturTest.java b/migreringer/src/test/java/no/nav/foreldrepenger/db/validering/SjekkDbStrukturTest.java index 115f13904ad..8696ef5d4e8 100644 --- a/migreringer/src/test/java/no/nav/foreldrepenger/db/validering/SjekkDbStrukturTest.java +++ b/migreringer/src/test/java/no/nav/foreldrepenger/db/validering/SjekkDbStrukturTest.java @@ -2,20 +2,24 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.io.File; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; -import no.nav.foreldrepenger.dbstoette.TestDatabaseInit; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.testcontainers.oracle.OracleContainer; +import org.testcontainers.utility.DockerImageName; +import org.testcontainers.utility.MountableFile; + +import no.nav.foreldrepenger.dbstoette.TestDatabaseInit; +import no.nav.foreldrepenger.konfig.Environment; /** * Tester at alle migreringer følger standarder for navn og god praksis. */ -@Disabled class SjekkDbStrukturTest { private static final String HJELP = """ @@ -30,9 +34,19 @@ class SjekkDbStrukturTest { @BeforeAll public static void setup() { - //TestDatabaseInit.migrerUnittestSkjemaer(); - //ds = TestDatabaseInit.initUnitTestDataSource(); - //schema = TestDatabaseInit.DEFAULTDS_USER; + var testDbContainer = Environment.current().getProperty("testcontainer.test.db", String.class, "gvenzl/oracle-free:23-slim-faststart"); + + var initPath = "../.oracle/oracle-init/fpsak.sql"; + while (!(new File(initPath)).exists()) { + initPath = "../" + initPath; + } + var testDatabase = new OracleContainer(DockerImageName.parse(testDbContainer)) + .withCopyFileToContainer(MountableFile.forHostPath(initPath), "/docker-entrypoint-initdb.d/init.sql") + .withReuse(true); + testDatabase.start(); + ds = TestDatabaseInit.settOppDatasourceOgMigrer(testDatabase.getJdbcUrl(), "fpsak", "fpsak", TestDatabaseInit.DEFAULT_DS_SCHEMA); + TestDatabaseInit.settJdniOppslag(ds); + schema = TestDatabaseInit.DEFAULT_DS_SCHEMA; } @Test @@ -49,7 +63,8 @@ void sjekk_at_alle_tabeller_er_dokumentert() throws Exception { try (var conn = ds.getConnection(); var stmt = conn.prepareStatement(sql); var rs = stmt.executeQuery()) { while (rs.next()) { - avvik.add(rs.getString(1)); + if (!"flyway_schema_history".equals(rs.getString(1))) + avvik.add(rs.getString(1)); } } @@ -82,7 +97,8 @@ AND upper(t.column_name) NOT IN ('OPPRETTET_TID','ENDRET_TID','OPPRETTET_AV','EN try (var conn = ds.getConnection(); var stmt = conn.prepareStatement(sql); var rs = stmt.executeQuery()) { while (rs.next()) { - avvik.add("\n" + rs.getString(1)); + if (!rs.getString(1).startsWith("flyway_schema_history")) + avvik.add("\n" + rs.getString(1)); } } diff --git a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/JpaExtension.java b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/JpaExtension.java index 15b81ff56db..ce90904bdf4 100644 --- a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/JpaExtension.java +++ b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/JpaExtension.java @@ -1,25 +1,33 @@ package no.nav.foreldrepenger.dbstoette; -import no.nav.foreldrepenger.konfig.Environment; -import no.nav.vedtak.felles.testutilities.db.EntityManagerAwareExtension; +import java.io.File; import org.testcontainers.oracle.OracleContainer; import org.testcontainers.utility.DockerImageName; import org.testcontainers.utility.MountableFile; +import no.nav.foreldrepenger.konfig.Environment; +import no.nav.vedtak.felles.testutilities.db.EntityManagerAwareExtension; + public class JpaExtension extends EntityManagerAwareExtension { public static final String DEFAULT_TEST_DB_SCHEMA_NAME; private static final String TEST_DB_CONTAINER = Environment.current().getProperty("testcontainer.test.db", String.class, "gvenzl/oracle-free:23-slim-faststart"); private static final OracleContainer TEST_DATABASE; + private static final String INIT_SCRIPT_PATH = "../.oracle/oracle-init/fpsak.sql"; static { + var initPath = INIT_SCRIPT_PATH; + while (!(new File(initPath)).exists()) { + initPath = "../" + initPath; + } TEST_DATABASE = new OracleContainer(DockerImageName.parse(TEST_DB_CONTAINER)) - .withCopyFileToContainer(MountableFile.forHostPath("../../.oracle/oracle-init/fpsak.sql"), "/docker-entrypoint-initdb.d/init.sql") + .withCopyFileToContainer(MountableFile.forHostPath(initPath), "/docker-entrypoint-initdb.d/init.sql") .withReuse(true); TEST_DATABASE.start(); DEFAULT_TEST_DB_SCHEMA_NAME = TEST_DATABASE.getUsername(); + var ds = TestDatabaseInit.settOppDatasourceOgMigrer(TEST_DATABASE.getJdbcUrl(), "fpsak", "fpsak", TestDatabaseInit.DEFAULT_DS_SCHEMA); TestDatabaseInit.settOppDatasourceOgMigrer(TEST_DATABASE.getJdbcUrl(), "fpsak_hist", "fpsak_hist", "dvhDS"); - TestDatabaseInit.settOppDatasourceOgMigrer(TEST_DATABASE.getJdbcUrl(), "fpsak", "fpsak", "defaultDS"); + TestDatabaseInit.settJdniOppslag(ds); } } diff --git a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/TestDatabaseInit.java b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/TestDatabaseInit.java index e5dab7a151e..c5477ef07b0 100644 --- a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/TestDatabaseInit.java +++ b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/TestDatabaseInit.java @@ -19,15 +19,16 @@ public final class TestDatabaseInit { + public static final String DEFAULT_DS_SCHEMA = "defaultDS"; + private static final AtomicBoolean GUARD_UNIT_TEST_SKJEMAER = new AtomicBoolean(); private static final Environment ENV = Environment.current(); private static final String DB_SCRIPT_LOCATION = "/db/migration/"; - public static void settOppDatasourceOgMigrer(String jdbcUrl, String username, String password, String schema) { + public static DataSource settOppDatasourceOgMigrer(String jdbcUrl, String username, String password, String schema) { var ds = createDatasource(jdbcUrl, username, password); - settJdniOppslag(ds); if (GUARD_UNIT_TEST_SKJEMAER.compareAndSet(false, true)) { var flyway = Flyway.configure() .dataSource(ds) @@ -50,6 +51,7 @@ public static void settOppDatasourceOgMigrer(String jdbcUrl, String username, St } } GUARD_UNIT_TEST_SKJEMAER.compareAndSet(true, false); + return ds; } private static String getScriptLocation(String schema) { @@ -70,7 +72,7 @@ private static String fileScriptLocation(String schema) { return "filesystem:" + location.getPath(); } - private static void settJdniOppslag(DataSource dataSource) { + public static void settJdniOppslag(DataSource dataSource) { try { new EnvEntry("jdbc/defaultDS", dataSource); // NOSONAR } catch (NamingException e) { diff --git a/web/src/test/java/no/nav/foreldrepenger/RapporterUnmappedKolonnerIDatabaseTest.java b/web/src/test/java/no/nav/foreldrepenger/RapporterUnmappedKolonnerIDatabaseTest.java index 0b039785b1b..e69afc60081 100644 --- a/web/src/test/java/no/nav/foreldrepenger/RapporterUnmappedKolonnerIDatabaseTest.java +++ b/web/src/test/java/no/nav/foreldrepenger/RapporterUnmappedKolonnerIDatabaseTest.java @@ -13,8 +13,6 @@ import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; -import no.nav.foreldrepenger.dbstoette.TestDatabaseInit; - import org.hibernate.boot.Metadata; import org.hibernate.boot.model.relational.Database; import org.hibernate.boot.model.relational.Namespace; @@ -24,18 +22,20 @@ import org.hibernate.service.spi.SessionFactoryServiceRegistry; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import no.nav.foreldrepenger.dbstoette.JpaExtension; + /** * Denne testen rapporterer kun tabeller og kolonner som ikke er mappet i * hibernate. Det kan være gyldige grunner til det (f.eks. dersom det kun * aksesseres gjennom native sql), men p.t. høyst sannsynlig ikke. Bør * gjennomgås jevnlig for å luke manglende contract av db skjema. */ -@Disabled +@ExtendWith(JpaExtension.class) class RapporterUnmappedKolonnerIDatabaseTest { private static final Logger LOG = LoggerFactory.getLogger(RapporterUnmappedKolonnerIDatabaseTest.class); @@ -53,14 +53,6 @@ public RapporterUnmappedKolonnerIDatabaseTest() { @BeforeAll public static void setup() { - // Kan ikke skrus på nå - trigger på CHAR kolonner som kunne vært VARCHAR. Må - // fikses først - // System.setProperty("hibernate.hbm2ddl.auto", "validate"); - try { - //TestDatabaseInit.settJdniOppslag(); - } catch (Exception e) { - throw new ExceptionInInitializerError(e); - } Map configuration = new HashMap<>(); From a5eb28caf212d63138e799ee5fe9f1db4ce54ba4 Mon Sep 17 00:00:00 2001 From: Jens-Otto Larsen Date: Thu, 9 Jan 2025 15:40:27 +0100 Subject: [PATCH 3/5] Mindre fiks --- .../nav/foreldrepenger/behandlingslager/EntityTest.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/EntityTest.java b/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/EntityTest.java index d6e4c66569d..076d05c3536 100644 --- a/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/EntityTest.java +++ b/behandlingslager/testutil/src/test/java/no/nav/foreldrepenger/behandlingslager/EntityTest.java @@ -42,15 +42,6 @@ class EntityTest { private static final EntityManagerFactory entityManagerFactory; static { - // Kan ikke skrus på nå - trigger på CHAR kolonner som kunne vært VARCHAR. Må - // fikses først - // System.setProperty("hibernate.hbm2ddl.auto", "validate"); - try { - // trenger å konfigurere opp jndi etc. - //TestDatabaseInit.settJdniOppslag(); - } catch (Exception e) { - throw new ExceptionInInitializerError(e); - } entityManagerFactory = Persistence.createEntityManagerFactory("pu-default"); } From ac3568a18580f389d88f0a38601fe71641f28ba5 Mon Sep 17 00:00:00 2001 From: Jens-Otto Larsen Date: Thu, 9 Jan 2025 22:04:34 +0100 Subject: [PATCH 4/5] Forenkling testdb-setup --- .../db/validering/SjekkDbStrukturTest.java | 17 ++++------------- .../foreldrepenger/dbstoette/JpaExtension.java | 15 ++------------- .../dbstoette/TestDatabaseInit.java | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/migreringer/src/test/java/no/nav/foreldrepenger/db/validering/SjekkDbStrukturTest.java b/migreringer/src/test/java/no/nav/foreldrepenger/db/validering/SjekkDbStrukturTest.java index 8696ef5d4e8..6a8675e47d1 100644 --- a/migreringer/src/test/java/no/nav/foreldrepenger/db/validering/SjekkDbStrukturTest.java +++ b/migreringer/src/test/java/no/nav/foreldrepenger/db/validering/SjekkDbStrukturTest.java @@ -2,7 +2,6 @@ import static org.assertj.core.api.Assertions.assertThat; -import java.io.File; import java.util.ArrayList; import java.util.List; @@ -15,7 +14,6 @@ import org.testcontainers.utility.MountableFile; import no.nav.foreldrepenger.dbstoette.TestDatabaseInit; -import no.nav.foreldrepenger.konfig.Environment; /** * Tester at alle migreringer følger standarder for navn og god praksis. @@ -34,19 +32,12 @@ class SjekkDbStrukturTest { @BeforeAll public static void setup() { - var testDbContainer = Environment.current().getProperty("testcontainer.test.db", String.class, "gvenzl/oracle-free:23-slim-faststart"); - - var initPath = "../.oracle/oracle-init/fpsak.sql"; - while (!(new File(initPath)).exists()) { - initPath = "../" + initPath; - } - var testDatabase = new OracleContainer(DockerImageName.parse(testDbContainer)) - .withCopyFileToContainer(MountableFile.forHostPath(initPath), "/docker-entrypoint-initdb.d/init.sql") + schema = TestDatabaseInit.DEFAULT_DS_SCHEMA; + var testDatabase = new OracleContainer(DockerImageName.parse(TestDatabaseInit.TEST_DB_CONTAINER)) + .withCopyFileToContainer(MountableFile.forHostPath(TestDatabaseInit.DB_SETUP_SCRIPT_PATH), "/docker-entrypoint-initdb.d/init.sql") .withReuse(true); testDatabase.start(); - ds = TestDatabaseInit.settOppDatasourceOgMigrer(testDatabase.getJdbcUrl(), "fpsak", "fpsak", TestDatabaseInit.DEFAULT_DS_SCHEMA); - TestDatabaseInit.settJdniOppslag(ds); - schema = TestDatabaseInit.DEFAULT_DS_SCHEMA; + ds = TestDatabaseInit.settOppDatasourceOgMigrer(testDatabase.getJdbcUrl(), "fpsak", "fpsak", schema); } @Test diff --git a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/JpaExtension.java b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/JpaExtension.java index ce90904bdf4..d309b7592b5 100644 --- a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/JpaExtension.java +++ b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/JpaExtension.java @@ -1,31 +1,20 @@ package no.nav.foreldrepenger.dbstoette; -import java.io.File; - import org.testcontainers.oracle.OracleContainer; import org.testcontainers.utility.DockerImageName; import org.testcontainers.utility.MountableFile; -import no.nav.foreldrepenger.konfig.Environment; import no.nav.vedtak.felles.testutilities.db.EntityManagerAwareExtension; public class JpaExtension extends EntityManagerAwareExtension { - public static final String DEFAULT_TEST_DB_SCHEMA_NAME; - private static final String TEST_DB_CONTAINER = Environment.current().getProperty("testcontainer.test.db", String.class, "gvenzl/oracle-free:23-slim-faststart"); private static final OracleContainer TEST_DATABASE; - private static final String INIT_SCRIPT_PATH = "../.oracle/oracle-init/fpsak.sql"; static { - var initPath = INIT_SCRIPT_PATH; - while (!(new File(initPath)).exists()) { - initPath = "../" + initPath; - } - TEST_DATABASE = new OracleContainer(DockerImageName.parse(TEST_DB_CONTAINER)) - .withCopyFileToContainer(MountableFile.forHostPath(initPath), "/docker-entrypoint-initdb.d/init.sql") + TEST_DATABASE = new OracleContainer(DockerImageName.parse(TestDatabaseInit.TEST_DB_CONTAINER)) + .withCopyFileToContainer(MountableFile.forHostPath(TestDatabaseInit.DB_SETUP_SCRIPT_PATH), "/docker-entrypoint-initdb.d/init.sql") .withReuse(true); TEST_DATABASE.start(); - DEFAULT_TEST_DB_SCHEMA_NAME = TEST_DATABASE.getUsername(); var ds = TestDatabaseInit.settOppDatasourceOgMigrer(TEST_DATABASE.getJdbcUrl(), "fpsak", "fpsak", TestDatabaseInit.DEFAULT_DS_SCHEMA); TestDatabaseInit.settOppDatasourceOgMigrer(TEST_DATABASE.getJdbcUrl(), "fpsak_hist", "fpsak_hist", "dvhDS"); TestDatabaseInit.settJdniOppslag(ds); diff --git a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/TestDatabaseInit.java b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/TestDatabaseInit.java index c5477ef07b0..f0509651822 100644 --- a/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/TestDatabaseInit.java +++ b/migreringer/src/test/java/no/nav/foreldrepenger/dbstoette/TestDatabaseInit.java @@ -21,6 +21,12 @@ public final class TestDatabaseInit { public static final String DEFAULT_DS_SCHEMA = "defaultDS"; + public static final String TEST_DB_CONTAINER = Environment.current() + .getProperty("testcontainer.test.db", String.class, "gvenzl/oracle-free:23-slim-faststart"); + public static final String DB_SETUP_SCRIPT_PATH = getTestDbSetupScriptPath(); + + private static final String INIT_SCRIPT_PATH = "../.oracle/oracle-init/fpsak.sql"; + private static final AtomicBoolean GUARD_UNIT_TEST_SKJEMAER = new AtomicBoolean(); private static final Environment ENV = Environment.current(); @@ -72,7 +78,15 @@ private static String fileScriptLocation(String schema) { return "filesystem:" + location.getPath(); } - public static void settJdniOppslag(DataSource dataSource) { + private static String getTestDbSetupScriptPath() { + var initPath = INIT_SCRIPT_PATH; + while (!(new File(initPath)).exists()) { + initPath = "../" + initPath; + } + return initPath; + } + + static void settJdniOppslag(DataSource dataSource) { try { new EnvEntry("jdbc/defaultDS", dataSource); // NOSONAR } catch (NamingException e) { From 3a770a870f9e642182dac3e406bf289e848b2570 Mon Sep 17 00:00:00 2001 From: Jens-Otto Larsen Date: Fri, 10 Jan 2025 11:12:48 +0100 Subject: [PATCH 5/5] Siste jms --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 59a671b75aa..97603cd59f8 100644 --- a/pom.xml +++ b/pom.xml @@ -150,7 +150,7 @@ no.nav.foreldrepenger.felles jms-integrasjon - 3.2.9 + 3.2.10