| 1 | package fr.sii.ogham.testing.assertion.wiremock; | |
| 2 | ||
| 3 | import com.github.tomakehurst.wiremock.client.WireMock; | |
| 4 | import com.github.tomakehurst.wiremock.matching.MatchResult; | |
| 5 | import com.github.tomakehurst.wiremock.matching.StringValuePattern; | |
| 6 | import fr.sii.ogham.testing.assertion.exception.ComparisonException; | |
| 7 | import fr.sii.ogham.testing.assertion.util.HtmlUtils; | |
| 8 | import org.slf4j.Logger; | |
| 9 | import org.slf4j.LoggerFactory; | |
| 10 | import org.w3c.dom.Document; | |
| 11 | import org.xmlunit.diff.Diff; | |
| 12 | ||
| 13 | import java.util.concurrent.atomic.AtomicInteger; | |
| 14 | ||
| 15 | import static com.github.tomakehurst.wiremock.matching.MatchResult.*; | |
| 16 | ||
| 17 | /** | |
| 18 | * Check if the HTML is similar to the expected. The HTML strings are parsed | |
| 19 | * into {@link Document}s. Two documents are considered to be "similar" if they | |
| 20 | * contain the same elements and attributes regardless of order. | |
| 21 | * | |
| 22 | * <p> | |
| 23 | * See {@link HtmlUtils} for more information about "similar" HTML. | |
| 24 | * | |
| 25 | * <p> | |
| 26 | * NOTE: {@link WireMock#equalToXml(String)} does an identical check. | |
| 27 | * | |
| 28 | * @author Aurélien Baudet | |
| 29 | * | |
| 30 | */ | |
| 31 | public class SimilarHtmlPattern extends StringValuePattern { | |
| 32 | private static final Logger LOG = LoggerFactory.getLogger(SimilarHtmlPattern.class); | |
| 33 | ||
| 34 | /** | |
| 35 | * Initialized with the expected HTML | |
| 36 | * | |
| 37 | * @param expectedValue | |
| 38 | * the expected HTML | |
| 39 | */ | |
| 40 | public SimilarHtmlPattern(@wiremock.com.fasterxml.jackson.annotation.JsonProperty("similarHtml") String expectedValue) { | |
| 41 | super(expectedValue); | |
| 42 | } | |
| 43 | ||
| 44 | @Override | |
| 45 | public MatchResult match(String value) { | |
| 46 | try { | |
| 47 | Diff diff = HtmlUtils.compare(expectedValue, value, false); | |
| 48 | return diff.hasDifferences() ? partialMatch(computeDistance(diff)) : exactMatch(); | |
| 49 | } catch(ComparisonException e) { | |
| 50 | // ignore because WireMock will try each value (body, multipart files, ...) so comparison may fail | |
| 51 | // due to not xml value | |
| 52 | LOG.debug("Can't match since ", e); | |
| 53 | return noMatch(); | |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | private double computeDistance(Diff diff) { | |
| 58 | final AtomicInteger count = new AtomicInteger(); | |
| 59 |
1
1. computeDistance : removed call to java/lang/Iterable::forEach → NO_COVERAGE |
diff.getDifferences().forEach((d) -> count.incrementAndGet()); |
| 60 |
1
1. computeDistance : replaced double return with 0.0d for fr/sii/ogham/testing/assertion/wiremock/SimilarHtmlPattern::computeDistance → NO_COVERAGE |
return count.doubleValue(); |
| 61 | } | |
| 62 | } | |
Mutations | ||
| 59 |
1.1 |
|
| 60 |
1.1 |