2828import java .math .BigDecimal ;
2929import java .math .RoundingMode ;
3030import java .sql .*;
31+ import java .text .SimpleDateFormat ;
3132import java .time .*;
3233import java .util .Date ;
3334import java .text .ParseException ;
4041public class BQValidation {
4142 static List <JsonObject > BigQueryResponse = new ArrayList <>();
4243 static List <Object > bigQueryRows = new ArrayList <>();
44+ static JsonObject json ;
4345
4446 /**
4547 * Extracts entire data from source and target tables.
46- *
4748 * @param sourceTable table at the source side
4849 * @param targetTable table at the sink side
4950 * @return true if the values in source and target side are equal
5051 */
5152 public static boolean validateDBToBQRecordValues (String schema , String sourceTable , String targetTable )
52- throws SQLException , ClassNotFoundException , IOException , InterruptedException {
53+ throws SQLException , ClassNotFoundException , IOException , InterruptedException , ParseException {
5354 getBigQueryTableData (targetTable , bigQueryRows );
5455 for (Object rows : bigQueryRows ) {
55- JsonObject json = new Gson ().fromJson (String .valueOf (rows ), JsonObject .class );
56+ json = new Gson ().fromJson (String .valueOf (rows ), JsonObject .class );
5657 BigQueryResponse .add (json );
5758 }
5859 String getSourceQuery = "SELECT * FROM " + schema + "." + sourceTable ;
59- try (Connection connect = CloudSqlPostgreSqlClient .getCloudSqlConnection ()) {
60- connect .setHoldability (ResultSet .HOLD_CURSORS_OVER_COMMIT );
61- Statement statement1 = connect .createStatement (ResultSet .TYPE_SCROLL_SENSITIVE , ResultSet .CONCUR_UPDATABLE ,
60+ try (Connection connection = CloudSqlPostgreSqlClient .getCloudSqlConnection ()) {
61+ connection .setHoldability (ResultSet .HOLD_CURSORS_OVER_COMMIT );
62+ Statement statement1 = connection .createStatement (ResultSet .TYPE_SCROLL_SENSITIVE , ResultSet .CONCUR_UPDATABLE ,
6263 ResultSet .HOLD_CURSORS_OVER_COMMIT );
6364
6465 ResultSet rsSource = statement1 .executeQuery (getSourceQuery );
6566 return compareResultSetandJsonData (rsSource , BigQueryResponse );
6667 }
6768 }
69+
6870 public static boolean validateBQToDBRecordValues (String schema , String sourceTable , String targetTable )
69- throws SQLException , ClassNotFoundException , IOException , InterruptedException {
71+ throws SQLException , ClassNotFoundException , IOException , InterruptedException , ParseException {
7072 getBigQueryTableData (sourceTable , bigQueryRows );
7173 for (Object rows : bigQueryRows ) {
72- JsonObject json = new Gson ().fromJson (String .valueOf (rows ), JsonObject .class );
74+ json = new Gson ().fromJson (String .valueOf (rows ), JsonObject .class );
7375 BigQueryResponse .add (json );
7476 }
7577 String getTargetQuery = "SELECT * FROM " + schema + "." + targetTable ;
76- try (Connection connect = CloudSqlPostgreSqlClient .getCloudSqlConnection ()) {
77- connect .setHoldability (ResultSet .HOLD_CURSORS_OVER_COMMIT );
78- Statement statement1 = connect .createStatement (ResultSet .TYPE_SCROLL_SENSITIVE , ResultSet .CONCUR_UPDATABLE ,
78+ try (Connection connection = CloudSqlPostgreSqlClient .getCloudSqlConnection ()) {
79+ connection .setHoldability (ResultSet .HOLD_CURSORS_OVER_COMMIT );
80+ Statement statement1 = connection .createStatement (ResultSet .TYPE_SCROLL_SENSITIVE , ResultSet .CONCUR_UPDATABLE ,
7981 ResultSet .HOLD_CURSORS_OVER_COMMIT );
8082
8183 ResultSet rsTarget = statement1 .executeQuery (getTargetQuery );
8284 return compareResultSetandJsonData (rsTarget , BigQueryResponse );
8385 }
8486 }
87+
8588 /**
8689 * Retrieves the data from a specified BigQuery table and populates it into the provided list of objects.
87- *
8890 * @param table The name of the BigQuery table to fetch data from.
8991 * @param bigQueryRows The list to store the fetched BigQuery data.
9092 */
@@ -100,15 +102,14 @@ private static void getBigQueryTableData(String table, List<Object> bigQueryRows
100102
101103 /**
102104 * Compares the data in the result set obtained from the Oracle database with the provided BigQuery JSON objects.
103- *
104105 * @param rsSource The result set obtained from the Oracle database.
105106 * @param bigQueryData The list of BigQuery JSON objects to compare with the result set data.
106107 * @return True if the result set data matches the BigQuery data, false otherwise.
107108 * @throws SQLException If an SQL error occurs during the result set operations.
108109 * @throws ParseException If an error occurs while parsing the data.
109110 */
110111 public static boolean compareResultSetandJsonData (ResultSet rsSource , List <JsonObject > bigQueryData )
111- throws SQLException {
112+ throws SQLException , ParseException {
112113 ResultSetMetaData mdSource = rsSource .getMetaData ();
113114 boolean result = false ;
114115 int columnCountSource = mdSource .getColumnCount ();
@@ -161,6 +162,11 @@ public static boolean compareResultSetandJsonData(ResultSet rsSource, List<JsonO
161162 break ;
162163
163164 case Types .TIMESTAMP :
165+ Timestamp sourceTS = rsSource .getTimestamp (columnName );
166+ SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'" );
167+ Date parsedDate = dateFormat .parse (bigQueryData .get (jsonObjectIdx ).get (columnName ).getAsString ());
168+ Timestamp targetTs = new Timestamp (parsedDate .getTime ());
169+ Assert .assertEquals (sourceTS , targetTs );
164170 break ;
165171
166172 case Types .TIME :
0 commit comments