#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using DA = HeuristicLab.Services.Access.DataAccess; using DT = HeuristicLab.Services.Access.DataTransfer; namespace HeuristicLab.Services.Access.Tests { [TestClass] public class UnitTest { [TestMethod] public void ClearDB() { using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) { context.ExecuteCommand("DELETE FROM dbo.ResourceResourceGroup"); context.ExecuteCommand("DELETE FROM dbo.ClientLog"); context.ExecuteCommand("DELETE FROM dbo.Resource"); context.ExecuteCommand("DELETE FROM dbo.ClientConfiguration"); context.ExecuteCommand("DELETE FROM dbo.ClientError"); context.ExecuteCommand("DELETE FROM dbo.ClientType"); context.ExecuteCommand("DELETE FROM dbo.Country"); context.ExecuteCommand("DELETE FROM dbo.OperatingSystem"); context.ExecuteCommand("DELETE FROM dbo.ResourcePlugin"); context.ExecuteCommand("DELETE FROM dbo.Plugin"); context.ExecuteCommand("DELETE FROM dbo.UserGroupUserGroup"); context.ExecuteCommand("DELETE FROM dbo.UserGroup"); } } [TestMethod] public void AddClientGroupTest() { ClearDB(); AccessService service = new AccessService(); DT.ClientConfiguration config = new DT.ClientConfiguration() { Description = "fatConfig", Hash = "xyz" }; DT.Country country = new DT.Country() { Name = "Austria" }; DT.ClientType clientType = new DT.ClientType() { Name = "Client" }; DT.OperatingSystem os = new DT.OperatingSystem() { Name = "Windows 7" }; DT.Client client = new DT.Client() { ClientConfiguration = config, ClientType = clientType, Country = country, Description = "testClient", HeuristicLabVersion = "3.3.6", MemorySize = 512, NumberOfCores = 4, Name = "testClient", OperatingSystem = os, ProcessorType = "Intel i7", Timestamp = DateTime.Now }; Guid id = service.AddClient(client); Assert.IsTrue(service.ClientExists(id)); Assert.AreEqual(1, service.GetAllClients().Count()); service.DeleteClient(service.GetAllClients().First()); Assert.AreEqual(0, service.GetAllClients().Count()); } [TestMethod] public void AddGroupTest() { ClearDB(); AccessService service = new AccessService(); DT.ClientGroup parentGroup = new DT.ClientGroup() { Name = "Parent", Description = "Parent Group" }; DT.ClientGroup childGroup = new DT.ClientGroup() { Name = "Child", Description = "Child Group" }; DT.ClientGroup otherGroup = new DT.ClientGroup() { Name = "Other", Description = "Other Group" }; parentGroup.Id = service.AddClientGroup(parentGroup); childGroup.Id = service.AddClientGroup(childGroup); otherGroup.Id = service.AddClientGroup(otherGroup); List groupGuids = new List(); groupGuids.Add(parentGroup.Id); groupGuids.Add(childGroup.Id); Assert.AreEqual(3, service.GetAllClientGroups().Count()); var childParentList = service.GetClientGroups(groupGuids); Assert.AreEqual(2, childParentList.Count()); service.AddResourceToGroup(childGroup, parentGroup); var mapping = service.GetClientGroupMapping(); Assert.AreEqual(1, mapping.Count()); Assert.AreEqual(mapping.First().Child, childGroup.Id); Assert.AreEqual(mapping.First().Parent, parentGroup.Id); service.DeleteClientGroup(otherGroup); Assert.AreEqual(2, service.GetAllClientGroups().Count()); service.RemoveResourceFromGroup(childGroup, parentGroup); Assert.AreEqual(0, service.GetClientGroupMapping().Count()); } [TestMethod] public void AddClientLogTest() { ClearDB(); AccessService service = new AccessService(); DT.Client client = new DT.Client() { Description = "testClient", HeuristicLabVersion = "3.3.6", MemorySize = 512, NumberOfCores = 4, Name = "testClient", ProcessorType = "Intel i7", Timestamp = DateTime.Now }; client.Id = service.AddClient(client); Assert.IsTrue(service.ClientExists(client.Id)); DateTime now = DateTime.Now; DT.ClientLog log = new DT.ClientLog() { Message = "testMessage", ResourceId = client.Id, Timestamp = now }; service.AddClientLog(log); Assert.AreEqual(1, service.GetClientLogsSince(now).Count()); } [TestMethod] public void AddUser() { ClearDB(); AccessService service = new AccessService(); DT.User user = new DT.User(); user.FullName = "Max Mustermann"; user.UserName = "max"; user.IsApproved = true; user.Comment = "this is a comment"; user.Email = "max@mail.com"; DT.User newUser = service.AddUser(user); var users = service.GetAllUsers(); Assert.AreEqual(1, users.Where(x => x.UserName == newUser.UserName).Count()); service.DeleteUser(newUser); users = service.GetAllUsers(); Assert.AreEqual(0, users.Where(x => x.UserName == newUser.UserName).Count()); } [TestMethod] public void AddUserGroup() { ClearDB(); AccessService service = new AccessService(); DT.User user = new DT.User(); user.FullName = "Max Mustermann"; user.UserName = "max"; user.IsApproved = true; user.Comment = "this is a comment"; user.Email = "max@mail.com"; DT.User user2 = new DT.User(); user2.FullName = "Franz Fritz"; user2.UserName = "Franz"; user2.IsApproved = true; user2.Comment = "this is a franz comment"; user2.Email = "franz@mail.com"; DT.User newUser = service.AddUser(user); DT.User newUser2 = service.AddUser(user2); DT.UserGroup userGroup = new DT.UserGroup(); userGroup.Name = "testGroup"; userGroup.Id = service.AddUserGroup(userGroup); Assert.AreEqual(1, service.GetAllUserGroups().Count()); service.AddUserGroupBaseToGroup(newUser, userGroup); service.AddUserGroupBaseToGroup(newUser2, userGroup); Assert.AreEqual(2, service.GetUserGroupMapping().Count()); DT.Role role = service.AddRole(new DT.Role() { Name = "NewGroup" }); Assert.AreEqual(1, service.GetRoles().Where(x => x.Name == role.Name).Count()); service.AddRoleToGroup(userGroup, role); Assert.AreEqual(1, service.GetUserRoles(newUser).Count()); Assert.AreEqual(1, service.GetUserRoles(newUser2).Count()); service.RemoveRoleFromGroup(userGroup, role); Assert.AreEqual(0, service.GetUserRoles(newUser).Count()); Assert.AreEqual(0, service.GetUserRoles(newUser2).Count()); service.DeleteUser(newUser); service.DeleteUser(newUser2); var users = service.GetAllUsers(); Assert.AreEqual(0, users.Where(x => x.UserName == newUser.UserName).Count()); service.DeleteRole(role); Assert.AreEqual(0, service.GetRoles().Where(x => x.Name == role.Name).Count()); } [TestMethod] public void AddClientError() { ClearDB(); AccessService service = new AccessService(); DT.ClientError error = new DT.ClientError(); error.Timestamp = DateTime.Now; error.UserComment = "this happend when i clicked on..."; error.Exception = "Exception"; error.ConfigDump = "config"; service.ReportError(error); Assert.AreEqual(1, service.GetClientErrors().Count()); service.DeleteError(service.GetClientErrors().First()); Assert.AreEqual(0, service.GetClientErrors().Count()); } } }