From 48c8497086058a5dfaae3d7b966b817208495a39 Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Fri, 2 Aug 2019 16:00:43 -0400 Subject: [PATCH] Changed migration test from derby to h2 Added a new test from 340 to latest version. --- .../java/ca/uhn/fhir/util/VersionEnum.java | 6 +- hapi-fhir-cli/hapi-fhir-cli-api/pom.xml | 5 + .../cli/HapiMigrateDatabaseCommandTest.java | 96 ++++++++++++++----- ..._340.sql => persistence_create_h2_340.sql} | 0 4 files changed, 80 insertions(+), 27 deletions(-) rename hapi-fhir-cli/hapi-fhir-cli-api/src/test/resources/{persistence_create_derby107_340.sql => persistence_create_h2_340.sql} (100%) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/VersionEnum.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/VersionEnum.java index 04568303f89..7669fa4e8e1 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/VersionEnum.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/VersionEnum.java @@ -31,6 +31,10 @@ public enum VersionEnum { V3_6_0, V3_7_0, V3_8_0, - V4_0_0 + V4_0_0; + public static VersionEnum latestVersion() { + VersionEnum[] values = VersionEnum.values(); + return values[values.length - 1]; + } } diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/pom.xml b/hapi-fhir-cli/hapi-fhir-cli-api/pom.xml index ce4a55202c4..c1169e57e21 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-api/pom.xml +++ b/hapi-fhir-cli/hapi-fhir-cli-api/pom.xml @@ -97,6 +97,11 @@ org.apache.derby derby + + com.h2database + h2 + test + org.mariadb.jdbc mariadb-java-client diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/HapiMigrateDatabaseCommandTest.java b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/HapiMigrateDatabaseCommandTest.java index 0f0a207744f..ba184732e21 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/HapiMigrateDatabaseCommandTest.java +++ b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/HapiMigrateDatabaseCommandTest.java @@ -1,6 +1,7 @@ package ca.uhn.fhir.cli; import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; +import ca.uhn.fhir.util.VersionEnum; import com.google.common.base.Charsets; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; @@ -12,6 +13,7 @@ import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatemen import org.springframework.jdbc.support.lob.DefaultLobHandler; import org.springframework.jdbc.support.lob.LobCreator; +import javax.validation.constraints.NotNull; import java.io.File; import java.io.IOException; import java.sql.PreparedStatement; @@ -31,23 +33,21 @@ import static org.junit.Assert.assertTrue; public class HapiMigrateDatabaseCommandTest { private static final Logger ourLog = LoggerFactory.getLogger(HapiMigrateDatabaseCommandTest.class); + public static final String DB_DIRECTORY = "target/h2_test"; static { System.setProperty("test", "true"); } @Test - public void testMigrate_340_370() throws IOException { + public void testMigrate_340_current() throws IOException { - File directory = new File("target/migrator_derby_test_340_360"); - if (directory.exists()) { - FileUtils.deleteDirectory(directory); - } + File location = getLocation("migrator_h2_test_340_current"); - String url = "jdbc:derby:directory:" + directory.getAbsolutePath() + ";create=true"; - DriverTypeEnum.ConnectionProperties connectionProperties = DriverTypeEnum.DERBY_EMBEDDED.newConnectionProperties(url, "", ""); + String url = "jdbc:h2:" + location.getAbsolutePath() + ";create=true"; + DriverTypeEnum.ConnectionProperties connectionProperties = DriverTypeEnum.H2_EMBEDDED.newConnectionProperties(url, "", ""); - String initSql = "/persistence_create_derby107_340.sql"; + String initSql = "/persistence_create_h2_340.sql"; executeSqlStatements(connectionProperties, initSql); seedDatabase340(connectionProperties); @@ -58,7 +58,57 @@ public class HapiMigrateDatabaseCommandTest { String[] args = new String[]{ "migrate-database", - "-d", "DERBY_EMBEDDED", + "-d", "H2_EMBEDDED", + "-u", url, + "-n", "", + "-p", "", + "-f", "V3_4_0", + "-t", VersionEnum.latestVersion().toString() + }; + App.main(args); + + connectionProperties.getTxTemplate().execute(t -> { + JdbcTemplate jdbcTemplate = connectionProperties.newJdbcTemplate(); + List> values = jdbcTemplate.queryForList("SELECT * FROM hfj_spidx_token"); + assertEquals(1, values.size()); + assertEquals("identifier", values.get(0).get("SP_NAME")); + assertEquals("12345678", values.get(0).get("SP_VALUE")); + assertTrue(values.get(0).keySet().contains("HASH_IDENTITY")); + assertEquals(7001889285610424179L, values.get(0).get("HASH_IDENTITY")); + return null; + }); + } + + @NotNull + private File getLocation(String theDatabaseName) throws IOException { + File directory = new File(DB_DIRECTORY); + if (directory.exists()) { + FileUtils.deleteDirectory(directory); + } + + return new File(DB_DIRECTORY + "/" + theDatabaseName); + } + + @Test + public void testMigrate_340_370() throws IOException { + + File location = getLocation("migrator_h2_test_340_360"); + + String url = "jdbc:h2:" + location.getAbsolutePath() + ";create=true"; + DriverTypeEnum.ConnectionProperties connectionProperties = DriverTypeEnum.H2_EMBEDDED.newConnectionProperties(url, "", ""); + + String initSql = "/persistence_create_h2_340.sql"; + executeSqlStatements(connectionProperties, initSql); + + seedDatabase340(connectionProperties); + + ourLog.info("**********************************************"); + ourLog.info("Done Setup, Starting Migration..."); + ourLog.info("**********************************************"); + + String[] args = new String[]{ + "migrate-database", + "-d", "H2_EMBEDDED", "-u", url, "-n", "", "-p", "", @@ -83,15 +133,12 @@ public class HapiMigrateDatabaseCommandTest { @Test public void testMigrate_340_350() throws IOException { - File directory = new File("target/migrator_derby_test_340_350"); - if (directory.exists()) { - FileUtils.deleteDirectory(directory); - } + File location = getLocation("migrator_h2_test_340_350"); - String url = "jdbc:derby:directory:" + directory.getAbsolutePath() + ";create=true"; - DriverTypeEnum.ConnectionProperties connectionProperties = DriverTypeEnum.DERBY_EMBEDDED.newConnectionProperties(url, "", ""); + String url = "jdbc:h2:" + location.getAbsolutePath() + ";create=true"; + DriverTypeEnum.ConnectionProperties connectionProperties = DriverTypeEnum.H2_EMBEDDED.newConnectionProperties(url, "", ""); - String initSql = "/persistence_create_derby107_340.sql"; + String initSql = "/persistence_create_h2_340.sql"; executeSqlStatements(connectionProperties, initSql); seedDatabase340(connectionProperties); @@ -102,7 +149,7 @@ public class HapiMigrateDatabaseCommandTest { String[] args = new String[]{ "migrate-database", - "-d", "DERBY_EMBEDDED", + "-d", "H2_EMBEDDED", "-u", url, "-n", "", "-p", "", @@ -125,7 +172,7 @@ public class HapiMigrateDatabaseCommandTest { args = new String[]{ "migrate-database", - "-d", "DERBY_EMBEDDED", + "-d", "H2_EMBEDDED", "-u", url, "-n", "", "-p", "", @@ -263,15 +310,12 @@ public class HapiMigrateDatabaseCommandTest { @Test public void testMigrate_340_350_NoMigrateHashes() throws IOException { - File directory = new File("target/migrator_derby_test_340_350_nmh"); - if (directory.exists()) { - FileUtils.deleteDirectory(directory); - } + File location = getLocation("migrator_h2_test_340_350_nmh"); - String url = "jdbc:derby:directory:" + directory.getAbsolutePath() + ";create=true"; - DriverTypeEnum.ConnectionProperties connectionProperties = DriverTypeEnum.DERBY_EMBEDDED.newConnectionProperties(url, "", ""); + String url = "jdbc:h2:" + location.getAbsolutePath() + ";create=true"; + DriverTypeEnum.ConnectionProperties connectionProperties = DriverTypeEnum.H2_EMBEDDED.newConnectionProperties(url, "", ""); - String initSql = "/persistence_create_derby107_340.sql"; + String initSql = "/persistence_create_h2_340.sql"; executeSqlStatements(connectionProperties, initSql); seedDatabase340(connectionProperties); @@ -282,7 +326,7 @@ public class HapiMigrateDatabaseCommandTest { String[] args = new String[]{ "migrate-database", - "-d", "DERBY_EMBEDDED", + "-d", "H2_EMBEDDED", "-u", url, "-n", "", "-p", "", diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/resources/persistence_create_derby107_340.sql b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/resources/persistence_create_h2_340.sql similarity index 100% rename from hapi-fhir-cli/hapi-fhir-cli-api/src/test/resources/persistence_create_derby107_340.sql rename to hapi-fhir-cli/hapi-fhir-cli-api/src/test/resources/persistence_create_h2_340.sql