using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using HeuristicLab.Services.Hive.Common.DataTransfer; using HeuristicLab.Services.Hive.Common.ServiceContracts; using HeuristicLab.Clients.Hive.Slave.Tests; using HeuristicLab.Clients.Hive; using HeuristicLab.Services.Hive.DataAccess; namespace HeuristicLab.Services.Hive.Tests { using DA = HeuristicLab.Services.Hive.DataAccess; 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.DateCreated = 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); DT.Job job1loaded = dao.GetJob(job1.Id); Assert.AreEqual(job1.Id, job1loaded.Id); Assert.AreEqual(job1.CoresNeeded, job1loaded.CoresNeeded); Assert.AreEqual(null, job1loaded.DateCalculated); Assert.AreEqual(job1.DateCreated.ToString(), job1loaded.DateCreated.ToString()); Assert.AreEqual(null, job1loaded.DateFinished); Assert.IsTrue(job1.PluginsNeededIds.SequenceEqual(job1loaded.PluginsNeededIds)); dao.DeleteJob(job1.Id); Assert.AreEqual(null, dao.GetJob(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 TestJobDaoWaiting() { IHiveDao dao = ServiceLocator.Instance.HiveDao; DT.Job job = new DT.Job(); job.DateCreated = DateTime.Now; job.JobState = JobState.Waiting; job.Id = dao.AddJob(job); // todo } [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)); } } }