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

com.github.siwenyan.common.MapsComparer Maven / Gradle / Ivy

There is a newer version: 1.25.a
Show newest version
package com.github.siwenyan.common;

import org.junit.Assert;

import java.util.*;

public class MapsComparer {

    private final List> mapsExpected;
    private final List> mapsActual;
    private final ScenarioLogger scenarioLogger;
    private List ignoredKeys = new ArrayList<>(0);

    public MapsComparer(List> mapsExpected, List> mapsActual, ScenarioLogger scenarioLogger) {
        this.mapsExpected = mapsExpected;
        this.mapsActual = mapsActual;
        this.scenarioLogger = scenarioLogger;
    }

    public void setIgnoredKeys(String... keys) {
        ignoredKeys = Arrays.asList(keys);
    }

    public void assertMatches() {
        assertMatches(true, null);
    }

    public void assertMatches(boolean strict) {
        assertMatches(strict, null);
    }

    public void assertMatches(boolean strict, Map mapping) {
        if (strict) {
            Assert.assertEquals("Number of rows mismatch.", mapsExpected.size(), mapsActual.size());
        } else {
            Assert.assertTrue("Too few actual rows.", mapsExpected.size() <= mapsActual.size());
        }

        if (strict) {
            if (null == mapping) {
                mapping = new LinkedHashMap<>(mapsExpected.size());
                for (int i = 0; i < mapsExpected.size(); i++) {
                    mapping.put(i + 1, i + 1);
                }
            }

            for (int rowExpected : mapping.keySet()) {
                int rowActual = mapping.get(rowExpected);

                Map mapExpected = new LinkedHashMap<>(mapsExpected.get(rowExpected - 1));
                Map mapActual = new LinkedHashMap<>(mapsActual.get(rowActual - 1));
                assertMatches(strict, mapExpected, mapActual, ignoredKeys);
            }
        } else {
            int rowExpected = 1;
            int rowActual = 1;

            while (rowExpected <= mapsExpected.size()) {
                Map mapExpected = new LinkedHashMap<>(mapsExpected.get(rowExpected - 1));
                Sys.out.pl("Expected map #" + rowExpected + ": " + mapExpected);
                
                boolean matches = false;
                while (rowActual <= mapsActual.size()) {
                    Map mapActual = new LinkedHashMap<>(mapsActual.get(rowActual - 1));
                    Sys.out.pl("Actual map #" + rowActual + ": " + mapActual);
                    try {
                        assertMatches(strict, mapExpected, mapActual, ignoredKeys);
                        matches = true;
                        break;
                    } catch (Throwable t) {

                    }
                    rowActual++;
                }
                Assert.assertTrue("No match.", matches);
                
                rowExpected++;
            }

        }
    }

    public void assertMatches(boolean strict, String anchorKey, Map mapping) {
        Map> indexedMapsExpected = DataTableTools.dataTableAsMapsIndexedByKeyHeader(anchorKey, mapsExpected);
        Map> indexedMapsActual = DataTableTools.dataTableAsMapsIndexedByKeyHeader(anchorKey, mapsActual);
        Set keysExpected = indexedMapsExpected.keySet();
        Set keysActual = indexedMapsActual.keySet();

        if (strict) {
            Assert.assertEquals("Keys mismatch.", keysExpected, keysActual);
        } else {
            Assert.assertTrue("Too few actual keys.", keysExpected.size() <= keysActual.size());
        }

        if (null == mapping) {
            mapping = new LinkedHashMap<>(keysExpected.size());
            for (String key : keysExpected) {
                mapping.put(key, key);
            }
        }

        int row = 1;
        for (String keyExpected : mapping.keySet()) {
            scenarioLogger.warn("\r\n>>>>>Comparing Row " + (row++));
            String keyActual = mapping.get(keyExpected);

            Map mapExpected = new LinkedHashMap<>(indexedMapsExpected.get(keyExpected));
            Map mapActual = new LinkedHashMap<>(indexedMapsActual.get(keyActual));
            List ignoredKeyList = new ArrayList<>(this.ignoredKeys);
            ignoredKeyList.add(anchorKey);
            assertMatches(strict, mapExpected, mapActual, ignoredKeyList);
        }

    }

    private void assertMatches(boolean strict, Map mapExpected, Map mapActual, List ignoredKeyList) {
        for (String ignoredKey : ignoredKeyList) {
            mapExpected.remove(ignoredKey.trim());
            mapActual.remove(ignoredKey.trim());
        }
        scenarioLogger.warn("Row Expected: " + mapExpected.toString());
        scenarioLogger.warn("Row Actual: " + mapActual.toString());


        List expectedHeaders = new ArrayList(mapExpected.keySet());
        List actualHeaders = new ArrayList(mapActual.keySet());
        scenarioLogger.warn("Headers Expected: " + expectedHeaders.toString());
        scenarioLogger.warn("Headers Actual: " + actualHeaders.toString());
        if (strict) {
            Assert.assertEquals("Headers mismatch.", expectedHeaders, actualHeaders);
        } else {
            Assert.assertTrue("Headers Actual is missing some values in Headers Expected", actualHeaders.containsAll(expectedHeaders));
        }

        for (String key : mapExpected.keySet()) {
            String valueActual0 = mapActual.get(key);
            String valueExpected0 = mapExpected.get(key);
            WhiteBoard.getInstance().putString(owner(), "actual", valueActual0);

            String valueActual1 = WhiteBoard.getInstance().resolve(owner(), valueActual0);
            String valueExpected1 = WhiteBoard.getInstance().resolve(owner(), valueExpected0);

            String valueActual2 = StringTools.isEmpty(valueActual1) ? "" : valueActual1;
            String valueExpected2 = StringTools.isEmpty(valueExpected1) ? "" : valueExpected1;

            scenarioLogger.warn("\r\nComparing Column " + key + ":");
            scenarioLogger.warn("\tExpected: " + valueExpected2 + (valueExpected2.equals(valueExpected0) ? "" : ("<<" + valueExpected0)));
            scenarioLogger.warn("\tActual: " + valueActual2 + (valueActual2.equals(valueActual0) ? "" : ("<<" + valueActual0)));

            if (strict) {
                Assert.assertEquals("Values not equal", valueExpected2, valueActual2);
            } else {
                Assert.assertTrue("Values mismatch: Expected=[" + valueExpected2 + "] Actual=[" + valueActual2 + "]",
                        valueActual2.equalsIgnoreCase(valueExpected2) || valueActual2.replaceAll("\n", " ").matches(valueExpected2));
            }
            scenarioLogger.warn("OK\r\n");
        }
    }

    private String owner() {
        return scenarioLogger.getOwner();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy