using System;
using System.Collections;
using System.Data;
using System.Threading;
using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Users;
using edu.jhu.Cas.Util;
using edu.jhu.Cas.Servers;

/**
  $Id: QTests.cs,v 1.2 2004/09/03 16:16:43 nli Exp $
  $Revision: 1.2 $
  $Date: 2004/09/03 16:16:43 $
  */

namespace edu.jhu.Cas.Test {

	public class TestObject{
		public string qry,title,cleanQry,target,verifyQry,verifyToken;
	}


	public class QTests {
		
		private ArrayList TestList;
		private CasJobs cJobs = new CasJobs();
		private long wsid = 193424723;
		private string target = "DR1";

		public QTests(){TestList = new ArrayList();}

		public void AddTest(string qry, string title,string target,
			string verifyQuery,string verifyToken,string cleanQry){
			TestObject to = new TestObject();
			to.qry = qry;
			to.title = title;
			to.target = target;
			to.cleanQry = cleanQry;
			to.verifyQry = verifyQuery;
			to.verifyToken = verifyToken;
			TestList.Add(to);
		}

		public void RunAllTests(){
			foreach(object o in TestList)
				RunTest((TestObject)o);
			Console.WriteLine("\nAll tests complete!");
		}
		protected void RunTest(TestObject to){
			Console.WriteLine("\r'"+to.title+"'");
			//test short queue
			Console.Write("Submitting Short...");
			cJobs.SubmitShortJob(wsid,to.qry,to.target,to.title,0);//run
			//verify
			string verify = "";
			if(to.verifyQry != null){
				Console.Write("\rVerifying Short.");
				verify = cJobs.SubmitShortJob(wsid,to.verifyQry,"MyDB","VERIFY: "+to.title,0).
					Tables[0].Rows[0][0].ToString();
				if(verify.CompareTo(to.verifyToken) != 0)
					throw new Exception("FOUND "+verify+", SHOULD BE "+to.verifyToken);
			}
			if(to.cleanQry != null){
				Console.Write("\rCleaning Short...");
				cJobs.SubmitShortJob(wsid,to.cleanQry,"MYDB","DROPPING "+to.title,0);//clean
			}
			Console.Write("\rSubmitting Long");
			//test long queue
			long jobid = cJobs.SubmitJob(wsid,to.qry,to.target,to.title,90,0);//run
			int status = -1;
			while((status = cJobs.StatusJob(wsid,jobid)) < 4){
				Console.Write(".");
				Thread.Sleep(3*1000);
			}
			if(status == 4){
				Job j = cJobs.FindJob(wsid,"jobid = "+jobid)[0];
				throw new Exception(">>>"+j.Message);
			}
			//verify
			if(to.verifyQry != null){
				Console.Write("\rVerifying Long...");
				verify = cJobs.SubmitShortJob(wsid,
					to.verifyQry,"MyDB","VERIFY: "+to.title,0).Tables[0].Rows[0][0].ToString();
				if(verify.CompareTo(to.verifyToken) != 0)
					throw new Exception("FOUND "+verify+", SHOULD BE "+to.verifyToken);
			}
			//clean
			if(to.cleanQry != null){
				Console.Write("\rCleaning Long...");
				cJobs.SubmitShortJob(wsid,to.cleanQry,"MyDB","DROPPING "+to.title,0);
			}
			Console.Write("\rComplete!                                          ");
		}
	}
}

/**
 * $Log: QTests.cs,v $
 * Revision 1.2  2004/09/03 16:16:43  nli
 * --output tests added
 * --output bug fixed
 * --groups page dead links removed
 * --groups page minor style adjustment
 *
 * Revision 1.1  2004/09/02 18:48:40  nli
 * --unit tests
 * --massive debugging, many changes to execution
 * --new help page
 *
 * Revision 1.2  2004/08/16 20:46:29  nli
 * updated casjobscl page
 *
 * Revision 1.1.1.1  2004/04/27 16:38:31  cvsuser
 * import CasJobs to FERMI
 *
 * Revision 1.1.1.1  2004/02/18 16:38:28  nli
 * new version
 *
 * Revision 1.1.1.1  2004/01/25 18:16:58  nli
 * first
 *
 * Revision 1.1.1.1  2004/01/20 20:59:57  nli
 * new
 *
 * Revision 1.41  2003/10/29 19:09:39  nli
 * test
 *
 * Revision 1.40  2003/10/24 20:22:25  nli
 * bug
 *
 * Revision 1.39  2003/10/24 17:31:22  nli
 * dbo check in linked substitution
 *
 * Revision 1.38  2003/10/24 17:20:31  nli
 * some bugfixes in linked server parser
 *
 * Revision 1.37  2003/10/21 17:13:51  nli
 * virtualhost implemented, all targets are virtual
 *
 * Revision 1.36  2003/09/03 17:27:26  nli
 * grammar, all mydbs changed to users
 *
 * Revision 1.35  2003/08/29 18:08:01  nli
 *
 * asdfCVS: ----------------------------------------------------------------------
 *
 * Revision 1.34  2003/08/29 17:17:17  nli
 * spoonman
 *
 * Revision 1.33  2003/08/27 18:41:35  nli
 * pretty rows
 *
 * Revision 1.32  2003/08/26 18:41:34  nli
 * linked mydbs
 *
 * Revision 1.31  2003/08/05 21:45:10  nli
 * allyourbase
 *
 * Revision 1.30  2003/07/31 21:33:10  nli
 * it broke
 *
 * Revision 1.29  2003/07/30 20:42:30  nli
 * short job timeouts should actually be working correctly now.  empty timeouts are in there but untested
 *
 * Revision 1.28  2003/07/29 19:08:09  nli
 * -groups backend all set up and ready to go
 *
 * Revision 1.27  2003/07/28 20:17:55  nli
 * Privileges simplified.  Not bool, just throws exception
 * -added priv to casjobs
 *
 * Revision 1.26  2003/07/28 20:01:26  nli
 * -authentication in CasUsers
 * -build.build compiles extras from reference
 *
 * Revision 1.25  2003/07/28 17:01:27  nli
 * timed out queries are failed, not compelted
 *
 * Revision 1.24  2003/07/28 16:42:29  nli
 * cleaning
 *
 * Revision 1.23  2003/07/28 16:22:22  nli
 * queue times be in minutes
 *
 * Revision 1.22  2003/07/24 17:26:55  nli
 * typo
 *
 * Revision 1.21  2003/07/24 17:15:43  nli
 * various fields should be filled out correctly by outputcore
 *
 * Revision 1.20  2003/07/23 21:11:06  nli
 * -outputloc arg removed from CasJobs.StartJob
 *     this is set only by the output proceesor
 * -hostip is correctly set in submitshortjob and queuethreadaction
 * -various changes in other stuff to make the above 2 thigns work
 *
 * Revision 1.19  2003/07/21 20:39:51  nli
 * OutputCore tested working, csv not implemented
 *
 * Revision 1.18  2003/07/21 19:57:58  nli
 * untested outputcore
 *
 * Revision 1.17  2003/07/17 19:57:47  nli
 * mydb target support
 *
 * Revision 1.16  2003/07/16 16:30:08  nli
 * pCore.getBestQueue now just uses CasServers.ClosestQueue
 *
 * Revision 1.15  2003/07/16 16:14:55  nli
 * somework on pCore.getBestQueue
 *
 * Revision 1.14  2003/07/15 19:42:37  nli
 * short jobs timeout too
 *
 * Revision 1.13  2003/07/15 18:10:12  nli
 * cancel looks like its working
 *
 * Revision 1.12  2003/07/14 21:55:49  nli
 * --cancel query seems to be working.  Minimal testing.
 *
 * Revision 1.11  2003/07/14 18:08:51  nli
 * -new method in jobsadmin -- CompleteCancel
 * -Core will cancel jobs that have not started yet (still will not timeout)
 *
 * Revision 1.10  2003/07/10 17:55:41  nli
 * filter system should work now
 *
 * Revision 1.9  2003/07/09 20:24:27  nli
 * qry filtering is done; except for find nearest queue bit.
 *
 * Revision 1.8  2003/07/02 20:29:53  nli
 * --full lazy creation mydb support
 * --changed some *.aspx so non ms cronies can run it
 * --failed submitJob should set failed status
 *
 * Revision 1.7  2003/07/01 19:31:40  nli
 * --though incomplete, mydb might work now
 *
 * Revision 1.6  2003/06/27 17:38:05  nli
 * -fixed bug in HasMyDB(wsid) method
 *
 * Revision 1.5  2003/06/26 21:36:45  nli
 * added mydb creation support.   Core methods are in casusers
 *
 * Revision 1.4  2003/06/26 20:31:08  nli
 * IsInLowestQueueStatement made more robust
 *
 * Revision 1.3  2003/06/26 19:13:06  nli
 * new webmethod in CasUsers called HasMyDB(wsid)
 *
 * Revision 1.2  2003/06/25 19:05:37  nli
 * procrat working, kinda
 *
 * Revision 1.1  2003/06/24 18:52:30  nli
 * test moved to test/
 *
 * Revision 1.3  2003/06/23 16:35:25  nli
 * generated wsids restricted to positive, added procrastinate base
 *
 * Revision 1.2  2003/06/23 16:04:08  nli
 * informative exceptions in users.GetWebServiceId
 *
 * Revision 1.1  2003/06/20 16:02:25  nli
 * users done and tested (somewhat)
 *
 * Revision 1.4  2003/06/19 19:37:19  womullan
 *  users added
 *
 * Revision 1.3  2003/06/19 18:52:08  nli
 *
 * blah blah cvs
 *
 */
