Skip to content
Merged
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
9 changes: 8 additions & 1 deletion app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion app/src/main/java/hexlet/code/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions app/src/main/java/hexlet/code/Differ.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hexlet.code.source;
package hexlet.code;

import java.nio.file.Path;

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/hexlet/code/Formatter.java
Original file line number Diff line number Diff line change
@@ -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<String, List<Object>> differenceMap)
public static String generateFormatString(String format, Map<String, List<Object>> differenceMap)
throws IllegalArgumentException, JsonProcessingException {
var normalizeFormat = format.trim().toLowerCase();
var result = "";
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/hexlet/code/TreeBuilder.java
Original file line number Diff line number Diff line change
@@ -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<String, List<Object>> getTreeDifference(Map<String, Object> firstDataMap,
Map<String, Object> secondDataMap) {

Map<String, List<Object>> result = new TreeMap<>();
Set<String> 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<Object> date = new ArrayList<>();
date.add(stage);
date.add(dataFirstValue);
date.add(dataSecondValue);

result.put(key, date);
});
return result;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,22 +33,22 @@ public static String plain(Map<String, List<Object>> 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 = "";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,15 +11,15 @@ public static String stylish(Map<String, List<Object>> 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";
}

Expand Down
67 changes: 0 additions & 67 deletions app/src/main/java/hexlet/code/source/Differ.java

This file was deleted.

19 changes: 14 additions & 5 deletions app/src/test/java/source/TestDiffer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion app/src/test/java/source/TestFIleManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading