diff --git a/build.gradle b/build.gradle index 3212fc6b..fd7ff909 100644 --- a/build.gradle +++ b/build.gradle @@ -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" @@ -21,6 +21,7 @@ buildscript { assertJVersion = "3.26.3" jetbrainsAnnotationsVersion = "26.0.1" junitVersion = "5.11.3" + snakeyamlVersion = "2.3" } } @@ -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 { diff --git a/src/main/java/com/znsio/teswiz/tools/JsonFile.java b/src/main/java/com/znsio/teswiz/tools/JsonFile.java index c9484658..cc355a25 100644 --- a/src/main/java/com/znsio/teswiz/tools/JsonFile.java +++ b/src/main/java/com/znsio/teswiz/tools/JsonFile.java @@ -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; @@ -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); + } + } } diff --git a/src/main/java/com/znsio/teswiz/tools/YamlFile.java b/src/main/java/com/znsio/teswiz/tools/YamlFile.java new file mode 100644 index 00000000..5407c59f --- /dev/null +++ b/src/main/java/com/znsio/teswiz/tools/YamlFile.java @@ -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 yaml1; + Map 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); + } + } +} diff --git a/src/test/java/com/znsio/teswiz/tools/JsonFileTest.java b/src/test/java/com/znsio/teswiz/tools/JsonFileTest.java new file mode 100644 index 00000000..d78bfcec --- /dev/null +++ b/src/test/java/com/znsio/teswiz/tools/JsonFileTest.java @@ -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(); + } +} diff --git a/src/test/java/com/znsio/teswiz/tools/YamlFileTest.java b/src/test/java/com/znsio/teswiz/tools/YamlFileTest.java new file mode 100644 index 00000000..f26ffb89 --- /dev/null +++ b/src/test/java/com/znsio/teswiz/tools/YamlFileTest.java @@ -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(); + } +}