Skip to content

Commit 62e6a10

Browse files
new scenarios e2e bq
1 parent dc01190 commit 62e6a10

File tree

16 files changed

+1143
-2
lines changed

16 files changed

+1143
-2
lines changed

src/e2e-test/features/bigquery/sink/BigQueryToBigQueryAdditional.feature

Lines changed: 502 additions & 0 deletions
Large diffs are not rendered by default.

src/e2e-test/java/io/cdap/plugin/bigquery/runners/sinkrunner/TestRunner.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
features = {"src/e2e-test/features"},
2828
glue = {"io.cdap.plugin.bigquery.stepsdesign", "io.cdap.plugin.gcs.stepsdesign",
2929
"stepsdesign", "io.cdap.plugin.common.stepsdesign"},
30-
tags = {"@BigQuery_Sink"},
30+
tags = {"@BigQuery_Sink and not @CDAP-20830"},
31+
//TODO: Enable test once issue is fixed https://cdap.atlassian.net/browse/CDAP-20830
3132
monochrome = true,
3233
plugin = {"pretty", "html:target/cucumber-html-report/bigquery-sink",
3334
"json:target/cucumber-reports/cucumber-bigquery-sink.json",
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
private static final Gson gson = new Gson();
30+
31+
/**
32+
* Validates the actual data in a BigQuery table against the expected data in a JSON file.
33+
* @param table The name of the BigQuery table to retrieve data from.
34+
* @param fileName The name of the JSON file containing the expected data.
35+
* @return True if the actual data matches the expected data, false otherwise.
36+
*/
37+
public static boolean validateActualDataToExpectedData(String table, String fileName) throws IOException,
38+
InterruptedException, URISyntaxException {
39+
Map<String, JsonObject> bigQueryMap = new HashMap<>();
40+
Map<String, JsonObject> fileMap = new HashMap<>();
41+
Path bqExpectedFilePath = Paths.get(BQValidationExistingTables.class.getResource("/" + fileName).toURI());
42+
43+
getBigQueryTableData(table, bigQueryMap);
44+
getFileData(bqExpectedFilePath.toString(), fileMap);
45+
boolean isMatched = bigQueryMap.equals(fileMap);
46+
return isMatched;
47+
}
48+
49+
/**
50+
* Reads a JSON file line by line and populates a map with JSON objects using a specified ID key.
51+
*@param fileName The path to the JSON file to be read.
52+
* @param fileMap A map where the extracted JSON objects will be stored with their ID values as keys.
53+
*/
54+
55+
public static void getFileData(String fileName, Map<String, JsonObject> fileMap) {
56+
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
57+
String line;
58+
while ((line = br.readLine()) != null) {
59+
JsonObject json = gson.fromJson(line, JsonObject.class);
60+
String idKey = getIdKey(json);
61+
if (idKey != null) {
62+
JsonElement idElement = json.get(idKey);
63+
if (idElement.isJsonPrimitive()) {
64+
String idValue = idElement.getAsString();
65+
fileMap.put(idValue, json);
66+
}
67+
} else {
68+
Log.error("ID key not found");
69+
}
70+
}
71+
} catch (IOException e) {
72+
Log.error("Error reading the file: " + e.getMessage());
73+
}
74+
}
75+
76+
private static void getBigQueryTableData(String targetTable, Map<String, JsonObject> bigQueryMap)
77+
throws IOException, InterruptedException {
78+
String dataset = PluginPropertyUtils.pluginProp("dataset");
79+
String projectId = PluginPropertyUtils.pluginProp("projectId");
80+
String selectQuery = "SELECT TO_JSON(t) FROM `" + projectId + "." + dataset + "." + targetTable + "` AS t";
81+
TableResult result = BigQueryClient.getQueryResult(selectQuery);
82+
83+
for (FieldValueList row : result.iterateAll()) {
84+
JsonObject json = gson.fromJson(row.get(0).getStringValue(), JsonObject.class);
85+
String idKey = getIdKey(json); // Get the actual ID key from the JSON object
86+
if (idKey != null) {
87+
JsonElement idElement = json.get(idKey);
88+
if (idElement.isJsonPrimitive()) {
89+
String id = idElement.getAsString();
90+
bigQueryMap.put(id, json);
91+
} else {
92+
Log.error("Data Mismatched");
93+
}
94+
}
95+
}
96+
}
97+
98+
/**
99+
* Retrieves the key for the ID element in the provided JSON object.
100+
*
101+
* @param json The JSON object to search for the ID key.
102+
*/
103+
private static String getIdKey(JsonObject json) {
104+
if (json.has("ID")) {
105+
return "ID";
106+
} else if (json.has("Name")) {
107+
return "Name";
108+
} else if (json.has("Price")) {
109+
return "Price";
110+
} else if (json.has("Customer_Exists")) {
111+
return "Customer_Exists";
112+
} else {
113+
return null;
114+
}
115+
}
116+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import stepsdesign.BeforeActions;
2626

2727
import java.io.IOException;
28+
import java.net.URISyntaxException;
2829

2930
/**
3031
* BigQuery Plugin validation common step design.
@@ -44,4 +45,13 @@ public void validateTheValuesOfRecordsTransferredToBQsinkIsEqualToTheValuesFromS
4445
Assert.assertTrue("Value of records transferred to the BQ sink should be equal to the value " +
4546
"of the records in the source table", recordsMatched);
4647
}
48+
49+
@Then("Validate the data transferred from BigQuery to BigQuery with actual And expected file for: {string}")
50+
public void validateTheDataFromBQToBQWithActualAndExpectedFileFor(String expectedFile) throws IOException,
51+
InterruptedException, URISyntaxException {
52+
boolean recordsMatched = BQValidationExistingTables.validateActualDataToExpectedData(
53+
PluginPropertyUtils.pluginProp("bqTargetTable"),
54+
PluginPropertyUtils.pluginProp(expectedFile));
55+
Assert.assertTrue("Value of records in actual and expected file is equal", recordsMatched);
56+
}
4757
}

0 commit comments

Comments
 (0)