using System; using System.Linq; using HeuristicLab.Services.Hive.Common.DataTransfer; using HeuristicLab.Services.Hive.Common.ServiceContracts; using HeuristicLab.Services.Hive.DataAccess; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Services.Hive.Tests { using System.Threading; using DT = HeuristicLab.Services.Hive.Common.DataTransfer; [TestClass] public class DaoTests { [ClassInitialize] public static void MyClassInitialize(TestContext testContext) { PluginLoader.pluginAssemblies.Any(); ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance); } private IHiveService GetLocalService() { return new HiveService(); } [TestMethod] public void TestJobDao() { IHiveDao dao = ServiceLocator.Instance.HiveDao; DT.Job job1 = new DT.Job(); job1.State = JobState.Offline; job1.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now }); job1.Command = Command.Pause; DT.JobData jobData1 = new DT.JobData(); jobData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 }; jobData1.LastUpdate = DateTime.Now; DT.Plugin plugin1 = new DT.Plugin(); plugin1.Name = "Tests.MyPlugin"; plugin1.Version = new Version("1.0.0.0"); plugin1.UserId = Guid.Empty; plugin1.IsLocal = true; plugin1.DateCreated = DateTime.Now; DT.PluginData pluginData1 = new DT.PluginData(); pluginData1.PluginId = plugin1.Id; pluginData1.FileName = "Tests.MyPlugin-1.0.dll"; pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 }; plugin1.Id = dao.AddPlugin(plugin1); pluginData1.PluginId = plugin1.Id; pluginData1.Id = dao.AddPluginData(pluginData1); job1.PluginsNeededIds.Add(plugin1.Id); job1.Id = dao.AddJob(job1); jobData1.JobId = job1.Id; dao.AddJobData(jobData1); DT.Job job1loaded = dao.GetJob(job1.Id); Assert.AreEqual(job1.Id, job1loaded.Id); Assert.AreEqual(job1.CoresNeeded, job1loaded.CoresNeeded); Assert.AreEqual(job1.DateCreated.ToString(), job1loaded.DateCreated.ToString()); Assert.AreEqual(null, job1loaded.DateFinished); Assert.IsTrue(job1.PluginsNeededIds.SequenceEqual(job1loaded.PluginsNeededIds)); Assert.AreEqual(job1.StateLog.Count, job1loaded.StateLog.Count); Assert.AreEqual(job1.Command, job1loaded.Command); Assert.IsTrue(Math.Abs((job1loaded.LastJobDataUpdate - jobData1.LastUpdate).TotalSeconds) < 1); for (int i = 0; i < job1.StateLog.Count; i++) { Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId); Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State); Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId); Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId); Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception); Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1); } job1 = job1loaded; // test jobstates job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10); job1.StateLog.Add(new DT.StateLog { State = JobState.Calculating, DateTime = DateTime.Now }); Thread.Sleep(10); job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10); job1.StateLog.Add(new DT.StateLog { State = JobState.Finished, DateTime = DateTime.Now }); Thread.Sleep(10); dao.UpdateJob(job1); job1loaded = dao.GetJob(job1.Id); for (int i = 0; i < job1.StateLog.Count; i++) { Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId); Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State); Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId); Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId); Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception); Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1); } DT.JobData jobData1Loaded = dao.GetJobData(job1.Id); Assert.AreEqual(jobData1.JobId, jobData1Loaded.JobId); Assert.IsTrue(Math.Abs((jobData1.LastUpdate - jobData1Loaded.LastUpdate).TotalSeconds) < 1); Assert.IsTrue(jobData1.Data.SequenceEqual(jobData1Loaded.Data)); dao.DeleteJob(job1.Id); Assert.AreEqual(null, dao.GetJob(job1.Id)); Assert.AreEqual(null, dao.GetJobData(job1.Id)); } [TestMethod] public void TestSlaveDao() { IHiveDao dao = ServiceLocator.Instance.HiveDao; DT.SlaveGroup slaveGroup = new DT.SlaveGroup() { Name = "Test" }; slaveGroup.Id = dao.AddSlaveGroup(slaveGroup); DT.Slave slave = new DT.Slave() { Id = Guid.NewGuid(), Name = "Test", OperatingSystem = null, //"Windows 3.11", Cores = 2, Memory = 1024, FreeMemory = 512 }; Assert.AreEqual(slave.Id, dao.AddSlave(slave)); // update slave.FreeMemory = 255; slave.OperatingSystem = Environment.OSVersion.VersionString; dao.UpdateSlave(slave); DT.Slave slaveLoaded = dao.GetSlave(slave.Id); Assert.AreEqual(slave.FreeMemory, slaveLoaded.FreeMemory); dao.DeleteSlave(slave.Id); dao.DeleteSlaveGroup(slaveGroup.Id); } [TestMethod] public void TestPluginDao() { IHiveDao dao = ServiceLocator.Instance.HiveDao; DT.Plugin plugin1 = new DT.Plugin(); plugin1.DateCreated = DateTime.Now; plugin1.IsLocal = false; plugin1.Name = "Tests.MyPlugin"; plugin1.Version = new Version("1.0.0.0"); plugin1.UserId = Guid.Empty; plugin1.Id = dao.AddPlugin(plugin1); DT.Plugin plugin1loaded = dao.GetPlugin(plugin1.Id); Assert.AreEqual(plugin1.Id, plugin1loaded.Id); Assert.AreEqual(plugin1.Name, plugin1loaded.Name); Assert.AreEqual(plugin1.Version, plugin1loaded.Version); Assert.AreEqual(plugin1.UserId, plugin1loaded.UserId); Assert.AreEqual(plugin1.DateCreated.ToString(), plugin1loaded.DateCreated.ToString()); Assert.AreEqual(plugin1.IsLocal, plugin1loaded.IsLocal); DT.PluginData pluginData1 = new DT.PluginData(); pluginData1.PluginId = plugin1.Id; pluginData1.FileName = "Tests.MyPlugin-1.0.dll"; pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 }; pluginData1.Id = dao.AddPluginData(pluginData1); DT.PluginData pluginData1loaded = dao.GetPluginData(pluginData1.Id); Assert.AreEqual(pluginData1.Id, pluginData1loaded.Id); Assert.AreEqual(pluginData1.PluginId, pluginData1loaded.PluginId); Assert.AreEqual(pluginData1.FileName, pluginData1loaded.FileName); Assert.IsTrue(pluginData1.Data.SequenceEqual(pluginData1loaded.Data)); dao.DeletePluginData(pluginData1.Id); dao.DeletePlugin(plugin1.Id); Assert.AreEqual(null, dao.GetPlugin(plugin1.Id)); Assert.AreEqual(null, dao.GetPluginData(pluginData1.Id)); } [TestMethod] public void TestHiveExperimentDao() { IHiveDao dao = ServiceLocator.Instance.HiveDao; DT.HiveExperiment he = new DT.HiveExperiment(); DT.Job job = new DT.Job(); job.State = JobState.Offline; job.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now }); job.Id = dao.AddJob(job); he.RootJobId = job.Id; he.DateCreated = DateTime.Now; he.Name = "TestExperiment"; he.OwnerUserId = Guid.NewGuid(); he.ResourceNames = "HEAL"; he.Id = dao.AddHiveExperiment(he); DT.HiveExperimentPermission perm = new DT.HiveExperimentPermission(); perm.HiveExperimentId = he.Id; perm.GrantedByUserId = he.OwnerUserId; perm.GrantedUserId = Guid.NewGuid(); perm.Permission = Permission.Write; dao.AddHiveExperimentPermission(perm); DT.HiveExperiment heLoaded = dao.GetHiveExperiment(he.Id); Assert.AreEqual(he.Id, heLoaded.Id); Assert.AreEqual(he.Name, heLoaded.Name); Assert.AreEqual(he.ResourceNames, heLoaded.ResourceNames); Assert.AreEqual(he.RootJobId, heLoaded.RootJobId); //Assert.AreEqual(he.LastAccessed, heLoaded.LastAccessed); //Assert.AreEqual(he.DateCreated, heLoaded.DateCreated); DT.Job jobLoaded = dao.GetJob(he.RootJobId); Assert.AreEqual(job.Id, jobLoaded.Id); Assert.AreEqual(job.State, jobLoaded.State); DT.HiveExperimentPermission permLoaded = dao.GetHiveExperimentPermission(he.Id, perm.GrantedUserId); Assert.AreEqual(perm.HiveExperimentId, permLoaded.HiveExperimentId); Assert.AreEqual(perm.GrantedUserId, permLoaded.GrantedUserId); Assert.AreEqual(perm.GrantedByUserId, permLoaded.GrantedByUserId); Assert.AreEqual(perm.Permission, permLoaded.Permission); dao.DeleteHiveExperiment(he.Id); Assert.AreEqual(null, dao.GetHiveExperiment(he.Id)); Assert.AreEqual(null, dao.GetJob(he.RootJobId)); Assert.AreEqual(null, dao.GetHiveExperimentPermission(perm.HiveExperimentId, perm.GrantedUserId)); } } }