-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added utility methods to compare 2 Yaml files and also 2 Json files
- Loading branch information
1 parent
3b02058
commit 6f4f677
Showing
5 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |