diff --git a/app/Makefile b/app/Makefile index 5e13694..50da5f9 100644 --- a/app/Makefile +++ b/app/Makefile @@ -19,7 +19,14 @@ run-dist: build install run-help: build install ./build/install/app/bin/app -h -run-json: build install +run-stylish: build install ./build/install/app/bin/app -f=stylish ./src/main/java/resources/json/FirstJsonFile.json ./src/main/java/resources/json/SecondJsonFile.json +run-plain: build install + ./build/install/app/bin/app -f=plain ./src/main/java/resources/json/FirstJsonFile.json ./src/main/java/resources/json/SecondJsonFile.json + +run-json: build install + ./build/install/app/bin/app -f=json ./src/main/java/resources/json/FirstJsonFile.json ./src/main/java/resources/json/SecondJsonFile.json + + .PHONY: build diff --git a/app/src/main/java/hexlet/code/App.java b/app/src/main/java/hexlet/code/App.java index 72c9b24..1e20e15 100644 --- a/app/src/main/java/hexlet/code/App.java +++ b/app/src/main/java/hexlet/code/App.java @@ -4,7 +4,6 @@ import picocli.CommandLine.Command; import picocli.CommandLine.Parameters; import picocli.CommandLine.Option; -import hexlet.code.source.Differ; import java.io.IOException; import java.util.concurrent.Callable; diff --git a/app/src/main/java/hexlet/code/Differ.java b/app/src/main/java/hexlet/code/Differ.java new file mode 100644 index 0000000..afc6094 --- /dev/null +++ b/app/src/main/java/hexlet/code/Differ.java @@ -0,0 +1,37 @@ +package hexlet.code; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; + + +public class Differ { + public static final String DEFAULT_FORMAT = "stylish"; + + public static String generate(String filePath1, String filePath2, String format) throws IOException { + var normalizedPath1 = FileManager.normaolizePath(Path.of(filePath1)); + var normalizedPath2 = FileManager.normaolizePath(Path.of(filePath2)); + + + if (Files.notExists(normalizedPath1) || Files.notExists(normalizedPath2)) { + throw new FileNotFoundException("Файл для чтения не найден"); + } + + ObjectMapper mapper = (FileManager.isJsonFile(normalizedPath1)) ? new ObjectMapper() : new YAMLMapper(); + var dataFirst = mapper.readValue(normalizedPath1.toFile(), Map.class); + var dataSecond = mapper.readValue(normalizedPath2.toFile(), Map.class); + + var differenceMap = TreeBuilder.getTreeDifference(dataFirst, dataSecond); + + return Formatter.generateFormatString(format, differenceMap); + } + + public static String generate(String filePath1, String filePath2) throws IOException { + return Differ.generate(filePath1, filePath2, DEFAULT_FORMAT); + } +} diff --git a/app/src/main/java/hexlet/code/source/FileManager.java b/app/src/main/java/hexlet/code/FileManager.java similarity index 95% rename from app/src/main/java/hexlet/code/source/FileManager.java rename to app/src/main/java/hexlet/code/FileManager.java index c7d8139..c985722 100644 --- a/app/src/main/java/hexlet/code/source/FileManager.java +++ b/app/src/main/java/hexlet/code/FileManager.java @@ -1,4 +1,4 @@ -package hexlet.code.source; +package hexlet.code; import java.nio.file.Path; diff --git a/app/src/main/java/hexlet/code/Formatter.java b/app/src/main/java/hexlet/code/Formatter.java index ff6690a..1e18340 100644 --- a/app/src/main/java/hexlet/code/Formatter.java +++ b/app/src/main/java/hexlet/code/Formatter.java @@ -1,16 +1,16 @@ package hexlet.code; import com.fasterxml.jackson.core.JsonProcessingException; -import hexlet.code.source.formatters.Json; -import hexlet.code.source.formatters.Plain; -import hexlet.code.source.formatters.Stylish; +import hexlet.code.formatters.Json; +import hexlet.code.formatters.Plain; +import hexlet.code.formatters.Stylish; import java.util.List; import java.util.Map; public class Formatter { - public static String getOrder(String format, Map> differenceMap) + public static String generateFormatString(String format, Map> differenceMap) throws IllegalArgumentException, JsonProcessingException { var normalizeFormat = format.trim().toLowerCase(); var result = ""; diff --git a/app/src/main/java/hexlet/code/TreeBuilder.java b/app/src/main/java/hexlet/code/TreeBuilder.java new file mode 100644 index 0000000..e46c774 --- /dev/null +++ b/app/src/main/java/hexlet/code/TreeBuilder.java @@ -0,0 +1,45 @@ +package hexlet.code; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.ArrayList; + +public class TreeBuilder { + public static final String ADDED = "ADDED"; + public static final String DELETED = "DELETED"; + public static final String CHANGED = "CHANGED"; + public static final String UNCHANGED = "UNCHANGED"; + + public static Map> getTreeDifference(Map firstDataMap, + Map secondDataMap) { + + Map> result = new TreeMap<>(); + Set keySet = new TreeSet<>(firstDataMap.keySet()); + keySet.addAll(secondDataMap.keySet()); + + keySet.forEach(key -> { + String stage; + var dataFirstValue = firstDataMap.get(key) == null ? "null" : firstDataMap.get(key); + var dataSecondValue = secondDataMap.get(key) == null ? "null" : secondDataMap.get(key); + + if (firstDataMap.containsKey(key) && secondDataMap.containsKey(key)) { + + stage = (dataFirstValue.equals(dataSecondValue)) ? UNCHANGED : CHANGED; + + } else { + stage = secondDataMap.containsKey(key) ? ADDED : DELETED; + } + + List date = new ArrayList<>(); + date.add(stage); + date.add(dataFirstValue); + date.add(dataSecondValue); + + result.put(key, date); + }); + return result; + } +} diff --git a/app/src/main/java/hexlet/code/source/formatters/Json.java b/app/src/main/java/hexlet/code/formatters/Json.java similarity index 91% rename from app/src/main/java/hexlet/code/source/formatters/Json.java rename to app/src/main/java/hexlet/code/formatters/Json.java index 08479b0..962168e 100644 --- a/app/src/main/java/hexlet/code/source/formatters/Json.java +++ b/app/src/main/java/hexlet/code/formatters/Json.java @@ -1,4 +1,4 @@ -package hexlet.code.source.formatters; +package hexlet.code.formatters; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/app/src/main/java/hexlet/code/source/formatters/Plain.java b/app/src/main/java/hexlet/code/formatters/Plain.java similarity index 81% rename from app/src/main/java/hexlet/code/source/formatters/Plain.java rename to app/src/main/java/hexlet/code/formatters/Plain.java index 02d99c9..8091538 100644 --- a/app/src/main/java/hexlet/code/source/formatters/Plain.java +++ b/app/src/main/java/hexlet/code/formatters/Plain.java @@ -1,6 +1,6 @@ -package hexlet.code.source.formatters; +package hexlet.code.formatters; -import hexlet.code.source.Differ; +import hexlet.code.TreeBuilder; import java.util.List; import java.util.Map; @@ -33,22 +33,22 @@ public static String plain(Map> differenceMap) { var currentState = value.getFirst(); - if (currentState.equals(Differ.ADDED)) { + if (currentState.equals(TreeBuilder.ADDED)) { Object newValue = getPrintString(value.getLast()); addString += "added with value: " + newValue + "\n"; } - if (currentState.equals(Differ.DELETED)) { + if (currentState.equals(TreeBuilder.DELETED)) { addString += "removed" + "\n"; } - if (currentState.equals(Differ.CHANGED)) { + if (currentState.equals(TreeBuilder.CHANGED)) { Object oldValue = getPrintString(value.get(1)); Object newValue = getPrintString(value.getLast()); addString += "updated. From " + oldValue + " to " + newValue + "\n"; } - if (currentState.equals(Differ.UNCHANGED)) { + if (currentState.equals(TreeBuilder.UNCHANGED)) { addString = ""; } diff --git a/app/src/main/java/hexlet/code/source/formatters/Stylish.java b/app/src/main/java/hexlet/code/formatters/Stylish.java similarity index 65% rename from app/src/main/java/hexlet/code/source/formatters/Stylish.java rename to app/src/main/java/hexlet/code/formatters/Stylish.java index 44c8a84..318e073 100644 --- a/app/src/main/java/hexlet/code/source/formatters/Stylish.java +++ b/app/src/main/java/hexlet/code/formatters/Stylish.java @@ -1,6 +1,6 @@ -package hexlet.code.source.formatters; +package hexlet.code.formatters; -import hexlet.code.source.Differ; +import hexlet.code.TreeBuilder; import java.util.List; import java.util.Map; @@ -11,15 +11,15 @@ public static String stylish(Map> differenceMap) { differenceMap.forEach((key, value) -> { String addedString = ""; - if (value.getFirst().equals(Differ.DELETED) || value.getFirst().equals(Differ.CHANGED)) { + if (value.getFirst().equals(TreeBuilder.DELETED) || value.getFirst().equals(TreeBuilder.CHANGED)) { addedString += "- " + key + ": " + value.get(1) + "\n"; } - if (value.getFirst().equals(Differ.ADDED) || value.getFirst().equals(Differ.CHANGED)) { + if (value.getFirst().equals(TreeBuilder.ADDED) || value.getFirst().equals(TreeBuilder.CHANGED)) { addedString += "+ " + key + ": " + value.get(2) + "\n"; } - if (value.getFirst().equals(Differ.UNCHANGED)) { + if (value.getFirst().equals(TreeBuilder.UNCHANGED)) { addedString += " " + key + ": " + value.get(1) + "\n"; } diff --git a/app/src/main/java/hexlet/code/source/Differ.java b/app/src/main/java/hexlet/code/source/Differ.java deleted file mode 100644 index e4d1c82..0000000 --- a/app/src/main/java/hexlet/code/source/Differ.java +++ /dev/null @@ -1,67 +0,0 @@ -package hexlet.code.source; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; -import hexlet.code.Formatter; - -import java.io.FileNotFoundException; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; -import java.util.ArrayList; - - -public class Differ { - public static final String ADDED = "ADDED"; - public static final String DELETED = "DELETED"; - public static final String CHANGED = "CHANGED"; - public static final String UNCHANGED = "UNCHANGED"; - - public static String generate(String filePath1, String filePath2, String format) throws IOException { - var normalizedPath1 = FileManager.normaolizePath(Path.of(filePath1)); - var normalizedPath2 = FileManager.normaolizePath(Path.of(filePath2)); - - - if (Files.notExists(normalizedPath1) || Files.notExists(normalizedPath2)) { - throw new FileNotFoundException("Файл для чтения не найден"); - } - - ObjectMapper mapper = (FileManager.isJsonFile(normalizedPath1)) ? new ObjectMapper() : new YAMLMapper(); - var dataFirst = mapper.readValue(normalizedPath1.toFile(), Map.class); - var dataSecond = mapper.readValue(normalizedPath2.toFile(), Map.class); - - - - Map> differenceMap = new TreeMap<>(); - Set keySet = new TreeSet<>(dataFirst.keySet()); - keySet.addAll(dataSecond.keySet()); - - keySet.forEach(key -> { - String stage; - var dataFirstValue = dataFirst.get(key) == null ? "null" : dataFirst.get(key); - var dataSecondValue = dataSecond.get(key) == null ? "null" : dataSecond.get(key); - - if (dataFirst.containsKey(key) && dataSecond.containsKey(key)) { - - stage = (dataFirstValue.equals(dataSecondValue)) ? UNCHANGED : CHANGED; - - } else { - stage = dataSecond.containsKey(key) ? ADDED : DELETED; - } - - List date = new ArrayList<>(); - date.add(stage); - date.add(dataFirstValue); - date.add(dataSecondValue); - - differenceMap.put(key, date); - }); - - return Formatter.getOrder(format, differenceMap); - } -} diff --git a/app/src/test/java/fixtures/Json b/app/src/test/java/resources/fixtures/Json similarity index 100% rename from app/src/test/java/fixtures/Json rename to app/src/test/java/resources/fixtures/Json diff --git a/app/src/test/java/fixtures/Plain b/app/src/test/java/resources/fixtures/Plain similarity index 100% rename from app/src/test/java/fixtures/Plain rename to app/src/test/java/resources/fixtures/Plain diff --git a/app/src/test/java/fixtures/Stylish b/app/src/test/java/resources/fixtures/Stylish similarity index 100% rename from app/src/test/java/fixtures/Stylish rename to app/src/test/java/resources/fixtures/Stylish diff --git a/app/src/test/java/fixtures/Stylish_same_file b/app/src/test/java/resources/fixtures/Stylish_same_file similarity index 100% rename from app/src/test/java/fixtures/Stylish_same_file rename to app/src/test/java/resources/fixtures/Stylish_same_file diff --git a/app/src/test/java/source/TestDiffer.java b/app/src/test/java/source/TestDiffer.java index d8cc4cf..585f569 100644 --- a/app/src/test/java/source/TestDiffer.java +++ b/app/src/test/java/source/TestDiffer.java @@ -1,6 +1,6 @@ package source; -import hexlet.code.source.Differ; +import hexlet.code.Differ; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -13,9 +13,9 @@ public class TestDiffer { - static final String STYLISH_REPORT_PATH = "src/test/java/fixtures/Stylish"; - static final String PLAIN_REPORT_PATH = "src/test/java/fixtures/Plain"; - static final String JSON_REPORT_PATH = "src/test/java/fixtures/Json"; + static final String STYLISH_REPORT_PATH = "src/test/java/resources/fixtures/Stylish"; + static final String PLAIN_REPORT_PATH = "src/test/java/resources/fixtures/Plain"; + static final String JSON_REPORT_PATH = "src/test/java/resources/fixtures/Json"; private static String differsStylish; @@ -85,7 +85,7 @@ public void testWrongPathFile() { @Test public void testGenerateWithSomeFile() throws IOException { - String differs = readReroptAsString(Path.of("src/test/java/fixtures/Stylish_same_file")); + String differs = readReroptAsString(Path.of("src/test/java/resources/fixtures/Stylish_same_file")); var formant = "stylish"; String differenceJson = Differ.generate(firstTestJsonFilePath, firstTestJsonFilePath, formant); assertEquals(differenceJson, differs); @@ -108,6 +108,15 @@ public void testWrongFormat() { assertEquals("Не найден формат", thrownSecondArg.getMessage()); } + @Test + public void testGenerateWithOutFormat() throws IOException { + var differenceJson = Differ.generate(firstTestJsonFilePath, secondTestJsonFilePath); + assertEquals(differsStylish, differenceJson); + + String differenceYaml = Differ.generate(firstTestYamlFilePath, secondTestYamlFilePath); + assertEquals(differenceYaml, differsStylish); + } + public static String readReroptAsString(Path path) throws IOException { var normalizePath = path.normalize().toAbsolutePath(); diff --git a/app/src/test/java/source/TestFIleManager.java b/app/src/test/java/source/TestFIleManager.java index 0335c43..d4c5d5e 100644 --- a/app/src/test/java/source/TestFIleManager.java +++ b/app/src/test/java/source/TestFIleManager.java @@ -1,6 +1,6 @@ package source; -import hexlet.code.source.FileManager; +import hexlet.code.FileManager; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows;