<commit_before>package alma.acs.tmcdb.compare;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.collections.CollectionUtils;
import org.custommonkey.xmlunit.XMLTestCase;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.omg.CORBA.ORB;

import alma.acs.util.ACSPorts;
import alma.cdbErrType.CDBFieldDoesNotExistEx;
import alma.cdbErrType.CDBRecordDoesNotExistEx;
import alma.cdbErrType.WrongCDBDataTypeEx;

import com.cosylab.CDB.DAL;
import com.cosylab.CDB.DAO;
import com.cosylab.CDB.DAOOperations;
import com.cosylab.CDB.JDAL;
import com.cosylab.CDB.JDALHelper;
import com.cosylab.cdb.client.CDBAccess;
import com.cosylab.cdb.client.DAOProxy;

/**
 * @author jschwarz
 * Compares the output from two different DAL implementations
 * Assuming that ACS is not running on instance 0, the following two commands should be issued before
 * running this test:
 * 
 * hibernateCdbJDal -configName TEST0 -loadXMLCDB -memory  # This should also be done with Oracle every now and then
 * cdbjDAL -OAport 3013
 */
public class TestHighLevelNodes extends XMLTestCase {
	
	private ORB orb;
	
	private final CDBAccess xmlAccess;
	private final CDBAccess rdbAccess;
	private final Logger logger;
	private final JDAL xmlDAL;
	private final JDAL rdbDAL;
	private String xmlXml;
	private String rdbXml;

	public TestHighLevelNodes(String name) {
		super(name);
		String rdbIOR = "corbaloc::" + ACSPorts.getIP() + ":" + "3012/CDB"; // ACSPorts.getCDBPort() + "/CDB";
		String xmlIOR = "corbaloc::" + ACSPorts.getIP() + ":" + "3013/CDB"; // ACSPorts.getCDBPort() + "/CDB";
		String args[] = {};
		logger = Logger.getAnonymousLogger();
		Properties props = new Properties();
		props.setProperty("org.omg.CORBA.ORBClass","org.jacorb.orb.ORB");
		props.setProperty("org.omg.CORBA.ORBSingletonClass","org.jacorb.orb.ORBSingleton");
		orb = ORB.init(args, props);
		xmlDAL = JDALHelper.narrow(orb.string_to_object(xmlIOR));
		xmlAccess = new CDBAccess(orb,logger);
		xmlAccess.setDAL(xmlDAL);
		rdbDAL = JDALHelper.narrow(orb.string_to_object(rdbIOR));
		rdbAccess = new CDBAccess(orb,logger);
		rdbAccess.setDAL(rdbDAL);
	}

	protected void setUp() throws Exception {
		super.setUp();
		xmlXml = xmlDAL.get_DAO("MACI/");
		rdbXml = rdbDAL.get_DAO("MACI/");
	}
	
	public void testMACI_Managers() throws Exception {
		HashSet<String> xmlNodes = new HashSet<String>(Arrays.asList(xmlDAL.list_nodes("MACI/Managers").split(" ")));
		HashSet<String> rdbNodes = new HashSet<String>(Arrays.asList(rdbDAL.list_nodes("MACI/Managers").split(" ")));
		logger.info("XML: "+xmlNodes.toString()+"; TMCDB: "+rdbNodes.toString());
		assertEquals(xmlNodes,rdbNodes);
		for (Iterator<String> iterator = xmlNodes.iterator(); iterator.hasNext();) {
			String xmlstring = "MACI/Managers/"+(String) iterator.next();
			DAO xmlDao = xmlDAL.get_DAO_Servant(xmlstring);
			DAO rdbDao = rdbDAL.get_DAO_Servant(xmlstring);
			examineLoggingConfig(xmlDao,rdbDao);
			assertEquals(xmlDao.get_string("Startup"),rdbDao.get_string("Startup"));
			assertEquals(xmlDao.get_string("ServiceComponents"),rdbDao.get_string("ServiceComponents"));
//			String sx = null;
//			boolean xbool = true;
//			try {
//				sx = xmlDao.get_string("ServiceDaemons");
//			} catch (CDBFieldDoesNotExistEx e) {
//				xbool = false;
//			}
//			String sr = null;
//			try {
//				sr = rdbDao.get_string("ServiceDaemons");
//			} catch (CDBFieldDoesNotExistEx e) {
//				if (xbool) fail("Service Daemons: XML CDB has value: "+sx+" but TMCDB can't find the field.");
//				continue; // Neither CDB can find it; move to next property
//			}
//			if (!xbool) fail("Service Daemons: TMCDB has value: "+sr+" but XML CDB can't find the field.");
//			assertEquals(sx,sr); // TODO: Redo this once Matej's implementation is complete
		}
	}
	
	public void testMACI_Containers() throws Exception {
		String[] xmlNodes = getSubNodes(xmlDAL, "MACI/Containers");
		String[] rdbNodes = getSubNodes(rdbDAL, "MACI/Containers");
		logger.info("XML: "+xmlNodes.toString()+"; TMCDB: "+rdbNodes.toString());
//		assertEquals(xmlNodes,rdbNodes);

		for (int i = 0; i < xmlNodes.length; i++) {
			String xmlstring = "MACI/Containers/"+xmlNodes[i];
			System.out.println(xmlstring);
			DAO xmlDao = xmlDAL.get_DAO_Servant(xmlstring);
			DAO rdbDao = rdbDAL.get_DAO_Servant(xmlstring);
			// check if real config, otherwice skip
			if (readLong(xmlDao, "LoggingConfig/minLogLevel", -1) < 0)
				continue;
			assertEquals(xmlDao.get_string("Autoload"),rdbDao.get_string("Autoload"));
			examineDeployInfo(xmlstring);
			examineLoggingConfig(xmlDao,rdbDao);

			assertEquals(xmlDao.get_string("ImplLang"),rdbDao.get_string("ImplLang"));
			assertEquals(xmlDao.get_double("Timeout"),rdbDao.get_double("Timeout"));
			checkEqualsBoolean("UseIFR", xmlDao, rdbDao);
			assertEquals(xmlDao.get_long("ManagerRetry"),rdbDao.get_long("ManagerRetry"));
			String str = "PingInterval";
			checkLongOptionalNoDefault(str, xmlDao, rdbDao);
//			assertEquals(xmlDao.get_string("DALtype"),rdbDao.get_string("DALtype")); // Get rid of this attribute in XML CDB??
			assertEquals(xmlDao.get_long("ServerThreads"),rdbDao.get_long("ServerThreads"));
			checkEqualsBoolean("Recovery",xmlDao, rdbDao); // Optional Boolean, but it works!

		}
	}
	public void testMACI_Components() throws Exception {
		String[] xmlAllNodes = retrieveComponentsList(xmlAccess);
		String[] rdbAllNodes = retrieveComponentsList(rdbAccess);
		logger.info("XML: "+xmlAllNodes.length+"; TMCDB: "+rdbAllNodes.length);
		HashSet<String> xmlNodes = new HashSet<String>(Arrays.asList(xmlAllNodes));
		HashSet<String> rdbNodes = new HashSet<String>(Arrays.asList(rdbAllNodes));
		logger.info("XML: "+xmlNodes.size()+"; TMCDB: "+rdbNodes.size());		
		logger.info("XML: "+xmlNodes.toString()+";\n TMCDB: "+rdbNodes.toString());

		assertTrue(xmlNodes.equals(rdbNodes));
		assertTrue(CollectionUtils.isEqualCollection(xmlNodes, rdbNodes));
		<commit_msg>Some temporary changes to get printout.<commit_after>package alma.acs.tmcdb.compare;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.collections.CollectionUtils;
import org.custommonkey.xmlunit.XMLTestCase;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.omg.CORBA.ORB;

import alma.acs.util.ACSPorts;
import alma.cdbErrType.CDBFieldDoesNotExistEx;
import alma.cdbErrType.CDBRecordDoesNotExistEx;
import alma.cdbErrType.WrongCDBDataTypeEx;

import com.cosylab.CDB.DAL;
import com.cosylab.CDB.DAO;
import com.cosylab.CDB.DAOOperations;
import com.cosylab.CDB.JDAL;
import com.cosylab.CDB.JDALHelper;
import com.cosylab.cdb.client.CDBAccess;
import com.cosylab.cdb.client.DAOProxy;

/**
 * @author jschwarz
 * Compares the output from two different DAL implementations
 * Assuming that ACS is not running on instance 0, the following two commands should be issued before
 * running this test:
 * 
 * hibernateCdbJDal -configName TEST0 -loadXMLCDB -memory  # This should also be done with Oracle every now and then
 * cdbjDAL -OAport 3013
 */
public class TestHighLevelNodes extends XMLTestCase {
	
	private ORB orb;
	
	private final CDBAccess xmlAccess;
	private final CDBAccess rdbAccess;
	private final Logger logger;
	private final JDAL xmlDAL;
	private final JDAL rdbDAL;
	private String xmlXml;
	private String rdbXml;

	public TestHighLevelNodes(String name) {
		super(name);
		String rdbIOR = "corbaloc::" + ACSPorts.getIP() + ":" + "3012/CDB"; // ACSPorts.getCDBPort() + "/CDB";
		String xmlIOR = "corbaloc::" + ACSPorts.getIP() + ":" + "3013/CDB"; // ACSPorts.getCDBPort() + "/CDB";
		String args[] = {};
		logger = Logger.getAnonymousLogger();
		Properties props = new Properties();
		props.setProperty("org.omg.CORBA.ORBClass","org.jacorb.orb.ORB");
		props.setProperty("org.omg.CORBA.ORBSingletonClass","org.jacorb.orb.ORBSingleton");
		orb = ORB.init(args, props);
		xmlDAL = JDALHelper.narrow(orb.string_to_object(xmlIOR));
		xmlAccess = new CDBAccess(orb,logger);
		xmlAccess.setDAL(xmlDAL);
		rdbDAL = JDALHelper.narrow(orb.string_to_object(rdbIOR));
		rdbAccess = new CDBAccess(orb,logger);
		rdbAccess.setDAL(rdbDAL);
	}

	protected void setUp() throws Exception {
		super.setUp();
		xmlXml = xmlDAL.get_DAO("MACI/");
		rdbXml = rdbDAL.get_DAO("MACI/");
	}
	
	public void testMACI_Managers() throws Exception {
		HashSet<String> xmlNodes = new HashSet<String>(Arrays.asList(xmlDAL.list_nodes("MACI/Managers").split(" ")));
		HashSet<String> rdbNodes = new HashSet<String>(Arrays.asList(rdbDAL.list_nodes("MACI/Managers").split(" ")));
		logger.info("XML: "+xmlNodes.toString()+"; TMCDB: "+rdbNodes.toString());
		assertEquals(xmlNodes,rdbNodes);
		for (Iterator<String> iterator = xmlNodes.iterator(); iterator.hasNext();) {
			String xmlstring = "MACI/Managers/"+(String) iterator.next();
			DAO xmlDao = xmlDAL.get_DAO_Servant(xmlstring);
			DAO rdbDao = rdbDAL.get_DAO_Servant(xmlstring);
			examineLoggingConfig(xmlDao,rdbDao);
			assertEquals(xmlDao.get_string("Startup"),rdbDao.get_string("Startup"));
			assertEquals(xmlDao.get_string("ServiceComponents"),rdbDao.get_string("ServiceComponents"));
			String sx = null;
			boolean xbool = true;
			try {
				sx = xmlDao.get_string("ServiceDaemons");
			} catch (CDBFieldDoesNotExistEx e) {
				xbool = false;
			}
			String sr = null;
			try {
				sr = rdbDao.get_string("ServiceDaemons");
			} catch (CDBFieldDoesNotExistEx e) {
				if (xbool) fail("Service Daemons: XML CDB has value: "+sx+" but TMCDB can't find the field.");
				continue; // Neither CDB can find it; move to next property
			}
			if (!xbool) fail("Service Daemons: TMCDB has value: "+sr+" but XML CDB can't find the field.");
			assertEquals(sx,sr); // TODO: Redo this once Matej's implementation is complete
		}
	}
	
	public void testMACI_Channels() throws Exception {
		String[] xmlNodes = xmlDAL.list_nodes("MACI/Channels").split(" ");
		String[] rdbNodes = rdbDAL.list_nodes("MACI/Channels").split(" ");
		System.out.println("list_nodes gives for XML: "+xmlNodes.length+"; and for RDB: "+rdbNodes.length);
		// TODO: Fix this
		//assertEquals(xmlNodes.length,rdbNodes.length);
		String[] xmlDaos = xmlDAL.list_daos("MACI/Channels").split(" ");
		String[] rdbDaos = rdbDAL.list_daos("MACI/Channels").split(" ");
		System.out.println("list_daos gives for XML: "+xmlDaos.length+"; and for RDB: "+rdbDaos.length);
		System.out.println("\n******** list_daos output ********");
		System.out.println("**** XML CDB ****");
		for (int i = 0; i < xmlDaos.length; i++) {
			System.out.println(xmlDaos[i]);
		}
		System.out.println("\n**** RDB CDB ****");

		for (int i = 0; i < rdbDaos.length; i++) {
			System.out.println(rdbDaos[i]);
		}

		System.out.println("\n\n******** list_nodes output ********");
		System.out.println("**** XML CDB ****");
		for (int i = 0; i < xmlNodes.length; i++) {
			System.out.println(xmlNodes[i]);
		}
		System.out.println("\n**** RDB CDB ****");
		for (int i = 0; i < rdbNodes.length; i++) {
			System.out.println(rdbNodes[i]);
		}

		//assertEquals(xmlDaos.length,rdbDaos.length);
	}
	
	public void testMACI_Containers() throws Exception {
		String[] xmlNodes = getSubNodes(xmlDAL, "MACI/Containers");
		String[] rdbNodes = getSubNodes(rdbDAL, "MACI/Containers");
		logger.info("XML: "+xmlNodes.toString()+"; TMCDB: "+rdbNodes.toString());
//		assertEquals(xmlNodes,rdbNodes);

		for (int i = 0; i < xmlNodes.length; i++) {
			String xmlstring = "MACI/Containers/"+xmlNodes[i];
			System.out.println(xmlstring);
			DAO xmlDao = xmlDAL.get_DAO_Servant(xmlstring);
			DAO rdbDao = rdbDAL.get_DAO_Servant(xmlstring);
			// check if real config, otherwice skip
			if (readLong(xmlDao, "LoggingConfig/minLogLevel", -1) < 0)
				continue;
			assertEquals(xmlDao.get_string("Autoload"),rdbDao.get_string("Autoload"));
			examineDeployInfo(xmlstring);
			examineLoggingConfig(xmlDao,rdbDao);

			assertEquals(xmlDao.get_string("ImplLang"),rdbDao.get_string("ImplLang"));
			assertEquals(xmlDao.get_double("Timeout"),rdbDao.get_double("Timeout"));
			checkEqualsBoolean("UseIFR", xmlDao, rdbDao);
			assertEquals(xmlDao.get_long("ManagerRetry"),rdbDao.get_long("ManagerRetry"));
			String str = "PingInterval";
			checkLongOptionalNoDefault(str, xmlDao, rdbDao);
//			assertEquals(xmlDao.get_string("DALtype"),rdbDao.get_string("DALtype")); // Get rid of this attribute in XML CDB??
			assertEquals(xmlDao.get_long("ServerThreads"),rdbDao.get_long("ServerThreads"));
			checkEqualsBoolean("Recovery",xmlDao, rdbDao); // Optional Boolean, but it works!

		}
	}
	public void testMACI_Components() throws Exception {
		String[] xmlAllNodes = retrieveComponentsList(xmlAccess);
		String[] rdbAllNodes = retrieveComponentsList(rdbAccess);
		assertEquals(xmlAllNodes.length,rdbAllNodes.length);
		HashSet<String> xmlNodes = new HashSet<String>(Arrays.asList(xmlAllNodes));
		HashSet<String> rdbNodes = new HashSet<String>(Arrays.asList(rdbAllNodes));
		logger.info("XML: "+xmlNodes.toString()+";\n TMCDB: "+rdbNodes.toString());

		assertTrue(xmlNodes.equals(rdbNodes));
		assertTrue(CollectionUtils.isEqualCollection(xmlNodes, rdbNodes));
		
{'fraction_non_alphanumeric': 0.14650778838561482, 'fraction_numerical': 0.0027277295240829805, 'mean_word_length': 5.267206477732794, 'pattern_counts': {'":': 4, '<': 29, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 129}, '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': '5328528', 'n_tokens_mistral': 5236, 'n_tokens_neox': 4928, 'n_words': 930}