From 04789fd3273e1b92f0b3b2a56e026eecb5e04f52 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Wed, 20 May 2020 06:46:40 +0200 Subject: [PATCH] Trying to make DBUnit to work with JSON columns Former-commit-id: 7d93f4dde4a0ce63e99553f4e338da2e876fa190 --- .../org/libreccm/modules/DatasetsTest.java | 4 +- .../scripts/002_create_ccm_core_tables.sql | 14 +- .../scripts/002_create_ccm_core_tables.sql | 8 +- .../ConfigurationLoaderTest/after-load.xml | 24 ++ .../ConfigurationLoaderTest/after-load.yml | 40 +-- .../libreccm/testutils/DatasetsVerifier.java | 235 ++++++++++-------- 6 files changed, 195 insertions(+), 130 deletions(-) create mode 100644 ccm-core/src/test/resources/datasets/org/libreccm/modules/ConfigurationLoaderTest/after-load.xml diff --git a/ccm-core/src/test/java/org/libreccm/modules/DatasetsTest.java b/ccm-core/src/test/java/org/libreccm/modules/DatasetsTest.java index fa7b0955b..304883d55 100644 --- a/ccm-core/src/test/java/org/libreccm/modules/DatasetsTest.java +++ b/ccm-core/src/test/java/org/libreccm/modules/DatasetsTest.java @@ -46,7 +46,7 @@ public class DatasetsTest extends DatasetsVerifier { @Parameterized.Parameters(name = "Dataset {0}") public static Collection data() { return Arrays.asList(new String[]{ - "/datasets/org/libreccm/modules/ConfigurationLoaderTest/after-load.yml",}); + "/datasets/org/libreccm/modules/ConfigurationLoaderTest/after-load.x,l",}); } public DatasetsTest(final String datasetPath) { @@ -60,7 +60,7 @@ public class DatasetsTest extends DatasetsVerifier { @Override public DatasetType getDatasetType() { - return YAML; + return FLAT_XML; } @BeforeClass diff --git a/ccm-core/src/test/resources-wildfly-remote-h2-mem/scripts/002_create_ccm_core_tables.sql b/ccm-core/src/test/resources-wildfly-remote-h2-mem/scripts/002_create_ccm_core_tables.sql index 7e528ef73..3f96c0d09 100644 --- a/ccm-core/src/test/resources-wildfly-remote-h2-mem/scripts/002_create_ccm_core_tables.sql +++ b/ccm-core/src/test/resources-wildfly-remote-h2-mem/scripts/002_create_ccm_core_tables.sql @@ -4,7 +4,7 @@ -- create schema CCM_CORE; - create table CCM_CORE.APPLICATIONS ( +create table CCM_CORE.APPLICATIONS ( APPLICATION_TYPE varchar(1024) not null, PRIMARY_URL varchar(1024) not null, OBJECT_ID bigint not null, @@ -148,7 +148,7 @@ create table CCM_CORE.FORMBUILDER_DATA_QUERIES ( DESCRIPTION json, - NAME json, + QUERY_NAME json, QUERY_ID varchar(255), OBJECT_ID bigint not null, primary key (OBJECT_ID) @@ -194,7 +194,7 @@ create table CCM_CORE.FORMBUILDER_PROCESS_LISTENERS ( DESCRIPTION json, LISTENER_CLASS varchar(255), - NAME json, + LISTENER_NAME json, PROCESS_LISTENER_ORDER bigint, OBJECT_ID bigint not null, formSection_OBJECT_ID bigint, @@ -452,12 +452,12 @@ SETTING_ID bigint not null, CONFIGURATION_CLASS varchar(512) not null, NAME varchar(512) not null, - SETTING_VALUE_DOUBLE double, - SETTING_VALUE_BIG_DECIMAL decimal(19,2), - SETTING_VALUE json, - SETTING_VALUE_BOOLEAN boolean, SETTING_VALUE_LONG bigint, + SETTING_VALUE_LOCALIZED_STRING json, + SETTING_VALUE_DOUBLE double, SETTING_VALUE_STRING varchar(1024), + SETTING_VALUE_BIG_DECIMAL decimal(19,2), + SETTING_VALUE_BOOLEAN boolean, primary key (SETTING_ID) ); diff --git a/ccm-core/src/test/resources-wildfly-remote-pgsql/scripts/002_create_ccm_core_tables.sql b/ccm-core/src/test/resources-wildfly-remote-pgsql/scripts/002_create_ccm_core_tables.sql index 878286d56..0ecf444b7 100644 --- a/ccm-core/src/test/resources-wildfly-remote-pgsql/scripts/002_create_ccm_core_tables.sql +++ b/ccm-core/src/test/resources-wildfly-remote-pgsql/scripts/002_create_ccm_core_tables.sql @@ -446,12 +446,12 @@ SETTING_ID int8 not null, CONFIGURATION_CLASS varchar(512) not null, NAME varchar(512) not null, - SETTING_VALUE_DOUBLE float8, - SETTING_VALUE_BIG_DECIMAL numeric(19, 2), - SETTING_VALUE_LOCALIZED_STRING jsonb, - SETTING_VALUE_BOOLEAN boolean, SETTING_VALUE_LONG int8, + SETTING_VALUE_LOCALIZED_STRING jsonb, + SETTING_VALUE_DOUBLE float8, SETTING_VALUE_STRING varchar(1024), + SETTING_VALUE_BIG_DECIMAL numeric(19, 2), + SETTING_VALUE_BOOLEAN boolean, primary key (SETTING_ID) ); diff --git a/ccm-core/src/test/resources/datasets/org/libreccm/modules/ConfigurationLoaderTest/after-load.xml b/ccm-core/src/test/resources/datasets/org/libreccm/modules/ConfigurationLoaderTest/after-load.xml new file mode 100644 index 000000000..377e5328b --- /dev/null +++ b/ccm-core/src/test/resources/datasets/org/libreccm/modules/ConfigurationLoaderTest/after-load.xml @@ -0,0 +1,24 @@ + + + + + + diff --git a/ccm-core/src/test/resources/datasets/org/libreccm/modules/ConfigurationLoaderTest/after-load.yml b/ccm-core/src/test/resources/datasets/org/libreccm/modules/ConfigurationLoaderTest/after-load.yml index be2bb1406..1190dff69 100644 --- a/ccm-core/src/test/resources/datasets/org/libreccm/modules/ConfigurationLoaderTest/after-load.yml +++ b/ccm-core/src/test/resources/datasets/org/libreccm/modules/ConfigurationLoaderTest/after-load.yml @@ -1,23 +1,31 @@ ccm_core.settings: - - setting_id: 100 + - dtype: LocalizedStringSetting + setting_id: 120 + configuration_class: org.libreccm.configuration.ExampleConfiguration + name: title + setting_value_long: null + setting_value_localized_string: "{\"de\": \"Versuch\", \"en\": \"Test\"}" + setting_value_double: null + setting_value_string: null + setting_value_big_decimal: null + setting_value_boolean: null + - dtype: BooleanSetting + setting_id: 100 configuration_class: org.libreccm.configuration.ExampleConfiguration name: enabled + setting_value_long: null + setting_value_localized_string: null + setting_value_double: null + setting_value_string: null + setting_value_big_decimal: null setting_value_boolean: true - dtype: BooleanSetting - - setting_id: 110 + - dtype: LongSetting + setting_id: 110 configuration_class: org.libreccm.configuration.ExampleConfiguration name: itemsPerPage setting_value_long: 100 - dtype: LongSetting - - setting_id: 120 - configuration_class: org.libreccm.configuration.ExampleConfiguration - name: title - dtype: LocalizedStringSetting -ccm_core.settings_l10n_str_values: - - entry_id: 120 - locale: de - localized_value: Versuch - - entry_id: 120 - locale: en - localized_value: Test - + setting_value_localized_string: null + setting_value_double: null + setting_value_string: null + setting_value_big_decimal: null + setting_value_boolean: null diff --git a/ccm-testutils/src/main/java/org/libreccm/testutils/DatasetsVerifier.java b/ccm-testutils/src/main/java/org/libreccm/testutils/DatasetsVerifier.java index 0974b90b6..f5e1b9462 100644 --- a/ccm-testutils/src/main/java/org/libreccm/testutils/DatasetsVerifier.java +++ b/ccm-testutils/src/main/java/org/libreccm/testutils/DatasetsVerifier.java @@ -42,12 +42,14 @@ import org.junit.runners.Parameterized; import static org.libreccm.testutils.DatasetType.*; +import org.dbunit.database.DatabaseConfig; import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; import org.jboss.arquillian.persistence.dbunit.dataset.yaml.YamlDataSet; import java.io.InputStream; import java.io.StringReader; import java.nio.charset.StandardCharsets; +import java.sql.ResultSet; /** * @@ -75,39 +77,38 @@ import java.nio.charset.StandardCharsets; */ public class DatasetsVerifier { - private final String datasetPath; +private final String datasetPath; - public DatasetsVerifier(final String datasetsPath) { - this.datasetPath = datasetsPath; - } +public DatasetsVerifier(final String datasetsPath) { + this.datasetPath = datasetsPath; +} - /** - * Overwrite this method if you are using another schema than the default - * one. - * - * @return An string array contains the names of the database schemata used - * by the datasets the test. - */ - public String[] getSchemas() { - return new String[]{}; - } +/** + * Overwrite this method if you are using another schema than the default one. + * + * @return An string array contains the names of the database schemata used by + * the datasets the test. + */ +public String[] getSchemas() { + return new String[]{}; +} - public DatasetType getDatasetType() { - return JSON; - } +public DatasetType getDatasetType() { + return JSON; +} - @Test - @edu.umd.cs.findbugs.annotations.SuppressWarnings( - value = "DMI_EMPTY_DB_PASSWORD", - justification = "H2 in mem database does not need a password") - public void verifyDataset() throws SQLException, - URISyntaxException, - IOException, - DatabaseUnitException { - //Create database connection to an in memory h2 database. Placed in - //try-with-resources block to ensure that the connection is closed. - final StringBuffer buffer = new StringBuffer("jdbc:h2:mem:testdatabase"); - //Create schema if necssary +@Test +@edu.umd.cs.findbugs.annotations.SuppressWarnings( + value = "DMI_EMPTY_DB_PASSWORD", + justification = "H2 in mem database does not need a password") +public void verifyDataset() throws SQLException, + URISyntaxException, + IOException, + DatabaseUnitException { + //Create database connection to an in memory h2 database. Placed in + //try-with-resources block to ensure that the connection is closed. + final StringBuffer buffer = new StringBuffer("jdbc:h2:mem:testdatabase"); + //Create schema if necssary // if (getSchemas().length > 0) { // buffer.append(";INIT="); // for (final String schema : getSchemas()) { @@ -118,90 +119,122 @@ public class DatasetsVerifier { // schema)); // } // } - final String connectionStr = buffer.toString(); - try (final Connection connection = DriverManager - .getConnection(connectionStr, "sa", "")) { - final StringBuffer schemaCreateBuffer = new StringBuffer(); - for (final String schema : getSchemas()) { - schemaCreateBuffer.append(String - .format("CREATE SCHEMA IF NOT EXISTS %s;%n", schema)); - } + final String connectionStr = buffer.toString(); + try (final Connection connection = DriverManager + .getConnection(connectionStr, "sa", "")) { + final StringBuffer schemaCreateBuffer = new StringBuffer(); + for (final String schema : getSchemas()) { + schemaCreateBuffer.append(String + .format("CREATE SCHEMA IF NOT EXISTS %s;%n", schema)); + } - try (final StringReader schemaCreateReader = new StringReader( - schemaCreateBuffer.toString())) { - RunScript.execute(connection, schemaCreateReader); - } + try (final StringReader schemaCreateReader = new StringReader( + schemaCreateBuffer.toString())) { + RunScript.execute(connection, schemaCreateReader); + } + + //Create DB tables etc + for (final String ddlFile : getDdlFiles()) { + processDdlFile(connection, ddlFile); + } - //Create DB tables etc - for (final String ddlFile : getDdlFiles()) { - processDdlFile(connection, ddlFile); - } // final Path schemaPath = Paths.get(getClass().getResource( // "/sql/ddl/auto/h2.sql").toURI()); // RunScript.execute(connection, Files.newBufferedReader( // schemaPath, StandardCharsets.UTF_8)); - connection.commit(); + connection.commit(); - //Get dataset to test - final IDataSet dataSet; - try (final InputStream inputStream = getClass().getResourceAsStream( - datasetPath)) { - switch (getDatasetType()) { - case FLAT_XML: - final FlatXmlDataSetBuilder builder - = new FlatXmlDataSetBuilder(); - dataSet = builder.build(inputStream); - break; - case JSON: - dataSet = new JsonDataSet(inputStream); - break; - case YAML: - dataSet = new YamlDataSet(inputStream); - break; - default: - throw new IllegalArgumentException(String.format( - "Unsupported DatasetType \"%s\"", - getDatasetType())); - } + System.out.println("DB Info:"); + System.out.println("Catalogs:"); + try (ResultSet resultSet = connection.getMetaData().getCatalogs()) { + while(resultSet.next()) { + System.out.println(resultSet.getString("table_cat")); + } + } + System.out.println("Schemas"); + try(ResultSet resultSet = connection.getMetaData().getSchemas()) { + while(resultSet.next()) { + System.out.println(resultSet.getString("TABLE_SCHEM")); + } + } + System.out.println("Tables"); + try (ResultSet resultSet = connection.getMetaData().getTables("TESTDATABASE", "CCM_CORE", null, null)) { + while(resultSet.next()) { + System.out.println(resultSet.getString("table_name")); + } + } + System.out.println("settings columns:"); + try (ResultSet resultSet = connection.getMetaData().getColumns( + "TESTDATABASE", "CCM_CORE", "SETTINGS", null + )) { + while (resultSet.next()) { + System.out.printf("table_name = %s%n", resultSet.getString( + "table_name")); + System.out.printf("col_name = %s%n", resultSet.getString( + "column_name")); } - - //Create DBUnit DB connection - final IDatabaseConnection dbUnitConn - = new DatabaseConnection(connection); - dbUnitConn.getConfig().setProperty( - "http://www.dbunit.org/features/qualifiedTableNames", true); - //Check if dumping works the DB works before loading the dataset. - System.out.println("Dump before loading dataset..."); - verifyDumping(dbUnitConn); - - //Put dataset into DB - DatabaseOperation.CLEAN_INSERT.execute(dbUnitConn, dataSet); - - //Check if dumping works after loading the dataset - System.out.println("Dump after loading dataset..."); - verifyDumping(dbUnitConn); } - } - protected String[] getDdlFiles() { - return new String[]{"/sql/ddl/auto/libreccmh2.sql"}; - } + //Get dataset to test + final IDataSet dataSet; + try (final InputStream inputStream = getClass().getResourceAsStream( + datasetPath)) { + switch (getDatasetType()) { + case FLAT_XML: + final FlatXmlDataSetBuilder builder + = new FlatXmlDataSetBuilder(); + dataSet = builder.build(inputStream); + break; + case JSON: + dataSet = new JsonDataSet(inputStream); + break; + case YAML: + dataSet = new YamlDataSet(inputStream); + break; + default: + throw new IllegalArgumentException(String.format( + "Unsupported DatasetType \"%s\"", + getDatasetType())); + } + } - private void processDdlFile(final Connection connection, - final String ddlFile) throws URISyntaxException, - SQLException, - IOException { - final Path schemaPath = Paths.get(getClass().getResource(ddlFile) - .toURI()); - RunScript.execute(connection, - Files.newBufferedReader(schemaPath, - StandardCharsets.UTF_8)); - } + //Create DBUnit DB connection + final IDatabaseConnection dbUnitConn + = new DatabaseConnection(connection); + dbUnitConn.getConfig().setProperty( + "http://www.dbunit.org/features/qualifiedTableNames", true); + //Check if dumping works the DB works before loading the dataset. + System.out.println("Dump before loading dataset..."); + verifyDumping(dbUnitConn); + + //Put dataset into DB + DatabaseOperation.CLEAN_INSERT.execute(dbUnitConn, dataSet); - private void verifyDumping(final IDatabaseConnection connection) - throws SQLException, IOException, DataSetException { - final IDataSet data = connection.createDataSet(); - FlatXmlDataSet.write(data, System.out); + //Check if dumping works after loading the dataset + System.out.println("Dump after loading dataset..."); + verifyDumping(dbUnitConn); } +} + +protected String[] getDdlFiles() { + return new String[]{"/sql/ddl/auto/libreccmh2.sql"}; +} + +private void processDdlFile(final Connection connection, + final String ddlFile) throws URISyntaxException, + SQLException, + IOException { + final Path schemaPath = Paths.get(getClass().getResource(ddlFile) + .toURI()); + RunScript.execute(connection, + Files.newBufferedReader(schemaPath, + StandardCharsets.UTF_8)); +} + +private void verifyDumping(final IDatabaseConnection connection) + throws SQLException, IOException, DataSetException { + final IDataSet data = connection.createDataSet(); + FlatXmlDataSet.write(data, System.out); +} }