Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat[CR-1175]: Need to Compare the data of .Avro file to .Avro file automatically #53

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions compare_two_avro_files/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testsigma.addons</groupId>
<artifactId>_compare_two_avro_files</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<testsigma.sdk.version>1.2.9_cloud</testsigma.sdk.version>
<junit.jupiter.version>5.8.0-M1</junit.jupiter.version>
<testsigma.addon.maven.plugin>1.0.0</testsigma.addon.maven.plugin>
<maven.source.plugin.version>3.2.1</maven.source.plugin.version>
<lombok.version>1.18.20</lombok.version>

</properties>

<dependencies>
<dependency>
<groupId>com.testsigma</groupId>
<artifactId>testsigma-java-sdk</artifactId>
<version>${testsigma.sdk.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.14.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>9.0.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>

<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.10.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-avro-starter</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>

</dependencies>
<build>
<finalName>_compare_two_avro_files</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven.source.plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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<String[]> file1Data = readCSV(csvFile1);
List<String[]> 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<GenericRecord> 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<String[]> readCSV(File csvFile) throws IOException {
logger.info("Reading the csv file");
List<String[]> 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<String[]> data1, List<String[]> 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;
}
}