Commit 19c22553 authored by L. Ellenbeck's avatar L. Ellenbeck
Browse files

Refactoring coscine/issues#157

parent 68dc0f68
...@@ -18,15 +18,15 @@ namespace Coscine.Api.Project.Tests ...@@ -18,15 +18,15 @@ namespace Coscine.Api.Project.Tests
{ {
protected T Controller { get; } protected T Controller { get; }
protected List<Coscine.Database.Model.Project> projects = new List<Coscine.Database.Model.Project>(); protected readonly List<Coscine.Database.Model.Project> Projects = new List<Coscine.Database.Model.Project>();
protected List<User> users = new List<User>(); protected readonly List<User> Users = new List<User>();
protected List<ProjectRole> projectRoles = new List<ProjectRole>(); protected readonly List<ProjectRole> ProjectRoles = new List<ProjectRole>();
protected List<ResourceType> resourceTypes = new List<ResourceType>(); protected readonly List<ResourceType> ResourceTypes = new List<ResourceType>();
protected List<Resource> resources = new List<Resource>(); protected readonly List<Resource> Resources = new List<Resource>();
protected int previous; protected int Previous;
public DefaultControllerTests(T controller) protected DefaultControllerTests(T controller)
{ {
Controller = controller; Controller = controller;
} }
...@@ -35,7 +35,7 @@ namespace Coscine.Api.Project.Tests ...@@ -35,7 +35,7 @@ namespace Coscine.Api.Project.Tests
public void Setup() public void Setup()
{ {
ProjectModel projectModel = new ProjectModel(); ProjectModel projectModel = new ProjectModel();
previous = projectModel.GetAll().ToArray().Length; Previous = projectModel.GetAll().ToArray().Length;
UserModel userModel = new UserModel(); UserModel userModel = new UserModel();
...@@ -46,7 +46,7 @@ namespace Coscine.Api.Project.Tests ...@@ -46,7 +46,7 @@ namespace Coscine.Api.Project.Tests
}; };
userModel.Insert(user); userModel.Insert(user);
users.Add(user); Users.Add(user);
FakeControllerContext(user); FakeControllerContext(user);
...@@ -62,9 +62,9 @@ namespace Coscine.Api.Project.Tests ...@@ -62,9 +62,9 @@ namespace Coscine.Api.Project.Tests
projectModel.Insert(project); projectModel.Insert(project);
var projectRole = projectModel.SetOwner(project, user); var projectRole = projectModel.SetOwner(project, user);
projectRoles.Add(projectRole); ProjectRoles.Add(projectRole);
projects.Add(projectModel.GetById(project.Id)); Projects.Add(projectModel.GetById(project.Id));
var project2 = new Coscine.Database.Model.Project() var project2 = new Coscine.Database.Model.Project()
{ {
...@@ -76,7 +76,7 @@ namespace Coscine.Api.Project.Tests ...@@ -76,7 +76,7 @@ namespace Coscine.Api.Project.Tests
Keywords = "Test1;Test2" Keywords = "Test1;Test2"
}; };
projectModel.Insert(project2); projectModel.Insert(project2);
projects.Add(projectModel.GetById(project2.Id)); Projects.Add(projectModel.GetById(project2.Id));
ResourceTypeModel resourceTypeModel = new ResourceTypeModel(); ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
var resourceType = new ResourceType() var resourceType = new ResourceType()
...@@ -84,7 +84,7 @@ namespace Coscine.Api.Project.Tests ...@@ -84,7 +84,7 @@ namespace Coscine.Api.Project.Tests
DisplayName = "TestType" DisplayName = "TestType"
}; };
resourceTypeModel.Insert(resourceType); resourceTypeModel.Insert(resourceType);
resourceTypes.Add(resourceType); ResourceTypes.Add(resourceType);
ResourceModel resourceModel = new ResourceModel(); ResourceModel resourceModel = new ResourceModel();
var resource = new Resource() var resource = new Resource()
...@@ -96,7 +96,7 @@ namespace Coscine.Api.Project.Tests ...@@ -96,7 +96,7 @@ namespace Coscine.Api.Project.Tests
}; };
resourceModel.Insert(resource); resourceModel.Insert(resource);
projectModel.AddResource(project, resource); projectModel.AddResource(project, resource);
resources.Add(resource); Resources.Add(resource);
var resource2 = new Resource() var resource2 = new Resource()
{ {
...@@ -107,7 +107,7 @@ namespace Coscine.Api.Project.Tests ...@@ -107,7 +107,7 @@ namespace Coscine.Api.Project.Tests
}; };
resourceModel.Insert(resource2); resourceModel.Insert(resource2);
projectModel.AddResource(project2, resource2); projectModel.AddResource(project2, resource2);
resources.Add(resource2); Resources.Add(resource2);
} }
...@@ -149,32 +149,32 @@ namespace Coscine.Api.Project.Tests ...@@ -149,32 +149,32 @@ namespace Coscine.Api.Project.Tests
public void End() public void End()
{ {
ProjectRoleModel projectRoleModel = new ProjectRoleModel(); ProjectRoleModel projectRoleModel = new ProjectRoleModel();
foreach (var projectRole in projectRoles) foreach (var projectRole in ProjectRoles)
{ {
projectRoleModel.Delete(projectRole); projectRoleModel.Delete(projectRole);
} }
ProjectResourceModel projectResourceModel = new ProjectResourceModel(); ProjectResourceModel projectResourceModel = new ProjectResourceModel();
foreach(var projectResource in projectResourceModel.GetAllWhere((projectResource) => projectResource.ResourceId == resources[0].Id || projectResource.ResourceId == resources[1].Id)) foreach(var projectResource in projectResourceModel.GetAllWhere((projectResource) => projectResource.ResourceId == Resources[0].Id || projectResource.ResourceId == Resources[1].Id))
{ {
projectResourceModel.Delete(projectResource); projectResourceModel.Delete(projectResource);
} }
ProjectModel projectModel = new ProjectModel(); ProjectModel projectModel = new ProjectModel();
foreach (var project in projects) foreach (var project in Projects)
{ {
projectModel.Delete(project); projectModel.Delete(project);
} }
UserModel userModel = new UserModel(); UserModel userModel = new UserModel();
foreach (var user in users) foreach (var user in Users)
{ {
userModel.Delete(user); userModel.Delete(user);
} }
ResourceModel resourceModel = new ResourceModel(); ResourceModel resourceModel = new ResourceModel();
foreach (var resource in resources) foreach (var resource in Resources)
{ {
resourceModel.Delete(resource); resourceModel.Delete(resource);
} }
ResourceTypeModel resourceTypeModel = new ResourceTypeModel(); ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
foreach (var resourceType in resourceTypes) foreach (var resourceType in ResourceTypes)
{ {
resourceTypeModel.Delete(resourceType); resourceTypeModel.Delete(resourceType);
} }
......
...@@ -9,6 +9,7 @@ using System; ...@@ -9,6 +9,7 @@ using System;
using Coscine.ApiCommons.Exceptions; using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories; using Coscine.ApiCommons.Factories;
#region DupFinder Exclusion
namespace Coscine.Api.Project.Tests namespace Coscine.Api.Project.Tests
{ {
[TestFixture] [TestFixture]
...@@ -23,12 +24,12 @@ namespace Coscine.Api.Project.Tests ...@@ -23,12 +24,12 @@ namespace Coscine.Api.Project.Tests
public void OwnsTest() public void OwnsTest()
{ {
ProjectModel projectModel = new ProjectModel(); ProjectModel projectModel = new ProjectModel();
Assert.IsTrue(projectModel.OwnsProject(users[0], projects[0])); Assert.IsTrue(projectModel.OwnsProject(Users[0], Projects[0]));
Assert.IsFalse(projectModel.OwnsProject(users[0], projects[1])); Assert.IsFalse(projectModel.OwnsProject(Users[0], Projects[1]));
var all = projectModel.GetAllWhere((project) => var all = projectModel.GetAllWhere((project) =>
(from projectRole in project.ProjectRolesProjectIdIds (from projectRole in project.ProjectRolesProjectIdIds
where projectRole.User == users[0] where projectRole.User == Users[0]
&& projectRole.Role.DisplayName == "Owner" && projectRole.Role.DisplayName == "Owner"
select projectRole).Any() select projectRole).Any()
); );
...@@ -45,7 +46,7 @@ namespace Coscine.Api.Project.Tests ...@@ -45,7 +46,7 @@ namespace Coscine.Api.Project.Tests
[Test] [Test]
public void TestControllerGet() public void TestControllerGet()
{ {
var actionResult = Controller.Get(projects[0].Id.ToString()); var actionResult = Controller.Get(Projects[0].Id.ToString());
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult)); Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
OkObjectResult okObjectResult = (OkObjectResult)actionResult; OkObjectResult okObjectResult = (OkObjectResult)actionResult;
...@@ -53,30 +54,29 @@ namespace Coscine.Api.Project.Tests ...@@ -53,30 +54,29 @@ namespace Coscine.Api.Project.Tests
ProjectObject projectObject = (ProjectObject)okObjectResult.Value; ProjectObject projectObject = (ProjectObject)okObjectResult.Value;
Assert.IsTrue(projectObject.Id == projects[0].Id); Assert.IsTrue(projectObject.Id == Projects[0].Id);
Assert.IsTrue(projectObject.Description == projects[0].Description); Assert.IsTrue(projectObject.Description == Projects[0].Description);
Assert.IsTrue(projectObject.DisplayName == projects[0].DisplayName); Assert.IsTrue(projectObject.DisplayName == Projects[0].DisplayName);
Assert.IsTrue(projectObject.Organization == projects[0].Organization); Assert.IsTrue(projectObject.Organization == Projects[0].Organization);
Assert.IsTrue(projectObject.StartDate == projects[0].StartDate); Assert.IsTrue(projectObject.StartDate == Projects[0].StartDate);
Assert.IsTrue(projectObject.EndDate == projects[0].EndDate); Assert.IsTrue(projectObject.EndDate == Projects[0].EndDate);
Assert.IsTrue(projectObject.Keywords == projects[0].Keywords); Assert.IsTrue(projectObject.Keywords == Projects[0].Keywords);
} }
[Test] [Test]
public void TestControllerUpdate() public void TestControllerUpdate()
{ {
var actionResult = Controller.Get(projects[0].Id.ToString()); var actionResult = Controller.Get(Projects[0].Id.ToString());
OkObjectResult okObjectResult = (OkObjectResult)actionResult;; ProjectObject projectObject = (ProjectObject)((OkObjectResult)actionResult).Value;
ProjectObject projectObject = (ProjectObject)okObjectResult.Value;
projectObject.Description = "ChangedDescription"; projectObject.Description = "ChangedDescription";
Stream stream = ObjectFactory<ProjectObject>.SerializeToStream(projectObject); Stream stream = ObjectFactory<ProjectObject>.SerializeToStream(projectObject);
FakeControllerContext(users[0], stream); FakeControllerContext(Users[0], stream);
actionResult = Controller.Update(projects[0].Id.ToString()); actionResult = Controller.Update(Projects[0].Id.ToString());
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult)); Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
// Cleanup // Cleanup
...@@ -84,11 +84,11 @@ namespace Coscine.Api.Project.Tests ...@@ -84,11 +84,11 @@ namespace Coscine.Api.Project.Tests
stream = ObjectFactory<ProjectObject>.SerializeToStream(projectObject); stream = ObjectFactory<ProjectObject>.SerializeToStream(projectObject);
FakeControllerContext(users[0], stream); FakeControllerContext(Users[0], stream);
try try
{ {
actionResult = Controller.Update(projects[1].Id.ToString()); Controller.Update(Projects[1].Id.ToString());
Assert.Fail(); Assert.Fail();
} }
catch (Exception e) catch (Exception e)
...@@ -103,11 +103,11 @@ namespace Coscine.Api.Project.Tests ...@@ -103,11 +103,11 @@ namespace Coscine.Api.Project.Tests
[Test] [Test]
public void TestControllerStore() public void TestControllerStore()
{ {
ProjectObject newProjectObject = new ProjectObject(System.Guid.NewGuid(), "NewProject", "NewDisplayName", "NewOrganisation", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3"); ProjectObject newProjectObject = new ProjectObject(Guid.NewGuid(), "NewProject", "NewDisplayName", "NewOrganisation", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3");
var stream = ObjectFactory<ProjectObject>.SerializeToStream(newProjectObject); var stream = ObjectFactory<ProjectObject>.SerializeToStream(newProjectObject);
FakeControllerContext(users[0], stream); FakeControllerContext(Users[0], stream);
var actionResult = Controller.Store(); var actionResult = Controller.Store();
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult)); Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
...@@ -142,7 +142,8 @@ namespace Coscine.Api.Project.Tests ...@@ -142,7 +142,8 @@ namespace Coscine.Api.Project.Tests
public void CountTest() public void CountTest()
{ {
ProjectModel projectModel = new ProjectModel(); ProjectModel projectModel = new ProjectModel();
Assert.IsTrue(previous == projectModel.GetAll().ToArray().Length - projects.Count); Assert.IsTrue(Previous == projectModel.GetAll().ToArray().Length - Projects.Count);
} }
} }
} }
#endregion
\ No newline at end of file
...@@ -27,7 +27,7 @@ namespace Coscine.Api.Project.Tests ...@@ -27,7 +27,7 @@ namespace Coscine.Api.Project.Tests
[Test] [Test]
public void TestControllerGet() public void TestControllerGet()
{ {
var actionResult = Controller.Get(resources[0].Id.ToString()); var actionResult = Controller.Get(Resources[0].Id.ToString());
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult)); Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
OkObjectResult okObjectResult = (OkObjectResult)actionResult; OkObjectResult okObjectResult = (OkObjectResult)actionResult;
...@@ -35,18 +35,18 @@ namespace Coscine.Api.Project.Tests ...@@ -35,18 +35,18 @@ namespace Coscine.Api.Project.Tests
ResourceObject projectObject = (ResourceObject)okObjectResult.Value; ResourceObject projectObject = (ResourceObject)okObjectResult.Value;
Assert.IsTrue(projectObject.Id == resources[0].Id); Assert.IsTrue(projectObject.Id == Resources[0].Id);
Assert.IsTrue(projectObject.ExternalId == resources[0].ExternalId); Assert.IsTrue(projectObject.ExternalId == Resources[0].ExternalId);
Assert.IsTrue(projectObject.Type.Id == resources[0].Type.Id); Assert.IsTrue(projectObject.Type.Id == Resources[0].Type.Id);
Assert.IsTrue(projectObject.Type.DisplayName == resources[0].Type.DisplayName); Assert.IsTrue(projectObject.Type.DisplayName == Resources[0].Type.DisplayName);
Assert.IsTrue(projectObject.Url == resources[0].Url); Assert.IsTrue(projectObject.Url == Resources[0].Url);
} }
[Test] [Test]
public void TestControllerUpdate() public void TestControllerUpdate()
{ {
var actionResult = Controller.Get(resources[0].Id.ToString()); var actionResult = Controller.Get(Resources[0].Id.ToString());
OkObjectResult okObjectResult = (OkObjectResult)actionResult; OkObjectResult okObjectResult = (OkObjectResult)actionResult;
ResourceObject resourceObject = (ResourceObject)okObjectResult.Value; ResourceObject resourceObject = (ResourceObject)okObjectResult.Value;
...@@ -54,9 +54,9 @@ namespace Coscine.Api.Project.Tests ...@@ -54,9 +54,9 @@ namespace Coscine.Api.Project.Tests
Stream stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject); Stream stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);
FakeControllerContext(users[0], stream); FakeControllerContext(Users[0], stream);
actionResult = Controller.Update(resources[0].Id.ToString()); actionResult = Controller.Update(Resources[0].Id.ToString());
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult)); Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
// Cleanup // Cleanup
...@@ -64,11 +64,11 @@ namespace Coscine.Api.Project.Tests ...@@ -64,11 +64,11 @@ namespace Coscine.Api.Project.Tests
stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject); stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);
FakeControllerContext(users[0], stream); FakeControllerContext(Users[0], stream);
try try
{ {
actionResult = Controller.Update(resources[1].Id.ToString()); Controller.Update(Resources[1].Id.ToString());
Assert.Fail(); Assert.Fail();
} }
catch (Exception e) catch (Exception e)
...@@ -83,13 +83,13 @@ namespace Coscine.Api.Project.Tests ...@@ -83,13 +83,13 @@ namespace Coscine.Api.Project.Tests
[Test] [Test]
public void TestControllerStoreToProject() public void TestControllerStoreToProject()
{ {
ResourceObject resourceObject = new ResourceObject(Guid.NewGuid(), "externalId", "http://rwth-aachen.de/newTest", new ResourceTypeObject(resourceTypes[0].Id, resourceTypes[0].DisplayName)); ResourceObject resourceObject = new ResourceObject(Guid.NewGuid(), "externalId", "http://rwth-aachen.de/newTest", new ResourceTypeObject(ResourceTypes[0].Id, ResourceTypes[0].DisplayName));
Stream stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject); Stream stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);
FakeControllerContext(users[0], stream); FakeControllerContext(Users[0], stream);
var actionResult = Controller.StoreToProject(projects[0].Id.ToString()); var actionResult = Controller.StoreToProject(Projects[0].Id.ToString());
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult)); Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
OkObjectResult okObjectResult = (OkObjectResult)actionResult; OkObjectResult okObjectResult = (OkObjectResult)actionResult;
resourceObject = (ResourceObject)okObjectResult.Value; resourceObject = (ResourceObject)okObjectResult.Value;
...@@ -101,7 +101,7 @@ namespace Coscine.Api.Project.Tests ...@@ -101,7 +101,7 @@ namespace Coscine.Api.Project.Tests
var resource = resourceModel.GetById(resourceObject.Id); var resource = resourceModel.GetById(resourceObject.Id);
ProjectResourceModel projectResourceModel = new ProjectResourceModel(); ProjectResourceModel projectResourceModel = new ProjectResourceModel();
foreach(var projectResource in projectResourceModel.GetAllWhere((projectResource) => projectResource.ProjectId == projects[0].Id && projectResource.ResourceId == resource.Id)) foreach(var projectResource in projectResourceModel.GetAllWhere((projectResource) => projectResource.ProjectId == Projects[0].Id && projectResource.ResourceId == resource.Id))
{ {
projectResourceModel.Delete(projectResource); projectResourceModel.Delete(projectResource);
} }
......
...@@ -29,10 +29,7 @@ namespace Coscine.Api.Project.Controllers ...@@ -29,10 +29,7 @@ namespace Coscine.Api.Project.Controllers
where projectRole.User == user where projectRole.User == user
&& projectRole.Role.DisplayName == "Owner" && projectRole.Role.DisplayName == "Owner"
select projectRole).Any() select projectRole).Any()
).Select((project) => ).Select((project) => new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords));
{
return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
});
})); }));
} }
......
...@@ -31,10 +31,7 @@ namespace Coscine.Api.Project.Controllers ...@@ -31,10 +31,7 @@ namespace Coscine.Api.Project.Controllers
&& projectRole.Role.DisplayName == "Owner" && projectRole.Role.DisplayName == "Owner"
select projectRole).Any() select projectRole).Any()
select projectResource).Any() select projectResource).Any()
).Select((resource) => ).Select((resource) => new ResourceObject(resource.Id, resource.ExternalId, resource.Url, new ResourceTypeObject(resource.Type.Id, resource.Type.DisplayName)));
{
return new ResourceObject(resource.Id, resource.ExternalId, resource.Url, new ResourceTypeObject(resource.Type.Id, resource.Type.DisplayName));
});
})); }));
} }
...@@ -84,15 +81,15 @@ namespace Coscine.Api.Project.Controllers ...@@ -84,15 +81,15 @@ namespace Coscine.Api.Project.Controllers
})); }));
} }
[HttpPost("[controller]/project/{project_id}")] [HttpPost("[controller]/project/{projectId}")]
public IActionResult StoreToProject(string project_id) public IActionResult StoreToProject(string projectId)
{ {
return Ok(_authenticator.ValidateAndExecute((user) => return Ok(_authenticator.ValidateAndExecute((user) =>
{ {
ResourceObject resourceObject = ObjectFactory<ResourceObject>.DeserializeFromStream(Request.Body); ResourceObject resourceObject = ObjectFactory<ResourceObject>.DeserializeFromStream(Request.Body);
ProjectModel projectModel = new ProjectModel(); ProjectModel projectModel = new ProjectModel();
var project = projectModel.GetById(Guid.Parse(project_id)); var project = projectModel.GetById(Guid.Parse(projectId));
if (projectModel.OwnsProject(user, project)) if (projectModel.OwnsProject(user, project))
{ {
ResourceModel resourceModel = new ResourceModel(); ResourceModel resourceModel = new ResourceModel();
......
...@@ -49,14 +49,11 @@ namespace Coscine.Api.Project.Models ...@@ -49,14 +49,11 @@ namespace Coscine.Api.Project.Models
public bool OwnsProject(User user, Coscine.Database.Model.Project project) public bool OwnsProject(User user, Coscine.Database.Model.Project project)
{ {
return DatabaseConnection.ConnectToDatabase((db) => return DatabaseConnection.ConnectToDatabase((db) => (from relation in db.ProjectRoles
{
return (from relation in db.ProjectRoles
where relation.Project == project where relation.Project == project
&& relation.User == user && relation.User == user
&& relation.Role.DisplayName == "Owner" && relation.Role.DisplayName == "Owner"
select relation).Any(); select relation).Any());
});
} }
public void AddResource(Coscine.Database.Model.Project project, Resource resource) public void AddResource(Coscine.Database.Model.Project project, Resource resource)
...@@ -96,9 +93,9 @@ namespace Coscine.Api.Project.Models ...@@ -96,9 +93,9 @@ namespace Coscine.Api.Project.Models
return db.Projects; return db.Projects;
} }
public override void SetObjectId(Database.Model.Project databaseObject, Guid Id) public override void SetObjectId(Database.Model.Project databaseObject, Guid id)
{ {
databaseObject.Id = Id; databaseObject.Id = id;
} }
} }
} }
...@@ -22,9 +22,9 @@ namespace Coscine.Api.Project.Models ...@@ -22,9 +22,9 @@ namespace Coscine.Api.Project.Models
return db.ProjectResources; return db.ProjectResources;
} }
public override void SetObjectId(ProjectResource databaseObject, Guid Id) public override void SetObjectId(ProjectResource databaseObject, Guid id)
{ {
databaseObject.RelationId = Id; databaseObject.RelationId = id;
} }
} }
} }
\ No newline at end of file