<commit_before>package org.jpos.jposext.isomsgaction.testing.service.support;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.betwixt.io.BeanWriter;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOUtil;
import org.jpos.jposext.isomsgaction.model.SimpleContextWrapper;
import org.jpos.jposext.isomsgaction.model.validation.ValidationError;
import org.jpos.jposext.isomsgaction.model.validation.ValidationErrorTypeEnum;
import org.jpos.jposext.isomsgaction.service.IISOMsgAction;
import org.jpos.jposext.isomsgaction.service.support.ISOMsgActionCheckField;
import org.jpos.jposext.isomsgaction.testing.model.ComparisonContext;
import org.jpos.jposext.isomsgaction.testing.model.ISOCmpDiff;
import org.jpos.jposext.isomsgaction.testing.model.ManualCheck;

public class MappingTest extends TestCase {

	class MyErreurValidation extends ValidationError {

		private MyErreurValidation() {
			super();
		}

		private MyErreurValidation(ValidationError validationError) {
			super();
			setTypeErreur(validationError.getTypeErreur());
			setParamName(validationError.getParamName());
		}

		private MyErreurValidation(ValidationErrorTypeEnum typeErreur,
				String paramName) {
			super(typeErreur, paramName);
		}

		private MyErreurValidation(ValidationErrorTypeEnum typeErreur) {
			super(typeErreur);
		}

		public int hashCode() {
			// On choisit les deux nombres impairs
			int result = 7;
			final int multiplier = 17;

			// Pour chaque attribut, on calcule le hashcode
			// que l'on ajoute au r�sultat apr�s l'avoir multipli�
			// par le nombre "multiplieur" :
			result = multiplier * result + this.getParamName().hashCode();
			result = multiplier * result + this.getTypeErreur().hashCode();

			// On retourne le r�sultat :
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			final MyErreurValidation other = (MyErreurValidation) obj;

			if (getParamName() == null) {
				if (other.getParamName() != null) {
					return false;
				}
			} else if (!(getParamName().equals(other.getParamName()))) {
				return false;
			}

			if (getTypeErreur() == null) {
				if (other.getTypeErreur() != null) {
					return false;
				}
			} else if (!(getTypeErreur().equals(other.getTypeErreur()))) {
				return false;
			}

			return true;
		}

	}

	private List<ISOMsg> lstSrcISOMsg = new ArrayList<ISOMsg>();

	private ISOMsg expectedISOMsg;

	private Map<String, Object> context = new HashMap<String, Object>();

	private Map<String, ManualCheck> mapManualChecks = new TreeMap<String, ManualCheck>();

	private Map<String, List<ValidationErrorTypeEnum>> mapExpectedErrsByIdPath;

	private IISOMsgAction action;

	private boolean showManualCheckReminder;

	private Map<String, Object> expectedContext = new HashMap<String, Object>();

	private List<String> expectedContextBinaryAttrs = new ArrayList<String>();
	
	private List<String> expectedContextNullAttrs = new ArrayList<String>();	

	@Override
	protected void runTest() throws Throwable {
		List<ISOCmpDiff> resList = new ArrayList<ISOCmpDiff>();

		try {
			ISOMsg targetMsg = new ISOMsg();

			ISOMsg[] tabISOMsg = new ISOMsg[1 + lstSrcISOMsg.size()];

			tabISOMsg[0] = targetMsg;

			for (int i = 1; i <= lstSrcISOMsg.size(); i++) {
				tabISOMsg[i] = lstSrcISOMsg.get(i - 1);
				dumpMessage(System.out, tabISOMsg[i],
						String.format("Source ISO Message [%d]", i));
			}

			if (null != expectedISOMsg) {
				dumpMessage(System.out, expectedISOMsg,
						"Expected ISO Message (excluding fields needing manual check)");
			}

			action.process(tabISOMsg, context);

			if (null != targetMsg) {
				dumpMessage(System.out, targetMsg, "Result ISO message");
			}

			ComparisonContext ctx = new ComparisonContext();
			ctx.setResList(resList);
			ctx.setMapManualChecks(mapManualChecks);

			Map<MyErreurValidation, ValidationError> mapErrsInattendues = new HashMap<MyErreurValidation, ValidationError>();
			Map<MyErreurValidation, ValidationError> mapErrsAttenduesTrouvees = new HashMap<MyErreurValidation, ValidationError>();
			Map<MyErreurValidation, ValidationError> mapErrsAttenduesNonTrouvees = new HashMap<MyErreurValidation, ValidationError>();

			List<ValidationError> lstErrsEnSortie = null;
			if (context
					.containsKey(ISOMsgActionCheckField.VALIDATION_ERRORS_LIST_ATTRNAME)) {
				lstErrsEnSortie = (List<ValidationError>) context
						.get(ISOMsgActionCheckField.VALIDATION_ERRORS_LIST_ATTRNAME);
				if (lstErrsEnSortie.size() > 0) {
					dumpErrorsList(
							System.out,
							(List<ValidationError>) context
									.get(ISOMsgActionCheckField.VALIDATION_ERRORS_LIST_ATTRNAME));
				}
				for (ValidationError validationError : lstErrsEnSortie) {
					if (mapExpectedErrsByIdPath.containsKey(validationError
							.getParamName())) {
						List<ValidationErrorTypeEnum> typesErreurAttendus = mapExpectedErrsByIdPath
								.get(validationError.getParamName());
						if (!typesErreurAttendus.contains(validationError
								.getTypeErreur())) {
							mapErrsInattendues.put(new MyErreurValidation(
									validationError), validationError);
						} else {
							mapErrsAttenduesTrouvees.put(
									new MyErreurValidation(validationError),
									validationError);
						}
					} else {
						mapErrsInattendues.put(new MyErreurValidation(
								validationError), validationError);
					}
				}

				for (Entry<String, List<ValidationErrorTypeEnum>> entry : mapExpectedErrsByIdPath
						.entrySet()) {
					String idPath = entry.getKey();
					for (ValidationErrorTypeEnum validationErrorTypeEnum : entry
							.getValue()) {
						MyErreurValidation errValid = new MyErreurValidation(
								validationErrorTypeEnum, idPath);
						if (!(mapErrsAttenduesTrouvees.containsKey(errValid))) {
							mapErrsAttenduesNonTrouvees.put(
									new MyErreurValidation(errValid), errValid);
						}
					}
				}

				for (Entry<MyErreurValidation, ValidationError> entry : mapErrsAttenduesNonTrouvees
						.entrySet()) {
					MyErreurValidation erreurValidation = entry.getKey();
					resList.add(new ISOCmpDiff(
							erreurValidation.getParamName(),
							String.format(
									"Field %s : a validation error [%s] was expected",
									erreurValidation.getParamName(),
									erreurValidation.getTypeErreur().name())));
				}

				for (Entry<MyErreurValidation, ValidationError> entry : mapErrsInattendues
						.entrySet()) {
					MyErreurValidation erreurValidation = entry.getKey();
					resList.add(new ISOCmpDiff(
							erreurValidation.getParamName(),
							String.format(
									"Field %s : validation error [%s] was not expected",
									erreurValidation.getParamName(),
									erreurValidation.getTypeErreur().name())));
				}
			}

			boolean expectedContextPopulationFailure = false;
			if (null != expectedContext) {
				for (Entry<String, Object> entry : expectedContext.entrySet()) {
					String ctxPath = entry.getKey();
					String expectedValue = (String) entry.getValue();

					Object effectiveObject = PropertyUtils.getProperty(
							new SimpleContextWrapper(context), ctxPath);

					boolean nullValueExpected = expectedContextNullAttrs.contains(ctxPath);
					if (nullValueExpected) {
						if (null != effectiveObject) {
							expectedContextPopulationFailure = true;
							resList.add(new ISOCmpDiff(
									ctxPath,
									String.format(
											"Attribute %s : null expected, but set=[%s]",
											ctxPath, effectiveObject.toString())));
						}
					} else {
						String effectiveValue = null;
						if (effectiveObject.getClass().isPrimitive()) {
							effectiveValue = (String) effectiveObject;
						} else if (effectiveObject instanceof String) {
							effectiveValue = (String) effectiveObject;
						} else {
							effectiveValue = effectiveObject.toString();
						}
						if (!(expectedValue.equals(effectiveValue))) {
							expectedContextPopulationFailure = true;
							boolean dumpHex = expectedContextBinaryAttrs
									.contains(ctxPath);
							if (null != effectiveValue) {
								resList.add(new ISOCmpDiff(
										ctxPath,
										String.format(
												"Attribute %s : expected %s=[%s], was %s=[%s]",
												ctxPath,
												dumpHex ? "(hex dump)" : "",
												dumpHex ? ISOUtil
														.hexdump(expectedValue
																.getBytes())
														: expectedValue,
												dumpHex ? "(hex dump)" : "",<commit_msg>Testing facility : enhance handling of null effective values<commit_after>package org.jpos.jposext.isomsgaction.testing.service.support;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.betwixt.io.BeanWriter;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOUtil;
import org.jpos.jposext.isomsgaction.model.SimpleContextWrapper;
import org.jpos.jposext.isomsgaction.model.validation.ValidationError;
import org.jpos.jposext.isomsgaction.model.validation.ValidationErrorTypeEnum;
import org.jpos.jposext.isomsgaction.service.IISOMsgAction;
import org.jpos.jposext.isomsgaction.service.support.ISOMsgActionCheckField;
import org.jpos.jposext.isomsgaction.testing.model.ComparisonContext;
import org.jpos.jposext.isomsgaction.testing.model.ISOCmpDiff;
import org.jpos.jposext.isomsgaction.testing.model.ManualCheck;

public class MappingTest extends TestCase {

	class MyErreurValidation extends ValidationError {

		private MyErreurValidation() {
			super();
		}

		private MyErreurValidation(ValidationError validationError) {
			super();
			setTypeErreur(validationError.getTypeErreur());
			setParamName(validationError.getParamName());
		}

		private MyErreurValidation(ValidationErrorTypeEnum typeErreur,
				String paramName) {
			super(typeErreur, paramName);
		}

		private MyErreurValidation(ValidationErrorTypeEnum typeErreur) {
			super(typeErreur);
		}

		public int hashCode() {
			// On choisit les deux nombres impairs
			int result = 7;
			final int multiplier = 17;

			// Pour chaque attribut, on calcule le hashcode
			// que l'on ajoute au r�sultat apr�s l'avoir multipli�
			// par le nombre "multiplieur" :
			result = multiplier * result + this.getParamName().hashCode();
			result = multiplier * result + this.getTypeErreur().hashCode();

			// On retourne le r�sultat :
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			final MyErreurValidation other = (MyErreurValidation) obj;

			if (getParamName() == null) {
				if (other.getParamName() != null) {
					return false;
				}
			} else if (!(getParamName().equals(other.getParamName()))) {
				return false;
			}

			if (getTypeErreur() == null) {
				if (other.getTypeErreur() != null) {
					return false;
				}
			} else if (!(getTypeErreur().equals(other.getTypeErreur()))) {
				return false;
			}

			return true;
		}

	}

	private List<ISOMsg> lstSrcISOMsg = new ArrayList<ISOMsg>();

	private ISOMsg expectedISOMsg;

	private Map<String, Object> context = new HashMap<String, Object>();

	private Map<String, ManualCheck> mapManualChecks = new TreeMap<String, ManualCheck>();

	private Map<String, List<ValidationErrorTypeEnum>> mapExpectedErrsByIdPath;

	private IISOMsgAction action;

	private boolean showManualCheckReminder;

	private Map<String, Object> expectedContext = new HashMap<String, Object>();

	private List<String> expectedContextBinaryAttrs = new ArrayList<String>();
	
	private List<String> expectedContextNullAttrs = new ArrayList<String>();	

	@Override
	protected void runTest() throws Throwable {
		List<ISOCmpDiff> resList = new ArrayList<ISOCmpDiff>();

		try {
			ISOMsg targetMsg = new ISOMsg();

			ISOMsg[] tabISOMsg = new ISOMsg[1 + lstSrcISOMsg.size()];

			tabISOMsg[0] = targetMsg;

			for (int i = 1; i <= lstSrcISOMsg.size(); i++) {
				tabISOMsg[i] = lstSrcISOMsg.get(i - 1);
				dumpMessage(System.out, tabISOMsg[i],
						String.format("Source ISO Message [%d]", i));
			}

			if (null != expectedISOMsg) {
				dumpMessage(System.out, expectedISOMsg,
						"Expected ISO Message (excluding fields needing manual check)");
			}

			action.process(tabISOMsg, context);

			if (null != targetMsg) {
				dumpMessage(System.out, targetMsg, "Result ISO message");
			}

			ComparisonContext ctx = new ComparisonContext();
			ctx.setResList(resList);
			ctx.setMapManualChecks(mapManualChecks);

			Map<MyErreurValidation, ValidationError> mapErrsInattendues = new HashMap<MyErreurValidation, ValidationError>();
			Map<MyErreurValidation, ValidationError> mapErrsAttenduesTrouvees = new HashMap<MyErreurValidation, ValidationError>();
			Map<MyErreurValidation, ValidationError> mapErrsAttenduesNonTrouvees = new HashMap<MyErreurValidation, ValidationError>();

			List<ValidationError> lstErrsEnSortie = null;
			if (context
					.containsKey(ISOMsgActionCheckField.VALIDATION_ERRORS_LIST_ATTRNAME)) {
				lstErrsEnSortie = (List<ValidationError>) context
						.get(ISOMsgActionCheckField.VALIDATION_ERRORS_LIST_ATTRNAME);
				if (lstErrsEnSortie.size() > 0) {
					dumpErrorsList(
							System.out,
							(List<ValidationError>) context
									.get(ISOMsgActionCheckField.VALIDATION_ERRORS_LIST_ATTRNAME));
				}
				for (ValidationError validationError : lstErrsEnSortie) {
					if (mapExpectedErrsByIdPath.containsKey(validationError
							.getParamName())) {
						List<ValidationErrorTypeEnum> typesErreurAttendus = mapExpectedErrsByIdPath
								.get(validationError.getParamName());
						if (!typesErreurAttendus.contains(validationError
								.getTypeErreur())) {
							mapErrsInattendues.put(new MyErreurValidation(
									validationError), validationError);
						} else {
							mapErrsAttenduesTrouvees.put(
									new MyErreurValidation(validationError),
									validationError);
						}
					} else {
						mapErrsInattendues.put(new MyErreurValidation(
								validationError), validationError);
					}
				}

				for (Entry<String, List<ValidationErrorTypeEnum>> entry : mapExpectedErrsByIdPath
						.entrySet()) {
					String idPath = entry.getKey();
					for (ValidationErrorTypeEnum validationErrorTypeEnum : entry
							.getValue()) {
						MyErreurValidation errValid = new MyErreurValidation(
								validationErrorTypeEnum, idPath);
						if (!(mapErrsAttenduesTrouvees.containsKey(errValid))) {
							mapErrsAttenduesNonTrouvees.put(
									new MyErreurValidation(errValid), errValid);
						}
					}
				}

				for (Entry<MyErreurValidation, ValidationError> entry : mapErrsAttenduesNonTrouvees
						.entrySet()) {
					MyErreurValidation erreurValidation = entry.getKey();
					resList.add(new ISOCmpDiff(
							erreurValidation.getParamName(),
							String.format(
									"Field %s : a validation error [%s] was expected",
									erreurValidation.getParamName(),
									erreurValidation.getTypeErreur().name())));
				}

				for (Entry<MyErreurValidation, ValidationError> entry : mapErrsInattendues
						.entrySet()) {
					MyErreurValidation erreurValidation = entry.getKey();
					resList.add(new ISOCmpDiff(
							erreurValidation.getParamName(),
							String.format(
									"Field %s : validation error [%s] was not expected",
									erreurValidation.getParamName(),
									erreurValidation.getTypeErreur().name())));
				}
			}

			boolean expectedContextPopulationFailure = false;
			if (null != expectedContext) {
				for (Entry<String, Object> entry : expectedContext.entrySet()) {
					String ctxPath = entry.getKey();
					String expectedValue = (String) entry.getValue();

					Object effectiveObject = PropertyUtils.getProperty(
							new SimpleContextWrapper(context), ctxPath);

					boolean nullValueExpected = expectedContextNullAttrs.contains(ctxPath);
					if (nullValueExpected) {
						if (null != effectiveObject) {
							expectedContextPopulationFailure = true;
							resList.add(new ISOCmpDiff(
									ctxPath,
									String.format(
											"Attribute %s : null expected, but set=[%s]",
											ctxPath, effectiveObject.toString())));
						}
					} else {
						String effectiveValue = null;
						if (effectiveObject.getClass().isPrimitive()) {
							effectiveValue = (String) effectiveObject;
						} else if (effectiveObject instanceof String) {
							effectiveValue = (String) effectiveObject;
						} else {
							if (null != effectiveObject) {
								effectiveValue = effectiveObject.toString();
							}
						}
						if (!(expectedValue.equals(effectiveValue))) {
							expectedContextPopulationFailure = true;
							boolean dumpHex = expectedContextBinaryAttrs
									.contains(ctxPath);
							if (null != effectiveValue) {
								resList.add(new ISOCmpDiff(
										ctxPath,
										String.format(
												"Attribute %s : expected %s=[%s], was %s=[%s]",
												ctxPath,
												dumpHex ? "(hex dump)" : "",
												dumpHex ? ISOUtil
														.hexdump(expectedValue
																.getBytes())
														: expectedValue,
												dumpHex ? "(hex dump)" : "",
{'fraction_non_alphanumeric': 0.10674188071635905, 'fraction_numerical': 0.0008762801905909415, 'mean_word_length': 3.7110423116615068, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4559327', 'n_tokens_mistral': 7161, 'n_tokens_neox': 5529, 'n_words': 1104}