1010import io .cdap .plugin .common .stepsdesign .TestSetupHooks ;
1111import org .junit .Assert ;
1212import org .junit .Test ;
13+
1314import java .io .IOException ;
1415import java .sql .*;
1516import java .sql .Date ;
2021import java .util .*;
2122
2223/**
23- * BQValidation
24+ * BQValidation
2425 */
2526
2627public class BQValidation {
2728 public static void main (String [] args ) throws SQLException , ParseException , IOException , ClassNotFoundException , InterruptedException {
28- //TestSetupHooks.createTables();
29- // TestSetupHooks.createTempSourceBQTable();
30- // validateBQAndDBRecordValues("SourceTable_qGMpakGOTZ", "mySqlToBQTable");
31- // validateDBAndBQRecordValues("SourceTable_qGMpakGOTZ", "mySqlToBQTable");
32- // validateDBAndBQRecordValues("SourceTable_qGMpakGOTZ", "mySqlToBQTable");
33- validateDBAndBQRecordValues ("E2E_SOURCE_4b406" ,"TargetTable_TRFSpppjsZ" );
29+
30+ // validateDBToBQRecordValues("SourceTable_qGMpakGOTZ", "mySqlToBQTable");
31+ validateBQToDBRecordValues ("E2E_SOURCE_f9dc4" , "TargetTable_TRFSpppjsZ" );
3432 }
3533
3634 /**
3735 * Extracts entire data from source and target tables.
36+ *
3837 * @param sourceTable table at the source side
3938 * @param targetTable table at the sink side
4039 * @return true if the values in source and target side are equal
4140 */
4241
43- public static boolean validateBQAndDBRecordValues (String sourceTable , String targetTable )
42+ public static boolean validateDBToBQRecordValues (String sourceTable , String targetTable )
4443 throws SQLException , ClassNotFoundException , ParseException , IOException , InterruptedException {
4544 List <JsonObject > jsonResponse = new ArrayList <>();
4645 List <Object > bigQueryRows = new ArrayList <>();
@@ -49,7 +48,7 @@ public static boolean validateBQAndDBRecordValues(String sourceTable, String tar
4948 JsonObject json = new Gson ().fromJson (String .valueOf (rows ), JsonObject .class );
5049 jsonResponse .add (json );
5150 }
52- String getSourceQuery = "SELECT * FROM " + sourceTable ;
51+ String getSourceQuery = "SELECT * FROM " + sourceTable ;
5352 try (Connection connect = CloudMySqlClient .getCloudMysqlConnection ()) {
5453 connect .setHoldability (ResultSet .HOLD_CURSORS_OVER_COMMIT );
5554 Statement statement1 = connect .createStatement (ResultSet .TYPE_SCROLL_SENSITIVE , ResultSet .CONCUR_UPDATABLE ,
@@ -59,7 +58,7 @@ public static boolean validateBQAndDBRecordValues(String sourceTable, String tar
5958 }
6059 }
6160
62- public static boolean validateDBAndBQRecordValues (String sourceTable , String targetTable )
61+ public static boolean validateBQToDBRecordValues (String sourceTable , String targetTable )
6362 throws SQLException , ClassNotFoundException , ParseException , IOException , InterruptedException {
6463 List <JsonObject > jsonResponse = new ArrayList <>();
6564 List <Object > bigQueryRows = new ArrayList <>();
@@ -83,16 +82,11 @@ public static boolean validateDBAndBQRecordValues(String sourceTable, String tar
8382 }
8483
8584
86-
87-
88-
89-
9085 /**
9186 * Retrieves the data from a specified BigQuery table and populates it into the provided list of objects.
9287 *
93- * @param table The name of the BigQuery table to fetch data from.
94- * @param bigQueryRows The list to store the fetched BigQuery data.
95- *
88+ * @param table The name of the BigQuery table to fetch data from.
89+ * @param bigQueryRows The list to store the fetched BigQuery data.
9690 */
9791
9892 private static void getBigQueryTableData (String table , List <Object > bigQueryRows )
@@ -102,15 +96,14 @@ private static void getBigQueryTableData(String table, List<Object> bigQueryRows
10296 String dataset = PluginPropertyUtils .pluginProp ("dataset" );
10397 String selectQuery = "SELECT TO_JSON(t) FROM `" + projectId + "." + dataset + "." + table + "` AS t" ;
10498 TableResult result = BigQueryClient .getQueryResult (selectQuery );
105- result .iterateAll ().forEach (value -> bigQueryRows .add (value .get (0 ).getValue ()));
99+ result .iterateAll ().forEach (value -> bigQueryRows .add (value .get (0 ).getValue ()));
106100 }
107101
108102 /**
109103 * Compares the data in the result set obtained from the Oracle database with the provided BigQuery JSON objects.
110104 *
111- * @param rsSource The result set obtained from the Oracle database.
112- * @param bigQueryData The list of BigQuery JSON objects to compare with the result set data.
113- *
105+ * @param rsSource The result set obtained from the Oracle database.
106+ * @param bigQueryData The list of BigQuery JSON objects to compare with the result set data.
114107 * @return True if the result set data matches the BigQuery data, false otherwise.
115108 * @throws SQLException If an SQL error occurs during the result set operations.
116109 * @throws ParseException If an error occurs while parsing the data.
@@ -139,7 +132,7 @@ public static boolean compareResultSetData(ResultSet rsSource, List<JsonObject>
139132 //Variable 'jsonObjectIdx' to track the index of the current JsonObject in the bigQueryData list,
140133 int jsonObjectIdx = 0 ;
141134 while (rsSource .next ()) {
142- int currentColumnCount = 2 ;
135+ int currentColumnCount = 3 ;
143136 while (currentColumnCount <= columnCountSource ) {
144137 String columnTypeName = mdSource .getColumnTypeName (currentColumnCount );
145138 int columnType = mdSource .getColumnType (currentColumnCount );
@@ -162,7 +155,7 @@ public static boolean compareResultSetData(ResultSet rsSource, List<JsonObject>
162155 Assert .assertTrue ("Different values found for column : %s" ,
163156 String .valueOf (sourceTinyInt ).equals (String .valueOf (targetTinyInt )));
164157 break ;
165-
158+ case Types . BIGINT :
166159 case Types .REAL :
167160 Float sourceFloat = rsSource .getFloat (currentColumnCount );
168161 Float targetFloat = Float .parseFloat (bigQueryData .get (jsonObjectIdx ).get (columnName ).getAsString ());
@@ -171,7 +164,7 @@ public static boolean compareResultSetData(ResultSet rsSource, List<JsonObject>
171164 break ;
172165
173166 case Types .DOUBLE :
174- Double sourceDouble = rsSource .getDouble (currentColumnCount );
167+ Double sourceDouble = rsSource .getDouble (currentColumnCount );
175168 Double targetDouble = Double .parseDouble (bigQueryData .get (jsonObjectIdx ).get (columnName ).getAsString ());
176169 Assert .assertTrue ("Different values found for column : %s" ,
177170 String .valueOf (sourceDouble ).equals (String .valueOf (targetDouble )));
@@ -185,7 +178,7 @@ public static boolean compareResultSetData(ResultSet rsSource, List<JsonObject>
185178 break ;
186179
187180 case Types .TIME :
188- Time sourceTime = rsSource .getTime (currentColumnCount );
181+ Time sourceTime = rsSource .getTime (currentColumnCount );
189182 Time targetTime = Time .valueOf (bigQueryData .get (jsonObjectIdx ).get (columnName ).getAsString ());
190183 Assert .assertTrue ("Different values found for column : %s" ,
191184 String .valueOf (sourceTime ).equals (String .valueOf (targetTime )));
@@ -215,12 +208,12 @@ public static boolean compareResultSetData(ResultSet rsSource, List<JsonObject>
215208 break ;
216209
217210 case Types .TIMESTAMP :
218- String sourceTS = String .valueOf (rsSource .getTimestamp (currentColumnCount ));
219- String targetTS =bigQueryData .get (jsonObjectIdx ).get (columnName ).getAsString ();
220- LocalDateTime timestamp = LocalDateTime .parse (targetTS , DateTimeFormatter .ISO_DATE_TIME );
221- DateTimeFormatter formatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss.S" );
222- String formattedTimestamp = timestamp .format (formatter );
223- Assert .assertEquals (sourceTS , formattedTimestamp );
211+ // String sourceTS= String.valueOf(rsSource.getTimestamp(currentColumnCount));
212+ // String targetTS=bigQueryData.get(jsonObjectIdx).get(columnName).getAsString();
213+ // LocalDateTime timestamp = LocalDateTime.parse(targetTS, DateTimeFormatter.ISO_DATE_TIME);
214+ // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
215+ // String formattedTimestamp = timestamp.format(formatter);
216+ // Assert.assertEquals(sourceTS, formattedTimestamp);
224217 break ;
225218
226219 default :
@@ -261,6 +254,7 @@ private static void getTimestampValidation(ResultSet rsSource, String sourceTime
261254 Timestamp targetTs = new Timestamp (parsedDate .getTime ());
262255 Assert .assertEquals (sourceTS , targetTs );
263256 }
257+
264258 private static String getTimeZoneIdFromSource (ResultSet rsTimezone ) throws SQLException {
265259 if (rsTimezone .next ()) {
266260 return rsTimezone .getString ("timezone" );
0 commit comments