Skip to content

Commit

Permalink
Add additional IBDO parameter diff options.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Cover jdcove2 committed Oct 5, 2023
1 parent ef13f6a commit d2ac679
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 3 deletions.
67 changes: 66 additions & 1 deletion src/main/java/emissary/core/DiffCheckConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DiffCheckConfiguration {
* Possible configuration options
*/
public enum DiffCheckOptions {
DATA, TIMESTAMP, INTERNAL_ID, TRANSFORM_HISTORY
DATA, TIMESTAMP, INTERNAL_ID, TRANSFORM_HISTORY, KEY_VALUE_PARAMETER_DIFF, DETAILED_PARAMETER_DIFF
}

/**
Expand Down Expand Up @@ -74,6 +74,24 @@ public boolean checkTransformHistory() {
return enabled.contains(DiffCheckOptions.TRANSFORM_HISTORY);
}

/**
* Check if the transform history should be diffed
*
* @return if checking the transform history is enabled
*/
public boolean checkDetailedParameterDiff() {
return enabled.contains(DiffCheckOptions.DETAILED_PARAMETER_DIFF);
}

/**
* Check if the transform history should be diffed
*
* @return if checking the transform history is enabled
*/
public boolean checkKeyValueParameterDiff() {
return enabled.contains(DiffCheckOptions.KEY_VALUE_PARAMETER_DIFF);
}

/**
* Accessor for enabled options
*
Expand Down Expand Up @@ -120,6 +138,11 @@ public DiffCheckConfiguration build() {
public DiffCheckConfiguration explicit(final DiffCheckOptions... options) {
reset();
building.addAll(Arrays.asList(options));

if (building.contains(DiffCheckOptions.DETAILED_PARAMETER_DIFF) && building.contains(DiffCheckOptions.KEY_VALUE_PARAMETER_DIFF)) {
throw new IllegalArgumentException("Cannot contain DETAILED_PARAMETER_DIFF and KEY_VALUE_PARAMETER_DIFF!");
}

return build();
}

Expand Down Expand Up @@ -219,5 +242,47 @@ public DiffCheckBuilder disableTransformHistory() {
building.remove(DiffCheckOptions.TRANSFORM_HISTORY);
return this;
}

/**
* Enable transform history for diff checking
*
* @return the builder
*/
public DiffCheckBuilder enableKeyValueParameterDiff() {
building.add(DiffCheckOptions.KEY_VALUE_PARAMETER_DIFF);
building.remove(DiffCheckOptions.DETAILED_PARAMETER_DIFF);
return this;
}

/**
* Disable transform history for diff checking
*
* @return the builder
*/
public DiffCheckBuilder disableKeyValueParameterDiff() {
building.remove(DiffCheckOptions.KEY_VALUE_PARAMETER_DIFF);
return this;
}

/**
* Enable transform history for diff checking
*
* @return the builder
*/
public DiffCheckBuilder enableDetailedParameterDiff() {
building.add(DiffCheckOptions.DETAILED_PARAMETER_DIFF);
building.remove(DiffCheckOptions.KEY_VALUE_PARAMETER_DIFF);
return this;
}

/**
* Disable transform history for diff checking
*
* @return the builder
*/
public DiffCheckBuilder disableDetailedParameterDiff() {
building.remove(DiffCheckOptions.DETAILED_PARAMETER_DIFF);
return this;
}
}
}
54 changes: 52 additions & 2 deletions src/main/java/emissary/core/IBaseDataObjectDiffHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class IBaseDataObjectDiffHelper {

Expand Down Expand Up @@ -58,8 +63,15 @@ public static void diff(final IBaseDataObject ibdo1, final IBaseDataObject ibdo2
}

diff(ibdo1.getFontEncoding(), ibdo2.getFontEncoding(), "fontEncoding", differences);
// TreeMap automatically sorts parameters by key
diff(convertMap(ibdo1.getParameters()), convertMap(ibdo2.getParameters()), "parameters", differences);

if (options.checkDetailedParameterDiff()) {
diff(convertMap(ibdo1.getParameters()), convertMap(ibdo2.getParameters()), "parameters", differences);
} else if (options.checkKeyValueParameterDiff()) {
keyValueMapDiff(convertMap(ibdo1.getParameters()), convertMap(ibdo2.getParameters()), "parameters", differences);
} else {
minimalMapDiff(convertMap(ibdo1.getParameters()), convertMap(ibdo2.getParameters()), "parameters", differences);
}

diff(ibdo1.getNumChildren(), ibdo2.getNumChildren(), "numChildren", differences);
diff(ibdo1.getNumSiblings(), ibdo2.getNumSiblings(), "numSiblings", differences);
diff(ibdo1.getBirthOrder(), ibdo2.getBirthOrder(), "birthOrder", differences);
Expand Down Expand Up @@ -226,6 +238,44 @@ public static void diff(final Map<String, byte[]> map1, final Map<String, byte[]
}
}

public static void keyValueMapDiff(final Map<String, Collection<String>> parameter1, final Map<String, Collection<String>> parameter2,
final String identifier, final List<String> differences) {
final Set<Entry<String, Collection<String>>> p1Entries = new HashSet<>(parameter1.entrySet());
final Set<Entry<String, Collection<String>>> p2Entries = new HashSet<>(parameter2.entrySet());
final Map<String, Collection<String>> p1 = new HashMap<>(parameter1);
final Map<String, Collection<String>> p2 = new HashMap<>(parameter2);

for (Entry<String, Collection<String>> p1Entry : p1Entries) {
if (p2Entries.contains(p1Entry)) {
p1.remove(p1Entry.getKey());
p2.remove(p1Entry.getKey());
}
}

if (!p1.isEmpty() || !p2.isEmpty()) {
differences.add(String.format("%s%s: %s : %s", identifier, ARE_NOT_EQUAL + "-Differening Keys/Valus", p1, p2));
}
}

public static void minimalMapDiff(final Map<String, Collection<String>> parameter1, final Map<String, Collection<String>> parameter2,
final String identifier, final List<String> differences) {
final Set<Entry<String, Collection<String>>> p1Entries = new HashSet<>(parameter1.entrySet());
final Set<Entry<String, Collection<String>>> p2Entries = new HashSet<>(parameter2.entrySet());
final Set<String> p1Keys = new TreeSet<>(parameter1.keySet());
final Set<String> p2Keys = new TreeSet<>(parameter2.keySet());

for (Entry<String, Collection<String>> p1Entry : p1Entries) {
if (p2Entries.contains(p1Entry)) {
p1Keys.remove(p1Entry.getKey());
p2Keys.remove(p1Entry.getKey());
}
}

if (!p1Keys.isEmpty() || !p2Keys.isEmpty()) {
differences.add(String.format("%s%s: %s : %s", identifier, ARE_NOT_EQUAL + "-Differening Keys", p1Keys, p2Keys));
}
}

/**
* This method converts the IBDO parameter map of Object values to a map of String values for better comparison.
*
Expand Down
134 changes: 134 additions & 0 deletions src/test/java/emissary/core/DiffCheckConfigurationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package emissary.core;

import emissary.core.DiffCheckConfiguration.DiffCheckBuilder;
import emissary.core.DiffCheckConfiguration.DiffCheckOptions;
import emissary.test.core.junit5.UnitTest;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class DiffCheckConfigurationTest extends UnitTest {
@Test
void testNonParameter() {
final DiffCheckBuilder diffCheckBuilder = DiffCheckConfiguration.configure();
final DiffCheckConfiguration emptyConfiguration = diffCheckBuilder.build();

assertEquals(0, emptyConfiguration.getEnabled().size(), "Configuration should be empty!");

diffCheckBuilder.enableData();
diffCheckBuilder.enableInternalId();
diffCheckBuilder.enableTimestamp();
diffCheckBuilder.enableTransformHistory();

final DiffCheckConfiguration nonParameterConfiguration = diffCheckBuilder.build();

assertTrue(nonParameterConfiguration.checkData());
assertTrue(nonParameterConfiguration.checkInternalId());
assertTrue(nonParameterConfiguration.checkTimestamp());
assertTrue(nonParameterConfiguration.checkTransformHistory());
assertFalse(nonParameterConfiguration.checkDetailedParameterDiff());
assertFalse(nonParameterConfiguration.checkKeyValueParameterDiff());

diffCheckBuilder.disableData();
diffCheckBuilder.disableInternalId();
diffCheckBuilder.disableTimestamp();
diffCheckBuilder.disableTransformHistory();

assertEquals(0, diffCheckBuilder.build().getEnabled().size(), "Configuration should be empty!");
}

@Test
void testKeyValueDetailed() {
final DiffCheckBuilder diffCheckBuilder = DiffCheckConfiguration.configure();

diffCheckBuilder.enableDetailedParameterDiff();

final DiffCheckConfiguration detailedParameterConfiguration = diffCheckBuilder.build();

assertTrue(detailedParameterConfiguration.checkDetailedParameterDiff());
assertFalse(detailedParameterConfiguration.checkKeyValueParameterDiff());

diffCheckBuilder.enableKeyValueParameterDiff();

final DiffCheckConfiguration keyValueParameterConfiguration = diffCheckBuilder.build();

assertFalse(keyValueParameterConfiguration.checkDetailedParameterDiff());
assertTrue(keyValueParameterConfiguration.checkKeyValueParameterDiff());

diffCheckBuilder.disableKeyValueParameterDiff();

assertEquals(0, diffCheckBuilder.build().getEnabled().size(), "Configuration should be empty!");

diffCheckBuilder.enableKeyValueParameterDiff();

final DiffCheckConfiguration KeyValueParameterConfiguration2 = diffCheckBuilder.build();

assertFalse(KeyValueParameterConfiguration2.checkDetailedParameterDiff());
assertTrue(KeyValueParameterConfiguration2.checkKeyValueParameterDiff());

diffCheckBuilder.enableDetailedParameterDiff();

final DiffCheckConfiguration detailedParameterConfiguration2 = diffCheckBuilder.build();

assertTrue(detailedParameterConfiguration2.checkDetailedParameterDiff());
assertFalse(detailedParameterConfiguration2.checkKeyValueParameterDiff());

diffCheckBuilder.disableDetailedParameterDiff();

assertEquals(0, diffCheckBuilder.build().getEnabled().size(), "Configuration should be empty!");
}

@Test
void checkReset() {
final DiffCheckBuilder diffCheckBuilder = DiffCheckConfiguration.configure();

diffCheckBuilder.enableData();
diffCheckBuilder.enableInternalId();
diffCheckBuilder.enableTimestamp();
diffCheckBuilder.enableTransformHistory();
diffCheckBuilder.enableDetailedParameterDiff();
diffCheckBuilder.reset();

assertEquals(0, diffCheckBuilder.build().getEnabled().size(), "Configuration should be empty!");
}

@Test
void checkExplicit() {
final DiffCheckBuilder diffCheckBuilder = DiffCheckConfiguration.configure();
final DiffCheckConfiguration explicitDetailedConfiguration = diffCheckBuilder.explicit(
DiffCheckOptions.DATA,
DiffCheckOptions.DETAILED_PARAMETER_DIFF,
DiffCheckOptions.INTERNAL_ID,
DiffCheckOptions.TIMESTAMP,
DiffCheckOptions.TRANSFORM_HISTORY);

assertTrue(explicitDetailedConfiguration.checkData());
assertTrue(explicitDetailedConfiguration.checkInternalId());
assertTrue(explicitDetailedConfiguration.checkTimestamp());
assertTrue(explicitDetailedConfiguration.checkTransformHistory());
assertTrue(explicitDetailedConfiguration.checkDetailedParameterDiff());
assertFalse(explicitDetailedConfiguration.checkKeyValueParameterDiff());

final DiffCheckConfiguration explicitKeyValueConfiguration = diffCheckBuilder.explicit(
DiffCheckOptions.DATA,
DiffCheckOptions.KEY_VALUE_PARAMETER_DIFF,
DiffCheckOptions.INTERNAL_ID,
DiffCheckOptions.TIMESTAMP,
DiffCheckOptions.TRANSFORM_HISTORY);

assertTrue(explicitKeyValueConfiguration.checkData());
assertTrue(explicitKeyValueConfiguration.checkInternalId());
assertTrue(explicitKeyValueConfiguration.checkTimestamp());
assertTrue(explicitKeyValueConfiguration.checkTransformHistory());
assertFalse(explicitKeyValueConfiguration.checkDetailedParameterDiff());
assertTrue(explicitKeyValueConfiguration.checkKeyValueParameterDiff());

assertThrows(IllegalArgumentException.class, () -> diffCheckBuilder.explicit(
DiffCheckOptions.DETAILED_PARAMETER_DIFF,
DiffCheckOptions.KEY_VALUE_PARAMETER_DIFF));
}
}
35 changes: 35 additions & 0 deletions src/test/java/emissary/core/IBaseDataObjectDiffHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,44 @@ void testDiffHistory() {

@Test
void testDiffParameters() {
final DiffCheckConfiguration checkKeyValueDiffParameter = DiffCheckConfiguration.configure().enableKeyValueParameterDiff().build();
final DiffCheckConfiguration checkDetailedDiffParameter = DiffCheckConfiguration.configure().enableDetailedParameterDiff().build();

ibdo1.putParameter("STRING", "string");
ibdo1.putParameter("LIST", Arrays.asList("first", "second", "third"));

verifyDiff(1);
verifyDiff(1, checkKeyValueDiffParameter);
verifyDiff(1, checkDetailedDiffParameter);

ibdo1.clearParameters();
ibdo2.clearParameters();
ibdo1.putParameter("STRING", "string");
ibdo1.putParameter("Integer", Integer.valueOf(1));
ibdo2.putParameter("STRING", "string");

verifyDiff(1);
verifyDiff(1, checkKeyValueDiffParameter);
verifyDiff(1, checkDetailedDiffParameter);

ibdo1.clearParameters();
ibdo2.clearParameters();
ibdo1.putParameter("STRING", "string");
ibdo2.putParameter("STRING", "string");
ibdo2.putParameter("Integer", Integer.valueOf(1));

verifyDiff(1);
verifyDiff(1, checkKeyValueDiffParameter);
verifyDiff(1, checkDetailedDiffParameter);

ibdo1.clearParameters();
ibdo2.clearParameters();
ibdo1.putParameter("STRING", "string");
ibdo2.putParameter("STRING", "string");

verifyDiff(0);
verifyDiff(0, checkKeyValueDiffParameter);
verifyDiff(0, checkDetailedDiffParameter);
}

@Test
Expand Down

0 comments on commit d2ac679

Please sign in to comment.