ResourceControllerTests.cs 4.38 KB
Newer Older
1 2 3
using Coscine.Api.Project.Controllers;
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
4 5
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
6
using Microsoft.AspNetCore.Mvc;
7
using Newtonsoft.Json.Linq;
8 9
using NUnit.Framework;
using System;
10
using System.Collections.Generic;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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();
26
            Assert.IsTrue(actionResult.GetType() == typeof(JsonResult));
27 28 29 30 31
        }

        [Test]
        public void TestControllerGet()
        {
L. Ellenbeck's avatar
L. Ellenbeck committed
32
            var actionResult = Controller.Get(Resources[0].Id.ToString());
33
            Assert.IsTrue(actionResult.GetType() == typeof(JsonResult));
34

35 36
            JsonResult result = (JsonResult)actionResult;
            Assert.IsTrue(result.Value.GetType() == typeof(ResourceObject));
37

38
            ResourceObject resourceObject = (ResourceObject)result.Value;
39

40 41 42 43 44
            Assert.IsTrue(resourceObject.Id == Resources[0].Id);
            Assert.IsTrue(resourceObject.DisplayName == Resources[0].DisplayName);
            Assert.IsTrue(resourceObject.Visibility.DisplayName == Resources[0].Visibility.DisplayName);
            Assert.IsTrue(resourceObject.Type.Id == Resources[0].Type.Id);
            Assert.IsTrue(resourceObject.Type.DisplayName == Resources[0].Type.DisplayName);
45 46 47 48 49 50
        }


        [Test]
        public void TestControllerUpdate()
        {
L. Ellenbeck's avatar
L. Ellenbeck committed
51
            var actionResult = Controller.Get(Resources[0].Id.ToString());
52 53
            JsonResult result = (JsonResult)actionResult;
            ResourceObject resourceObject = (ResourceObject)result.Value;
54

55
            resourceObject.DisplayName = "OtherName";
56
            resourceObject.ResourceTypeOption = JObject.FromObject(new RDSResourceTypeObject(Guid.NewGuid(), "PITLABTTEST", 0));
57 58 59

            Stream stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);

L. Ellenbeck's avatar
L. Ellenbeck committed
60
            FakeControllerContext(Users[0], stream);
61

L. Ellenbeck's avatar
L. Ellenbeck committed
62
            actionResult = Controller.Update(Resources[0].Id.ToString());
63
            Assert.IsTrue(actionResult.GetType() == typeof(JsonResult));
64 65 66 67 68 69

            // Cleanup
            stream.Close();

            stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);

L. Ellenbeck's avatar
L. Ellenbeck committed
70
            FakeControllerContext(Users[0], stream);
71

72 73
            actionResult = Controller.Update(Resources[1].Id.ToString());
            Assert.IsTrue(actionResult.GetType() == typeof(UnauthorizedObjectResult));
74 75 76 77 78 79 80 81

            // Cleanup
            stream.Close();
        }

        [Test]
        public void TestControllerStoreToProject()
        {
82 83 84 85
            ResourceObject resourceObject = new ResourceObject(
                Guid.NewGuid(),
                "Test",
                "testR",
86
                "testD",
87 88 89
                "keys",
                "usageR",
                new ResourceTypeObject(Resources[0].Type.Id, Resources[0].Type.DisplayName),
90
                new List<DisciplineObject>() { new DisciplineObject(Discipline.Id, Discipline.Url, Discipline.DisplayNameDe, Discipline.DisplayNameEn) },
91 92
                new VisibilityObject(Visibility.Id, Visibility.DisplayName),
                new LicenseObject(License.Id, License.DisplayName),
93
                JObject.FromObject(new RDSResourceTypeObject(Guid.NewGuid(), "PITLABTTEST", 0)),
94 95
                "link",
                JToken.Parse("{}")
96
            );
97 98 99

            Stream stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);

L. Ellenbeck's avatar
L. Ellenbeck committed
100
            FakeControllerContext(Users[0], stream);
101

L. Ellenbeck's avatar
L. Ellenbeck committed
102
            var actionResult = Controller.StoreToProject(Projects[0].Id.ToString());
103 104 105
            Assert.IsTrue(actionResult.GetType() == typeof(JsonResult));
            JsonResult result = (JsonResult)actionResult;
            resourceObject = (ResourceObject)result.Value;
106 107 108 109

            // Cleanup
            stream.Close();

110
            stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);
111

112 113 114
            FakeControllerContext(Users[0], stream);

            actionResult = Controller.Delete(resourceObject.Id.ToString());
115
            Assert.IsTrue(actionResult.GetType() == typeof(JsonResult));
116

117
            stream.Close();
118 119 120
        }
    }
}