diff --git a/compare_two_avro_files/pom.xml b/compare_two_avro_files/pom.xml new file mode 100644 index 00000000..cfa8f392 --- /dev/null +++ b/compare_two_avro_files/pom.xml @@ -0,0 +1,123 @@ + + + 4.0.0 + com.testsigma.addons + _compare_two_avro_files + 1.0.0 + jar + + + UTF-8 + 11 + 11 + 1.2.9_cloud + 5.8.0-M1 + 1.0.0 + 3.2.1 + 1.18.20 + + + + + + com.testsigma + testsigma-java-sdk + ${testsigma.sdk.version} + + + org.projectlombok + lombok + ${lombok.version} + true + + + org.junit.jupiter + junit-jupiter-api + ${junit.jupiter.version} + test + + + org.testng + testng + 6.14.3 + + + + org.seleniumhq.selenium + selenium-java + 4.14.1 + + + + io.appium + java-client + 9.0.0 + + + com.fasterxml.jackson.core + jackson-annotations + 2.13.0 + + + commons-io + commons-io + 2.8.0 + + + + org.apache.avro + avro + 1.10.2 + + + org.apache.camel.springboot + camel-avro-starter + 4.4.0 + + + com.opencsv + opencsv + 5.6 + + + org.apache.commons + commons-lang3 + 3.14.0 + + + + + _compare_two_avro_files + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven.source.plugin.version} + + + attach-sources + + jar + + + + + + + diff --git a/compare_two_avro_files/src/main/java/com/testsigma/addons/web/CompareAvroFiles.java b/compare_two_avro_files/src/main/java/com/testsigma/addons/web/CompareAvroFiles.java new file mode 100644 index 00000000..f38e770b --- /dev/null +++ b/compare_two_avro_files/src/main/java/com/testsigma/addons/web/CompareAvroFiles.java @@ -0,0 +1,134 @@ +package com.testsigma.addons.web; + +import com.opencsv.CSVWriter; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.avro.Schema; +import org.apache.avro.file.DataFileStream; +import org.apache.avro.generic.GenericDatumReader; +import org.apache.avro.generic.GenericRecord; +import org.apache.avro.util.Utf8; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.io.*; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +@Data +@Action(actionText = "Verify that the data in avro file1 test-data1 and avro file2 test-data2 is equal", + description = "Verifies that the two avro files data is same", + applicationType = ApplicationType.WEB, + useCustomScreenshot = false) +public class CompareAvroFiles extends WebAction { + + @TestData(reference = "test-data1") + private com.testsigma.sdk.TestData file1; + + @TestData(reference = "test-data2") + private com.testsigma.sdk.TestData file2; + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + try { + File avroFile1 = urlToFileConverter("avrofile1.avro", file1.getValue().toString()); + File avroFile2 = urlToFileConverter("avrofile2.avro", file2.getValue().toString()); + + File csvFile1 = File.createTempFile("csvfile1",".csv"); + File csvFile2 = File.createTempFile("csvfile2",".csv"); + + logger.info("Converting avrofile1 to csv"); + convertAvroToCsv(avroFile1, csvFile1); + logger.info("Converted"); + + logger.info("Converting avrofile2 to csv"); + convertAvroToCsv(avroFile2, csvFile2); + logger.info("Converted"); + + List file1Data = readCSV(csvFile1); + List file2Data = readCSV(csvFile2); + + boolean comparisonResult = compareCSV(file1Data, file2Data); + + if (comparisonResult) { + setSuccessMessage("Successfully verified that both Avro files data is same"); + } else { + setErrorMessage("Both avro files data is not same"); + result = Result.FAILED; + } + + } catch (IOException e) { + logger.info("Exception occurred: " + ExceptionUtils.getStackTrace(e)); + setErrorMessage("Unable to compare both the avro files"); + result = Result.FAILED; + } + return result; + } + + private File urlToFileConverter(String fileName, String s3url) throws IOException { + logger.info("File name:" + fileName); + URL urlObject = new URL(s3url); + File tempFile = File.createTempFile(fileName.split("\\.")[0], "." + fileName.split("\\.")[1]); + FileUtils.copyURLToFile(urlObject,tempFile); + logger.info("Temp file created with name" + tempFile.getName() + " at path " + tempFile.getAbsolutePath()); + return tempFile; + } + + private void convertAvroToCsv(File avroFilePath, File csvFilePath) throws IOException { + try (DataFileStream dataFileReader = new DataFileStream<>(new FileInputStream(avroFilePath), new GenericDatumReader<>())) { + try (CSVWriter csvWriter = new CSVWriter(new FileWriter(csvFilePath))) { + + Schema schema = dataFileReader.getSchema(); + String[] header = new String[schema.getFields().size()]; + int i = 0; + for (Schema.Field field : schema.getFields()) { + header[i++] = field.name(); + } + csvWriter.writeNext(header); + + for (GenericRecord record : dataFileReader) { + String[] row = new String[header.length]; + for (i = 0; i < header.length; i++) { + Object value = record.get(i); + row[i] = value instanceof Utf8 ? value.toString() : String.valueOf(value); + } + csvWriter.writeNext(row); + } + } + } + } + + private List readCSV(File csvFile) throws IOException { + logger.info("Reading the csv file"); + List data = new ArrayList<>(); + BufferedReader reader = new BufferedReader(new FileReader(csvFile)); + String line; + while ((line = reader.readLine()) != null) { + String[] row = line.split(","); + data.add(row); + } + reader.close(); + logger.info("Reading completed"); + return data; + } + private boolean compareCSV(List data1, List data2) { + logger.info("Comparison started.."); + int rows = Math.min(data1.size(), data2.size()); + int columns = Math.min(data1.get(0).length, data2.get(0).length); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < columns; j++) { + if (!data1.get(i)[j].equals(data2.get(i)[j])) { + return false; + } + } + } + return true; + } +} \ No newline at end of file