Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide more info when SBCF are different #771

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 27 additions & 19 deletions src/main/java/emissary/core/IBaseDataObjectDiffHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
import java.util.TreeSet;

public class IBaseDataObjectDiffHelper {
private static final String DIFF_OUTPUT_FORMAT = "%s%s: %s : %s";
private static final String DIFF_NOT_NULL_MSG = "Required: differences not null";
private static final String ID_NOT_NULL_MSG = "Required: identifier not null";
private static final String ARE_NOT_EQUAL = " are not equal";
private static final String ARE_NOT_EQUAL = " elements are not equal";
private static final String SHORT_NAME = "shortName";
private static final String INTERNAL_ID = "internalId";
private static final String TRANSFORM_HISTORY = "transformHistory";
Expand Down Expand Up @@ -121,7 +120,7 @@ public static void diff(final List<IBaseDataObject> ibdoList1, final List<IBaseD
final int ibdoList2Size = (ibdoList2 == null) ? 0 : ibdoList2.size();

if (ibdoList1Size != ibdoList2Size) {
differences.add(String.format("%s%s: 1.s=%s 2.s=%s", identifier, ARE_NOT_EQUAL, ibdoList1Size, ibdoList2Size));
differences.add(diffNotEqStr(identifier, "IBaseDataObject_list_size", ibdoList1Size, ibdoList2Size));
} else if (ibdoList1 != null && ibdoList2 != null) {
final List<String> childDifferences = new ArrayList<>();
for (int i = 0; i < ibdoList1.size(); i++) {
Expand Down Expand Up @@ -151,23 +150,26 @@ public static void diff(final SeekableByteChannelFactory sbcf1, final SeekableBy
Validate.notNull(identifier, ID_NOT_NULL_MSG);
Validate.notNull(differences, DIFF_NOT_NULL_MSG);

if (sbcf1 != null && sbcf2 != null) {
try (SeekableByteChannel sbc1 = sbcf1.create();
if (!(sbcf1 == null && sbcf2 == null)) {
try {
if (sbcf1 == null || sbcf2 == null)
differences.add(diffNotEqStr(identifier, "SeekableByteChannelFactory", sbcf1, sbcf2));
else {
SeekableByteChannel sbc1 = sbcf1.create();
SeekableByteChannel sbc2 = sbcf2.create();
InputStream is1 = Channels.newInputStream(sbc1);
InputStream is2 = Channels.newInputStream(sbc2)) {
if (!IOUtils.contentEquals(is1, is2)) {
differences.add(String.format("%s not equal. 1.cs=%s 2.cs=%s",
identifier, sbc1.size(), sbc2.size()));
InputStream is2 = Channels.newInputStream(sbc2);
if (!IOUtils.contentEquals(is1, is2)) {
final String suffix = "%s_byte_stream";
differences.add(diffNotEqStr(identifier, "SeekableByteChannelFactory",
String.format(suffix, sbc1.size()), String.format(suffix, sbc2.size())));
}
}
} catch (IOException e) {
differences.add(String.format("Failed to compare %s: %s", identifier, e.getMessage()));
}
} else if (sbcf1 == null && sbcf2 == null) {
// Do nothing as they are considered equal.
} else {
differences.add(String.format("%s not equal. sbcf1=%s sbcf2=%s", identifier, sbcf1, sbcf2));
}
} // if both null, do nothing as they're considered equal

}

/**
Expand All @@ -184,7 +186,7 @@ public static void diff(final Object object1, final Object object2, final String
Validate.notNull(differences, DIFF_NOT_NULL_MSG);

if (!Objects.deepEquals(object1, object2)) {
differences.add(String.format(DIFF_OUTPUT_FORMAT, identifier, ARE_NOT_EQUAL, object1, object2));
differences.add(diffNotEqStr(identifier, "Object", object1, object2));
}
}

Expand All @@ -202,7 +204,7 @@ public static void diff(final int integer1, final int integer2, final String ide
Validate.notNull(differences, DIFF_NOT_NULL_MSG);

if (integer1 != integer2) {
differences.add(identifier + ARE_NOT_EQUAL);
differences.add(diffNotEqStr(identifier, "Integer", integer1, integer2));
}
}

Expand All @@ -220,7 +222,7 @@ public static void diff(final boolean boolean1, final boolean boolean2, final St
Validate.notNull(differences, DIFF_NOT_NULL_MSG);

if (boolean1 != boolean2) {
differences.add(identifier + ARE_NOT_EQUAL);
differences.add(diffNotEqStr(identifier, "Boolean", boolean1, boolean2));
}
}

Expand Down Expand Up @@ -268,7 +270,7 @@ public static void keyValueMapDiff(final Map<String, Collection<String>> paramet
}

if (!p1.isEmpty() || !p2.isEmpty()) {
differences.add(String.format(DIFF_OUTPUT_FORMAT, identifier, ARE_NOT_EQUAL + "-Differing Keys/Values", p1, p2));
differences.add(diffNotEqStr(identifier, "key_value_set", p1, p2));
}
}

Expand All @@ -295,7 +297,7 @@ public static void minimalMapDiff(final Map<String, Collection<String>> paramete
}

if (!p1Keys.isEmpty() || !p2Keys.isEmpty()) {
differences.add(String.format(DIFF_OUTPUT_FORMAT, identifier, ARE_NOT_EQUAL + "-Differing Keys", p1Keys, p2Keys));
differences.add(diffNotEqStr(identifier, "minimal_map_key_set", p1Keys, p2Keys));
}
}

Expand All @@ -320,4 +322,10 @@ public static Map<String, Collection<String>> convertMap(final Map<String, Colle

return newMap;
}

public static String diffNotEqStr(String id, String objName, Object comparand1, Object comparand2) {
String comp1Msg = String.format("%s_1=%s : ", objName, comparand1);
String comp2Msg = String.format("%s_2=%s ", objName, comparand2);
return String.format("<%s>%s: ", id, ARE_NOT_EQUAL) + comp1Msg + comp2Msg;
}
}
5 changes: 2 additions & 3 deletions src/main/java/emissary/util/PlaceComparisonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static String checkDifferences(final IBaseDataObject ibdoForOldPlace, fin
if (i != 0) {
sb.append(StringUtils.LF);
}
sb.append(identifier).append(": PDiff: ");
sb.append(identifier).append(": parent_difference: ");
sb.append(parentDifferences.get(i));
}
if (!parentDifferences.isEmpty() && !childDifferences.isEmpty()) {
Expand All @@ -156,10 +156,9 @@ public static String checkDifferences(final IBaseDataObject ibdoForOldPlace, fin
if (i != 0) {
sb.append(StringUtils.LF);
}
sb.append(identifier).append(": CDiff: ");
sb.append(identifier).append(": child_difference: ");
sb.append(childDifferences.get(i));
}

return sb.toString();
}

Expand Down
19 changes: 13 additions & 6 deletions src/test/java/emissary/place/ComparisonPlaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import emissary.test.core.junit5.UnitTest;

import ch.qos.logback.classic.Level;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand Down Expand Up @@ -58,14 +59,16 @@ void testProcessPlaceNoChanges() throws Exception {

@Test
void testProcessPlaceAChanges() throws Exception {
final String logMessage = "COMPARISONPLACETEST: PDiff: meta are not equal-Differing Keys: [KEY] : []";
final String logMessage = "COMPARISONPLACETEST: parent_difference: <meta> elements are not equal: " +
"minimal_map_key_set_1=[KEY] : minimal_map_key_set_2=[] ";

testComparisonPlace(PROCESS_PLACE_A_CHANGES, logMessage);
}

@Test
void testProcessPlaceBChanges() throws Exception {
final String logMessage = "COMPARISONPLACETEST: PDiff: meta are not equal-Differing Keys: [] : [KEY]";
final String logMessage = "COMPARISONPLACETEST: parent_difference: <meta> elements are not equal: " +
"minimal_map_key_set_1=[] : minimal_map_key_set_2=[KEY] ";

testComparisonPlace(PROCESS_PLACE_B_CHANGES, logMessage);
}
Expand All @@ -77,16 +80,20 @@ void testProcessHDPlaceNoChanges() throws Exception {

@Test
void testProcessHDPlaceAChanges() throws Exception {
final String logMessage = "COMPARISONPLACETEST: PDiff: meta are not equal-Differing Keys: [KEY] : []\n" +
"COMPARISONPLACETEST: CDiff: COMPARISONPLACETEST : 0 : meta are not equal-Differing Keys: [KEY] : []";
final String logMessage = "COMPARISONPLACETEST: parent_difference: <meta> elements are not equal: " +
"minimal_map_key_set_1=[KEY] : minimal_map_key_set_2=[] " + StringUtils.LF +
"COMPARISONPLACETEST: child_difference: COMPARISONPLACETEST : 0 : <meta> elements are not equal: " +
"minimal_map_key_set_1=[KEY] : minimal_map_key_set_2=[] ";

testComparisonPlace(PROCESSHD_PLACE_A_CHANGES, logMessage);
}

@Test
void testProcessHDPlaceBChanges() throws Exception {
final String logMessage = "COMPARISONPLACETEST: PDiff: meta are not equal-Differing Keys: [] : [KEY]\n" +
"COMPARISONPLACETEST: CDiff: COMPARISONPLACETEST : 0 : meta are not equal-Differing Keys: [] : [KEY]";
final String logMessage = "COMPARISONPLACETEST: parent_difference: <meta> elements are not equal: " +
"minimal_map_key_set_1=[] : minimal_map_key_set_2=[KEY] " + StringUtils.LF +
"COMPARISONPLACETEST: child_difference: COMPARISONPLACETEST : 0 : <meta> elements are not equal: " +
"minimal_map_key_set_1=[] : minimal_map_key_set_2=[KEY] ";

testComparisonPlace(PROCESSHD_PLACE_B_CHANGES, logMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static void checkAnswers(final Document answers, final IBaseDataObject pa
final String differences = PlaceComparisonHelper.checkDifferences(expectedIbdo, payload, expectedAttachments,
attachments, placeName, DIFF_CHECK);

assertNull(differences, differences);
assertNull(differences);

final List<SimplifiedLogEvent> expectedSimplifiedLogEvents = getSimplifiedLogEvents(parent);

Expand Down