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

com.kenshoo.pl.simulation.internal.ResultComparator Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.pl.simulation.internal;

import com.kenshoo.pl.entity.*;
import com.kenshoo.pl.simulation.ComparisonMismatch;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static java.util.stream.Collectors.toList;
import static org.jooq.lambda.Seq.seq;


public class ResultComparator> {

    private final Collection> inspectedFields;

    public ResultComparator(Collection> inspectedFields) {
        this.inspectedFields = inspectedFields;
    }

    public List> findMismatches(
            Iterable> simulatedResults,
            Iterable actualDbResults) {

        return seq(simulatedResults).zip(actualDbResults)
                .map(pair -> findMismatch(pair.v1, pair.v2))
                .filter(Optional::isPresent)
                .map(Optional::get)
                .collect(toList());
    }

    private Optional> findMismatch(SimulatedResult simulatedResult, ActualResult actualResult) {

        if (simulatedResult.isError() && actualResult.isError()) {
            return Optional.empty();
        }

        if (simulatedResult.isSuccess() && actualResult.isError()) {
            return Optional.of(new ComparisonMismatch<>(simulatedResult.getId(), "Simulated mutation was successful but real mutation finished with the following error: " + actualResult.getErrorDescription()));
        }

        if (simulatedResult.isError() && actualResult.isSuccess()) {
            return Optional.of(new ComparisonMismatch<>(simulatedResult.getId(), "Real mutation was successful but simulated mutation finished with the following errors: " + simulatedResult.getErrors()));
        }

        final var mismatchingFields = inspectedFields.stream()
                .map(field -> getFieldMismatch(field, simulatedResult.getCommand(), actualResult))
                .filter(Optional::isPresent)
                .map(Optional::get)
                .collect(toList());

        return mismatchingFields.isEmpty()
                ? Optional.empty()
                : Optional.of(new ComparisonMismatch<>(simulatedResult.getId(), "Found field mismatch: " + mismatchingFields));
    }

    private Optional getFieldMismatch(EntityField field, EntityChange simulated, ActualResult actualResult) {

        if (!simulated.isFieldChanged(field) && !actualResult.isReallyChanged(field)) {
            return Optional.empty();
        }

        if (!simulated.isFieldChanged(field) && actualResult.isReallyChanged(field)) {
            return Optional.of("Field \"" + field + "\" is not populated in the simulated command although it was changed in DB");
        }

        return Objects.equals(simulated.get(field), actualResult.getFinalValue(field))
                ? Optional.empty()
                : Optional.of("Field \"" + field + "\" has mismatch values. Simulated: \"" + simulated.get(field) + "\"" + ", Actual: \"" + actualResult.getFinalValue(field) + "\"");
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy