using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using edu.jhu.ZTestS;
using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Servers;
using edu.jhu.Cas.Users;
using edu.jhu.Cas.Util;
namespace edu.jhu.Cas.WebTest{
	
	public class CJobsTestLoader : ZTestLoader {
		public static long wsid=-1;
        protected string def_context= System.Configuration.ConfigurationManager.AppSettings["def_context"];
        protected string def_testid = System.Configuration.ConfigurationManager.AppSettings["def_testid"];
		public override bool IsInitialized(){
			return wsid != -1;
		}
		public override void Page_Load(object sender, System.EventArgs eee){
			try{
				wsid = long.Parse(Request.Params["WSID"]);
			}catch{return;}
			base.Page_Load(sender,eee);
		}
		public override void LoadGroups(){
			if(!IsInitialized())return;
			groups = new ZGroup[3];
			ZGroup g = new ZGroup("MyDB Link Tests");
			g.Tests = GetPrimaryLinkTests();
            g.Description = "Are the MyDB servers linked to each context server? This is what this test is for.  Tests each mydb server against all other servers it should be linked to.  As an example, it might verify the mydb server is linked to the dr4 server, and all the rest.";
			groups[0] = g;
			g = new ZGroup("Other Link Tests");
            g.Tests = GetSecondaryLinkTests();
            g.Description = "Are the context servers linked to each other?  This is what this test is for.  Tests each non-mydb server against all other servers it should be linked to.  As an example, it might verify DR4 is linked to TARGDR4, etc...";
			groups[1] = g;
			g = new ZGroup("Parse Tests");
            g.Tests = GetBasicQueryTests();
            g.Description = "Verifies queries are being correctly parsed and executed by running and verifying the results of a select group of queries.";
			groups[2] = g;
            AddNUnitTests();
		}
        private ZTest[] GetBasicQueryTests() {
            ZTest[] rst = new ZTest[14];
            rst[0] = QueryTests.GetSimpCompound(this, wsid, def_context);
            rst[1] = QueryTests.GetIntoMyDB(this, wsid, def_context);
            rst[2] = QueryTests.GetIntoTmp(this, wsid, def_context);
            rst[3] = QueryTests.GetIntoBMyDB(this, wsid, def_context);
            rst[4] = QueryTests.GetCreateTab(this, wsid, def_context);
            rst[5] = QueryTests.GetCreateFunc(this, wsid, def_context);
            rst[6] = QueryTests.GetCreateSP(this, wsid, def_context);
            rst[7] = QueryTests.GetDepCompound(this, wsid, def_context);
            rst[8] = QueryTests.GetMyDBFromOut(this, wsid, def_context);
            rst[9] = QueryTests.GetOtherFromMyDB(this, wsid, def_context);
            rst[10] = QueryTests.GetSelectIntoMultiRows(this, wsid, def_context);
            rst[11] = QueryTests.GetMultiSelectInto(this, wsid, def_context);
            rst[12] = QueryTests.GetExtraGo(this, wsid, def_context);
            rst[13] = QueryTests.DeclareSetInto(this, wsid, def_context);
            return rst;
		}
        private void AddNUnitTests() {
            ZGroup[] ngroups = NUnitFactory.NUnitDll2ZGroups(this, Server.MapPath("../bin/CasJobs.dll"));
            ZGroup[] g2 = new ZGroup[groups.Length + ngroups.Length];
            for (int i = 0; i < groups.Length; i++)
                g2[i] = groups[i];
            for (int i = 0; i < ngroups.Length; i++)
                g2[i+groups.Length] = ngroups[i];
            groups = g2;
        }
        private ZTest[] GetPrimaryLinkTests() {
			DataSet ds = SqlFactory.select("select server as host, usr,pass as password, cstringextra from mydbhosts");
            edu.jhu.Cas.Util.Server[] sr = DataConverter.dataSet2Server(ds);
            
			ZTest[] rst = new ZTest[ds.Tables[0].Rows.Count];
			for(int x=0;x<rst.Length;x++){
				rst[x] = new LinkTest(this,wsid,sr[x].Host,sr[x].ConnectionString);
			}
			return rst;
		}	
		private ZTest[] GetSecondaryLinkTests(){
			DataSet ds = SqlFactory.select(
				@"
select distinct usr, host, cat, cstringextra, password from servers
where target not like 'mydb'");
			ZTest[] rst = new ZTest[ds.Tables[0].Rows.Count];
            edu.jhu.Cas.Util.Server[] sr = DataConverter.dataSet2Server(ds);
			for(int x=0;x<rst.Length;x++){
				rst[x] = new LinkTest(this,wsid,sr[x].Host+"."+sr[x].Catalog,sr[x].ConnectionString);
			}
			return rst;
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e) {
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
