using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
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
{
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 {
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();
}
}
class ResourceEqualityComparer : IEqualityComparer {
public bool Equals(DT.Resource rg1, DT.Resource rg2) {
return (
rg1.Name == rg2.Name &&
rg1.Id == rg2.Id
);
}
public int GetHashCode(DT.Resource 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 bool CompareResources(DT.Resource r1, DT.Resource r2) {
return (
r1.Name == r2.Name &&
r1.Id == r2.Id
);
}
private static int GetNumberOfEntries(string sqlCommand) {
DA.ClientManagmentDataContext dc = new DA.ClientManagmentDataContext();
try {
IEnumerable cnt = dc.ExecuteQuery(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;
public DA.ResourceResourceGroup rrg4 = null;
public DA.ResourceResourceGroup rrg5 = 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().InsertOnSubmit(HL.Convert.ToEntity(rg1));
rg2 = new DT.ResourceGroup();
rg2.Name = groupName2;
rg2.Id = groupID2;
IList rgList = new List();
rgList.Add(rg1.Id);
rg2.ResourceGroup = rgList;
rg2.Description = "descrrg2";
dc.GetTable().InsertOnSubmit(HL.Convert.ToEntity(rg2));
rg3 = new DT.ResourceGroup();
rg3.Name = groupName3;
rg3.Id = groupID3;
rg3.Description = "descrrg3";
dc.GetTable().InsertOnSubmit(HL.Convert.ToEntity(rg3));
// create clients
cl1 = new DT.Client();
cl1.Name = clientName1;
cl1.Id = clientID1;
IList rgList1 = new List();
rgList.Add(groupID1);
cl1.ResourceGroup = rgList1;
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().InsertOnSubmit(HL.Convert.ToEntity(cl1));
cl2 = new DT.Client();
cl2.Name = clientName2;
cl2.Id = clientID2;
IList rgList2 = new List();
rgList.Add(groupID2);
cl2.ResourceGroup = rgList2;
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().InsertOnSubmit(HL.Convert.ToEntity(cl2));
// ResourceResourceGroups
rrg1 = new DA.ResourceResourceGroup();
rrg1.ResourceId = cl1.Id;
rrg1.ResourceGroupId = rg1.Id;
dc.GetTable().InsertOnSubmit(rrg1);
// ResourceResourceGroups
rrg5 = new DA.ResourceResourceGroup();
rrg5.ResourceId = cl1.Id;
rrg5.ResourceGroupId = rg2.Id;
dc.GetTable().InsertOnSubmit(rrg5);
DA.ResourceResourceGroup rrg2 = new DA.ResourceResourceGroup();
rrg2.ResourceId = cl2.Id;
rrg2.ResourceGroupId = rg2.Id;
dc.GetTable().InsertOnSubmit(rrg2);
//DA.ResourceResourceGroup rrg3 = new DA.ResourceResourceGroup();
//rrg3.ResourceId = rg3.Id;
//rrg3.ResourceGroupId = rg1.Id;
//dc.GetTable().InsertOnSubmit(rrg3);
rrg4 = new DA.ResourceResourceGroup();
rrg4.ResourceId = rg2.Id;
rrg4.ResourceGroupId = rg1.Id;
dc.GetTable().InsertOnSubmit(rrg4);
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
///
/// Add ResourceGroup
///
[TestMethod]
public void TestAddResourceGroup() {
AuthenticationService cs = new AuthenticationService();
DT.ResourceGroup rg4 = new DT.ResourceGroup();
rg4.Name = "groupName4";
rg4.Description = "descrrg4";
dc.GetTable().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().First(x => x.Id == rg4.Id);
Assert.IsTrue(CompareGroups(HL.Convert.ToDto(group), rg4));
}
///
///
[TestMethod]
public void TestUpdateResourceGroup() {
AuthenticationService cs = new AuthenticationService();
DA.ResourceGroup group = dc.Resources.OfType().First(x => x.Id == rg1.Id);
DT.ResourceGroup modGroup = HeuristicLab.Services.Authentication.Convert.ToDto(group);
modGroup.Name = "clientName1_modified";
IList rgList = new List();
rgList.Add(groupID3);
modGroup.ResourceGroup = rgList;
modGroup.Description = "Test-group_modifiet";
cs.UpdateResourceGroup(modGroup);
}
///
/// GetResourceGroup with existing id
///
[TestMethod]
public void TestGetResourceGroup() {
AuthenticationService cs = new AuthenticationService();
DT.ResourceGroup rg = cs.GetResourceGroup(groupID1);
Assert.AreEqual(rg.Id, groupID1);
Assert.IsTrue(CompareGroups(rg, rg1));
}
///
/// GetResourceGroups
///
[TestMethod]
public void TestGetResourceGroups() {
AuthenticationService cs = new AuthenticationService();
IEnumerable groups = cs.GetResourceGroups();
Assert.AreEqual(3, groups.Count());
Assert.IsTrue(
groups.Contains(rg1, new ResourceGroupEqualityComparer()) &&
groups.Contains(rg2, new ResourceGroupEqualityComparer()) &&
groups.Contains(rg3, new ResourceGroupEqualityComparer())
);
}
///
/// Delete existing ResourceGroup which is no parent group
///
[TestMethod]
public void TestDeleteResourceGroup1() {
AuthenticationService cs = new AuthenticationService();
int cntPrev = GetNumberOfEntries(sqlCountGroups);
cs.DeleteResourceGroup(rg2);
int cntNew = GetNumberOfEntries(sqlCountGroups);
Assert.AreEqual(cntPrev, (cntNew + 1));
}
///
/// try to delete existing ResourceGroup wich is parent group
///
[TestMethod]
public void TestDeleteResourceGroup2() {
AuthenticationService cs = new AuthenticationService();
int cntPrev = GetNumberOfEntries(sqlCountGroups);
cs.DeleteResourceGroup(rg1);
int cntNew = GetNumberOfEntries(sqlCountGroups);
Assert.AreEqual(cntPrev, cntNew);
}
#endregion
#region TestClient
///
/// AddClient
///
[TestMethod]
public void TestAddClient1() {
AuthenticationService cs = new AuthenticationService();
DT.Client cl3 = new DT.Client();
cl3.Name = "clientName3";
IList rgList = new List();
rgList.Add(groupID3);
cl3.ResourceGroup = rgList;
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().First(x => x.Id == cl3.Id);
Assert.IsTrue(CompareClients(HL.Convert.ToDto(client), cl3));
}
///
/// try to AddClient with not existing ResourceGroup
///
[TestMethod]
public void TestAddClient2() {
AuthenticationService cs = new AuthenticationService();
DT.Client cl3 = new DT.Client();
cl3.Name = "clientNamexxxx";
IList rgList = new List();
rgList.Add(Guid.NewGuid());
cl3.ResourceGroup = rgList;
int cntPrev = GetNumberOfEntries(sqlCountClients);
cl3.Id = cs.AddClient(cl3);
Assert.AreEqual(Guid.Empty, cl3.Id);
int cntNew = GetNumberOfEntries(sqlCountClients);
Assert.AreEqual(cntPrev, cntNew);
}
///
/// GetClient by Id
///
[TestMethod]
public void TestGetClient1() {
AuthenticationService cs = new AuthenticationService();
DT.Client cl = cs.GetClient(clientID1);
Assert.AreEqual(cl.Id, clientID1);
Assert.IsTrue(CompareClients(cl, cl1));
}
///
/// causes error
/// !!!!!!!!!!!!!!!!!!!!!!
///
/// try GetClient with not existin id
///
//[TestMethod]
//public void TestGetClient2() {
// AuthenticationService cs = new AuthenticationService();
// DT.Client cl = cs.GetClient(Guid.NewGuid());
// Assert.IsNull(cl);
//}
/////
///// Test of Method "Client GetClient(Guid id)"
/////
//[TestMethod]
//public void TestGetClient3() {
// AuthenticationService cs = new AuthenticationService();
// DT.Client cl = cs.GetClient(Guid.Empty);
// Assert.IsNull(cl);
//}
//[TestMethod]
//public void TestUpdateClient() {
//}
///
/// delete existing client
///
[TestMethod]
public void TestDeleteClient1() {
AuthenticationService cs = new AuthenticationService();
int cntPrev = GetNumberOfEntries(sqlCountClients);
cs.DeleteClient(cl1);
int cntNew = GetNumberOfEntries(sqlCountClients);
Assert.AreEqual(cntPrev, (cntNew + 1));
}
/////
///// try to delete not existing client
/////
//[TestMethod]
//public void TestDeleteClient2() {
// AuthenticationService cs = new AuthenticationService();
// DT.Client cl3 = new DT.Client();
// cl3.Name = "clientName3";
// IList rgList = new List();
// rgList.Add(groupID3);
// cl3.ResourceGroup = rgList;
// 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);
// cs.DeleteClient(cl3);
// int cntNew = GetNumberOfEntries(sqlCountClients);
// Assert.AreEqual(cntPrev, cntNew);
//}
///
/// get all clients
///
[TestMethod]
public void TestGetClients() {
AuthenticationService cs = new AuthenticationService();
IEnumerable clients = cs.GetClients();
Assert.AreEqual(2, clients.Count());
Assert.IsTrue(
clients.Contains(cl1, new ClientEqualityComparer()) &&
clients.Contains(cl2, new ClientEqualityComparer())
);
}
///
/// update existing client
///
[TestMethod]
public void TestUpdateClient1() {
AuthenticationService cs = new AuthenticationService();
DA.Client client = dc.Resources.OfType().First(x => x.Id == cl1.Id);
DT.Client modClient = HeuristicLab.Services.Authentication.Convert.ToDto(client);
//cl1.Id ;
//cl1.ResourceType ;
modClient.Name = "clientName1_modified";
IList rgList = new List();
rgList.Add(groupID3);
modClient.ResourceGroup = rgList;
modClient.Description = "Test-PC-1_modifiet";
modClient.IPAdress = "192.168.0.11";
modClient.NumberOfProcessors = "4";
modClient.NumberOfThreads = "2";
modClient.OperatingSystem = "Vista";
modClient.MemorySize = "2196";
modClient.ProcessorType = "Intel";
cs.UpdateClient(modClient);
//DA.Client client1 = dc.Resources.OfType().First(x => x.Id == cl1.Id);
//Assert.IsTrue(CompareClients(HL.Convert.ToDto(client1), modClient));
}
/////
///// try to update not existing client
/////
//[TestMethod]
//public void TestUpdateClient2() {
// AuthenticationService cs = new AuthenticationService();
// DT.Client cl3 = new DT.Client();
// cl3.Id = Guid.NewGuid();
// cl3.Name = "clientName3";
// IList rgList = new List();
// rgList.Add(groupID3);
// cl3.ResourceGroup = rgList;
// 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().First(x => x.Id == cl3.Id);
// Assert.IsNull(client);
//}
#endregion
///
/// get Members
///
[TestMethod]
public void TestGetMembers() {
AuthenticationService cs = new AuthenticationService();
IEnumerable members = cs.GetMembers(rg1.Id);
Assert.AreEqual(2, members.Count());
}
///
/// get ResourceGroupsOf
///
[TestMethod]
public void TestGetResourceGroupsOf() {
AuthenticationService cs = new AuthenticationService();
IEnumerable resGroups = cs.GetResourceGroupsOf(cl1.Id);
Assert.AreEqual(2, resGroups.Count());
}
///
/// get Members
///
[TestMethod]
public void TestGetRootResources() {
AuthenticationService cs = new AuthenticationService();
IEnumerable rootResourceList = cs.GetRootResources();
Assert.AreEqual(2, rootResourceList.Count());
}
#endregion //Testmethods
}
}