1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
4 | using HeuristicLab.Services.Hive.Common.ServiceContracts;
|
---|
5 | using HeuristicLab.Services.Hive.DataAccess;
|
---|
6 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Services.Hive.Tests {
|
---|
9 | using System.Threading;
|
---|
10 | using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
11 |
|
---|
12 | [TestClass]
|
---|
13 | public class DaoTests {
|
---|
14 |
|
---|
15 | [ClassInitialize]
|
---|
16 | public static void MyClassInitialize(TestContext testContext) {
|
---|
17 | ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance);
|
---|
18 | }
|
---|
19 |
|
---|
20 | private IHiveService GetLocalService() {
|
---|
21 | return new HiveService();
|
---|
22 | }
|
---|
23 |
|
---|
24 | [TestMethod]
|
---|
25 | public void TestJobDao() {
|
---|
26 | IHiveDao dao = ServiceLocator.Instance.HiveDao;
|
---|
27 |
|
---|
28 | DT.HiveExperiment he = new DT.HiveExperiment();
|
---|
29 | he.DateCreated = DateTime.Now;
|
---|
30 | he.Name = "TestExperiment";
|
---|
31 | he.OwnerUserId = Guid.NewGuid();
|
---|
32 | he.ResourceNames = "HEAL";
|
---|
33 | he.Id = dao.AddHiveExperiment(he);
|
---|
34 |
|
---|
35 | DT.Job job1 = new DT.Job();
|
---|
36 | job1.State = JobState.Offline;
|
---|
37 | job1.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now });
|
---|
38 | job1.Command = Command.Pause;
|
---|
39 | job1.HiveExperimentId = he.Id;
|
---|
40 |
|
---|
41 | DT.JobData jobData1 = new DT.JobData();
|
---|
42 | jobData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
|
---|
43 | jobData1.LastUpdate = DateTime.Now;
|
---|
44 |
|
---|
45 | DT.Plugin plugin1 = new DT.Plugin();
|
---|
46 | plugin1.Name = "Tests.MyPlugin";
|
---|
47 | plugin1.Version = new Version("1.0.0.0");
|
---|
48 | plugin1.UserId = Guid.Empty;
|
---|
49 | plugin1.IsLocal = true;
|
---|
50 | plugin1.DateCreated = DateTime.Now;
|
---|
51 |
|
---|
52 | DT.PluginData pluginData1 = new DT.PluginData();
|
---|
53 | pluginData1.PluginId = plugin1.Id;
|
---|
54 | pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
|
---|
55 | pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
|
---|
56 |
|
---|
57 | plugin1.Id = dao.AddPlugin(plugin1);
|
---|
58 | pluginData1.PluginId = plugin1.Id;
|
---|
59 | pluginData1.Id = dao.AddPluginData(pluginData1);
|
---|
60 |
|
---|
61 | job1.PluginsNeededIds.Add(plugin1.Id);
|
---|
62 |
|
---|
63 | job1.Id = dao.AddJob(job1);
|
---|
64 | jobData1.JobId = job1.Id;
|
---|
65 | dao.AddJobData(jobData1);
|
---|
66 |
|
---|
67 | DT.Job job1loaded = dao.GetJob(job1.Id);
|
---|
68 | Assert.AreEqual(job1.Id, job1loaded.Id);
|
---|
69 | Assert.AreEqual(job1.CoresNeeded, job1loaded.CoresNeeded);
|
---|
70 | Assert.AreEqual(job1.DateCreated.ToString(), job1loaded.DateCreated.ToString());
|
---|
71 | Assert.AreEqual(null, job1loaded.DateFinished);
|
---|
72 | Assert.IsTrue(job1.PluginsNeededIds.SequenceEqual(job1loaded.PluginsNeededIds));
|
---|
73 | Assert.AreEqual(job1.StateLog.Count, job1loaded.StateLog.Count);
|
---|
74 | Assert.AreEqual(job1.Command, job1loaded.Command);
|
---|
75 | Assert.AreEqual(job1.HiveExperimentId, job1loaded.HiveExperimentId);
|
---|
76 | Assert.IsTrue(Math.Abs((job1loaded.LastJobDataUpdate - jobData1.LastUpdate).TotalSeconds) < 1);
|
---|
77 | for (int i = 0; i < job1.StateLog.Count; i++) {
|
---|
78 | Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId);
|
---|
79 | Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
|
---|
80 | Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
|
---|
81 | Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
|
---|
82 | Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
|
---|
83 | Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
|
---|
84 | }
|
---|
85 |
|
---|
86 | job1 = job1loaded;
|
---|
87 |
|
---|
88 | // test jobstates
|
---|
89 | job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10);
|
---|
90 | job1.StateLog.Add(new DT.StateLog { State = JobState.Calculating, DateTime = DateTime.Now }); Thread.Sleep(10);
|
---|
91 | job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10);
|
---|
92 | job1.StateLog.Add(new DT.StateLog { State = JobState.Finished, DateTime = DateTime.Now }); Thread.Sleep(10);
|
---|
93 | dao.UpdateJob(job1);
|
---|
94 |
|
---|
95 | job1loaded = dao.GetJob(job1.Id);
|
---|
96 | for (int i = 0; i < job1.StateLog.Count; i++) {
|
---|
97 | Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId);
|
---|
98 | Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
|
---|
99 | Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
|
---|
100 | Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
|
---|
101 | Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
|
---|
102 | Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
|
---|
103 | }
|
---|
104 |
|
---|
105 | DT.JobData jobData1Loaded = dao.GetJobData(job1.Id);
|
---|
106 | Assert.AreEqual(jobData1.JobId, jobData1Loaded.JobId);
|
---|
107 | Assert.IsTrue(Math.Abs((jobData1.LastUpdate - jobData1Loaded.LastUpdate).TotalSeconds) < 1);
|
---|
108 | Assert.IsTrue(jobData1.Data.SequenceEqual(jobData1Loaded.Data));
|
---|
109 |
|
---|
110 | dao.DeleteHiveExperiment(he.Id);
|
---|
111 |
|
---|
112 | Assert.AreEqual(null, dao.GetJob(job1.Id));
|
---|
113 | Assert.AreEqual(null, dao.GetJobData(job1.Id));
|
---|
114 | Assert.AreEqual(null, dao.GetHiveExperiment(he.Id));
|
---|
115 | }
|
---|
116 |
|
---|
117 | [TestMethod]
|
---|
118 | public void TestSlaveDao() {
|
---|
119 | IHiveDao dao = ServiceLocator.Instance.HiveDao;
|
---|
120 | DT.SlaveGroup slaveGroup = new DT.SlaveGroup() {
|
---|
121 | Name = "Test"
|
---|
122 | };
|
---|
123 | slaveGroup.Id = dao.AddSlaveGroup(slaveGroup);
|
---|
124 |
|
---|
125 | DT.Slave slave = new DT.Slave() {
|
---|
126 | Id = Guid.NewGuid(),
|
---|
127 | Name = "Test",
|
---|
128 | OperatingSystem = null, //"Windows 3.11",
|
---|
129 | Cores = 2,
|
---|
130 | Memory = 1024,
|
---|
131 | FreeMemory = 512
|
---|
132 | };
|
---|
133 |
|
---|
134 | Assert.AreEqual(slave.Id, dao.AddSlave(slave));
|
---|
135 |
|
---|
136 | // update
|
---|
137 | slave.FreeMemory = 255;
|
---|
138 | slave.OperatingSystem = Environment.OSVersion.VersionString;
|
---|
139 | dao.UpdateSlave(slave);
|
---|
140 |
|
---|
141 | DT.Slave slaveLoaded = dao.GetSlave(slave.Id);
|
---|
142 | Assert.AreEqual(slave.FreeMemory, slaveLoaded.FreeMemory);
|
---|
143 |
|
---|
144 | dao.DeleteSlave(slave.Id);
|
---|
145 | dao.DeleteSlaveGroup(slaveGroup.Id);
|
---|
146 | }
|
---|
147 |
|
---|
148 | [TestMethod]
|
---|
149 | public void TestPluginDao() {
|
---|
150 | IHiveDao dao = ServiceLocator.Instance.HiveDao;
|
---|
151 |
|
---|
152 | DT.Plugin plugin1 = new DT.Plugin();
|
---|
153 | plugin1.DateCreated = DateTime.Now;
|
---|
154 | plugin1.IsLocal = false;
|
---|
155 | plugin1.Name = "Tests.MyPlugin";
|
---|
156 | plugin1.Version = new Version("1.0.0.0");
|
---|
157 | plugin1.UserId = Guid.Empty;
|
---|
158 |
|
---|
159 | plugin1.Id = dao.AddPlugin(plugin1);
|
---|
160 |
|
---|
161 | DT.Plugin plugin1loaded = dao.GetPlugin(plugin1.Id);
|
---|
162 | Assert.AreEqual(plugin1.Id, plugin1loaded.Id);
|
---|
163 | Assert.AreEqual(plugin1.Name, plugin1loaded.Name);
|
---|
164 | Assert.AreEqual(plugin1.Version, plugin1loaded.Version);
|
---|
165 | Assert.AreEqual(plugin1.UserId, plugin1loaded.UserId);
|
---|
166 | Assert.AreEqual(plugin1.DateCreated.ToString(), plugin1loaded.DateCreated.ToString());
|
---|
167 | Assert.AreEqual(plugin1.IsLocal, plugin1loaded.IsLocal);
|
---|
168 |
|
---|
169 | DT.PluginData pluginData1 = new DT.PluginData();
|
---|
170 | pluginData1.PluginId = plugin1.Id;
|
---|
171 | pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
|
---|
172 | pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
|
---|
173 |
|
---|
174 | pluginData1.Id = dao.AddPluginData(pluginData1);
|
---|
175 |
|
---|
176 | DT.PluginData pluginData1loaded = dao.GetPluginData(pluginData1.Id);
|
---|
177 | Assert.AreEqual(pluginData1.Id, pluginData1loaded.Id);
|
---|
178 |
|
---|
179 | Assert.AreEqual(pluginData1.PluginId, pluginData1loaded.PluginId);
|
---|
180 | Assert.AreEqual(pluginData1.FileName, pluginData1loaded.FileName);
|
---|
181 | Assert.IsTrue(pluginData1.Data.SequenceEqual(pluginData1loaded.Data));
|
---|
182 |
|
---|
183 | dao.DeletePluginData(pluginData1.Id);
|
---|
184 | dao.DeletePlugin(plugin1.Id);
|
---|
185 |
|
---|
186 | Assert.AreEqual(null, dao.GetPlugin(plugin1.Id));
|
---|
187 | Assert.AreEqual(null, dao.GetPluginData(pluginData1.Id));
|
---|
188 | }
|
---|
189 |
|
---|
190 | [TestMethod]
|
---|
191 | public void TestHiveExperimentDao() {
|
---|
192 | IHiveDao dao = ServiceLocator.Instance.HiveDao;
|
---|
193 |
|
---|
194 | DT.HiveExperiment he = new DT.HiveExperiment();
|
---|
195 | he.DateCreated = DateTime.Now;
|
---|
196 | he.Name = "TestExperiment";
|
---|
197 | he.OwnerUserId = Guid.NewGuid();
|
---|
198 | he.ResourceNames = "HEAL";
|
---|
199 | he.Id = dao.AddHiveExperiment(he);
|
---|
200 |
|
---|
201 | DT.Job job = new DT.Job();
|
---|
202 | job.State = JobState.Offline;
|
---|
203 | job.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now });
|
---|
204 | job.HiveExperimentId = he.Id;
|
---|
205 | job.Id = dao.AddJob(job);
|
---|
206 |
|
---|
207 | DT.HiveExperimentPermission perm = new DT.HiveExperimentPermission();
|
---|
208 | perm.HiveExperimentId = he.Id;
|
---|
209 | perm.GrantedByUserId = he.OwnerUserId;
|
---|
210 | perm.GrantedUserId = Guid.NewGuid();
|
---|
211 | perm.Permission = Permission.Write;
|
---|
212 | dao.AddHiveExperimentPermission(perm);
|
---|
213 |
|
---|
214 | DT.HiveExperiment heLoaded = dao.GetHiveExperiment(he.Id);
|
---|
215 | Assert.AreEqual(he.Id, heLoaded.Id);
|
---|
216 | Assert.AreEqual(he.Name, heLoaded.Name);
|
---|
217 | Assert.AreEqual(he.ResourceNames, heLoaded.ResourceNames);
|
---|
218 | //Assert.AreEqual(he.LastAccessed, heLoaded.LastAccessed);
|
---|
219 | //Assert.AreEqual(he.DateCreated, heLoaded.DateCreated);
|
---|
220 |
|
---|
221 | DT.Job jobLoaded = dao.GetJob(job.Id);
|
---|
222 | Assert.AreEqual(job.Id, jobLoaded.Id);
|
---|
223 | Assert.AreEqual(job.State, jobLoaded.State);
|
---|
224 | Assert.AreEqual(job.HiveExperimentId, jobLoaded.HiveExperimentId);
|
---|
225 |
|
---|
226 | DT.HiveExperimentPermission permLoaded = dao.GetHiveExperimentPermission(he.Id, perm.GrantedUserId);
|
---|
227 | Assert.AreEqual(perm.HiveExperimentId, permLoaded.HiveExperimentId);
|
---|
228 | Assert.AreEqual(perm.GrantedUserId, permLoaded.GrantedUserId);
|
---|
229 | Assert.AreEqual(perm.GrantedByUserId, permLoaded.GrantedByUserId);
|
---|
230 | Assert.AreEqual(perm.Permission, permLoaded.Permission);
|
---|
231 |
|
---|
232 | dao.DeleteHiveExperiment(he.Id);
|
---|
233 | Assert.AreEqual(null, dao.GetHiveExperiment(he.Id));
|
---|
234 | Assert.AreEqual(null, dao.GetJob(job.Id));
|
---|
235 | Assert.AreEqual(null, dao.GetHiveExperimentPermission(perm.HiveExperimentId, perm.GrantedUserId));
|
---|
236 |
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|