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
{
protected T Controller { get; }
protected List<Coscine.Database.Model.Project> projects = new List<Coscine.Database.Model.Project>();
protected List<User> users = new List<User>();
protected List<ProjectRole> projectRoles = new List<ProjectRole>();
protected List<ResourceType> resourceTypes = new List<ResourceType>();
protected List<Resource> resources = new List<Resource>();
protected readonly List<Coscine.Database.Model.Project> Projects = new List<Coscine.Database.Model.Project>();
protected readonly List<User> Users = new List<User>();
protected readonly List<ProjectRole> ProjectRoles = new List<ProjectRole>();
protected readonly List<ResourceType> ResourceTypes = new List<ResourceType>();
protected readonly List<Resource> Resources = new List<Resource>();
protected int previous;
protected int Previous;
public DefaultControllerTests(T controller)
protected DefaultControllerTests(T controller)
{
Controller = controller;
}
......@@ -35,7 +35,7 @@ namespace Coscine.Api.Project.Tests
public void Setup()
{
ProjectModel projectModel = new ProjectModel();
previous = projectModel.GetAll().ToArray().Length;
Previous = projectModel.GetAll().ToArray().Length;
UserModel userModel = new UserModel();
......@@ -46,7 +46,7 @@ namespace Coscine.Api.Project.Tests
};
userModel.Insert(user);
users.Add(user);
Users.Add(user);
FakeControllerContext(user);
......@@ -62,9 +62,9 @@ namespace Coscine.Api.Project.Tests
projectModel.Insert(project);
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()
{
......@@ -76,7 +76,7 @@ namespace Coscine.Api.Project.Tests
Keywords = "Test1;Test2"
};
projectModel.Insert(project2);
projects.Add(projectModel.GetById(project2.Id));
Projects.Add(projectModel.GetById(project2.Id));
ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
var resourceType = new ResourceType()
......@@ -84,7 +84,7 @@ namespace Coscine.Api.Project.Tests
DisplayName = "TestType"
};
resourceTypeModel.Insert(resourceType);
resourceTypes.Add(resourceType);
ResourceTypes.Add(resourceType);
ResourceModel resourceModel = new ResourceModel();
var resource = new Resource()
......@@ -96,7 +96,7 @@ namespace Coscine.Api.Project.Tests
};
resourceModel.Insert(resource);
projectModel.AddResource(project, resource);
resources.Add(resource);
Resources.Add(resource);
var resource2 = new Resource()
{
......@@ -107,7 +107,7 @@ namespace Coscine.Api.Project.Tests
};
resourceModel.Insert(resource2);
projectModel.AddResource(project2, resource2);
resources.Add(resource2);
Resources.Add(resource2);
}
......@@ -149,32 +149,32 @@ namespace Coscine.Api.Project.Tests
public void End()
{
ProjectRoleModel projectRoleModel = new ProjectRoleModel();
foreach (var projectRole in projectRoles)
foreach (var projectRole in ProjectRoles)
{
projectRoleModel.Delete(projectRole);
}
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);
}
ProjectModel projectModel = new ProjectModel();
foreach (var project in projects)
foreach (var project in Projects)
{
projectModel.Delete(project);
}
UserModel userModel = new UserModel();
foreach (var user in users)
foreach (var user in Users)
{
userModel.Delete(user);
}
ResourceModel resourceModel = new ResourceModel();
foreach (var resource in resources)
foreach (var resource in Resources)
{
resourceModel.Delete(resource);
}
ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
foreach (var resourceType in resourceTypes)
foreach (var resourceType in ResourceTypes)
{
resourceTypeModel.Delete(resourceType);
}
......
......@@ -9,6 +9,7 @@ using System;
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
#region DupFinder Exclusion
namespace Coscine.Api.Project.Tests
{
[TestFixture]
......@@ -23,12 +24,12 @@ namespace Coscine.Api.Project.Tests
public void OwnsTest()
{
ProjectModel projectModel = new ProjectModel();
Assert.IsTrue(projectModel.OwnsProject(users[0], projects[0]));
Assert.IsFalse(projectModel.OwnsProject(users[0], projects[1]));
Assert.IsTrue(projectModel.OwnsProject(Users[0], Projects[0]));
Assert.IsFalse(projectModel.OwnsProject(Users[0], Projects[1]));
var all = projectModel.GetAllWhere((project) =>
(from projectRole in project.ProjectRolesProjectIdIds
where projectRole.User == users[0]
where projectRole.User == Users[0]
&& projectRole.Role.DisplayName == "Owner"
select projectRole).Any()
);
......@@ -45,7 +46,7 @@ namespace Coscine.Api.Project.Tests
[Test]
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));
OkObjectResult okObjectResult = (OkObjectResult)actionResult;
......@@ -53,30 +54,29 @@ namespace Coscine.Api.Project.Tests
ProjectObject projectObject = (ProjectObject)okObjectResult.Value;
Assert.IsTrue(projectObject.Id == projects[0].Id);
Assert.IsTrue(projectObject.Description == projects[0].Description);
Assert.IsTrue(projectObject.DisplayName == projects[0].DisplayName);
Assert.IsTrue(projectObject.Organization == projects[0].Organization);
Assert.IsTrue(projectObject.StartDate == projects[0].StartDate);
Assert.IsTrue(projectObject.EndDate == projects[0].EndDate);
Assert.IsTrue(projectObject.Keywords == projects[0].Keywords);
Assert.IsTrue(projectObject.Id == Projects[0].Id);
Assert.IsTrue(projectObject.Description == Projects[0].Description);
Assert.IsTrue(projectObject.DisplayName == Projects[0].DisplayName);
Assert.IsTrue(projectObject.Organization == Projects[0].Organization);
Assert.IsTrue(projectObject.StartDate == Projects[0].StartDate);
Assert.IsTrue(projectObject.EndDate == Projects[0].EndDate);
Assert.IsTrue(projectObject.Keywords == Projects[0].Keywords);
}
[Test]
public void TestControllerUpdate()
{
var actionResult = Controller.Get(projects[0].Id.ToString());
OkObjectResult okObjectResult = (OkObjectResult)actionResult;;
ProjectObject projectObject = (ProjectObject)okObjectResult.Value;
var actionResult = Controller.Get(Projects[0].Id.ToString());
ProjectObject projectObject = (ProjectObject)((OkObjectResult)actionResult).Value;
projectObject.Description = "ChangedDescription";
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));
// Cleanup
......@@ -84,11 +84,11 @@ namespace Coscine.Api.Project.Tests
stream = ObjectFactory<ProjectObject>.SerializeToStream(projectObject);
FakeControllerContext(users[0], stream);
FakeControllerContext(Users[0], stream);
try
{
actionResult = Controller.Update(projects[1].Id.ToString());
Controller.Update(Projects[1].Id.ToString());
Assert.Fail();
}
catch (Exception e)
......@@ -103,11 +103,11 @@ namespace Coscine.Api.Project.Tests
[Test]
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);
FakeControllerContext(users[0], stream);
FakeControllerContext(Users[0], stream);
var actionResult = Controller.Store();
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
......@@ -142,7 +142,8 @@ namespace Coscine.Api.Project.Tests
public void CountTest()
{
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
[Test]
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));
OkObjectResult okObjectResult = (OkObjectResult)actionResult;
......@@ -35,18 +35,18 @@ namespace Coscine.Api.Project.Tests
ResourceObject projectObject = (ResourceObject)okObjectResult.Value;
Assert.IsTrue(projectObject.Id == resources[0].Id);
Assert.IsTrue(projectObject.ExternalId == resources[0].ExternalId);
Assert.IsTrue(projectObject.Type.Id == resources[0].Type.Id);
Assert.IsTrue(projectObject.Type.DisplayName == resources[0].Type.DisplayName);
Assert.IsTrue(projectObject.Url == resources[0].Url);
Assert.IsTrue(projectObject.Id == Resources[0].Id);
Assert.IsTrue(projectObject.ExternalId == Resources[0].ExternalId);
Assert.IsTrue(projectObject.Type.Id == Resources[0].Type.Id);
Assert.IsTrue(projectObject.Type.DisplayName == Resources[0].Type.DisplayName);
Assert.IsTrue(projectObject.Url == Resources[0].Url);
}
[Test]
public void TestControllerUpdate()
{
var actionResult = Controller.Get(resources[0].Id.ToString());
var actionResult = Controller.Get(Resources[0].Id.ToString());
OkObjectResult okObjectResult = (OkObjectResult)actionResult;
ResourceObject resourceObject = (ResourceObject)okObjectResult.Value;
......@@ -54,9 +54,9 @@ namespace Coscine.Api.Project.Tests
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));
// Cleanup
......@@ -64,11 +64,11 @@ namespace Coscine.Api.Project.Tests
stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);
FakeControllerContext(users[0], stream);
FakeControllerContext(Users[0], stream);
try
{
actionResult = Controller.Update(resources[1].Id.ToString());
Controller.Update(Resources[1].Id.ToString());
Assert.Fail();
}
catch (Exception e)
......@@ -83,13 +83,13 @@ namespace Coscine.Api.Project.Tests
[Test]
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);
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));
OkObjectResult okObjectResult = (OkObjectResult)actionResult;
resourceObject = (ResourceObject)okObjectResult.Value;
......@@ -101,7 +101,7 @@ namespace Coscine.Api.Project.Tests
var resource = resourceModel.GetById(resourceObject.Id);
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);
}
......
......@@ -29,10 +29,7 @@ namespace Coscine.Api.Project.Controllers
where projectRole.User == user
&& projectRole.Role.DisplayName == "Owner"
select projectRole).Any()
).Select((project) =>
{
return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
});
).Select((project) => 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
&& projectRole.Role.DisplayName == "Owner"
select projectRole).Any()
select projectResource).Any()
).Select((resource) =>
{
return new ResourceObject(resource.Id, resource.ExternalId, resource.Url, new ResourceTypeObject(resource.Type.Id, resource.Type.DisplayName));
});
).Select((resource) => 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
}));
}
[HttpPost("[controller]/project/{project_id}")]
public IActionResult StoreToProject(string project_id)
[HttpPost("[controller]/project/{projectId}")]
public IActionResult StoreToProject(string projectId)
{
return Ok(_authenticator.ValidateAndExecute((user) =>
{
ResourceObject resourceObject = ObjectFactory<ResourceObject>.DeserializeFromStream(Request.Body);
ProjectModel projectModel = new ProjectModel();
var project = projectModel.GetById(Guid.Parse(project_id));
var project = projectModel.GetById(Guid.Parse(projectId));
if (projectModel.OwnsProject(user, project))
{
ResourceModel resourceModel = new ResourceModel();
......
......@@ -49,14 +49,11 @@ namespace Coscine.Api.Project.Models
public bool OwnsProject(User user, Coscine.Database.Model.Project project)
{
return DatabaseConnection.ConnectToDatabase((db) =>
{
return (from relation in db.ProjectRoles
return DatabaseConnection.ConnectToDatabase((db) => (from relation in db.ProjectRoles
where relation.Project == project
&& relation.User == user
&& relation.Role.DisplayName == "Owner"
select relation).Any();
});
select relation).Any());
}
public void AddResource(Coscine.Database.Model.Project project, Resource resource)
......@@ -96,9 +93,9 @@ namespace Coscine.Api.Project.Models
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
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
......@@ -23,9 +23,9 @@ namespace Coscine.Api.Project.Models
return db.ProjectRoles;
}
public override void SetObjectId(ProjectRole databaseObject, Guid Id)
public override void SetObjectId(ProjectRole databaseObject, Guid id)
{
databaseObject.RelationId = Id;
databaseObject.RelationId = id;
}
}
}
......@@ -21,9 +21,9 @@ namespace Coscine.Api.Project.Models
{
ExternalId = resourceObject.ExternalId,
Url = resourceObject.Url,
TypeId = resourceObject.Type.Id
TypeId = resourceObject.Type.Id,
Type = new ResourceTypeModel().GetById(resourceObject.Type.Id)
};
resource.Type = new ResourceTypeModel().GetById(resourceObject.Type.Id);
Insert(resource);
return resource;
}
......@@ -51,9 +51,9 @@ namespace Coscine.Api.Project.Models
return db.Resources;
}
public override void SetObjectId(Resource databaseObject, Guid Id)
public override void SetObjectId(Resource databaseObject, Guid id)
{
databaseObject.Id = Id;
databaseObject.Id = id;
}
public int UpdateByObject(Resource resource, ResourceObject resourceObject)
......
......@@ -23,9 +23,9 @@ namespace Coscine.Api.Project.Models
return db.ResourceTypes;
}
public override void SetObjectId(ResourceType databaseObject, Guid Id)
public override void SetObjectId(ResourceType databaseObject, Guid id)
{
databaseObject.Id = Id;
databaseObject.Id = id;
}
}
}
......@@ -25,20 +25,17 @@ namespace Coscine.Api.Project.Models
return db.Roles;
}
public override void SetObjectId(Role databaseObject, Guid Id)
public override void SetObjectId(Role databaseObject, Guid id)
{
databaseObject.Id = Id;
databaseObject.Id = id;
}
public Role GetOwnerRole()
{
return DatabaseConnection.ConnectToDatabase((db) =>
{
return
return DatabaseConnection.ConnectToDatabase((db) =>
(from tableEntry in GetITableFromDatabase(db)
where tableEntry.DisplayName == "Owner"
select tableEntry).First();
});
select tableEntry).First());
}
}
}
using Coscine.ApiCommons.Models;
using Coscine.Database.Model;
using LinqToDB;
using System;
using System.Linq.Expressions;
namespace Coscine.Api.Project.Models
{
......
using Coscine.ApiCommons;
using Coscine.Configuration;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System.IO;
namespace Coscine.Api.Project
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment