Index: branches/ClientManagement/HeuristicLab.Services.Authentication/TestWebService/TestWebServiec.cs
===================================================================
--- branches/ClientManagement/HeuristicLab.Services.Authentication/TestWebService/TestWebServiec.cs	(revision 4958)
+++ branches/ClientManagement/HeuristicLab.Services.Authentication/TestWebService/TestWebServiec.cs	(revision 5004)
@@ -4,19 +4,517 @@
 using System.Linq;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
-
-namespace TestWebService
-{
-    [TestClass]
-    public class WebserviceUnitTest
-    {
-        public void Init()
-        {
-            
-        }
-
-        [TestMethod]
-        public void TestMethod1()
-        {
-        }
-    }
+using HeuristicLab.Services.Authentication;
+using HL = HeuristicLab.Services.Authentication;
+using DA = HeuristicLab.Services.Authentication.DataAccess;
+using DT = HeuristicLab.Services.Authentication.DataTransfer;
+using System.Data;
+using System.Data.Common;
+using System.Data.Linq;
+using System.Collections;
+
+namespace TestWebService {
+  [TestClass]
+  public class WebserviceUnitTest {
+
+    private string sqlCountClients = @"SELECT COUNT(*) FROM Resource where ResourceType = 'Client'";
+    private string sqlCountGroups = @"SELECT COUNT(*) FROM Resource where ResourceType = 'ResourceGroup'";
+
+    #region TestIdsAndNames
+    private Guid groupID1 = Guid.NewGuid();
+    private string groupName1 = "groupName1";
+    private Guid groupID2 = Guid.NewGuid();
+    private string groupName2 = "groupName2";
+    private Guid groupID3 = Guid.NewGuid();
+    private string groupName3 = "groupName3";
+
+    private Guid clientID1 = Guid.NewGuid();
+    private string clientName1 = "clientName1";
+    private Guid clientID2 = Guid.NewGuid();
+    private string clientName2 = "clientName2";
+    #endregion
+
+    #region ValidationMethods
+    class ClientEqualityComparer : IEqualityComparer<DT.Client> {
+      public bool Equals(DT.Client c1, DT.Client c2) {
+        return (
+          c1.Name == c2.Name &&
+          c1.Id == c2.Id &&
+          c1.Description == c2.Description &&
+          c1.IPAdress == c2.IPAdress &&
+          c1.NumberOfProcessors == c2.NumberOfProcessors &&
+          c1.NumberOfThreads == c2.NumberOfThreads &&
+          c1.OperatingSystem == c2.OperatingSystem &&
+          c1.ProcessorType == c2.ProcessorType
+        );
+      }
+
+      public int GetHashCode(DT.Client obj) {
+        throw new NotImplementedException();
+      }
+    }
+
+    class ResourceGroupEqualityComparer : IEqualityComparer<DT.ResourceGroup> {
+      public bool Equals(DT.ResourceGroup rg1, DT.ResourceGroup rg2) {
+        return (
+          rg1.Name == rg2.Name &&
+          rg1.Id == rg2.Id &&
+          rg1.Description == rg2.Description
+        );
+      }
+
+      public int GetHashCode(DT.ResourceGroup obj) {
+        throw new NotImplementedException();
+      }
+    }
+
+    private bool CompareClients(DT.Client c1, DT.Client c2) {
+      return (
+        c1.Name == c2.Name &&
+        c1.Id == c2.Id &&
+        //c1.ResourceType == c2.ResourceType &&
+        c1.Description == c2.Description &&
+        c1.IPAdress == c2.IPAdress &&
+        c1.NumberOfProcessors == c2.NumberOfProcessors &&
+        c1.NumberOfThreads == c2.NumberOfThreads &&
+        c1.OperatingSystem == c2.OperatingSystem &&
+        c1.ProcessorType == c2.ProcessorType
+        );
+    }
+
+    private bool CompareGroups(DT.ResourceGroup r1, DT.ResourceGroup r2) {
+      return (
+        r1.Name == r2.Name &&
+        r1.Id == r2.Id &&
+        //c1.ResourceType == c2.ResourceType &&
+        r1.Description == r2.Description
+        );
+    }
+
+    private static int GetNumberOfEntries(string sqlCommand) {
+      DA.ClientManagmentDataContext dc = new DA.ClientManagmentDataContext();
+      try {
+        IEnumerable<int> cnt = dc.ExecuteQuery<int>(sqlCommand);
+        return (int)cnt.First();
+      }
+      catch (Exception e) {
+        return 0;
+      }
+    }
+    #endregion
+
+    #region PublicResources
+    DA.ClientManagmentDataContext dc = null;
+
+    public DT.Client cl1 = null;
+    public DT.Client cl2 = null;
+    public DT.Client cl3 = null;
+
+    public DT.ResourceGroup rg1 = null;
+    public DT.ResourceGroup rg2 = null;
+    public DT.ResourceGroup rg3 = null;
+
+    public DA.ResourceResourceGroup rrg1 = null;
+    public DA.ResourceResourceGroup rrg2 = null;
+    public DA.ResourceResourceGroup rrg3 = null;
+    #endregion
+    
+    #region Additional test attributes
+    //
+    // You can use the following additional attributes as you write your tests:
+    //
+    // Use ClassInitialize to run code before running the first test in the class
+    // [ClassInitialize()]
+    // public static void MyClassInitialize(TestContext testContext) { }
+    //
+    // Use ClassCleanup to run code after all tests in a class have run
+    // [ClassCleanup()]
+    // public static void MyClassCleanup() { }
+    //
+    // Use TestInitialize to run code before running each test 
+    // [TestInitialize()]
+    // public void MyTestInitialize() { }
+    //
+    // Use TestCleanup to run code after each test has run
+    // c
+    // public void MyTestCleanup() { }
+    //
+    #endregion
+
+    [TestInitialize()]
+    public void Init() {  
+
+      dc = new DA.ClientManagmentDataContext();
+      if (dc.DatabaseExists()) {
+        //dc.DeleteDatabase();
+        dc.ExecuteCommand("delete from ResourceResourceGroup");
+        dc.ExecuteCommand("delete from Resource");
+        dc.SubmitChanges();
+      }
+      else {
+        dc.CreateDatabase();
+        dc.SubmitChanges();
+      }
+
+      
+      // create groups
+      rg1 = new DT.ResourceGroup();
+      rg1.Name = groupName1;
+      rg1.Id = groupID1;      
+      rg1.Description = "descr rg1";
+      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(rg1));
+
+      rg2 = new DT.ResourceGroup();
+      rg2.Name = groupName2;
+      rg2.Id = groupID2;
+      rg2.ResourceGroup = rg1.Id;
+      rg2.Description = "descrrg2";
+      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(rg2));
+
+      rg3 = new DT.ResourceGroup();
+      rg3.Name = groupName3;
+      rg3.Id = groupID3;
+      rg3.Description = "descrrg3";
+      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(rg3));
+
+      // create clients
+      cl1 = new DT.Client();
+      cl1.Name = clientName1;
+      cl1.Id = clientID1;
+      cl1.ResourceGroup = groupID1;
+      cl1.Description = "Test-PC-1";
+      cl1.IPAdress = "192.168.0.10";
+      cl1.NumberOfProcessors = "2";
+      cl1.NumberOfThreads = "4";
+      cl1.OperatingSystem = "WINXP";
+      cl1.MemorySize = "2048";
+      cl1.ProcessorType = "AMD";
+      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(cl1));
+
+      cl2 = new DT.Client();
+      cl2.Name = clientName2;
+      cl2.Id = clientID2;
+      cl2.ResourceGroup = groupID2;
+      cl2.Description = "Test-PC-2";
+      cl2.IPAdress = "192.168.0.20";
+      cl2.NumberOfProcessors = "4";
+      cl2.NumberOfThreads = "4";
+      cl2.OperatingSystem = "Vista";
+      cl2.MemorySize = "4096";
+      cl2.ProcessorType = "Intel";
+      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(cl2));
+
+      // ResourceResourceGroups
+      rrg1 = new DA.ResourceResourceGroup();
+      rrg1.ResourceId = cl1.Id;
+      rrg1.ResourceGroupId = rg1.Id;
+      dc.GetTable<DA.ResourceResourceGroup>().InsertOnSubmit(rrg1);
+
+      DA.ResourceResourceGroup rrg2 = new DA.ResourceResourceGroup();
+      rrg2.ResourceId = cl2.Id;
+      rrg2.ResourceGroupId = rg2.Id;
+      dc.GetTable<DA.ResourceResourceGroup>().InsertOnSubmit(rrg2);
+
+      DA.ResourceResourceGroup rrg3 = new DA.ResourceResourceGroup();
+      rrg3.ResourceId = rg1.Id;
+      rrg3.ResourceGroupId = rg3.Id;
+      dc.GetTable<DA.ResourceResourceGroup>().InsertOnSubmit(rrg3);
+
+      dc.SubmitChanges();
+    }
+
+    [TestCleanup()]
+    public void CleanUp() {
+      dc = new DA.ClientManagmentDataContext();
+      if (dc.DatabaseExists()) {
+        //dc.DeleteDatabase();
+        dc.ExecuteCommand("delete from ResourceResourceGroup");
+        dc.ExecuteCommand("delete from Resource");
+        dc.SubmitChanges();
+      }
+      else {
+        dc.CreateDatabase();
+        dc.SubmitChanges();
+      }
+    }
+
+    #region TestMethods    
+
+    #region TestGroup
+
+    /// <summary>
+    /// Add ResourceGroup
+    /// </summary>
+    [TestMethod]
+    public void TestAddResourceGroup() {
+      ClientService cs = new ClientService();
+
+      DT.ResourceGroup rg4 = new DT.ResourceGroup();
+      rg4.Name = "groupName4";
+      rg4.Description = "descrrg4";
+      dc.GetTable<DA.Resource>().InsertOnSubmit(HL.Convert.ToEntity(rg4));
+
+      int cntPrev = GetNumberOfEntries(sqlCountGroups);
+      rg4.Id = cs.AddResourceGroup(rg4);
+      Assert.AreNotEqual(Guid.Empty, rg4.Id);
+      int cntNew = GetNumberOfEntries(sqlCountGroups);
+      Assert.AreEqual(cntPrev, (cntNew - 1));
+
+      DA.ResourceGroup group = dc.Resources.OfType<DA.ResourceGroup>().First(x => x.Id == rg4.Id);
+      Assert.IsTrue(CompareGroups(HL.Convert.ToDto(group), rg4));
+    }
+
+    /// <summary>
+    /// TODO
+    /// </summary>
+    [TestMethod]
+    public void TestUpdateResourceGroup() {
+    }
+
+    /// <summary>
+    /// GetResourceGroup with existing id
+    /// </summary>
+    [TestMethod]
+    public void TestGetResourceGroup() {
+      ClientService cs = new ClientService();
+      DT.ResourceGroup rg = cs.GetResourceGroup(groupID1);
+      Assert.AreEqual(rg.Id, groupID1);
+      Assert.IsTrue(CompareGroups(rg, rg1));
+    }
+
+    /// <summary>
+    /// GetResourceGroups
+    /// </summary>
+    [TestMethod]
+    public void TestGetResourceGroups() {
+      ClientService cs = new ClientService();
+      IEnumerable<DT.ResourceGroup> groups = cs.GetResourceGroups();
+      Assert.AreEqual(3, groups.Count<DT.ResourceGroup>());
+      Assert.IsTrue(
+        groups.Contains(rg1, new ResourceGroupEqualityComparer()) &&
+        groups.Contains(rg2, new ResourceGroupEqualityComparer()) &&
+        groups.Contains(rg3, new ResourceGroupEqualityComparer())
+      );
+    }
+
+    /// <summary>
+    /// Delete existing ResourceGroup whisch is no parent group
+    /// </summary>
+    [TestMethod]
+    public void TestDeleteResourceGroup1() {
+      ClientService cs = new ClientService();
+
+      int cntPrev = GetNumberOfEntries(sqlCountGroups);
+      Assert.IsTrue(cs.DeleteResourceGroup(rg2));
+      int cntNew = GetNumberOfEntries(sqlCountGroups);
+      Assert.AreEqual(cntPrev, (cntNew + 1));
+    }
+
+    /// <summary>
+    /// try to delete existing ResourceGroup whisch is parent group
+    /// </summary>
+    [TestMethod]
+    public void TestDeleteResourceGroup2() {
+      ClientService cs = new ClientService();
+
+      int cntPrev = GetNumberOfEntries(sqlCountGroups);
+      Assert.IsFalse(cs.DeleteResourceGroup(rg1));
+      int cntNew = GetNumberOfEntries(sqlCountGroups);
+      Assert.AreEqual(cntPrev, cntNew);
+    }
+    #endregion
+
+
+    #region TestClient
+
+    /// <summary>
+    /// AddClient
+    /// </summary>
+    [TestMethod]
+    public void TestAddClient1() {
+      ClientService cs = new ClientService();
+
+      DT.Client cl3 = new DT.Client();
+      cl3.Name = "clientName3";
+      cl3.ResourceGroup = groupID3;
+      cl3.Description = "Test-PC-3";
+      cl3.IPAdress = "192.168.0.30";
+      cl3.NumberOfProcessors = "2";
+      cl3.NumberOfThreads = "2";
+      cl3.OperatingSystem = "Vista";
+      cl3.MemorySize = "4096";
+      cl3.ProcessorType = "Intel";
+
+      int cntPrev = GetNumberOfEntries(sqlCountClients);
+      cl3.Id = cs.AddClient(cl3);
+      Assert.AreNotEqual(Guid.Empty, cl3.Id); 
+      int cntNew = GetNumberOfEntries(sqlCountClients);
+      Assert.AreEqual(cntPrev, (cntNew - 1));
+
+      DA.Client client = dc.Resources.OfType<DA.Client>().First(x => x.Id == cl3.Id);
+      Assert.IsTrue(CompareClients(HL.Convert.ToDto(client), cl3));
+    }
+
+    /// <summary>
+    /// try to AddClient with not existing ResourceGroup
+    /// </summary>
+    [TestMethod]
+    public void TestAddClient2() {
+      ClientService cs = new ClientService();
+
+      DT.Client cl3 = new DT.Client();
+      cl3.Name = "clientNamexxxx";
+      cl3.ResourceGroup = Guid.NewGuid();
+
+      int cntPrev = GetNumberOfEntries(sqlCountClients);
+      cl3.Id = cs.AddClient(cl3);
+      Assert.AreEqual(Guid.Empty, cl3.Id);
+      int cntNew = GetNumberOfEntries(sqlCountClients);
+      Assert.AreEqual(cntPrev, cntNew); 
+    }
+
+    /// <summary>
+    /// GetClient by Id
+    /// </summary>
+    [TestMethod]
+    public void TestGetClient1() {
+      ClientService cs = new ClientService();
+      DT.Client cl = cs.GetClient(clientID1);
+      Assert.AreEqual(cl.Id, clientID1);
+      Assert.IsTrue(CompareClients(cl, cl1));
+    }
+
+    /// <summary>
+    /// causes error
+    ///   !!!!!!!!!!!!!!!!!!!!!!
+    ///   
+    /// try GetClient with not existin id
+    /// </summary>
+    //[TestMethod]
+    //public void TestGetClient2() {
+    //  ClientService cs = new ClientService();
+    //  DT.Client cl = cs.GetClient(Guid.NewGuid());
+    //  Assert.IsNull(cl);
+    //}
+
+    ///// <summary>
+    ///// Test of Method "Client GetClient(Guid id)"
+    ///// </summary>
+    //[TestMethod]
+    //public void TestGetClient3() {
+    //  ClientService cs = new ClientService();
+    //  DT.Client cl = cs.GetClient(Guid.Empty);
+    //  Assert.IsNull(cl);
+    //}
+
+    //[TestMethod]
+    //public void TestUpdateClient() {
+    //}
+
+    /// <summary>
+    /// delete existing client
+    /// </summary>
+    [TestMethod]
+    public void TestDeleteClient1() {
+      ClientService cs = new ClientService();
+
+      int cntPrev = GetNumberOfEntries(sqlCountClients);
+      Assert.IsTrue(cs.DeleteClient(cl1));
+      int cntNew = GetNumberOfEntries(sqlCountClients);
+      Assert.AreEqual(cntPrev, (cntNew + 1));
+    }
+
+    /// <summary>
+    /// try to delete not existing client
+    /// </summary>
+    [TestMethod]
+    public void TestDeleteClient2() {
+      ClientService cs = new ClientService();
+      DT.Client cl3 = new DT.Client();
+      cl3.Name = "clientName3";
+      cl3.ResourceGroup = groupID3;
+      cl3.Description = "Test-PC-3";
+      cl3.IPAdress = "192.168.0.30";
+      cl3.NumberOfProcessors = "2";
+      cl3.NumberOfThreads = "2";
+      cl3.OperatingSystem = "Vista";
+      cl3.MemorySize = "4096";
+      cl3.ProcessorType = "Intel";
+
+      int cntPrev = GetNumberOfEntries(sqlCountClients);
+      Assert.IsFalse(cs.DeleteClient(cl3));
+      int cntNew = GetNumberOfEntries(sqlCountClients);
+      Assert.AreEqual(cntPrev, cntNew);
+    }
+
+    /// <summary>
+    /// get all clients
+    /// </summary>
+    [TestMethod]
+    public void TestGetClients() {
+      ClientService cs = new ClientService();
+      IEnumerable<DT.Client> clients = cs.GetClients();
+      Assert.AreEqual(2, clients.Count<DT.Client>());
+      Assert.IsTrue(
+        clients.Contains(cl1, new ClientEqualityComparer()) &&
+        clients.Contains(cl2, new ClientEqualityComparer())
+      );
+    }
+
+    /// <summary>
+    /// update existing client
+    /// </summary>
+    [TestMethod]
+    public void TestUpdateClient1() {
+      ClientService cs = new ClientService();
+      DT.Client cl1 = new DT.Client();
+      cl1.Name = "clientName1_modified";
+      cl1.ResourceGroup = groupID3;
+      cl1.Description = "Test-PC-1_modifiet";
+      cl1.IPAdress = "192.168.0.11";
+      cl1.NumberOfProcessors = "4";
+      cl1.NumberOfThreads = "2";
+      cl1.OperatingSystem = "Vista";
+      cl1.MemorySize = "2096";
+      cl1.ProcessorType = "Intel";
+
+      int cntPrev = GetNumberOfEntries(sqlCountClients);
+      Assert.IsTrue(cs.UpdateClient(cl1));
+      int cntNew = GetNumberOfEntries(sqlCountClients);
+      Assert.AreEqual(cntPrev, cntNew);
+
+      DA.Client client = dc.Resources.OfType<DA.Client>().First(x => x.Id == cl1.Id);
+      Assert.IsTrue(CompareClients(HL.Convert.ToDto(client), cl1));
+    }
+
+    /// <summary>
+    /// try to update not existing client
+    /// </summary>
+    [TestMethod]
+    public void TestUpdateClient2() {
+      ClientService cs = new ClientService();
+      DT.Client cl3 = new DT.Client();
+      cl3.Id = Guid.NewGuid();
+      cl3.Name = "clientName3";
+      cl3.ResourceGroup = groupID3;
+      cl3.Description = "Test-PC-3";
+      cl3.IPAdress = "192.168.0.30";
+      cl3.NumberOfProcessors = "2";
+      cl3.NumberOfThreads = "2";
+      cl3.OperatingSystem = "Vista";
+      cl3.MemorySize = "4096";
+      cl3.ProcessorType = "Intel";
+
+      int cntPrev = GetNumberOfEntries(sqlCountClients);
+      Assert.IsFalse(cs.UpdateClient(cl3));
+      int cntNew = GetNumberOfEntries(sqlCountClients);
+      Assert.AreEqual(cntPrev, cntNew);
+
+      DA.Client client = dc.Resources.OfType<DA.Client>().First(x => x.Id == cl3.Id);
+      Assert.IsNull(client);
+    }
+
+    #endregion
+   
+    #endregion //Testmethods
+  }
 }
