-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/sip 1199 advanced body validation (#236)
* - Refactoring CamelBodyValidatorTest * - Refactoring CamelBodyValidatorTest: formatting * - Advanced body validator with XML and JSON comparators * - changelogs and docs * - Reverting message properties changes * correcting sip test kit version * adding commons-io dependency * adding commons-io dependency * adding jaxb-api dependency * Excluding transitive dependencies that cause licence issue * Excluding transitive dependencies that cause licence issue * Upgrade jaxb-api to 2.3.1 to solve license issue --------- Co-authored-by: H.Schröder <[email protected]> Co-authored-by: Leto Bukarica <[email protected]>
- Loading branch information
1 parent
efc0742
commit 96e675c
Showing
18 changed files
with
557 additions
and
84 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,5 @@ | ||
{ | ||
"author": "vladiIkor", | ||
"pullrequestId": "236", | ||
"message": "Support for XML and JSON tree comparison." | ||
} |
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
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
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
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
13 changes: 13 additions & 0 deletions
13
...ip/foundation/testkit/workflow/thenphase/validator/impl/comparators/ComparatorResult.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,13 @@ | ||
package de.ikor.sip.foundation.testkit.workflow.thenphase.validator.impl.comparators; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ComparatorResult { | ||
private Boolean status; | ||
private String failureDescription; | ||
} |
3 changes: 3 additions & 0 deletions
3
...n/testkit/workflow/thenphase/validator/impl/comparators/IncompatibleStringComparator.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,3 @@ | ||
package de.ikor.sip.foundation.testkit.workflow.thenphase.validator.impl.comparators; | ||
|
||
public class IncompatibleStringComparator extends RuntimeException {} |
52 changes: 52 additions & 0 deletions
52
.../sip/foundation/testkit/workflow/thenphase/validator/impl/comparators/JsonComparator.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,52 @@ | ||
package de.ikor.sip.foundation.testkit.workflow.thenphase.validator.impl.comparators; | ||
|
||
import static java.lang.String.format; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.collect.MapDifference; | ||
import com.google.common.collect.Maps; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* A comparator for comparing JSON strings, based on Jackson object mapper. | ||
* | ||
* <p>This comparator converts JSON strings into maps and then compares the maps for equality. The | ||
* comparison result provides information about whether the two JSON strings are equal and a | ||
* description of the differences if they are not. | ||
*/ | ||
@Slf4j | ||
public class JsonComparator implements StringComparator { | ||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
/** | ||
* Compares two JSON strings for equality. | ||
* | ||
* @param expected The expected JSON string. | ||
* @param actual The actual JSON string to be compared against the expected. | ||
* @return A {@link ComparatorResult} containing the result of the comparison. | ||
*/ | ||
public ComparatorResult compare(String expected, String actual) { | ||
try { | ||
return doCompare(expected, actual); | ||
} catch (Exception e) { | ||
throw new IncompatibleStringComparator(); | ||
} | ||
} | ||
|
||
private ComparatorResult doCompare(String expected, String actual) | ||
throws JsonProcessingException { | ||
log.trace(format("Comparing .%n Expected: %s %n Actual: %s ", expected, actual)); | ||
TypeReference<HashMap<String, Object>> type = new TypeReference<>() {}; | ||
|
||
Map<String, Object> expectedAsMap = mapper.readValue(expected, type); | ||
Map<String, Object> actualAsMap = mapper.readValue(actual, type); | ||
|
||
MapDifference<String, Object> difference = Maps.difference(expectedAsMap, actualAsMap); | ||
return new ComparatorResult( | ||
difference.areEqual(), difference.areEqual() ? null : difference.toString()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...sip/foundation/testkit/workflow/thenphase/validator/impl/comparators/RegexComparator.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,11 @@ | ||
package de.ikor.sip.foundation.testkit.workflow.thenphase.validator.impl.comparators; | ||
|
||
import de.ikor.sip.foundation.testkit.util.RegexUtil; | ||
|
||
public class RegexComparator implements StringComparator { | ||
@Override | ||
public ComparatorResult compare(String expected, String actual) { | ||
boolean matches = RegexUtil.compare(expected, actual); | ||
return new ComparatorResult(matches, null); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ip/foundation/testkit/workflow/thenphase/validator/impl/comparators/StringComparator.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,12 @@ | ||
package de.ikor.sip.foundation.testkit.workflow.thenphase.validator.impl.comparators; | ||
|
||
import de.ikor.sip.foundation.testkit.workflow.thenphase.validator.impl.CamelBodyValidator; | ||
|
||
/** | ||
* SIP abstraction used by Test Kit. Used to implement different comparators in order to provide | ||
* more powerful body validation process. Any implementation of StringComparator should be | ||
* instantiated in {@link CamelBodyValidator} in order to take part in body validation process. | ||
*/ | ||
public interface StringComparator { | ||
ComparatorResult compare(String expected, String actual); | ||
} |
51 changes: 51 additions & 0 deletions
51
...r/sip/foundation/testkit/workflow/thenphase/validator/impl/comparators/XMLComparator.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,51 @@ | ||
package de.ikor.sip.foundation.testkit.workflow.thenphase.validator.impl.comparators; | ||
|
||
import static java.lang.String.format; | ||
|
||
import de.ikor.sip.foundation.testkit.workflow.thenphase.validator.impl.comparators.util.SilentDocumentFactory; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.xmlunit.builder.DiffBuilder; | ||
import org.xmlunit.diff.DefaultNodeMatcher; | ||
import org.xmlunit.diff.Diff; | ||
import org.xmlunit.diff.ElementSelectors; | ||
|
||
/** | ||
* A comparator for comparing XML strings, based on xmlunit library. | ||
* | ||
* <p>This comparator uses the DiffBuilder to compare two XML strings for similarity. The comparison | ||
* result provides information about whether the two XML strings are similar and a description of | ||
* the differences if they are not. | ||
*/ | ||
@Slf4j | ||
public class XMLComparator implements StringComparator { | ||
|
||
/** | ||
* Compares two XML strings for similarity. | ||
* | ||
* @param expected The expected XML string. | ||
* @param actual The actual XML string to be compared against the expected. | ||
* @return A {@link ComparatorResult} containing the result of the comparison. | ||
*/ | ||
public ComparatorResult compare(String expected, String actual) { | ||
try { | ||
return doCompare(expected, actual); | ||
} catch (Exception e) { | ||
throw new IncompatibleStringComparator(); | ||
} | ||
} | ||
|
||
private ComparatorResult doCompare(String expected, String actual) { | ||
log.trace(format("Comparing xml content.%n Expected: %s %n Actual: %s ", expected, actual)); | ||
Diff diff = | ||
DiffBuilder.compare(expected) | ||
.withDocumentBuilderFactory(new SilentDocumentFactory()) | ||
.withTest(actual) | ||
.normalizeWhitespace() | ||
.checkForSimilar() | ||
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)) | ||
.build(); | ||
|
||
return new ComparatorResult( | ||
!diff.hasDifferences(), !diff.hasDifferences() ? null : diff.fullDescription()); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...ion/testkit/workflow/thenphase/validator/impl/comparators/util/SilentDocumentFactory.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,54 @@ | ||
package de.ikor.sip.foundation.testkit.workflow.thenphase.validator.impl.comparators.util; | ||
|
||
import com.ctc.wstx.shaded.msv_core.verifier.jaxp.DocumentBuilderFactoryImpl; | ||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.xml.sax.ErrorHandler; | ||
import org.xml.sax.SAXException; | ||
import org.xml.sax.SAXParseException; | ||
|
||
/** | ||
* A custom implementation of {@link DocumentBuilderFactoryImpl} that provides a silent document | ||
* builder. This factory produces document builders that suppress errors and log them instead of | ||
* throwing them directly. | ||
*/ | ||
public class SilentDocumentFactory extends DocumentBuilderFactoryImpl { | ||
/** | ||
* Creates a new instance of a silent document builder. | ||
* | ||
* @return a new instance of {@link DocumentBuilder} with a custom error handler. | ||
* @throws ParserConfigurationException if a DocumentBuilder cannot be created which satisfies the | ||
* configuration requested. | ||
*/ | ||
@Override | ||
public DocumentBuilder newDocumentBuilder() throws ParserConfigurationException { | ||
DocumentBuilder documentBuilder = super.newDocumentBuilder(); | ||
documentBuilder.setErrorHandler(new SilentErrorHandler()); | ||
return documentBuilder; | ||
} | ||
|
||
/** | ||
* A custom error handler that logs SAX parse exceptions silently without throwing them. This | ||
* handler is intended to be used with the {@link DocumentBuilder} produced by {@link | ||
* SilentDocumentFactory}. | ||
*/ | ||
@Slf4j | ||
private static class SilentErrorHandler implements ErrorHandler { | ||
@Override | ||
public void warning(SAXParseException exception) throws SAXException { | ||
log.debug("WARNING: " + exception.getMessage(), exception); | ||
} | ||
|
||
@Override | ||
public void error(SAXParseException exception) throws SAXException { | ||
log.debug("ERROR: " + exception.getMessage(), exception); | ||
} | ||
|
||
@Override | ||
public void fatalError(SAXParseException exception) throws SAXException { | ||
log.debug("ERROR: " + exception.getMessage(), exception); | ||
throw exception; | ||
} | ||
} | ||
} |
Oops, something went wrong.