<commit_before>
package edu.wustl.catissuecore.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import edu.common.dynamicextensions.bizlogic.BizLogicFactory;
import edu.common.dynamicextensions.domain.integration.EntityMap;
import edu.common.dynamicextensions.domain.integration.EntityMapCondition;
import edu.common.dynamicextensions.domain.integration.FormContext;
import edu.common.dynamicextensions.entitymanager.EntityManager;
import edu.common.dynamicextensions.entitymanager.EntityManagerInterface;
import edu.common.dynamicextensions.exception.DynamicExtensionsSystemException;
import edu.wustl.catissuecore.util.AssociatesCps;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.catissuecore.util.global.Utility;
import edu.wustl.common.bizlogic.DefaultBizLogic;
import edu.wustl.common.util.dbManager.DAOException;

/**
 * @author suhas_khot
 * Test cases for the Show_hide_forms based on collection protocol
 *
 */
public class TestShowHideForms extends TestCase
{

	/**
	 * Specify the name of the XML file. This file must be present at the path ShowHideForms/ under the project root directory
	 */
	private String XMLFileName = "./ShowHideForms/Show_Hide_Forms.xml";
	private String XMLFileName_ALL = "./ShowHideForms/Show_Hide_Forms_ALL.xml";
	private String XMLFileName_NONE = "./ShowHideForms/Show_Hide_Forms_NONE.xml";

	/**
	 * Test Show_hide_forms_based_on_CP for the normal XML file.
	 * throws DynamicExtensionsSystemException when it fails to show/hide forms based on Cps
	 */
	public void testShowHideForms()
	{
		try
		{
			//takes XML file name as argument
			String[] args = {XMLFileName_ALL};
			//calls the main method of AssociatesCps class 
			AssociatesCps.main(args);
			System.out
					.println("--------------- Test Case of ShowHideForms executed successful ------------");
		}
		catch (DynamicExtensionsSystemException e)
		{
			e.printStackTrace();
			fail("Exception occured");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			fail("Exception occured");
		}
	}

	/**
	 * Test case for associating all forms to CPs
	 */
	public void testForAssociateAllFormsToCP()
	{
		try
		{
			//takes XML file name as argument
			String[] args = {XMLFileName_ALL};
			XMLParser xmlParser = new XMLParser(XMLFileName);
			Collection<String> overrideColl=xmlParser.getCpIdVsOverride().values();
			for(String override : overrideColl)
			{
				assertEquals(override,"true");
			}	
			//calls the main method of AssociatesCps class
			AssociatesCps.main(args);
			DefaultBizLogic defaultBizLogic = BizLogicFactory.getDefaultBizLogic();
			Map<Long, Long> entityIdsVsContId = new HashMap<Long, Long>();
			//store containerIds and its Corresponding entityId
			entityIdsVsContId = Utility.getAllContainers();;
			if (entityIdsVsContId != null)
			{
				for (Long containerId : entityIdsVsContId.values())
				{
					if (containerId != null)
					{
						//retrives the entityMapList from the database based on container Id
						List<EntityMap> entityMapList = defaultBizLogic.retrieve(EntityMap.class
								.getName(), Constants.CONTAINERID, containerId);
						if (entityMapList != null && entityMapList.size() > 0)
						{
							EntityMap entityMap = entityMapList.get(0);
							Collection<FormContext> formContextColl = entityMap
									.getFormContextCollection();
							if (formContextColl != null)
							{
								for (FormContext formContext : formContextColl)
								{
									Collection<EntityMapCondition> entityMapCondColl = formContext
											.getEntityMapConditionCollection();
									if (!entityMapCondColl.isEmpty()
											|| entityMapCondColl.size() > 0)
									{
										int count=0;
//										throw new DynamicExtensionsSystemException(XMLFileName);
										for(EntityMapCondition condition: entityMapCondColl)
										{
											if(condition.getStaticRecordId()==Long.valueOf(-1) || condition.getStaticRecordId()==-1)
											{
												count=count+1;
											}
										}
										if(count==0)
										{
											throw new DynamicExtensionsSystemException(XMLFileName);
										}
									}
									if (entityMapCondColl.isEmpty()
											|| entityMapCondColl.size() <= 0)
									{
										throw new DynamicExtensionsSystemException(XMLFileName);
									}
								}
							}
						}
					}
				}
			}
			System.out
					.println("--------------- Test Case of ShowHideForms_ALL executed successful ------------");
		}
		catch (DynamicExtensionsSystemException e)
		{
			e.printStackTrace();
			fail("Exception occured");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			fail("Exception occured");
		}
	}

	/**
	 * Test case for disAssociating all forms from CPs
	 */
	public void testForAssociateNoFormsToCP()
	{
		try
		{
			//takes XML file name as argument
			String[] args = {XMLFileName_NONE};
			XMLParser xmlParser = new XMLParser(XMLFileName);
			Collection<String> overrideColl=xmlParser.getCpIdVsOverride().values();
			for(String override : overrideColl)
			{
				assertEquals(override,"true");
			}			
			//calls the main method of AssociatesCps class
			AssociatesCps.main(args);
			DefaultBizLogic defaultBizLogic = BizLogicFactory.getDefaultBizLogic();
			Map<Long, Long> entityIdsVsContId = new HashMap<Long, Long>();
			
			entityIdsVsContId = Utility.getAllContainers();
			if (entityIdsVsContId != null)
			{
				for (Long containerId : entityIdsVsContId.values())
				{
					if (containerId != null)
					{
						//retrives the entityMapList from the database based on container Id
						List<EntityMap> entityMapList = defaultBizLogic.retrieve(EntityMap.class
								.getName(), Constants.CONTAINERID, containerId);
						if (entityMapList != null && entityMapList.size() > 0)
						{
							EntityMap entityMap = entityMapList.get(0);
							Collection<FormContext> formContextColl = entityMap
									.getFormContextCollection();
							if (formContextColl != null)
							{
								for (FormContext formContext : formContextColl)
								{
									Collection<EntityMapCondition> entityMapCondColl = formContext
											.getEntityMapConditionCollection();
									if (!entityMapCondColl.isEmpty()
											|| entityMapCondColl.size() > 0)
									{
										int count=0;
										for(EntityMapCondition condition: entityMapCondColl)
										{
											if(condition.getStaticRecordId()==Long.valueOf(0) || condition.getStaticRecordId()==0)
											{
												count=count+1;<commit_msg>-- Bug 11822 : Performance Issue with the form associated with 12 k specimens<commit_after>
package edu.wustl.catissuecore.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import edu.common.dynamicextensions.bizlogic.BizLogicFactory;
import edu.common.dynamicextensions.domain.integration.EntityMap;
import edu.common.dynamicextensions.domain.integration.EntityMapCondition;
import edu.common.dynamicextensions.domain.integration.FormContext;
import edu.common.dynamicextensions.entitymanager.EntityManager;
import edu.common.dynamicextensions.entitymanager.EntityManagerInterface;
import edu.common.dynamicextensions.exception.DynamicExtensionsSystemException;
import edu.wustl.catissuecore.util.AssociatesCps;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.catissuecore.util.global.Utility;
import edu.wustl.common.bizlogic.DefaultBizLogic;
import edu.wustl.common.util.dbManager.DAOException;

/**
 * @author suhas_khot
 * Test cases for the Show_hide_forms based on collection protocol
 *
 */
public class TestShowHideForms extends TestCase
{

	/**
	 * Specify the name of the XML file. This file must be present at the path ShowHideForms/ under the project root directory
	 */
	private String XMLFileName = "./ShowHideForms/Show_Hide_Forms.xml";
	private String XMLFileName_ALL = "./ShowHideForms/Show_Hide_Forms_ALL.xml";
	private String XMLFileName_NONE = "./ShowHideForms/Show_Hide_Forms_NONE.xml";

	/**
	 * Test Show_hide_forms_based_on_CP for the normal XML file.
	 * throws DynamicExtensionsSystemException when it fails to show/hide forms based on Cps
	 */
	public void testShowHideForms()
	{
		try
		{
			//takes XML file name as argument
			String[] args = {XMLFileName_ALL};
			//calls the main method of AssociatesCps class 
			AssociatesCps.main(args);
			System.out
					.println("--------------- Test Case of ShowHideForms executed successful ------------");
		}
		catch (DynamicExtensionsSystemException e)
		{
			e.printStackTrace();
			fail("Exception occured");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			fail("Exception occured");
		}
	}

	/**
	 * Test case for associating all forms to CPs
	 */
	public void testForAssociateAllFormsToCP()
	{
		try
		{
			//takes XML file name as argument
			String[] args = {XMLFileName_ALL};
			XMLParser xmlParser = new XMLParser(XMLFileName);
			Collection<String> overrideColl=xmlParser.getCpIdVsOverride().values();
			for(String override : overrideColl)
			{
				assertEquals(override,"true");
			}	
			//calls the main method of AssociatesCps class
			AssociatesCps.main(args);
			DefaultBizLogic defaultBizLogic = BizLogicFactory.getDefaultBizLogic();
			Map<Long, Long> entityIdsVsContId = new HashMap<Long, Long>();
			//store containerIds and its Corresponding entityId
			entityIdsVsContId = Utility.getAllContainers();;
			if (entityIdsVsContId != null)
			{
				for (Long containerId : entityIdsVsContId.values())
				{
					if (containerId != null)
					{
						//retrives the entityMapList from the database based on container Id
						List<EntityMap> entityMapList = defaultBizLogic.retrieve(EntityMap.class
								.getName(), Constants.CONTAINERID, containerId);
						if (entityMapList != null && entityMapList.size() > 0)
						{
							EntityMap entityMap = entityMapList.get(0);
							Collection<FormContext> formContextColl = Utility.getFormContexts(entityMap.getId());
							if (formContextColl != null)
							{
								for (FormContext formContext : formContextColl)
								{
									Collection<EntityMapCondition> entityMapCondColl = Utility.getEntityMapConditions(formContext.getId());
									if (!entityMapCondColl.isEmpty()
											|| entityMapCondColl.size() > 0)
									{
										int count=0;
//										throw new DynamicExtensionsSystemException(XMLFileName);
										for(EntityMapCondition condition: entityMapCondColl)
										{
											if(condition.getStaticRecordId()==Long.valueOf(-1) || condition.getStaticRecordId()==-1)
											{
												count=count+1;
											}
										}
										if(count==0)
										{
											throw new DynamicExtensionsSystemException(XMLFileName);
										}
									}
									if (entityMapCondColl.isEmpty()
											|| entityMapCondColl.size() <= 0)
									{
										throw new DynamicExtensionsSystemException(XMLFileName);
									}
								}
							}
						}
					}
				}
			}
			System.out
					.println("--------------- Test Case of ShowHideForms_ALL executed successful ------------");
		}
		catch (DynamicExtensionsSystemException e)
		{
			e.printStackTrace();
			fail("Exception occured");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			fail("Exception occured");
		}
	}

	/**
	 * Test case for disAssociating all forms from CPs
	 */
	public void testForAssociateNoFormsToCP()
	{
		try
		{
			//takes XML file name as argument
			String[] args = {XMLFileName_NONE};
			XMLParser xmlParser = new XMLParser(XMLFileName);
			Collection<String> overrideColl=xmlParser.getCpIdVsOverride().values();
			for(String override : overrideColl)
			{
				assertEquals(override,"true");
			}			
			//calls the main method of AssociatesCps class
			AssociatesCps.main(args);
			DefaultBizLogic defaultBizLogic = BizLogicFactory.getDefaultBizLogic();
			Map<Long, Long> entityIdsVsContId = new HashMap<Long, Long>();
			
			entityIdsVsContId = Utility.getAllContainers();
			if (entityIdsVsContId != null)
			{
				for (Long containerId : entityIdsVsContId.values())
				{
					if (containerId != null)
					{
						//retrives the entityMapList from the database based on container Id
						List<EntityMap> entityMapList = defaultBizLogic.retrieve(EntityMap.class
								.getName(), Constants.CONTAINERID, containerId);
						if (entityMapList != null && entityMapList.size() > 0)
						{
							EntityMap entityMap = entityMapList.get(0);
							Collection<FormContext> formContextColl = Utility.getFormContexts(entityMap.getId());
							if (formContextColl != null)
							{
								for (FormContext formContext : formContextColl)
								{
									Collection<EntityMapCondition> entityMapCondColl = Utility.getEntityMapConditions(formContext.getId());
									if (!entityMapCondColl.isEmpty()
											|| entityMapCondColl.size() > 0)
									{
										int count=0;
										for(EntityMapCondition condition: entityMapCondColl)
										{
											if(condition.getStaticRecordId()==Long.valueOf(0) || condition.getStaticRecordId()==0)
											{
												count=count+1;
{'fraction_non_alphanumeric': 0.1057852491291837, 'fraction_numerical': 0.0029532030895047705, 'mean_word_length': 3.198029243483789, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '4440516', 'n_tokens_mistral': 5139, 'n_tokens_neox': 3825, 'n_words': 825}