Skip to content

Commit

Permalink
added utility methods to compare 2 Yaml files and also 2 Json files
Browse files Browse the repository at this point in the history
  • Loading branch information
anandbagmar committed Nov 21, 2024
1 parent 3b02058 commit 6f4f677
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 2 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
jadbVersion = "1.2.1"
unirestVersion = "3.14.5"
specmaticVersion = '1.3.39'
webDriverManager = '5.9.2'
webDriverManagerVersion = '5.9.2'
jodaTimeVersion = "2.13.0"
masterThoughtVersion = "5.8.4"
commonsLang3Version = "3.17.0"
Expand All @@ -21,6 +21,7 @@ buildscript {
assertJVersion = "3.26.3"
jetbrainsAnnotationsVersion = "26.0.1"
junitVersion = "5.11.3"
snakeyamlVersion = "2.3"
}
}

Expand Down Expand Up @@ -92,7 +93,8 @@ dependencies {
implementation "net.masterthought:cucumber-reporting:$masterThoughtVersion"
implementation "org.aspectj:aspectjrt:$project.aspectJVersion"
implementation "in.specmatic:specmatic-core:${project.specmaticVersion}"
implementation "io.github.bonigarcia:webdrivermanager:$project.webDriverManager"
implementation "io.github.bonigarcia:webdrivermanager:$project.webDriverManagerVersion"
implementation "org.yaml:snakeyaml:$project.snakeyamlVersion"
}

shadowJar {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/znsio/teswiz/tools/JsonFile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.znsio.teswiz.tools;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.*;
import com.znsio.teswiz.exceptions.EnvironmentSetupException;
import com.znsio.teswiz.exceptions.InvalidTestDataException;
Expand Down Expand Up @@ -105,4 +107,23 @@ public static JsonObject convertToMap(String jsonAsString) {
public static JsonArray convertToArray(String jsonAsString) {
return JsonParser.parseString(jsonAsString).getAsJsonArray();
}

public static boolean compareFiles(String file1, String file2) {
try {
ObjectMapper objectMapper = new ObjectMapper();

JsonNode json1 = objectMapper.readTree(Files.newBufferedReader(Paths.get(file1)));
JsonNode json2 = objectMapper.readTree(Files.newBufferedReader(Paths.get(file2)));

if (json1.equals(json2)) {
LOGGER.info("The JSON files (file1: '%s' and file2: '%s') are identical.");
return true;
} else {
LOGGER.info("The JSON files (file1: '%s' and file2: '%s') are different.");
return false;
}
} catch (Exception e) {
throw new InvalidTestDataException("Invalid file provided", e);
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/znsio/teswiz/tools/YamlFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.znsio.teswiz.tools;

import com.znsio.teswiz.exceptions.InvalidTestDataException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

public class YamlFile {
private static final Logger LOGGER = LogManager.getLogger(YamlFile.class.getName());

private YamlFile() {
}

public static boolean compareFiles(String file1, String file2) {
try {
Yaml yaml = new Yaml();
Map<String, Object> yaml1;
Map<String, Object> yaml2;

try (InputStream input1 = Files.newInputStream(Paths.get(file1));
InputStream input2 = Files.newInputStream(Paths.get(file2))) {
yaml1 = yaml.load(input1);
yaml2 = yaml.load(input2);
}

if (yaml1.equals(yaml2)) {
LOGGER.info("The YAML files (file1: '%s' and file2: '%s') are identical.");
return true;
} else {
LOGGER.info("The YAML files (file1: '%s' and file2: '%s') are different.");
return false;
}
} catch (Exception e) {
throw new InvalidTestDataException("Invalid file provided", e);
}
}
}
32 changes: 32 additions & 0 deletions src/test/java/com/znsio/teswiz/tools/JsonFileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.znsio.teswiz.tools;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.File;

import static org.assertj.core.api.Assertions.assertThat;

class JsonFileTest {
private static final Logger LOGGER = LogManager.getLogger(JsonFileTest.class.getName());
private static final String LOG_DIR = "./target/testLogs";

@BeforeAll
public static void setupBefore() {
LOGGER.info("Create LOG_DIR: " + LOG_DIR);
System.setProperty("LOG_DIR", LOG_DIR);
new File(LOG_DIR).mkdirs();
}

@Test
void compareIdenticalFiles() {
assertThat(JsonFile.compareFiles("configs/browser_config.json", "configs/browser_config.json")).as("Files are not identical").isTrue();
}

@Test
void compareDifferentFiles() {
assertThat(JsonFile.compareFiles("configs/browser_config.json", "configs/applitools_config.json")).as("Files are not identical").isFalse();
}
}
32 changes: 32 additions & 0 deletions src/test/java/com/znsio/teswiz/tools/YamlFileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.znsio.teswiz.tools;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.File;

import static org.assertj.core.api.Assertions.assertThat;

class YamlFileTest {
private static final Logger LOGGER = LogManager.getLogger(YamlFileTest.class.getName());
private static final String LOG_DIR = "./target/testLogs";

@BeforeAll
public static void setupBefore() {
LOGGER.info("Create LOG_DIR: " + LOG_DIR);
System.setProperty("LOG_DIR", LOG_DIR);
new File(LOG_DIR).mkdirs();
}

@Test
void compareIdenticalFiles() {
assertThat(YamlFile.compareFiles(".github/workflows/HardGate_PassingBuild.yml", ".github/workflows/HardGate_PassingBuild.yml")).as("Files are not identical").isTrue();
}

@Test
void compareDifferentFiles() {
assertThat(YamlFile.compareFiles(".github/workflows/HardGate_PassingBuild.yml", ".github/workflows/HardGate_FailingBuild.yml")).as("Files are not identical").isFalse();
}
}

0 comments on commit 6f4f677

Please sign in to comment.