Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5335 was 5335, checked in by fruehrli, 13 years ago

#1197
modified and new service methods

File size: 19.7 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    class ResourceEqualityComparer : IEqualityComparer<DT.Resource> {
71      public bool Equals(DT.Resource rg1, DT.Resource rg2) {
72        return (
73          rg1.Name == rg2.Name &&
74          rg1.Id == rg2.Id
75        );
76      }
77
78      public int GetHashCode(DT.Resource obj) {
79        throw new NotImplementedException();
80      }
81    }
82
83    private bool CompareClients(DT.Client c1, DT.Client c2) {
84      return (
85        c1.Name == c2.Name &&
86        c1.Id == c2.Id &&
87        c1.ResourceType == c2.ResourceType &&
88        c1.Description == c2.Description &&
89        c1.IPAdress == c2.IPAdress &&
90        c1.NumberOfProcessors == c2.NumberOfProcessors &&
91        c1.NumberOfThreads == c2.NumberOfThreads &&
92        c1.OperatingSystem == c2.OperatingSystem &&
93        c1.ProcessorType == c2.ProcessorType
94        );
95    }
96
97    private bool CompareGroups(DT.ResourceGroup r1, DT.ResourceGroup r2) {
98      return (
99        r1.Name == r2.Name &&
100        r1.Id == r2.Id &&
101        //c1.ResourceType == c2.ResourceType &&
102        r1.Description == r2.Description
103        );
104    }
105
106    private bool CompareResources(DT.Resource r1, DT.Resource r2) {
107      return (
108        r1.Name == r2.Name &&
109        r1.Id == r2.Id
110        );
111    }
112
113    private static int GetNumberOfEntries(string sqlCommand) {
114      DA.ClientManagmentDataContext dc = new DA.ClientManagmentDataContext();
115      try {
116        IEnumerable<int> cnt = dc.ExecuteQuery<int>(sqlCommand);
117        return (int)cnt.First();
118      }
119      catch (Exception e) {
120        return 0;
121      }
122    }
123    #endregion
124
125    #region PublicResources
126    DA.ClientManagmentDataContext dc = null;
127
128    public DT.Client cl1 = null;
129    public DT.Client cl2 = null;
130    public DT.Client cl3 = null;
131
132    public DT.ResourceGroup rg1 = null;
133    public DT.ResourceGroup rg2 = null;
134    public DT.ResourceGroup rg3 = null;
135
136    public DA.ResourceResourceGroup rrg1 = null;
137    public DA.ResourceResourceGroup rrg2 = null;
138    public DA.ResourceResourceGroup rrg3 = null;
139    public DA.ResourceResourceGroup rrg4 = null;
140    public DA.ResourceResourceGroup rrg5 = null;
141    #endregion
142
143    #region Additional test attributes
144    //
145    // You can use the following additional attributes as you write your tests:
146    //
147    // Use ClassInitialize to run code before running the first test in the class
148    // [ClassInitialize()]
149    // public static void MyClassInitialize(TestContext testContext) { }
150    //
151    // Use ClassCleanup to run code after all tests in a class have run
152    // [ClassCleanup()]
153    // public static void MyClassCleanup() { }
154    //
155    // Use TestInitialize to run code before running each test
156    // [TestInitialize()]
157    // public void MyTestInitialize() { }
158    //
159    // Use TestCleanup to run code after each test has run
160    // c
161    // public void MyTestCleanup() { }
162    //
163    #endregion
164
165    [TestInitialize()]
166    public void Init() {
167
168      dc = new DA.ClientManagmentDataContext();
169      if (dc.DatabaseExists()) {
170        //dc.DeleteDatabase();
171        dc.ExecuteCommand("delete from ResourceResourceGroup");
172        dc.ExecuteCommand("delete from Resource");
173        dc.SubmitChanges();
174      }
175      else {
176        dc.CreateDatabase();
177        dc.SubmitChanges();
178      }
179
180
181      // create groups
182      rg1 = new DT.ResourceGroup();
183      rg1.Name = groupName1;
184      rg1.Id = groupID1;
185      rg1.Description = "descr rg1";
186      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(rg1));
187
188      rg2 = new DT.ResourceGroup();
189      rg2.Name = groupName2;
190      rg2.Id = groupID2;
191
192      IList<Guid> rgList = new List<Guid>();
193      rgList.Add(rg1.Id);
194      rg2.ResourceGroup = rgList;
195
196      rg2.Description = "descrrg2";
197      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(rg2));
198
199      rg3 = new DT.ResourceGroup();
200      rg3.Name = groupName3;
201      rg3.Id = groupID3;
202      rg3.Description = "descrrg3";
203      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(rg3));
204
205      // create clients
206      cl1 = new DT.Client();
207      cl1.Name = clientName1;
208      cl1.Id = clientID1;
209
210      IList<Guid> rgList1 = new List<Guid>();
211      rgList.Add(groupID1);
212      cl1.ResourceGroup = rgList1;
213
214      cl1.Description = "Test-PC-1";
215      cl1.IPAdress = "192.168.0.10";
216      cl1.NumberOfProcessors = "2";
217      cl1.NumberOfThreads = "4";
218      cl1.OperatingSystem = "WINXP";
219      cl1.MemorySize = "2048";
220      cl1.ProcessorType = "AMD";
221      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(cl1));
222
223      cl2 = new DT.Client();
224      cl2.Name = clientName2;
225      cl2.Id = clientID2;
226
227      IList<Guid> rgList2 = new List<Guid>();
228      rgList.Add(groupID2);
229      cl2.ResourceGroup = rgList2;
230
231      cl2.Description = "Test-PC-2";
232      cl2.IPAdress = "192.168.0.20";
233      cl2.NumberOfProcessors = "4";
234      cl2.NumberOfThreads = "4";
235      cl2.OperatingSystem = "Vista";
236      cl2.MemorySize = "4096";
237      cl2.ProcessorType = "Intel";
238      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(cl2));
239
240      // ResourceResourceGroups
241      rrg1 = new DA.ResourceResourceGroup();
242      rrg1.ResourceId = cl1.Id;
243      rrg1.ResourceGroupId = rg1.Id;
244      dc.GetTable<DA.ResourceResourceGroup>().InsertOnSubmit(rrg1);
245
246      // ResourceResourceGroups
247      rrg5 = new DA.ResourceResourceGroup();
248      rrg5.ResourceId = cl1.Id;
249      rrg5.ResourceGroupId = rg2.Id;
250      dc.GetTable<DA.ResourceResourceGroup>().InsertOnSubmit(rrg5);
251
252      DA.ResourceResourceGroup rrg2 = new DA.ResourceResourceGroup();
253      rrg2.ResourceId = cl2.Id;
254      rrg2.ResourceGroupId = rg2.Id;
255      dc.GetTable<DA.ResourceResourceGroup>().InsertOnSubmit(rrg2);
256
257      //DA.ResourceResourceGroup rrg3 = new DA.ResourceResourceGroup();
258      //rrg3.ResourceId = rg3.Id;
259      //rrg3.ResourceGroupId = rg1.Id;
260      //dc.GetTable<DA.ResourceResourceGroup>().InsertOnSubmit(rrg3);
261
262      rrg4 = new DA.ResourceResourceGroup();
263      rrg4.ResourceId = rg2.Id;
264      rrg4.ResourceGroupId = rg1.Id;
265      dc.GetTable<DA.ResourceResourceGroup>().InsertOnSubmit(rrg4);
266
267      dc.SubmitChanges();
268    }
269
270    [TestCleanup()]
271    public void CleanUp() {
272      //dc = new DA.ClientManagmentDataContext();
273      //if (dc.DatabaseExists()) {
274      //  //dc.DeleteDatabase();
275      //  dc.ExecuteCommand("delete from ResourceResourceGroup");
276      //  dc.ExecuteCommand("delete from Resource");
277      //  dc.SubmitChanges();
278      //}
279      //else {
280      //  dc.CreateDatabase();
281      //  dc.SubmitChanges();
282      //}
283    }
284
285    #region TestMethods
286
287    #region TestGroup
288
289    /// <summary>
290    /// Add ResourceGroup
291    /// </summary>
292    [TestMethod]
293    public void TestAddResourceGroup() {
294      AuthenticationService cs = new AuthenticationService();
295
296      DT.ResourceGroup rg4 = new DT.ResourceGroup();
297      rg4.Name = "groupName4";
298      rg4.Description = "descrrg4";
299      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(rg4));
300
301      int cntPrev = GetNumberOfEntries(sqlCountGroups);
302      rg4.Id = cs.AddResourceGroup(rg4);
303      Assert.AreNotEqual(Guid.Empty, rg4.Id);
304      int cntNew = GetNumberOfEntries(sqlCountGroups);
305      Assert.AreEqual(cntPrev, (cntNew - 1));
306
307      DA.ResourceGroup group = dc.Resources.OfType<DA.ResourceGroup>().First(x => x.Id == rg4.Id);
308      Assert.IsTrue(CompareGroups(HL.Convert.ToDto(group), rg4));
309    }
310
311    /// <summary>
312
313    /// </summary>
314    [TestMethod]
315    public void TestUpdateResourceGroup() {
316      AuthenticationService cs = new AuthenticationService();
317
318      DA.ResourceGroup group = dc.Resources.OfType<DA.ResourceGroup>().First(x => x.Id == rg1.Id);
319      DT.ResourceGroup modGroup = HeuristicLab.Services.Authentication.Convert.ToDto(group);
320
321
322      modGroup.Name = "clientName1_modified";
323
324      IList<Guid> rgList = new List<Guid>();
325      rgList.Add(groupID3);
326      modGroup.ResourceGroup = rgList;
327      modGroup.Description = "Test-group_modifiet";
328
329      cs.UpdateResourceGroup(modGroup);
330    }
331
332    /// <summary>
333    /// GetResourceGroup with existing id
334    /// </summary>
335    [TestMethod]
336    public void TestGetResourceGroup() {
337      AuthenticationService cs = new AuthenticationService();
338      DT.ResourceGroup rg = cs.GetResourceGroup(groupID1);
339      Assert.AreEqual(rg.Id, groupID1);
340      Assert.IsTrue(CompareGroups(rg, rg1));
341    }
342
343    /// <summary>
344    /// GetResourceGroups
345    /// </summary>
346    [TestMethod]
347    public void TestGetResourceGroups() {
348      AuthenticationService cs = new AuthenticationService();
349      IEnumerable<DT.ResourceGroup> groups = cs.GetResourceGroups();
350      Assert.AreEqual(3, groups.Count<DT.ResourceGroup>());
351      Assert.IsTrue(
352        groups.Contains(rg1, new ResourceGroupEqualityComparer()) &&
353        groups.Contains(rg2, new ResourceGroupEqualityComparer()) &&
354        groups.Contains(rg3, new ResourceGroupEqualityComparer())
355      );
356    }
357
358    /// <summary>
359    /// Delete existing ResourceGroup which is no parent group
360    /// </summary>
361    [TestMethod]
362    public void TestDeleteResourceGroup1() {
363      AuthenticationService cs = new AuthenticationService();
364
365      int cntPrev = GetNumberOfEntries(sqlCountGroups);
366      cs.DeleteResourceGroup(rg2);
367      int cntNew = GetNumberOfEntries(sqlCountGroups);
368      Assert.AreEqual(cntPrev, (cntNew + 1));
369    }
370
371    /// <summary>
372    /// try to delete existing ResourceGroup wich is parent group
373    /// </summary>
374    [TestMethod]
375    public void TestDeleteResourceGroup2() {
376      AuthenticationService cs = new AuthenticationService();
377      int cntPrev = GetNumberOfEntries(sqlCountGroups);
378      cs.DeleteResourceGroup(rg1);
379      int cntNew = GetNumberOfEntries(sqlCountGroups);
380      Assert.AreEqual(cntPrev, cntNew);
381    }
382    #endregion
383
384
385    #region TestClient
386
387    /// <summary>
388    /// AddClient
389    /// </summary>
390    [TestMethod]
391    public void TestAddClient1() {
392      AuthenticationService cs = new AuthenticationService();
393
394      DT.Client cl3 = new DT.Client();
395      cl3.Name = "clientName3";
396
397      IList<Guid> rgList = new List<Guid>();
398      rgList.Add(groupID3);
399      cl3.ResourceGroup = rgList;
400
401      cl3.Description = "Test-PC-3";
402      cl3.IPAdress = "192.168.0.30";
403      cl3.NumberOfProcessors = "2";
404      cl3.NumberOfThreads = "2";
405      cl3.OperatingSystem = "Vista";
406      cl3.MemorySize = "4096";
407      cl3.ProcessorType = "Intel";
408
409      int cntPrev = GetNumberOfEntries(sqlCountClients);
410      cl3.Id = cs.AddClient(cl3);
411      Assert.AreNotEqual(Guid.Empty, cl3.Id);
412      int cntNew = GetNumberOfEntries(sqlCountClients);
413      Assert.AreEqual(cntPrev, (cntNew - 1));
414
415      DA.Client client = dc.Resources.OfType<DA.Client>().First(x => x.Id == cl3.Id);
416      Assert.IsTrue(CompareClients(HL.Convert.ToDto(client), cl3));
417    }
418
419    /// <summary>
420    /// try to AddClient with not existing ResourceGroup
421    /// </summary>
422    [TestMethod]
423    public void TestAddClient2() {
424      AuthenticationService cs = new AuthenticationService();
425
426      DT.Client cl3 = new DT.Client();
427      cl3.Name = "clientNamexxxx";
428
429      IList<Guid> rgList = new List<Guid>();
430      rgList.Add(Guid.NewGuid());
431      cl3.ResourceGroup = rgList;
432
433      int cntPrev = GetNumberOfEntries(sqlCountClients);
434      cl3.Id = cs.AddClient(cl3);
435      Assert.AreEqual(Guid.Empty, cl3.Id);
436      int cntNew = GetNumberOfEntries(sqlCountClients);
437      Assert.AreEqual(cntPrev, cntNew);
438    }
439
440    /// <summary>
441    /// GetClient by Id
442    /// </summary>
443    [TestMethod]
444    public void TestGetClient1() {
445      AuthenticationService cs = new AuthenticationService();
446      DT.Client cl = cs.GetClient(clientID1);
447      Assert.AreEqual(cl.Id, clientID1);
448      Assert.IsTrue(CompareClients(cl, cl1));
449    }
450
451    /// <summary>
452    /// causes error
453    ///   !!!!!!!!!!!!!!!!!!!!!!
454    ///   
455    /// try GetClient with not existin id
456    /// </summary>
457    //[TestMethod]
458    //public void TestGetClient2() {
459    //  AuthenticationService cs = new AuthenticationService();
460    //  DT.Client cl = cs.GetClient(Guid.NewGuid());
461    //  Assert.IsNull(cl);
462    //}
463
464    ///// <summary>
465    ///// Test of Method "Client GetClient(Guid id)"
466    ///// </summary>
467    //[TestMethod]
468    //public void TestGetClient3() {
469    //  AuthenticationService cs = new AuthenticationService();
470    //  DT.Client cl = cs.GetClient(Guid.Empty);
471    //  Assert.IsNull(cl);
472    //}
473
474    //[TestMethod]
475    //public void TestUpdateClient() {
476    //}
477
478    /// <summary>
479    /// delete existing client
480    /// </summary>
481    [TestMethod]
482    public void TestDeleteClient1() {
483      AuthenticationService cs = new AuthenticationService();
484
485      int cntPrev = GetNumberOfEntries(sqlCountClients);
486      cs.DeleteClient(cl1);
487      int cntNew = GetNumberOfEntries(sqlCountClients);
488      Assert.AreEqual(cntPrev, (cntNew + 1));
489    }
490
491    ///// <summary>
492    ///// try to delete not existing client
493    ///// </summary>
494    //[TestMethod]
495    //public void TestDeleteClient2() {
496    //  AuthenticationService cs = new AuthenticationService();
497    //  DT.Client cl3 = new DT.Client();
498    //  cl3.Name = "clientName3";
499
500    //  IList<Guid> rgList = new List<Guid>();
501    //  rgList.Add(groupID3);
502    //  cl3.ResourceGroup = rgList;
503
504    //  cl3.Description = "Test-PC-3";
505    //  cl3.IPAdress = "192.168.0.30";
506    //  cl3.NumberOfProcessors = "2";
507    //  cl3.NumberOfThreads = "2";
508    //  cl3.OperatingSystem = "Vista";
509    //  cl3.MemorySize = "4096";
510    //  cl3.ProcessorType = "Intel";
511
512    //  int cntPrev = GetNumberOfEntries(sqlCountClients);
513    //  cs.DeleteClient(cl3);
514    //  int cntNew = GetNumberOfEntries(sqlCountClients);
515    //  Assert.AreEqual(cntPrev, cntNew);
516    //}
517
518    /// <summary>
519    /// get all clients
520    /// </summary>
521    [TestMethod]
522    public void TestGetClients() {
523      AuthenticationService cs = new AuthenticationService();
524      IEnumerable<DT.Client> clients = cs.GetClients();
525      Assert.AreEqual(2, clients.Count<DT.Client>());
526      Assert.IsTrue(
527        clients.Contains(cl1, new ClientEqualityComparer()) &&
528        clients.Contains(cl2, new ClientEqualityComparer())
529      );
530    }
531
532    /// <summary>
533    /// update existing client
534    /// </summary>
535    [TestMethod]
536    public void TestUpdateClient1() {
537      AuthenticationService cs = new AuthenticationService();
538
539      DA.Client client = dc.Resources.OfType<DA.Client>().First(x => x.Id == cl1.Id);
540      DT.Client modClient = HeuristicLab.Services.Authentication.Convert.ToDto(client);
541
542      //cl1.Id ;
543      //cl1.ResourceType ;
544      modClient.Name = "clientName1_modified";
545
546      IList<Guid> rgList = new List<Guid>();
547      rgList.Add(groupID3);
548      modClient.ResourceGroup = rgList;
549
550      modClient.Description = "Test-PC-1_modifiet";
551      modClient.IPAdress = "192.168.0.11";
552      modClient.NumberOfProcessors = "4";
553      modClient.NumberOfThreads = "2";
554      modClient.OperatingSystem = "Vista";
555      modClient.MemorySize = "2196";
556      modClient.ProcessorType = "Intel";
557
558      cs.UpdateClient(modClient);
559
560      //DA.Client client1 = dc.Resources.OfType<DA.Client>().First(x => x.Id == cl1.Id);
561      //Assert.IsTrue(CompareClients(HL.Convert.ToDto(client1), modClient));
562    }
563
564    ///// <summary>
565    ///// try to update not existing client
566    ///// </summary>
567    //[TestMethod]
568    //public void TestUpdateClient2() {
569    //  AuthenticationService cs = new AuthenticationService();
570    //  DT.Client cl3 = new DT.Client();
571    //  cl3.Id = Guid.NewGuid();
572    //  cl3.Name = "clientName3";
573
574    //  IList<Guid> rgList = new List<Guid>();
575    //  rgList.Add(groupID3);
576    //  cl3.ResourceGroup = rgList;
577
578    //  cl3.Description = "Test-PC-3";
579    //  cl3.IPAdress = "192.168.0.30";
580    //  cl3.NumberOfProcessors = "2";
581    //  cl3.NumberOfThreads = "2";
582    //  cl3.OperatingSystem = "Vista";
583    //  cl3.MemorySize = "4096";
584    //  cl3.ProcessorType = "Intel";
585
586    //  int cntPrev = GetNumberOfEntries(sqlCountClients);
587    //  //Assert.IsFalse(cs.UpdateClient(cl3));
588    //  int cntNew = GetNumberOfEntries(sqlCountClients);
589    //  Assert.AreEqual(cntPrev, cntNew);
590
591    //  DA.Client client = dc.Resources.OfType<DA.Client>().First(x => x.Id == cl3.Id);
592    //  Assert.IsNull(client);
593    //}
594
595    #endregion
596
597    /// <summary>
598    /// get Members
599    /// </summary>
600    [TestMethod]
601    public void TestGetMembers() {
602      AuthenticationService cs = new AuthenticationService();
603      IEnumerable<DT.Resource> members = cs.GetMembers(rg1.Id);
604      Assert.AreEqual(2, members.Count());
605    }
606
607    /// <summary>
608    /// get ResourceGroupsOf
609    /// </summary>
610    [TestMethod]
611    public void TestGetResourceGroupsOf() {
612      AuthenticationService cs = new AuthenticationService();
613      IEnumerable<DT.Resource> resGroups = cs.GetResourceGroupsOf(cl1.Id);
614      Assert.AreEqual(2, resGroups.Count());
615    }
616
617    /// <summary>
618    /// get Members
619    /// </summary>
620    [TestMethod]
621    public void TestGetRootResources() {
622      AuthenticationService cs = new AuthenticationService();
623      IEnumerable<DT.Resource> rootResourceList = cs.GetRootResources();
624      Assert.AreEqual(2, rootResourceList.Count());
625    }
626
627    #endregion //Testmethods
628  }
629}
Note: See TracBrowser for help on using the repository browser.