-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathValidationHelperSqlEngine.java
More file actions
112 lines (101 loc) · 4.21 KB
/
ValidationHelperSqlEngine.java
File metadata and controls
112 lines (101 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright © 2024 Cask Data, Inc.
*
* 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 io.cdap.plugin.bigquery.stepsdesign;
import com.esotericsoftware.minlog.Log;
import com.google.cloud.bigquery.FieldValueList;
import com.google.cloud.bigquery.TableResult;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.cdap.e2e.utils.BigQueryClient;
import io.cdap.e2e.utils.PluginPropertyUtils;
import io.cucumber.core.logging.Logger;
import io.cucumber.core.logging.LoggerFactory;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
/**
* Validation Helper.
*/
public class ValidationHelperSqlEngine {
private static final Logger LOG = LoggerFactory.getLogger(ValidationHelperSqlEngine.class);
static Gson gson = new Gson();
/**
* Validates the actual data from a BigQuery table against the expected data from a file.
*
* @param table The name of the BigQuery table to fetch data from
* @param fileName The name of the file containing the expected data
* @return True if the actual data matches the expected data, otherwise false
*/
public static boolean validateActualDataToExpectedData(String table, String fileName) throws IOException,
InterruptedException, URISyntaxException {
// Initialize maps to store data from BigQuery and file
Map<String, JsonObject> bigQueryMap = new HashMap<>();
Map<String, JsonObject> fileMap = new HashMap<>();
// Get the path of the expected file
Path importExpectedFile = Paths.get(ValidationHelperSqlEngine.class.getResource("/" + fileName).toURI());
getBigQueryTableData(table, bigQueryMap);
getFileData(importExpectedFile.toString(), fileMap);
// Compare the data from BigQuery with the data from the file
boolean isMatched = bigQueryMap.equals(fileMap);
return isMatched;
}
public static void getFileData(String fileName, Map<String, JsonObject> fileMap) {
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
JsonObject json = gson.fromJson(line, JsonObject.class);
if (json.has("id")) { // Check if the JSON object has the "id" key
JsonElement idElement = json.get("id");
if (idElement.isJsonPrimitive()) {
String idKey = idElement.getAsString();
fileMap.put(idKey, json);
} else {
Log.error("ID key not found");
}
}
}
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
}
}
private static void getBigQueryTableData(String targetTable, Map<String, JsonObject> bigQueryMap)
throws IOException, InterruptedException {
String dataset = PluginPropertyUtils.pluginProp("dataset");
String projectId = PluginPropertyUtils.pluginProp("projectId");
String selectQuery = "SELECT TO_JSON(t) FROM `" + projectId + "." + dataset + "." + targetTable + "` AS t";
TableResult result = BigQueryClient.getQueryResult(selectQuery);
for (FieldValueList row : result.iterateAll()) {
JsonObject json = gson.fromJson(row.get(0).getStringValue(), JsonObject.class);
if (json.has("id")) { // Check if the JSON object has the "id" key
JsonElement idElement = json.get("id");
if (idElement.isJsonPrimitive()) {
String idKey = idElement.getAsString();
bigQueryMap.put(idKey, json);
} else {
LOG.error("Data Mismatched");
}
} else {
LOG.error("ID Key not found in JSON object");
}
}
}
}