Skip to content

Commit aca8121

Browse files
e2e BQ test scenarios Validation step for Existing Table
1 parent 8866bd9 commit aca8121

6 files changed

Lines changed: 137 additions & 4 deletions

File tree

src/e2e-test/features/bigquery/source/BigQueryToBigQuery.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ Feature: BigQuery source - Verification of BigQuery to BigQuery successful data
301301
Then Wait till pipeline is in running state
302302
Then Open and capture logs
303303
Then Verify the pipeline status is "Succeeded"
304+
Then Validate the data transferred from BQ to BQ with actual And expected file for: "bqexpectedfile"
304305

305306
@BQ_EXISTING_SOURCE_TEST @BQ_SINK_TEST
306307
Scenario: Validate successful record transfer from BigQuery source(existing table) without clicking on the validate button of BigQuery source to BigQuery sink(new table)
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package io.cdap.plugin.bigquery.stepsdesign;
2+
3+
import com.esotericsoftware.minlog.Log;
4+
import com.google.cloud.bigquery.FieldValueList;
5+
import com.google.cloud.bigquery.TableResult;
6+
import com.google.gson.Gson;
7+
import com.google.gson.JsonElement;
8+
import com.google.gson.JsonObject;
9+
import io.cdap.e2e.utils.BigQueryClient;
10+
import io.cdap.e2e.utils.PluginPropertyUtils;
11+
import io.cucumber.core.logging.Logger;
12+
import io.cucumber.core.logging.LoggerFactory;
13+
14+
import java.io.BufferedReader;
15+
import java.io.FileReader;
16+
import java.io.IOException;
17+
import java.net.URISyntaxException;
18+
import java.nio.file.Path;
19+
import java.nio.file.Paths;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
/**
24+
* BigQuery Plugin Existing Table validation.
25+
*/
26+
public class BQValidationExistingTables {
27+
28+
private static final Logger LOG = LoggerFactory.getLogger(BQValidationExistingTables.class);
29+
static Gson gson = new Gson();
30+
public static boolean validateActualDataToExpectedData(String table, String fileName) throws IOException,
31+
InterruptedException, URISyntaxException {
32+
Map<String, JsonObject> bigQueryMap = new HashMap<>();
33+
Map<String, JsonObject> fileMap = new HashMap<>();
34+
Path importbqexpectedfile = Paths.get(BQValidationExistingTables.class.getResource("/" + fileName).toURI());
35+
36+
getBigQueryTableData(table, bigQueryMap);
37+
getFileData(importbqexpectedfile.toString(), fileMap);
38+
39+
boolean isMatched = matchJsonMaps(bigQueryMap, fileMap);
40+
41+
return isMatched;
42+
}
43+
44+
public static void getFileData(String fileName, Map<String, JsonObject> fileMap) {
45+
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
46+
String line;
47+
while ((line = br.readLine()) != null) {
48+
JsonObject json = gson.fromJson(line, JsonObject.class);
49+
String idKey = getIdKey(json);
50+
if (idKey != null) {
51+
JsonElement idElement = json.get(idKey);
52+
if (idElement.isJsonPrimitive()) {
53+
String idValue = idElement.getAsString();
54+
fileMap.put(idValue, json);
55+
}
56+
} else {
57+
Log.error("ID key not found");
58+
}
59+
}
60+
} catch (IOException e) {
61+
System.err.println("Error reading the file: " + e.getMessage());
62+
}
63+
}
64+
65+
private static void getBigQueryTableData(String targetTable, Map<String, JsonObject> bigQueryMap)
66+
throws IOException, InterruptedException {
67+
String dataset = PluginPropertyUtils.pluginProp("dataset");
68+
String projectId = PluginPropertyUtils.pluginProp("projectId");
69+
String selectQuery = "SELECT TO_JSON(t) FROM `" + projectId + "." + dataset + "." + targetTable + "` AS t";
70+
TableResult result = BigQueryClient.getQueryResult(selectQuery);
71+
72+
for (FieldValueList row : result.iterateAll()) {
73+
JsonObject json = gson.fromJson(row.get(0).getStringValue(), JsonObject.class);
74+
String idKey = getIdKey(json); // Get the actual ID key from the JSON object
75+
if (idKey != null) {
76+
JsonElement idElement = json.get(idKey);
77+
if (idElement.isJsonPrimitive()) {
78+
String id = idElement.getAsString();
79+
bigQueryMap.put(id, json);
80+
} else {
81+
Log.error("Data Mismatched");
82+
}
83+
}
84+
}
85+
}
86+
87+
/**
88+
* Retrieves the key for the ID element in the provided JSON object.
89+
*
90+
* @param json The JSON object to search for the ID key.
91+
*/
92+
private static String getIdKey(JsonObject json) {
93+
if (json.has("ID")) {
94+
return "ID";
95+
} else if (json.has("Name")) {
96+
return "Name";
97+
} else if (json.has("Price")) {
98+
return "Price";
99+
} else if (json.has("Customer_Exists")) {
100+
return "Customer_Exists";
101+
} else {
102+
return null;
103+
}
104+
}
105+
106+
private static boolean matchJsonMaps(Map<String, JsonObject> map1, Map<String, JsonObject> map2) {
107+
if (!map1.keySet().equals(map2.keySet())) {
108+
return false;
109+
}
110+
for (String key : map1.keySet()) {
111+
JsonObject json1 = map1.get(key);
112+
JsonObject json2 = map2.get(key);
113+
if (!json1.equals(json2)) {
114+
return false;
115+
}
116+
}
117+
return true;
118+
}
119+
}

src/e2e-test/java/io/cdap/plugin/bigquery/stepsdesign/BigQuerySource.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.Assert;
3333

3434
import java.io.IOException;
35+
import java.net.URISyntaxException;
3536
import java.text.SimpleDateFormat;
3637
import java.util.Date;
3738
import java.util.UUID;
@@ -160,4 +161,13 @@ public void enterTheBigQuerySourcePropertyForViewMaterializationDataset(String v
160161
public void enterBigQuerySourcePropertyTableNameAsView() {
161162
CdfBigQueryPropertiesActions.enterBigQueryTable(TestSetupHooks.bqSourceView);
162163
}
164+
165+
@Then("Validate the data transferred from BQ to BQ with actual And expected file for: {string}")
166+
public void validateTheDataFromBQToBQWithActualAndExpectedFileFor(String expectedFile) throws IOException,
167+
InterruptedException, URISyntaxException {
168+
boolean recordsMatched = BQValidationExistingTables.validateActualDataToExpectedData(
169+
PluginPropertyUtils.pluginProp("bqTargetTable"),
170+
PluginPropertyUtils.pluginProp(expectedFile));
171+
Assert.assertTrue("Value of records in actual and expected file is equal", recordsMatched);
172+
}
163173
}

src/e2e-test/java/io/cdap/plugin/common/stepsdesign/TestSetupHooks.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -925,11 +925,11 @@ public static void createSourceBQUpdateTable() throws IOException, InterruptedEx
925925
public static void createSourceBQExistingTable() throws IOException, InterruptedException {
926926
bqSourceTable = "E2E_SOURCE_" + UUID.randomUUID().toString().replaceAll("-" , "_");
927927
io.cdap.e2e.utils.BigQueryClient.getSoleQueryResult("create table `" + datasetName + "." + bqSourceTable + "` " +
928-
"(CustomerID INT64, Name STRING, " + "Price FLOAT64," +
928+
"(ID INT64, Name STRING, " + "Price FLOAT64," +
929929
"Customer_Exists BOOL ) ");
930930
try {
931931
io.cdap.e2e.utils.BigQueryClient.getSoleQueryResult("INSERT INTO `" + datasetName + "." + bqSourceTable + "` " +
932-
"(CustomerID, Name, Price, Customer_Exists)" +
932+
"(ID, Name, Price, Customer_Exists)" +
933933
"VALUES" + "(1, 'Raja Sharma', 200.0, true)");
934934

935935
} catch (NoSuchElementException e) {
@@ -946,12 +946,12 @@ public static void createSinkBQExistingTable() throws IOException, InterruptedEx
946946
PluginPropertyUtils.addPluginProp("bqTargetTable", bqTargetTable);
947947
BeforeActions.scenario.write("BQ Target table name - " + bqTargetTable);
948948
io.cdap.e2e.utils.BigQueryClient.getSoleQueryResult("create table `" + datasetName + "." + bqTargetTable + "` " +
949-
"(CustomerID INT64,Name STRING," +
949+
"(ID INT64,Name STRING," +
950950
"Price FLOAT64, Customer_Exists BOOL ) ");
951951

952952
try {
953953
io.cdap.e2e.utils.BigQueryClient.getSoleQueryResult("INSERT INTO `" + datasetName + "." + bqTargetTable + "` " +
954-
"(CustomerID, Name, Price, Customer_Exists)" +
954+
"(ID, Name, Price, Customer_Exists)" +
955955
"VALUES" + "(3, 'Rajan Kumar', 100.0, true)");
956956

957957
} catch (NoSuchElementException e) {

src/e2e-test/resources/pluginParameters.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ bqInvalidRefName=invalidRef&^*&&*
186186
bqDatatypeChange1=[{"key":"Id","value":"long"},{"key":"Value","value":"long"}]
187187
bqDataTypeTestFileSchema1=[{"key":"Id","value":"long"},{"key":"Value","value":"long"},\
188188
{"key":"UID","value":"string"}]
189+
bqexpectedfile=testdata/BigQuery/BQExistingTableFile
189190
## BIGQUERY-PLUGIN-PROPERTIES-END
190191

191192
## PUBSUBSINK-PLUGIN-PROPERTIES-START
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"Customer_Exists":true,"ID":1,"Name":"Raja Sharma","Price":200.0}
2+
{"Customer_Exists":true,"ID":3,"Name":"Rajan Kumar","Price":100.0}

0 commit comments

Comments
 (0)