1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
26 | using DA = HeuristicLab.Services.Access.DataAccess;
|
---|
27 | using DT = HeuristicLab.Services.Access.DataTransfer;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Services.Access.Tests {
|
---|
30 | [TestClass]
|
---|
31 | public class UnitTest {
|
---|
32 |
|
---|
33 | [TestMethod]
|
---|
34 | public void ClearDB() {
|
---|
35 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
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 |
|
---|
60 | DT.Client client = new DT.Client() {
|
---|
61 | ClientConfiguration = config,
|
---|
62 | ClientType = clientType,
|
---|
63 | Country = country,
|
---|
64 | Description = "testClient",
|
---|
65 | HeuristicLabVersion = "3.3.6",
|
---|
66 | MemorySize = 512,
|
---|
67 | NumberOfCores = 4,
|
---|
68 | Name = "testClient",
|
---|
69 | OperatingSystem = os,
|
---|
70 | ProcessorType = "Intel i7",
|
---|
71 | Timestamp = DateTime.Now
|
---|
72 | };
|
---|
73 |
|
---|
74 | Guid id = service.AddClient(client);
|
---|
75 | Assert.IsTrue(service.ClientExists(id));
|
---|
76 | Assert.AreEqual(1, service.GetAllClients().Count());
|
---|
77 |
|
---|
78 | service.DeleteClient(service.GetAllClients().First());
|
---|
79 | Assert.AreEqual(0, service.GetAllClients().Count());
|
---|
80 | }
|
---|
81 |
|
---|
82 | [TestMethod]
|
---|
83 | public void AddGroupTest() {
|
---|
84 | ClearDB();
|
---|
85 | AccessService service = new AccessService();
|
---|
86 |
|
---|
87 | DT.ClientGroup parentGroup = new DT.ClientGroup() { Name = "Parent", Description = "Parent Group" };
|
---|
88 | DT.ClientGroup childGroup = new DT.ClientGroup() { Name = "Child", Description = "Child Group" };
|
---|
89 | DT.ClientGroup otherGroup = new DT.ClientGroup() { Name = "Other", Description = "Other Group" };
|
---|
90 |
|
---|
91 | parentGroup.Id = service.AddClientGroup(parentGroup);
|
---|
92 | childGroup.Id = service.AddClientGroup(childGroup);
|
---|
93 | otherGroup.Id = service.AddClientGroup(otherGroup);
|
---|
94 |
|
---|
95 | List<Guid> groupGuids = new List<Guid>();
|
---|
96 | groupGuids.Add(parentGroup.Id);
|
---|
97 | groupGuids.Add(childGroup.Id);
|
---|
98 |
|
---|
99 | Assert.AreEqual(3, service.GetAllClientGroups().Count());
|
---|
100 | var childParentList = service.GetClientGroups(groupGuids);
|
---|
101 | Assert.AreEqual(2, childParentList.Count());
|
---|
102 |
|
---|
103 | service.AddResourceToGroup(childGroup, parentGroup);
|
---|
104 | var mapping = service.GetClientGroupMapping();
|
---|
105 | Assert.AreEqual(1, mapping.Count());
|
---|
106 | Assert.AreEqual(mapping.First().Child, childGroup.Id);
|
---|
107 | Assert.AreEqual(mapping.First().Parent, parentGroup.Id);
|
---|
108 |
|
---|
109 | service.DeleteClientGroup(otherGroup);
|
---|
110 | Assert.AreEqual(2, service.GetAllClientGroups().Count());
|
---|
111 |
|
---|
112 | service.RemoveResourceFromGroup(childGroup, parentGroup);
|
---|
113 | Assert.AreEqual(0, service.GetClientGroupMapping().Count());
|
---|
114 | }
|
---|
115 |
|
---|
116 | [TestMethod]
|
---|
117 | public void AddClientLogTest() {
|
---|
118 | ClearDB();
|
---|
119 | AccessService service = new AccessService();
|
---|
120 |
|
---|
121 | DT.Client client = new DT.Client() {
|
---|
122 | Description = "testClient",
|
---|
123 | HeuristicLabVersion = "3.3.6",
|
---|
124 | MemorySize = 512,
|
---|
125 | NumberOfCores = 4,
|
---|
126 | Name = "testClient",
|
---|
127 | ProcessorType = "Intel i7",
|
---|
128 | Timestamp = DateTime.Now
|
---|
129 | };
|
---|
130 |
|
---|
131 | client.Id = service.AddClient(client);
|
---|
132 | Assert.IsTrue(service.ClientExists(client.Id));
|
---|
133 |
|
---|
134 | DateTime now = DateTime.Now;
|
---|
135 | DT.ClientLog log = new DT.ClientLog() { Message = "testMessage", ResourceId = client.Id, Timestamp = now };
|
---|
136 | service.AddClientLog(log);
|
---|
137 | Assert.AreEqual(1, service.GetClientLogsSince(now).Count());
|
---|
138 | }
|
---|
139 |
|
---|
140 | [TestMethod]
|
---|
141 | public void AddUser() {
|
---|
142 | ClearDB();
|
---|
143 | AccessService service = new AccessService();
|
---|
144 |
|
---|
145 | DT.User user = new DT.User();
|
---|
146 | user.FullName = "Max Mustermann";
|
---|
147 | user.UserName = "max";
|
---|
148 | user.IsApproved = true;
|
---|
149 | user.Comment = "this is a comment";
|
---|
150 | user.Email = "max@mail.com";
|
---|
151 |
|
---|
152 | DT.User newUser = service.AddUser(user);
|
---|
153 | var users = service.GetAllUsers();
|
---|
154 | Assert.AreEqual(1, users.Where(x => x.UserName == newUser.UserName).Count());
|
---|
155 | service.DeleteUser(newUser);
|
---|
156 | users = service.GetAllUsers();
|
---|
157 | Assert.AreEqual(0, users.Where(x => x.UserName == newUser.UserName).Count());
|
---|
158 | }
|
---|
159 |
|
---|
160 | [TestMethod]
|
---|
161 | public void AddUserGroup() {
|
---|
162 | ClearDB();
|
---|
163 | AccessService service = new AccessService();
|
---|
164 |
|
---|
165 | DT.User user = new DT.User();
|
---|
166 | user.FullName = "Max Mustermann";
|
---|
167 | user.UserName = "max";
|
---|
168 | user.IsApproved = true;
|
---|
169 | user.Comment = "this is a comment";
|
---|
170 | user.Email = "max@mail.com";
|
---|
171 |
|
---|
172 | DT.User user2 = new DT.User();
|
---|
173 | user2.FullName = "Franz Fritz";
|
---|
174 | user2.UserName = "Franz";
|
---|
175 | user2.IsApproved = true;
|
---|
176 | user2.Comment = "this is a franz comment";
|
---|
177 | user2.Email = "franz@mail.com";
|
---|
178 |
|
---|
179 | DT.User newUser = service.AddUser(user);
|
---|
180 | DT.User newUser2 = service.AddUser(user2);
|
---|
181 |
|
---|
182 | DT.UserGroup userGroup = new DT.UserGroup();
|
---|
183 | userGroup.Name = "testGroup";
|
---|
184 | userGroup.Id = service.AddUserGroup(userGroup);
|
---|
185 | Assert.AreEqual(1, service.GetAllUserGroups().Count());
|
---|
186 |
|
---|
187 | service.AddUserGroupBaseToGroup(newUser, userGroup);
|
---|
188 | service.AddUserGroupBaseToGroup(newUser2, userGroup);
|
---|
189 | Assert.AreEqual(2, service.GetUserGroupMapping().Count());
|
---|
190 |
|
---|
191 | DT.Role role = service.AddRole(new DT.Role() { Name = "NewGroup" });
|
---|
192 | Assert.AreEqual(1, service.GetRoles().Where(x => x.Name == role.Name).Count());
|
---|
193 |
|
---|
194 | service.AddRoleToGroup(userGroup, role);
|
---|
195 | Assert.AreEqual(1, service.GetUserRoles(newUser).Count());
|
---|
196 | Assert.AreEqual(1, service.GetUserRoles(newUser2).Count());
|
---|
197 |
|
---|
198 | service.RemoveRoleFromGroup(userGroup, role);
|
---|
199 |
|
---|
200 | Assert.AreEqual(0, service.GetUserRoles(newUser).Count());
|
---|
201 | Assert.AreEqual(0, service.GetUserRoles(newUser2).Count());
|
---|
202 |
|
---|
203 | service.DeleteUser(newUser);
|
---|
204 | service.DeleteUser(newUser2);
|
---|
205 | var users = service.GetAllUsers();
|
---|
206 | Assert.AreEqual(0, users.Where(x => x.UserName == newUser.UserName).Count());
|
---|
207 |
|
---|
208 | service.DeleteRole(role);
|
---|
209 | Assert.AreEqual(0, service.GetRoles().Where(x => x.Name == role.Name).Count());
|
---|
210 | }
|
---|
211 |
|
---|
212 | [TestMethod]
|
---|
213 | public void AddClientError() {
|
---|
214 | ClearDB();
|
---|
215 | AccessService service = new AccessService();
|
---|
216 |
|
---|
217 | DT.ClientError error = new DT.ClientError();
|
---|
218 | error.Timestamp = DateTime.Now;
|
---|
219 | error.UserComment = "this happend when i clicked on...";
|
---|
220 | error.Exception = "Exception";
|
---|
221 | error.ConfigDump = "config";
|
---|
222 |
|
---|
223 | service.ReportError(error);
|
---|
224 | Assert.AreEqual(1, service.GetClientErrors().Count());
|
---|
225 | service.DeleteError(service.GetClientErrors().First());
|
---|
226 | Assert.AreEqual(0, service.GetClientErrors().Count());
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|