All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tomakehurst.wiremock.matching.EqualToXmlPattern Maven / Gradle / Ivy

/*
 * Copyright (C) 2011 Thomas Akehurst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.tomakehurst.wiremock.matching;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.tomakehurst.wiremock.common.Xml;
import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xmlunit.XMLUnitException;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.*;
import org.xmlunit.placeholder.PlaceholderDifferenceEvaluator;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import static com.github.tomakehurst.wiremock.common.LocalNotifier.notifier;
import static com.google.common.base.Strings.isNullOrEmpty;
import static org.xmlunit.diff.ComparisonType.*;

public class EqualToXmlPattern extends StringValuePattern {

    private static List COUNTED_COMPARISONS = ImmutableList.of(
        ELEMENT_TAG_NAME,
        SCHEMA_LOCATION,
        NO_NAMESPACE_SCHEMA_LOCATION,
        NODE_TYPE,
        NAMESPACE_URI,
        TEXT_VALUE,
        PROCESSING_INSTRUCTION_TARGET,
        PROCESSING_INSTRUCTION_DATA,
        ELEMENT_NUM_ATTRIBUTES,
        ATTR_VALUE,
        CHILD_NODELIST_LENGTH,
        CHILD_LOOKUP,
        ATTR_NAME_LOOKUP
    );

    private final Document xmlDocument;
    private final Boolean enablePlaceholders;
    private final String placeholderOpeningDelimiterRegex;
    private final String placeholderClosingDelimiterRegex;
    private final DifferenceEvaluator diffEvaluator;

    public EqualToXmlPattern(@JsonProperty("equalToXml") String expectedValue) {
        this(expectedValue, null, null, null);
    }

    public EqualToXmlPattern(@JsonProperty("equalToXml") String expectedValue,
                             @JsonProperty("enablePlaceholders") Boolean enablePlaceholders,
                             @JsonProperty("placeholderOpeningDelimiterRegex") String placeholderOpeningDelimiterRegex,
                             @JsonProperty("placeholderClosingDelimiterRegex") String placeholderClosingDelimiterRegex) {
        super(expectedValue);
        xmlDocument = Xml.read(expectedValue);
        this.enablePlaceholders = enablePlaceholders;
        this.placeholderOpeningDelimiterRegex = placeholderOpeningDelimiterRegex;
        this.placeholderClosingDelimiterRegex = placeholderClosingDelimiterRegex;
        if (enablePlaceholders != null && enablePlaceholders) {
            diffEvaluator = DifferenceEvaluators.chain(IGNORE_UNCOUNTED_COMPARISONS,
                    new PlaceholderDifferenceEvaluator(placeholderOpeningDelimiterRegex, placeholderClosingDelimiterRegex));
        } else {
            diffEvaluator = IGNORE_UNCOUNTED_COMPARISONS;
        }
    }

    public String getEqualToXml() {
        return expectedValue;
    }

    @Override
    public String getExpected() {
        return Xml.prettyPrint(getValue());
    }

    public Boolean isEnablePlaceholders() {
        return enablePlaceholders;
    }

    public String getPlaceholderOpeningDelimiterRegex() {
        return placeholderOpeningDelimiterRegex;
    }

    public String getPlaceholderClosingDelimiterRegex() {
        return placeholderClosingDelimiterRegex;
    }

    @Override
    public MatchResult match(final String value) {
        return new MatchResult() {
            @Override
            public boolean isExactMatch() {
                if (isNullOrEmpty(value)) {
                    return false;
                }
                try {
                    Diff diff = DiffBuilder.compare(Input.from(expectedValue))
                            .withTest(value)
                            .withComparisonController(ComparisonControllers.StopWhenDifferent)
                            .ignoreWhitespace()
                            .ignoreComments()
                            .withDifferenceEvaluator(diffEvaluator)
                            .withNodeMatcher(new OrderInvariantNodeMatcher())
                            .withDocumentBuilderFactory(Xml.newDocumentBuilderFactory())
                            .build();

                    return !diff.hasDifferences();
                } catch (XMLUnitException e) {
                    notifier().info("Failed to process XML. " + e.getMessage() +
                            "\nExpected:\n" + expectedValue +
                            "\n\nActual:\n" + value);
                    return false;
                }
            }

            @Override
            public double getDistance() {
                if (isNullOrEmpty(value)) {
                    return 1.0;
                }

                final AtomicInteger totalComparisons = new AtomicInteger(0);
                final AtomicInteger differences = new AtomicInteger(0);

                Diff diff = null;
                try {
                    diff = DiffBuilder.compare(Input.from(expectedValue))
                            .withTest(value)
                            .ignoreWhitespace()
                            .ignoreComments()
                            .withDifferenceEvaluator(diffEvaluator)
                            .withComparisonListeners(new ComparisonListener() {
                                @Override
                                public void comparisonPerformed(Comparison comparison, ComparisonResult outcome) {
                                    if (COUNTED_COMPARISONS.contains(comparison.getType()) && comparison.getControlDetails().getValue() != null) {
                                        totalComparisons.incrementAndGet();
                                        if (outcome == ComparisonResult.DIFFERENT) {
                                            differences.incrementAndGet();
                                        }
                                    }
                                }
                            })
                            .withDocumentBuilderFactory(Xml.newDocumentBuilderFactory())
                            .build();
                } catch (XMLUnitException e) {
                    notifier().info("Failed to process XML. " + e.getMessage() +
                            "\nExpected:\n" + expectedValue +
                            "\n\nActual:\n" + value);
                    return 1.0;
                }

                notifier().info(
                        Joiner.on("\n").join(diff.getDifferences())
                );

                return differences.doubleValue() / totalComparisons.doubleValue();
            }
        };
    }

    private static final DifferenceEvaluator IGNORE_UNCOUNTED_COMPARISONS = new DifferenceEvaluator() {
        @Override
        public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) {
            if (COUNTED_COMPARISONS.contains(comparison.getType()) && comparison.getControlDetails().getValue() != null) {
                return outcome;
            }

            return ComparisonResult.EQUAL;
        }
    };


    private static final class OrderInvariantNodeMatcher extends DefaultNodeMatcher {
        @Override
        public Iterable> match(Iterable controlNodes, Iterable testNodes) {

            return super.match(
                    sort(controlNodes),
                    sort(testNodes)
            );
        }

        private static Iterable sort(Iterable nodes) {
            return FluentIterable.from(nodes).toSortedList(COMPARATOR);
        }

        private static final Comparator COMPARATOR = new Comparator() {
            @Override
            public int compare(Node node1, Node node2) {
                return node1.getLocalName().compareTo(node2.getLocalName());
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy