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