Skip to content
Snippets Groups Projects
Select Git revision
  • master
1 result

192.nfa

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ResourceControllerTests.cs 4.03 KiB
    using Coscine.Api.Project.Controllers;
    using Coscine.Api.Project.Exceptions;
    using Coscine.Api.Project.Factories;
    using Coscine.Api.Project.Models;
    using Coscine.Api.Project.ReturnObjects;
    using Microsoft.AspNetCore.Mvc;
    using NUnit.Framework;
    using System;
    using System.IO;
    
    namespace Coscine.Api.Project.Tests
    {
        [TestFixture]
        public class ResourceControllerTests : DefaultControllerTests<ResourceController>
        {
            public ResourceControllerTests() : base(new ResourceController())
            {
            }
    
            [Test]
            public void TestControllerIndex()
            {
                var actionResult = Controller.Index();
                Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
            }
    
            [Test]
            public void TestControllerGet()
            {
                var actionResult = Controller.Get(resources[0].Id.ToString());
                Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
    
                OkObjectResult okObjectResult = (OkObjectResult)actionResult;
                Assert.IsTrue(okObjectResult.Value.GetType() == typeof(ResourceObject));
    
                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);
            }
    
    
            [Test]
            public void TestControllerUpdate()
            {
                var actionResult = Controller.Get(resources[0].Id.ToString());
                OkObjectResult okObjectResult = (OkObjectResult)actionResult;
                ResourceObject resourceObject = (ResourceObject)okObjectResult.Value;
    
                resourceObject.ExternalId = "ChangedExternalId";
    
                Stream stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);
    
                FakeControllerContext(users[0], stream);
    
                actionResult = Controller.Update(resources[0].Id.ToString());
                Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
    
                // Cleanup
                stream.Close();
    
                stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);
    
                FakeControllerContext(users[0], stream);
    
                try
                {
                    actionResult = Controller.Update(resources[1].Id.ToString());
                    Assert.Fail();
                }
                catch (Exception e)
                {
                    Assert.IsTrue(e.GetType() == typeof(NotAuthorizedException));
                }
    
                // Cleanup
                stream.Close();
            }
    
            [Test]
            public void TestControllerStoreToProject()
            {
                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);
    
                var actionResult = Controller.StoreToProject(projects[0].Id.ToString());
                Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
                OkObjectResult okObjectResult = (OkObjectResult)actionResult;
                resourceObject = (ResourceObject)okObjectResult.Value;
    
                // Cleanup
                stream.Close();
    
                ResourceModel resourceModel = new ResourceModel();
                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))
                {
                    projectResourceModel.Delete(projectResource);
                }
    
                resourceModel.Delete(resource);
            }
        }
    }