|
| 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 | +} |
0 commit comments