Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClientUserManagement/HeuristicLab.Services.Access-3.3.Tests/UnitTest.cs @ 7623

Last change on this file since 7623 was 7619, checked in by ascheibe, 13 years ago

#1648

  • added service description file
  • renamed database to correct name
  • fixed sql scripts for creating the db
File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26using DA = HeuristicLab.Services.Access.DataAccess;
27using DT = HeuristicLab.Services.Access.DataTransfer;
28
29namespace HeuristicLab.Services.Access.Tests {
30  [TestClass]
31  public class UnitTest {
32
33    [TestMethod]
34    public void ClearDB() {
35      using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
36        context.ExecuteCommand("DELETE FROM dbo.ResourceResourceGroup");
37        context.ExecuteCommand("DELETE FROM dbo.ClientLog");
38        context.ExecuteCommand("DELETE FROM dbo.Resource");
39        context.ExecuteCommand("DELETE FROM dbo.ClientConfiguration");
40        context.ExecuteCommand("DELETE FROM dbo.ClientError");
41        context.ExecuteCommand("DELETE FROM dbo.ClientType");
42        context.ExecuteCommand("DELETE FROM dbo.Country");
43        context.ExecuteCommand("DELETE FROM dbo.OperatingSystem");
44        context.ExecuteCommand("DELETE FROM dbo.ResourcePlugin");
45        context.ExecuteCommand("DELETE FROM dbo.Plugin");
46        context.ExecuteCommand("DELETE FROM dbo.UserGroupUserGroup");
47        context.ExecuteCommand("DELETE FROM dbo.UserGroup");
48      }
49    }
50
51    [TestMethod]
52    public void AddClientGroupTest() {
53      ClearDB();
54      AccessService service = new AccessService();
55      DT.ClientConfiguration config = new DT.ClientConfiguration() { Description = "fatConfig", Hash = "xyz" };
56      DT.Country country = new DT.Country() { Name = "Austria" };
57      DT.ClientType clientType = new DT.ClientType() { Name = "Client" };
58      DT.OperatingSystem os = new DT.OperatingSystem() { Name = "Windows 7" };
59      Guid id = Guid.NewGuid();
60
61      DT.Client client = new DT.Client() {
62        ClientConfiguration = config,
63        ClientType = clientType,
64        Country = country,
65        Description = "testClient",
66        HeuristicLabVersion = "3.3.6",
67        MemorySize = 512,
68        NumberOfCores = 4,
69        Name = "testClient",
70        OperatingSystem = os,
71        ProcessorType = "Intel i7",
72        Timestamp = DateTime.Now,
73        Id = id
74      };
75
76      service.AddClient(client);
77      Assert.IsTrue(service.ClientExists(id));
78      Assert.AreEqual(1, service.GetAllClients().Count());
79
80      service.DeleteClient(service.GetAllClients().First());
81      Assert.AreEqual(0, service.GetAllClients().Count());
82    }
83
84    [TestMethod]
85    public void AddGroupTest() {
86      ClearDB();
87      AccessService service = new AccessService();
88
89      DT.ClientGroup parentGroup = new DT.ClientGroup() { Name = "Parent", Description = "Parent Group" };
90      DT.ClientGroup childGroup = new DT.ClientGroup() { Name = "Child", Description = "Child Group" };
91      DT.ClientGroup otherGroup = new DT.ClientGroup() { Name = "Other", Description = "Other Group" };
92
93      parentGroup.Id = service.AddClientGroup(parentGroup);
94      childGroup.Id = service.AddClientGroup(childGroup);
95      otherGroup.Id = service.AddClientGroup(otherGroup);
96
97      List<Guid> groupGuids = new List<Guid>();
98      groupGuids.Add(parentGroup.Id);
99      groupGuids.Add(childGroup.Id);
100
101      Assert.AreEqual(3, service.GetAllClientGroups().Count());
102      var childParentList = service.GetClientGroups(groupGuids);
103      Assert.AreEqual(2, childParentList.Count());
104
105      service.AddResourceToGroup(childGroup, parentGroup);
106      var mapping = service.GetClientGroupMapping();
107      Assert.AreEqual(1, mapping.Count());
108      Assert.AreEqual(mapping.First().Child, childGroup.Id);
109      Assert.AreEqual(mapping.First().Parent, parentGroup.Id);
110
111      service.DeleteClientGroup(otherGroup);
112      Assert.AreEqual(2, service.GetAllClientGroups().Count());
113
114      service.RemoveResourceFromGroup(childGroup, parentGroup);
115      Assert.AreEqual(0, service.GetClientGroupMapping().Count());
116    }
117
118    [TestMethod]
119    public void AddClientLogTest() {
120      ClearDB();
121      AccessService service = new AccessService();
122      Guid id = Guid.NewGuid();
123
124      DT.Client client = new DT.Client() {
125        Description = "testClient",
126        HeuristicLabVersion = "3.3.6",
127        MemorySize = 512,
128        NumberOfCores = 4,
129        Name = "testClient",
130        ProcessorType = "Intel i7",
131        Timestamp = DateTime.Now,
132        Id = id
133      };
134
135      service.AddClient(client);
136      Assert.IsTrue(service.ClientExists(client.Id));
137
138      DateTime now = DateTime.Now;
139      DT.ClientLog log = new DT.ClientLog() { Message = "testMessage", ResourceId = client.Id, Timestamp = now };
140      service.AddClientLog(log);
141      Assert.AreEqual(1, service.GetClientLogsSince(now).Count());
142    }
143
144    [TestMethod]
145    public void AddUser() {
146      ClearDB();
147      AccessService service = new AccessService();
148
149      DT.User user = new DT.User();
150      user.FullName = "Max Mustermann";
151      user.UserName = "max";
152      user.IsApproved = true;
153      user.Comment = "this is a comment";
154      user.Email = "max@mail.com";
155
156      DT.User newUser = service.AddUser(user);
157      var users = service.GetAllUsers();
158      Assert.AreEqual(1, users.Where(x => x.UserName == newUser.UserName).Count());
159      service.DeleteUser(newUser);
160      users = service.GetAllUsers();
161      Assert.AreEqual(0, users.Where(x => x.UserName == newUser.UserName).Count());
162    }
163
164    [TestMethod]
165    public void AddUserGroup() {
166      ClearDB();
167      AccessService service = new AccessService();
168
169      DT.User user = new DT.User();
170      user.FullName = "Max Mustermann";
171      user.UserName = "max";
172      user.IsApproved = true;
173      user.Comment = "this is a comment";
174      user.Email = "max@mail.com";
175
176      DT.User user2 = new DT.User();
177      user2.FullName = "Franz Fritz";
178      user2.UserName = "Franz";
179      user2.IsApproved = true;
180      user2.Comment = "this is a franz comment";
181      user2.Email = "franz@mail.com";
182
183      DT.User newUser = service.AddUser(user);
184      DT.User newUser2 = service.AddUser(user2);
185
186      DT.UserGroup userGroup = new DT.UserGroup();
187      userGroup.Name = "testGroup";
188      userGroup.Id = service.AddUserGroup(userGroup);
189      Assert.AreEqual(1, service.GetAllUserGroups().Count());
190
191      service.AddUserGroupBaseToGroup(newUser, userGroup);
192      service.AddUserGroupBaseToGroup(newUser2, userGroup);
193      Assert.AreEqual(2, service.GetUserGroupMapping().Count());
194
195      DT.Role role = service.AddRole(new DT.Role() { Name = "NewGroup" });
196      Assert.AreEqual(1, service.GetRoles().Where(x => x.Name == role.Name).Count());
197
198      service.AddRoleToGroup(userGroup, role);
199      Assert.AreEqual(1, service.GetUserRoles(newUser).Count());
200      Assert.AreEqual(1, service.GetUserRoles(newUser2).Count());
201
202      service.RemoveRoleFromGroup(userGroup, role);
203
204      Assert.AreEqual(0, service.GetUserRoles(newUser).Count());
205      Assert.AreEqual(0, service.GetUserRoles(newUser2).Count());
206
207      service.DeleteUser(newUser);
208      service.DeleteUser(newUser2);
209      var users = service.GetAllUsers();
210      Assert.AreEqual(0, users.Where(x => x.UserName == newUser.UserName).Count());
211
212      service.DeleteRole(role);
213      Assert.AreEqual(0, service.GetRoles().Where(x => x.Name == role.Name).Count());
214    }
215
216    [TestMethod]
217    public void AddClientError() {
218      ClearDB();
219      AccessService service = new AccessService();
220
221      DT.ClientError error = new DT.ClientError();
222      error.Timestamp = DateTime.Now;
223      error.UserComment = "this happend when i clicked on...";
224      error.Exception = "Exception";
225      error.ConfigDump = "config";
226
227      service.ReportError(error);
228      Assert.AreEqual(1, service.GetClientErrors().Count());
229      service.DeleteError(service.GetClientErrors().First());
230      Assert.AreEqual(0, service.GetClientErrors().Count());
231    }
232  }
233}
Note: See TracBrowser for help on using the repository browser.