From 04d5310e1537b9415aa8cc14db792d9f7617c0d8 Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 27 May 2022 10:24:54 +0200 Subject: [PATCH] SVV_TABLES integration test Added svv_tables.sql for querying data Added SvvTablesTest test --- .../redshift-tests/build.gradle | 4 + .../edwmigration/dumper/csv/CsvUtil.java | 39 --- .../edwmigration/dumper/jdbc/JdbcUtil.java | 80 ++---- .../dumper/pojo/SvvColumnsRow.java | 231 ------------------ .../edwmigration/dumper/sql/SqlUtil.java | 38 --- .../src/main/resources/sql/svv_tables.sql | 15 ++ .../dumper/integration/SvvTablesTest.java | 88 +++++++ .../google/integration/SvvColumnsTest.java | 81 ------ 8 files changed, 126 insertions(+), 450 deletions(-) delete mode 100644 dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/csv/CsvUtil.java delete mode 100644 dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/pojo/SvvColumnsRow.java create mode 100644 dumper-integration-tests/redshift-tests/src/main/resources/sql/svv_tables.sql create mode 100644 dumper-integration-tests/redshift-tests/src/test/java/com/google/edwmigration/dumper/integration/SvvTablesTest.java delete mode 100644 dumper-integration-tests/redshift-tests/src/test/java/com/google/integration/SvvColumnsTest.java diff --git a/dumper-integration-tests/redshift-tests/build.gradle b/dumper-integration-tests/redshift-tests/build.gradle index 5804241ff..2dedd5580 100644 --- a/dumper-integration-tests/redshift-tests/build.gradle +++ b/dumper-integration-tests/redshift-tests/build.gradle @@ -28,6 +28,10 @@ dependencies { implementation 'commons-io:commons-io:2.11.0' implementation 'org.apache.commons:commons-collections4:4.4' implementation 'com.opencsv:opencsv:5.6' + implementation 'com.google.truth:truth:1.1.3' + implementation 'com.google.truth.extensions:truth-java8-extension:1.1.3' + + implementation 'com.amazon.redshift:redshift-jdbc42:2.1.0.7' annotationProcessor 'com.google.auto.value:auto-value:1.9' compileOnly 'com.google.auto.value:auto-value-annotations:1.9' } diff --git a/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/csv/CsvUtil.java b/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/csv/CsvUtil.java deleted file mode 100644 index 8397a25c2..000000000 --- a/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/csv/CsvUtil.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2022 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.edwmigration.dumper.csv; - -import static com.google.edwmigration.dumper.base.TestConstants.TRAILING_SPACES_REGEX; -import static java.lang.Integer.parseInt; - -/** A helper class for reading and extracting data from CSV files. */ -public final class CsvUtil { - - private CsvUtil() {} - - /** - * @return String or an empty string if null. - */ - public static String getStringNotNull(String value) { - return value == null ? "" : TRAILING_SPACES_REGEX.matcher(value).replaceFirst(""); - } - - /** - * @return int or 0 if "". - */ - public static int getIntNotNull(String value) { - return getStringNotNull(value).equals("") ? 0 : parseInt(value); - } -} diff --git a/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/jdbc/JdbcUtil.java b/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/jdbc/JdbcUtil.java index d265845fd..03b16e1d2 100644 --- a/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/jdbc/JdbcUtil.java +++ b/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/jdbc/JdbcUtil.java @@ -15,22 +15,19 @@ */ package com.google.edwmigration.dumper.jdbc; -import static com.google.edwmigration.dumper.base.TestConstants.TRAILING_SPACES_REGEX; - -import java.math.BigDecimal; import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.SQLException; -import java.sql.Timestamp; -import java.time.ZonedDateTime; -import java.util.Calendar; -import java.util.TimeZone; +import java.util.ArrayList; +import java.util.List; /** * A helper class for checking Null values returned by executing SELECT request against a database. */ public final class JdbcUtil { - private JdbcUtil() {} + private JdbcUtil() { + } /** * @param rs A row with SELECT results. @@ -39,67 +36,28 @@ private JdbcUtil() {} */ public static String getStringNotNull(ResultSet rs, String column) throws SQLException { String string = rs.getString(column); - return rs.wasNull() ? "" : TRAILING_SPACES_REGEX.matcher(string).replaceFirst(""); + return rs.wasNull() ? "" : string; } /** * @param rs A row with SELECT results. - * @param column Database column name. - * @return int or 0 if null. - */ - public static int getIntNotNull(ResultSet rs, String column) throws SQLException { - int intValue = rs.getInt(column); - return rs.wasNull() ? 0 : intValue; - } - - /** - * @param rs A row with SELECT results. - * @param column Database column name. - * @return long or 0L if null. - */ - public static long getLongNotNull(ResultSet rs, String column) throws SQLException { - long longValue = rs.getLong(column); - return rs.wasNull() ? 0L : longValue; - } - - /** - * @param rs A row with SELECT results. - * @param column Database column name. - * @return byte[] or empty byte[] if null. - */ - public static byte[] getBytesNotNull(ResultSet rs, String column) throws SQLException { - try { - byte[] bytesValue = rs.getBytes(column); - return rs.wasNull() ? new byte[0] : bytesValue; - } catch (SQLException e) { - BigDecimal bigDecimal = rs.getBigDecimal(column); - return rs.wasNull() ? new byte[0] : bigDecimal.toBigInteger().toByteArray(); - } - } - - /** - * @param rs A row with SELECT results. - * @param column Database column name. - * @return double or 0.0 if null. + * @param columnIndex Database column index. + * @return String or an empty string if null. */ - public static double getDoubleNotNull(ResultSet rs, String column) throws SQLException { - double doubleValue = rs.getDouble(column); - return rs.wasNull() ? 0.0 : doubleValue; + public static String getStringNotNull(ResultSet rs, int columnIndex) throws SQLException { + String string = rs.getString(columnIndex); + return rs.wasNull() ? "" : string; } /** - * @param rs A row with SELECT results. - * @param column Database column name. - * @return long or 0L if null. + * @param rsmd Metadata of the executed SQL query. + * @return List of column names. */ - public static long getTimestampNotNull(ResultSet rs, String column) throws SQLException { - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - Timestamp timestamp = rs.getTimestamp(column, cal); - if (rs.wasNull()) { - return 0L; + public static List getDbColumnNames(ResultSetMetaData rsmd) throws SQLException { + List columnNames = new ArrayList<>(); + for (int i = 1; i <= rsmd.getColumnCount(); i++) { + columnNames.add(rsmd.getColumnName(i)); } - return Timestamp.from( - ZonedDateTime.of(timestamp.toLocalDateTime(), cal.getTimeZone().toZoneId()).toInstant()) - .getTime(); + return columnNames; } -} +} \ No newline at end of file diff --git a/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/pojo/SvvColumnsRow.java b/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/pojo/SvvColumnsRow.java deleted file mode 100644 index 94c4a7a1d..000000000 --- a/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/pojo/SvvColumnsRow.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * Copyright 2022 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.edwmigration.dumper.pojo; - -import static com.google.edwmigration.dumper.jdbc.JdbcUtil.getIntNotNull; -import static com.google.edwmigration.dumper.jdbc.JdbcUtil.getStringNotNull; -import static java.lang.System.lineSeparator; - -import com.google.auto.value.AutoValue; -import com.google.edwmigration.dumper.csv.CsvUtil; -import java.sql.ResultSet; -import java.sql.SQLException; - -/** POJO class for serialization data from DB and CSV files. */ -@AutoValue -public abstract class SvvColumnsRow { - - public static SvvColumnsRow create( - String tableCatalog, - String tableSchema, - String tableName, - String columnName, - int ordinalPosition, - String columnDefault, - String isNullable, - String dataType, - int characterMaximumLength, - int numericPrecision, - int numericPrecisionRadix, - int numericScale, - int datetimePrecision, - String intervalType, - String intervalPrecision, - String characterSetCatalog, - String characterSetSchema, - String characterSetName, - String collationCatalog, - String collationSchema, - String collationName, - String domainName, - String remarks) { - return new AutoValue_SvvColumnsRow( - tableCatalog, - tableSchema, - tableName, - columnName, - ordinalPosition, - columnDefault, - isNullable, - dataType, - characterMaximumLength, - numericPrecision, - numericPrecisionRadix, - numericScale, - datetimePrecision, - intervalType, - intervalPrecision, - characterSetCatalog, - characterSetSchema, - characterSetName, - collationCatalog, - collationSchema, - collationName, - domainName, - remarks); - } - - public static SvvColumnsRow create(ResultSet rs) throws SQLException { - return SvvColumnsRow.create( - getStringNotNull(rs, "table_catalog"), - getStringNotNull(rs, "table_schema"), - getStringNotNull(rs, "table_name"), - getStringNotNull(rs, "column_name"), - getIntNotNull(rs, "ordinal_position"), - getStringNotNull(rs, "column_default"), - getStringNotNull(rs, "is_nullable"), - getStringNotNull(rs, "data_type"), - getIntNotNull(rs, "character_maximum_length"), - getIntNotNull(rs, "numeric_precision"), - getIntNotNull(rs, "numeric_precision_radix"), - getIntNotNull(rs, "numeric_scale"), - getIntNotNull(rs, "datetime_precision"), - getStringNotNull(rs, "interval_type"), - getStringNotNull(rs, "interval_precision"), - getStringNotNull(rs, "character_set_catalog"), - getStringNotNull(rs, "character_set_schema"), - getStringNotNull(rs, "character_set_name"), - getStringNotNull(rs, "collation_catalog"), - getStringNotNull(rs, "collation_schema"), - getStringNotNull(rs, "collation_name"), - getStringNotNull(rs, "domain_name"), - getStringNotNull(rs, "remarks")); - } - - public static SvvColumnsRow create(String[] csvLine) { - return new AutoValue_SvvColumnsRow( - CsvUtil.getStringNotNull(csvLine[0]), - CsvUtil.getStringNotNull(csvLine[1]), - CsvUtil.getStringNotNull(csvLine[2]), - CsvUtil.getStringNotNull(csvLine[3]), - CsvUtil.getIntNotNull(csvLine[4]), - CsvUtil.getStringNotNull(csvLine[5]), - CsvUtil.getStringNotNull(csvLine[6]), - CsvUtil.getStringNotNull(csvLine[7]), - CsvUtil.getIntNotNull(csvLine[8]), - CsvUtil.getIntNotNull(csvLine[9]), - CsvUtil.getIntNotNull(csvLine[10]), - CsvUtil.getIntNotNull(csvLine[11]), - CsvUtil.getIntNotNull(csvLine[12]), - CsvUtil.getStringNotNull(csvLine[13]), - CsvUtil.getStringNotNull(csvLine[14]), - CsvUtil.getStringNotNull(csvLine[15]), - CsvUtil.getStringNotNull(csvLine[16]), - CsvUtil.getStringNotNull(csvLine[17]), - CsvUtil.getStringNotNull(csvLine[18]), - CsvUtil.getStringNotNull(csvLine[19]), - CsvUtil.getStringNotNull(csvLine[20]), - CsvUtil.getStringNotNull(csvLine[21]), - CsvUtil.getStringNotNull(csvLine[22])); - } - - public abstract String tableCatalog(); - - public abstract String tableSchema(); - - public abstract String tableName(); - - public abstract String columnName(); - - public abstract int ordinalPosition(); - - public abstract String columnDefault(); - - public abstract String isNullable(); - - public abstract String dataType(); - - public abstract int characterMaximumLength(); - - public abstract int numericPrecision(); - - public abstract int numericPrecisionRadix(); - - public abstract int numericScale(); - - public abstract int datetimePrecision(); - - public abstract String intervalType(); - - public abstract String intervalPrecision(); - - public abstract String characterSetCatalog(); - - public abstract String characterSetSchema(); - - public abstract String characterSetName(); - - public abstract String collationCatalog(); - - public abstract String collationSchema(); - - public abstract String collationName(); - - public abstract String domainName(); - - public abstract String remarks(); - - @Override - public String toString() { - return "tableCatalog=" - + tableCatalog() - + ", tableSchema=" - + tableSchema() - + ", tableName=" - + tableName() - + ", columnName=" - + columnName() - + ", ordinalPosition=" - + ordinalPosition() - + ", columnDefault=" - + columnDefault() - + ", isNullable=" - + isNullable() - + ", dataType=" - + dataType() - + ", characterMaximumLength=" - + characterMaximumLength() - + ", numericPrecision=" - + numericPrecision() - + ", numericPrecisionRadix=" - + numericPrecisionRadix() - + ", numericScale=" - + numericScale() - + ", datetimePrecision=" - + datetimePrecision() - + ", intervalType=" - + intervalType() - + ", intervalPrecision=" - + intervalPrecision() - + ", characterSetCatalog=" - + characterSetCatalog() - + ", characterSetSchema=" - + characterSetSchema() - + ", characterSetName=" - + characterSetName() - + ", collationCatalog=" - + collationCatalog() - + ", collationSchema=" - + collationSchema() - + ", collationName=" - + collationName() - + ", domainName=" - + domainName() - + ", remarks=" - + remarks() - + lineSeparator(); - } -} diff --git a/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/sql/SqlUtil.java b/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/sql/SqlUtil.java index 36b658f91..b758ebf91 100644 --- a/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/sql/SqlUtil.java +++ b/dumper-integration-tests/redshift-tests/src/main/java/com/google/edwmigration/dumper/sql/SqlUtil.java @@ -15,17 +15,10 @@ */ package com.google.edwmigration.dumper.sql; -import static com.google.edwmigration.dumper.base.TestConstants.URL_DB; -import static java.lang.String.format; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.io.Resources; import java.io.IOException; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,35 +37,4 @@ public static String getSql(String sqlPath) throws IOException { return Resources.toString(Resources.getResource(sqlPath), UTF_8); } - /** - * @param connection DB connection parameter - * @param queries List of strings each of the contains a parametrized SQL request - */ - public static void executeQueries(Connection connection, List queries) - throws SQLException { - for (String query : queries) { - try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { - preparedStatement.execute(); - } catch (SQLException e) { - LOGGER.error(format("Cannot execute query: %n%s%n", query)); - throw e; - } - } - } - - /** - * @param username DB username - * @param password DB password - * @param query A single string of a parametrized SQL request - */ - public static void connectAndExecuteQueryAsUser(String username, String password, String query) - throws SQLException { - Connection connection = DriverManager.getConnection(URL_DB, username, password); - try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { - preparedStatement.execute(); - } catch (SQLException e) { - LOGGER.error(format("Cannot execute query: %n%s%n", query)); - throw e; - } - } } diff --git a/dumper-integration-tests/redshift-tests/src/main/resources/sql/svv_tables.sql b/dumper-integration-tests/redshift-tests/src/main/resources/sql/svv_tables.sql new file mode 100644 index 000000000..d24a92bda --- /dev/null +++ b/dumper-integration-tests/redshift-tests/src/main/resources/sql/svv_tables.sql @@ -0,0 +1,15 @@ +-- Copyright 2022 Google LLC +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +SELECT * FROM SVV_TABLES; \ No newline at end of file diff --git a/dumper-integration-tests/redshift-tests/src/test/java/com/google/edwmigration/dumper/integration/SvvTablesTest.java b/dumper-integration-tests/redshift-tests/src/test/java/com/google/edwmigration/dumper/integration/SvvTablesTest.java new file mode 100644 index 000000000..4aa9cee43 --- /dev/null +++ b/dumper-integration-tests/redshift-tests/src/test/java/com/google/edwmigration/dumper/integration/SvvTablesTest.java @@ -0,0 +1,88 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.edwmigration.dumper.integration; + +import static com.google.common.collect.Iterables.getFirst; +import static com.google.common.collect.LinkedHashMultiset.create; +import static com.google.common.truth.Truth.assertThat; +import static com.google.edwmigration.dumper.base.TestBase.CSV_PARSER; +import static com.google.edwmigration.dumper.base.TestBase.assertDbCsvDataEqual; +import static com.google.edwmigration.dumper.base.TestConstants.EXPORTED_FILES_BASE_PATH; +import static com.google.edwmigration.dumper.base.TestConstants.PASSWORD_DB; +import static com.google.edwmigration.dumper.base.TestConstants.SQL_REQUESTS_BASE_PATH; +import static com.google.edwmigration.dumper.base.TestConstants.URL_DB; +import static com.google.edwmigration.dumper.base.TestConstants.USERNAME_DB; +import static com.google.edwmigration.dumper.jdbc.JdbcUtil.getDbColumnNames; +import static com.google.edwmigration.dumper.jdbc.JdbcUtil.getStringNotNull; +import static com.google.edwmigration.dumper.sql.SqlUtil.getSql; +import static java.sql.DriverManager.getConnection; + +import com.google.common.collect.LinkedHashMultiset; +import com.opencsv.CSVReaderHeaderAware; +import com.opencsv.CSVReaderHeaderAwareBuilder; +import com.opencsv.exceptions.CsvException; +import java.io.FileReader; +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; +import org.testng.annotations.Test; + +public class SvvTablesTest { + + private static final String SQL_PATH = SQL_REQUESTS_BASE_PATH + "svv_tables.sql"; + private static final String CSV_FILE_PATH = EXPORTED_FILES_BASE_PATH + "svv_tables.csv"; + + @Test + public void svvTablesTest() throws SQLException, IOException, CsvException { + LinkedHashMultiset> dbMultiset = create(); + LinkedHashMultiset> csvMultiset = create(); + LinkedHashMultiset dbColumnHeaders, csvColumnHeaders; + + try (Connection connection = getConnection(URL_DB, USERNAME_DB, PASSWORD_DB); + PreparedStatement preparedStatement = connection.prepareStatement(getSql(SQL_PATH))) { + ResultSet rs = preparedStatement.executeQuery(); + + dbColumnHeaders = create(getDbColumnNames(rs.getMetaData())); + + while (rs.next()) { + Map dbRow = new HashMap<>(); + for (String header : dbColumnHeaders) { + dbRow.put(header, getStringNotNull(rs, header)); + } + dbMultiset.add(dbRow); + } + } + + try (FileReader fileReader = new FileReader(CSV_FILE_PATH); + CSVReaderHeaderAware reader = + new CSVReaderHeaderAwareBuilder(fileReader).withCSVParser(CSV_PARSER).build()) { + Map csvRow; + while ((csvRow = reader.readMap()) != null) { + csvMultiset.add(csvRow); + } + } + + csvColumnHeaders = + create(getFirst(csvMultiset.elementSet(), new HashMap()).keySet()); + + assertThat(dbColumnHeaders).containsExactlyElementsIn(csvColumnHeaders); + assertDbCsvDataEqual(dbMultiset, csvMultiset); + } +} diff --git a/dumper-integration-tests/redshift-tests/src/test/java/com/google/integration/SvvColumnsTest.java b/dumper-integration-tests/redshift-tests/src/test/java/com/google/integration/SvvColumnsTest.java deleted file mode 100644 index ac5a39f17..000000000 --- a/dumper-integration-tests/redshift-tests/src/test/java/com/google/integration/SvvColumnsTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2022 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.integration; - -import static com.google.edwmigration.dumper.base.TestBase.assertListsEqual; -import static com.google.edwmigration.dumper.base.TestConstants.EXPORTED_FILES_BASE_PATH; -import static com.google.edwmigration.dumper.base.TestConstants.PASSWORD_DB; -import static com.google.edwmigration.dumper.base.TestConstants.SQL_REQUESTS_BASE_PATH; -import static com.google.edwmigration.dumper.base.TestConstants.URL_DB; -import static com.google.edwmigration.dumper.base.TestConstants.USERNAME_DB; - -import com.google.common.collect.LinkedHashMultiset; -import com.google.edwmigration.dumper.pojo.SvvColumnsRow; -import com.google.edwmigration.dumper.sql.SqlUtil; -import com.opencsv.CSVReader; -import com.opencsv.CSVReaderBuilder; -import com.opencsv.exceptions.CsvValidationException; -import java.io.FileReader; -import java.io.IOException; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -public class SvvColumnsTest { - - private static final String SQL_PATH = SQL_REQUESTS_BASE_PATH + "svv_columns.sql"; - private static final String CSV_FILE_PATH = EXPORTED_FILES_BASE_PATH + "svv_columns.csv"; - private static Connection connection; - - @BeforeClass - public static void beforeClass() throws SQLException { - connection = DriverManager.getConnection(URL_DB, USERNAME_DB, PASSWORD_DB); - } - - @Test - public void svvColumnsTest() throws SQLException, IOException, CsvValidationException { - LinkedHashMultiset dbList = LinkedHashMultiset.create(); - LinkedHashMultiset csvList = LinkedHashMultiset.create(); - - try (PreparedStatement preparedStatement = - connection.prepareStatement(SqlUtil.getSql(SQL_PATH))) { - ResultSet rs = preparedStatement.executeQuery(); - - while (rs.next()) { - dbList.add(SvvColumnsRow.create(rs)); - } - } - - FileReader fileReader = new FileReader(CSV_FILE_PATH); - try (CSVReader reader = new CSVReaderBuilder(fileReader).withSkipLines(1).build()) { - String[] line; - while ((line = reader.readNext()) != null) { - SvvColumnsRow csvRow = SvvColumnsRow.create(line); - csvList.add(csvRow); - } - } - - LinkedHashMultiset dbListCopy = LinkedHashMultiset.create(dbList); - csvList.forEach(dbList::remove); - dbListCopy.forEach(csvList::remove); - - assertListsEqual(dbList, csvList); - } -}