Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClientManagement/HeuristicLab.Services.Authentication/TestWebService/TestWebServiec.cs @ 5306

Last change on this file since 5306 was 5004, checked in by fruehrli, 14 years ago

#1197
UnitTests added
TODO: test of entries in ResourceResourceGroup, update methods

File size: 16.3 KB
Line 
1using System;
2using System.Text;
3using System.Collections.Generic;
4using System.Linq;
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6using HeuristicLab.Services.Authentication;
7using HL = HeuristicLab.Services.Authentication;
8using DA = HeuristicLab.Services.Authentication.DataAccess;
9using DT = HeuristicLab.Services.Authentication.DataTransfer;
10using System.Data;
11using System.Data.Common;
12using System.Data.Linq;
13using System.Collections;
14
15namespace TestWebService {
16  [TestClass]
17  public class WebserviceUnitTest {
18
19    private string sqlCountClients = @"SELECT COUNT(*) FROM Resource where ResourceType = 'Client'";
20    private string sqlCountGroups = @"SELECT COUNT(*) FROM Resource where ResourceType = 'ResourceGroup'";
21
22    #region TestIdsAndNames
23    private Guid groupID1 = Guid.NewGuid();
24    private string groupName1 = "groupName1";
25    private Guid groupID2 = Guid.NewGuid();
26    private string groupName2 = "groupName2";
27    private Guid groupID3 = Guid.NewGuid();
28    private string groupName3 = "groupName3";
29
30    private Guid clientID1 = Guid.NewGuid();
31    private string clientName1 = "clientName1";
32    private Guid clientID2 = Guid.NewGuid();
33    private string clientName2 = "clientName2";
34    #endregion
35
36    #region ValidationMethods
37    class ClientEqualityComparer : IEqualityComparer<DT.Client> {
38      public bool Equals(DT.Client c1, DT.Client c2) {
39        return (
40          c1.Name == c2.Name &&
41          c1.Id == c2.Id &&
42          c1.Description == c2.Description &&
43          c1.IPAdress == c2.IPAdress &&
44          c1.NumberOfProcessors == c2.NumberOfProcessors &&
45          c1.NumberOfThreads == c2.NumberOfThreads &&
46          c1.OperatingSystem == c2.OperatingSystem &&
47          c1.ProcessorType == c2.ProcessorType
48        );
49      }
50
51      public int GetHashCode(DT.Client obj) {
52        throw new NotImplementedException();
53      }
54    }
55
56    class ResourceGroupEqualityComparer : IEqualityComparer<DT.ResourceGroup> {
57      public bool Equals(DT.ResourceGroup rg1, DT.ResourceGroup rg2) {
58        return (
59          rg1.Name == rg2.Name &&
60          rg1.Id == rg2.Id &&
61          rg1.Description == rg2.Description
62        );
63      }
64
65      public int GetHashCode(DT.ResourceGroup obj) {
66        throw new NotImplementedException();
67      }
68    }
69
70    private bool CompareClients(DT.Client c1, DT.Client c2) {
71      return (
72        c1.Name == c2.Name &&
73        c1.Id == c2.Id &&
74        //c1.ResourceType == c2.ResourceType &&
75        c1.Description == c2.Description &&
76        c1.IPAdress == c2.IPAdress &&
77        c1.NumberOfProcessors == c2.NumberOfProcessors &&
78        c1.NumberOfThreads == c2.NumberOfThreads &&
79        c1.OperatingSystem == c2.OperatingSystem &&
80        c1.ProcessorType == c2.ProcessorType
81        );
82    }
83
84    private bool CompareGroups(DT.ResourceGroup r1, DT.ResourceGroup r2) {
85      return (
86        r1.Name == r2.Name &&
87        r1.Id == r2.Id &&
88        //c1.ResourceType == c2.ResourceType &&
89        r1.Description == r2.Description
90        );
91    }
92
93    private static int GetNumberOfEntries(string sqlCommand) {
94      DA.ClientManagmentDataContext dc = new DA.ClientManagmentDataContext();
95      try {
96        IEnumerable<int> cnt = dc.ExecuteQuery<int>(sqlCommand);
97        return (int)cnt.First();
98      }
99      catch (Exception e) {
100        return 0;
101      }
102    }
103    #endregion
104
105    #region PublicResources
106    DA.ClientManagmentDataContext dc = null;
107
108    public DT.Client cl1 = null;
109    public DT.Client cl2 = null;
110    public DT.Client cl3 = null;
111
112    public DT.ResourceGroup rg1 = null;
113    public DT.ResourceGroup rg2 = null;
114    public DT.ResourceGroup rg3 = null;
115
116    public DA.ResourceResourceGroup rrg1 = null;
117    public DA.ResourceResourceGroup rrg2 = null;
118    public DA.ResourceResourceGroup rrg3 = null;
119    #endregion
120   
121    #region Additional test attributes
122    //
123    // You can use the following additional attributes as you write your tests:
124    //
125    // Use ClassInitialize to run code before running the first test in the class
126    // [ClassInitialize()]
127    // public static void MyClassInitialize(TestContext testContext) { }
128    //
129    // Use ClassCleanup to run code after all tests in a class have run
130    // [ClassCleanup()]
131    // public static void MyClassCleanup() { }
132    //
133    // Use TestInitialize to run code before running each test
134    // [TestInitialize()]
135    // public void MyTestInitialize() { }
136    //
137    // Use TestCleanup to run code after each test has run
138    // c
139    // public void MyTestCleanup() { }
140    //
141    #endregion
142
143    [TestInitialize()]
144    public void Init() { 
145
146      dc = new DA.ClientManagmentDataContext();
147      if (dc.DatabaseExists()) {
148        //dc.DeleteDatabase();
149        dc.ExecuteCommand("delete from ResourceResourceGroup");
150        dc.ExecuteCommand("delete from Resource");
151        dc.SubmitChanges();
152      }
153      else {
154        dc.CreateDatabase();
155        dc.SubmitChanges();
156      }
157
158     
159      // create groups
160      rg1 = new DT.ResourceGroup();
161      rg1.Name = groupName1;
162      rg1.Id = groupID1;     
163      rg1.Description = "descr rg1";
164      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(rg1));
165
166      rg2 = new DT.ResourceGroup();
167      rg2.Name = groupName2;
168      rg2.Id = groupID2;
169      rg2.ResourceGroup = rg1.Id;
170      rg2.Description = "descrrg2";
171      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(rg2));
172
173      rg3 = new DT.ResourceGroup();
174      rg3.Name = groupName3;
175      rg3.Id = groupID3;
176      rg3.Description = "descrrg3";
177      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(rg3));
178
179      // create clients
180      cl1 = new DT.Client();
181      cl1.Name = clientName1;
182      cl1.Id = clientID1;
183      cl1.ResourceGroup = groupID1;
184      cl1.Description = "Test-PC-1";
185      cl1.IPAdress = "192.168.0.10";
186      cl1.NumberOfProcessors = "2";
187      cl1.NumberOfThreads = "4";
188      cl1.OperatingSystem = "WINXP";
189      cl1.MemorySize = "2048";
190      cl1.ProcessorType = "AMD";
191      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(cl1));
192
193      cl2 = new DT.Client();
194      cl2.Name = clientName2;
195      cl2.Id = clientID2;
196      cl2.ResourceGroup = groupID2;
197      cl2.Description = "Test-PC-2";
198      cl2.IPAdress = "192.168.0.20";
199      cl2.NumberOfProcessors = "4";
200      cl2.NumberOfThreads = "4";
201      cl2.OperatingSystem = "Vista";
202      cl2.MemorySize = "4096";
203      cl2.ProcessorType = "Intel";
204      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(cl2));
205
206      // ResourceResourceGroups
207      rrg1 = new DA.ResourceResourceGroup();
208      rrg1.ResourceId = cl1.Id;
209      rrg1.ResourceGroupId = rg1.Id;
210      dc.GetTable<DA.ResourceResourceGroup>().InsertOnSubmit(rrg1);
211
212      DA.ResourceResourceGroup rrg2 = new DA.ResourceResourceGroup();
213      rrg2.ResourceId = cl2.Id;
214      rrg2.ResourceGroupId = rg2.Id;
215      dc.GetTable<DA.ResourceResourceGroup>().InsertOnSubmit(rrg2);
216
217      DA.ResourceResourceGroup rrg3 = new DA.ResourceResourceGroup();
218      rrg3.ResourceId = rg1.Id;
219      rrg3.ResourceGroupId = rg3.Id;
220      dc.GetTable<DA.ResourceResourceGroup>().InsertOnSubmit(rrg3);
221
222      dc.SubmitChanges();
223    }
224
225    [TestCleanup()]
226    public void CleanUp() {
227      dc = new DA.ClientManagmentDataContext();
228      if (dc.DatabaseExists()) {
229        //dc.DeleteDatabase();
230        dc.ExecuteCommand("delete from ResourceResourceGroup");
231        dc.ExecuteCommand("delete from Resource");
232        dc.SubmitChanges();
233      }
234      else {
235        dc.CreateDatabase();
236        dc.SubmitChanges();
237      }
238    }
239
240    #region TestMethods   
241
242    #region TestGroup
243
244    /// <summary>
245    /// Add ResourceGroup
246    /// </summary>
247    [TestMethod]
248    public void TestAddResourceGroup() {
249      ClientService cs = new ClientService();
250
251      DT.ResourceGroup rg4 = new DT.ResourceGroup();
252      rg4.Name = "groupName4";
253      rg4.Description = "descrrg4";
254      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(rg4));
255
256      int cntPrev = GetNumberOfEntries(sqlCountGroups);
257      rg4.Id = cs.AddResourceGroup(rg4);
258      Assert.AreNotEqual(Guid.Empty, rg4.Id);
259      int cntNew = GetNumberOfEntries(sqlCountGroups);
260      Assert.AreEqual(cntPrev, (cntNew - 1));
261
262      DA.ResourceGroup group = dc.Resources.OfType<DA.ResourceGroup>().First(x => x.Id == rg4.Id);
263      Assert.IsTrue(CompareGroups(HL.Convert.ToDto(group), rg4));
264    }
265
266    /// <summary>
267    /// TODO
268    /// </summary>
269    [TestMethod]
270    public void TestUpdateResourceGroup() {
271    }
272
273    /// <summary>
274    /// GetResourceGroup with existing id
275    /// </summary>
276    [TestMethod]
277    public void TestGetResourceGroup() {
278      ClientService cs = new ClientService();
279      DT.ResourceGroup rg = cs.GetResourceGroup(groupID1);
280      Assert.AreEqual(rg.Id, groupID1);
281      Assert.IsTrue(CompareGroups(rg, rg1));
282    }
283
284    /// <summary>
285    /// GetResourceGroups
286    /// </summary>
287    [TestMethod]
288    public void TestGetResourceGroups() {
289      ClientService cs = new ClientService();
290      IEnumerable<DT.ResourceGroup> groups = cs.GetResourceGroups();
291      Assert.AreEqual(3, groups.Count<DT.ResourceGroup>());
292      Assert.IsTrue(
293        groups.Contains(rg1, new ResourceGroupEqualityComparer()) &&
294        groups.Contains(rg2, new ResourceGroupEqualityComparer()) &&
295        groups.Contains(rg3, new ResourceGroupEqualityComparer())
296      );
297    }
298
299    /// <summary>
300    /// Delete existing ResourceGroup whisch is no parent group
301    /// </summary>
302    [TestMethod]
303    public void TestDeleteResourceGroup1() {
304      ClientService cs = new ClientService();
305
306      int cntPrev = GetNumberOfEntries(sqlCountGroups);
307      Assert.IsTrue(cs.DeleteResourceGroup(rg2));
308      int cntNew = GetNumberOfEntries(sqlCountGroups);
309      Assert.AreEqual(cntPrev, (cntNew + 1));
310    }
311
312    /// <summary>
313    /// try to delete existing ResourceGroup whisch is parent group
314    /// </summary>
315    [TestMethod]
316    public void TestDeleteResourceGroup2() {
317      ClientService cs = new ClientService();
318
319      int cntPrev = GetNumberOfEntries(sqlCountGroups);
320      Assert.IsFalse(cs.DeleteResourceGroup(rg1));
321      int cntNew = GetNumberOfEntries(sqlCountGroups);
322      Assert.AreEqual(cntPrev, cntNew);
323    }
324    #endregion
325
326
327    #region TestClient
328
329    /// <summary>
330    /// AddClient
331    /// </summary>
332    [TestMethod]
333    public void TestAddClient1() {
334      ClientService cs = new ClientService();
335
336      DT.Client cl3 = new DT.Client();
337      cl3.Name = "clientName3";
338      cl3.ResourceGroup = groupID3;
339      cl3.Description = "Test-PC-3";
340      cl3.IPAdress = "192.168.0.30";
341      cl3.NumberOfProcessors = "2";
342      cl3.NumberOfThreads = "2";
343      cl3.OperatingSystem = "Vista";
344      cl3.MemorySize = "4096";
345      cl3.ProcessorType = "Intel";
346
347      int cntPrev = GetNumberOfEntries(sqlCountClients);
348      cl3.Id = cs.AddClient(cl3);
349      Assert.AreNotEqual(Guid.Empty, cl3.Id);
350      int cntNew = GetNumberOfEntries(sqlCountClients);
351      Assert.AreEqual(cntPrev, (cntNew - 1));
352
353      DA.Client client = dc.Resources.OfType<DA.Client>().First(x => x.Id == cl3.Id);
354      Assert.IsTrue(CompareClients(HL.Convert.ToDto(client), cl3));
355    }
356
357    /// <summary>
358    /// try to AddClient with not existing ResourceGroup
359    /// </summary>
360    [TestMethod]
361    public void TestAddClient2() {
362      ClientService cs = new ClientService();
363
364      DT.Client cl3 = new DT.Client();
365      cl3.Name = "clientNamexxxx";
366      cl3.ResourceGroup = Guid.NewGuid();
367
368      int cntPrev = GetNumberOfEntries(sqlCountClients);
369      cl3.Id = cs.AddClient(cl3);
370      Assert.AreEqual(Guid.Empty, cl3.Id);
371      int cntNew = GetNumberOfEntries(sqlCountClients);
372      Assert.AreEqual(cntPrev, cntNew);
373    }
374
375    /// <summary>
376    /// GetClient by Id
377    /// </summary>
378    [TestMethod]
379    public void TestGetClient1() {
380      ClientService cs = new ClientService();
381      DT.Client cl = cs.GetClient(clientID1);
382      Assert.AreEqual(cl.Id, clientID1);
383      Assert.IsTrue(CompareClients(cl, cl1));
384    }
385
386    /// <summary>
387    /// causes error
388    ///   !!!!!!!!!!!!!!!!!!!!!!
389    ///   
390    /// try GetClient with not existin id
391    /// </summary>
392    //[TestMethod]
393    //public void TestGetClient2() {
394    //  ClientService cs = new ClientService();
395    //  DT.Client cl = cs.GetClient(Guid.NewGuid());
396    //  Assert.IsNull(cl);
397    //}
398
399    ///// <summary>
400    ///// Test of Method "Client GetClient(Guid id)"
401    ///// </summary>
402    //[TestMethod]
403    //public void TestGetClient3() {
404    //  ClientService cs = new ClientService();
405    //  DT.Client cl = cs.GetClient(Guid.Empty);
406    //  Assert.IsNull(cl);
407    //}
408
409    //[TestMethod]
410    //public void TestUpdateClient() {
411    //}
412
413    /// <summary>
414    /// delete existing client
415    /// </summary>
416    [TestMethod]
417    public void TestDeleteClient1() {
418      ClientService cs = new ClientService();
419
420      int cntPrev = GetNumberOfEntries(sqlCountClients);
421      Assert.IsTrue(cs.DeleteClient(cl1));
422      int cntNew = GetNumberOfEntries(sqlCountClients);
423      Assert.AreEqual(cntPrev, (cntNew + 1));
424    }
425
426    /// <summary>
427    /// try to delete not existing client
428    /// </summary>
429    [TestMethod]
430    public void TestDeleteClient2() {
431      ClientService cs = new ClientService();
432      DT.Client cl3 = new DT.Client();
433      cl3.Name = "clientName3";
434      cl3.ResourceGroup = groupID3;
435      cl3.Description = "Test-PC-3";
436      cl3.IPAdress = "192.168.0.30";
437      cl3.NumberOfProcessors = "2";
438      cl3.NumberOfThreads = "2";
439      cl3.OperatingSystem = "Vista";
440      cl3.MemorySize = "4096";
441      cl3.ProcessorType = "Intel";
442
443      int cntPrev = GetNumberOfEntries(sqlCountClients);
444      Assert.IsFalse(cs.DeleteClient(cl3));
445      int cntNew = GetNumberOfEntries(sqlCountClients);
446      Assert.AreEqual(cntPrev, cntNew);
447    }
448
449    /// <summary>
450    /// get all clients
451    /// </summary>
452    [TestMethod]
453    public void TestGetClients() {
454      ClientService cs = new ClientService();
455      IEnumerable<DT.Client> clients = cs.GetClients();
456      Assert.AreEqual(2, clients.Count<DT.Client>());
457      Assert.IsTrue(
458        clients.Contains(cl1, new ClientEqualityComparer()) &&
459        clients.Contains(cl2, new ClientEqualityComparer())
460      );
461    }
462
463    /// <summary>
464    /// update existing client
465    /// </summary>
466    [TestMethod]
467    public void TestUpdateClient1() {
468      ClientService cs = new ClientService();
469      DT.Client cl1 = new DT.Client();
470      cl1.Name = "clientName1_modified";
471      cl1.ResourceGroup = groupID3;
472      cl1.Description = "Test-PC-1_modifiet";
473      cl1.IPAdress = "192.168.0.11";
474      cl1.NumberOfProcessors = "4";
475      cl1.NumberOfThreads = "2";
476      cl1.OperatingSystem = "Vista";
477      cl1.MemorySize = "2096";
478      cl1.ProcessorType = "Intel";
479
480      int cntPrev = GetNumberOfEntries(sqlCountClients);
481      Assert.IsTrue(cs.UpdateClient(cl1));
482      int cntNew = GetNumberOfEntries(sqlCountClients);
483      Assert.AreEqual(cntPrev, cntNew);
484
485      DA.Client client = dc.Resources.OfType<DA.Client>().First(x => x.Id == cl1.Id);
486      Assert.IsTrue(CompareClients(HL.Convert.ToDto(client), cl1));
487    }
488
489    /// <summary>
490    /// try to update not existing client
491    /// </summary>
492    [TestMethod]
493    public void TestUpdateClient2() {
494      ClientService cs = new ClientService();
495      DT.Client cl3 = new DT.Client();
496      cl3.Id = Guid.NewGuid();
497      cl3.Name = "clientName3";
498      cl3.ResourceGroup = groupID3;
499      cl3.Description = "Test-PC-3";
500      cl3.IPAdress = "192.168.0.30";
501      cl3.NumberOfProcessors = "2";
502      cl3.NumberOfThreads = "2";
503      cl3.OperatingSystem = "Vista";
504      cl3.MemorySize = "4096";
505      cl3.ProcessorType = "Intel";
506
507      int cntPrev = GetNumberOfEntries(sqlCountClients);
508      Assert.IsFalse(cs.UpdateClient(cl3));
509      int cntNew = GetNumberOfEntries(sqlCountClients);
510      Assert.AreEqual(cntPrev, cntNew);
511
512      DA.Client client = dc.Resources.OfType<DA.Client>().First(x => x.Id == cl3.Id);
513      Assert.IsNull(client);
514    }
515
516    #endregion
517   
518    #endregion //Testmethods
519  }
520}
Note: See TracBrowser for help on using the repository browser.