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

com.arextest.diff.compare.ScriptCompare Maven / Gradle / Ivy

There is a newer version: 0.2.15
Show newest version
package com.arextest.diff.compare;

import com.arextest.diff.handler.log.LogMarker;
import com.arextest.diff.handler.log.register.LogRegister;
import com.arextest.diff.model.compare.CompareContext;
import com.arextest.diff.model.exception.FindErrorException;
import com.arextest.diff.model.script.ScriptCompareConfig.ScriptMethod;
import com.arextest.diff.model.script.ScriptMethodContext;
import com.arextest.diff.model.script.ScriptSandbox;
import com.arextest.diff.utils.JacksonHelperUtil;
import java.util.List;
import javax.script.ScriptException;

public class ScriptCompare {

  public static boolean isScriptComparisonRequired(List fuzzyPath,
      CompareContext compareContext) {
    return compareContext.scriptCompareConfigMap != null
        && compareContext.scriptCompareConfigMap.containsKey(fuzzyPath);
  }

  // custom compare
  public static void scriptCompare(Object obj1, Object obj2, List fuzzyPath,
      CompareContext compareContext)
      throws ScriptException, NoSuchMethodException, FindErrorException {

    ScriptMethodContext context = new ScriptMethodContext();
    context.setBasePath(compareContext.currentNodeLeft);
    context.setTestPath(compareContext.currentNodeRight);

    Object baseValue = JacksonHelperUtil.objectMapper.convertValue(obj1, Object.class);
    Object testValue = JacksonHelperUtil.objectMapper.convertValue(obj2, Object.class);
    ScriptSandbox scriptSandbox = compareContext.scriptSandbox;
    ScriptMethod scriptMethod = compareContext.scriptCompareConfigMap.get(fuzzyPath);
    Boolean result = scriptSandbox.invoke(context, baseValue, testValue, scriptMethod);
    if (result) {
      return;
    }
    LogRegister.register(obj1, obj2, LogMarker.VALUE_DIFF, compareContext);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy