This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Pozzi
committed
Mar 15, 2021
1 parent
fa262df
commit c3b54a6
Showing
2 changed files
with
165 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package de.bonndan.nivio.model; | ||
|
||
import org.apache.commons.collections.CollectionUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.*; | ||
|
||
public class ComponentDiff { | ||
|
||
/** | ||
* Compares changes in two strings. | ||
* | ||
* @param a first | ||
* @param b second | ||
* @param key label | ||
* @param changes array of changes | ||
*/ | ||
public static void compareStrings(String a, String b, String key, List<String> changes) { | ||
if (!org.apache.commons.lang3.StringUtils.equals(a, b)) { | ||
changes.add(key + " changed to: " + b); | ||
} | ||
} | ||
|
||
/** | ||
* Compares changes in string representation of two objects | ||
* | ||
* @param a first | ||
* @param b second | ||
* @param key label | ||
* @param changes array of changes | ||
*/ | ||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") | ||
public static void compareOptionals(Optional<Object> a, Optional<Object> b, String key, List<String> changes) { | ||
if (a.isEmpty() && b.isEmpty()) { | ||
return; | ||
} | ||
if (a.isPresent() && b.isEmpty() || a.isEmpty() && b.isPresent()) { | ||
changes.add(key + " changed to: " + b.orElse("")); | ||
return; | ||
} | ||
if (!org.apache.commons.lang3.StringUtils.equals(a.map(Object::toString).orElse(""), b.map(Object::toString).orElse(""))) { | ||
changes.add(key + " changed to: " + b); | ||
} | ||
} | ||
|
||
/** | ||
* Compares to string collections. | ||
* | ||
* @param one first | ||
* @param two second | ||
* @param key label | ||
* @param changes list of changes | ||
*/ | ||
public static void compareCollections(Collection<String> one, Collection<String> two, String key, List<String> changes) { | ||
Collection<String> disjunction = CollectionUtils.disjunction(one, two); | ||
String changedKeys = String.join(",", disjunction); | ||
if (!StringUtils.isEmpty(changedKeys)) { | ||
changes.add(String.format("%s have differences : '%s'", key, changedKeys)); | ||
} | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
src/test/java/de/bonndan/nivio/model/ComponentDiffTest.java
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,104 @@ | ||
package de.bonndan.nivio.model; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ComponentDiffTest { | ||
|
||
private List<String> changes; | ||
|
||
@BeforeEach | ||
void setup() { | ||
changes = new ArrayList<>(); | ||
} | ||
|
||
@Test | ||
void twoNulls() { | ||
ComponentDiff.compareStrings(null, null, "foo", changes); | ||
assertThat(changes).hasSize(0); | ||
} | ||
|
||
@Test | ||
void twoNull() { | ||
ComponentDiff.compareStrings("a", null, "foo", changes); | ||
assertThat(changes).hasSize(1); | ||
} | ||
|
||
@Test | ||
void oneNull() { | ||
ComponentDiff.compareStrings(null, "b", "foo", changes); | ||
assertThat(changes).hasSize(1); | ||
} | ||
|
||
@Test | ||
void similarString() { | ||
ComponentDiff.compareStrings("b", "b", "foo", changes); | ||
assertThat(changes).hasSize(0); | ||
} | ||
|
||
@Test | ||
void differentString() { | ||
ComponentDiff.compareStrings("a", "b", "foo", changes); | ||
assertThat(changes).hasSize(1); | ||
} | ||
|
||
@Test | ||
void bothEmpty() { | ||
ComponentDiff.compareOptionals(Optional.empty(), Optional.empty(), "foo", changes); | ||
assertThat(changes).hasSize(0); | ||
} | ||
|
||
@Test | ||
void oneEmpty() { | ||
ComponentDiff.compareOptionals(Optional.empty(), Optional.of("a"), "foo", changes); | ||
assertThat(changes).hasSize(1); | ||
} | ||
|
||
@Test | ||
void twoEmpty() { | ||
ComponentDiff.compareOptionals(Optional.of("a"), Optional.empty(), "foo", changes); | ||
assertThat(changes).hasSize(1); | ||
} | ||
|
||
@Test | ||
void similarOptionals() { | ||
ComponentDiff.compareOptionals(Optional.of("a"), Optional.of("a"), "foo", changes); | ||
assertThat(changes).hasSize(0); | ||
} | ||
|
||
@Test | ||
void differentOptionals() { | ||
ComponentDiff.compareOptionals(Optional.of("a"), Optional.of("b"), "foo", changes); | ||
assertThat(changes).hasSize(1); | ||
} | ||
|
||
@Test | ||
void similarCollections() { | ||
ComponentDiff.compareCollections(List.of("a", "b"), List.of("b", "a"), "foo", changes); | ||
assertThat(changes).hasSize(0); | ||
} | ||
|
||
@Test | ||
void differentCollectionEntries() { | ||
ComponentDiff.compareCollections(List.of("a", "b"), List.of("c", "a"), "foo", changes); | ||
assertThat(changes).hasSize(1); | ||
} | ||
|
||
@Test | ||
void differentCollectionSize() { | ||
ComponentDiff.compareCollections(List.of("a", "b"), List.of("a", "b", "c"), "foo", changes); | ||
assertThat(changes).hasSize(1); | ||
} | ||
|
||
@Test | ||
void differentCollectionSize2() { | ||
ComponentDiff.compareCollections(List.of("a", "b", "c"), List.of("a", "b"), "foo", changes); | ||
assertThat(changes).hasSize(1); | ||
} | ||
} |