Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
3.2 kB
2
Indexable
Never
using System.Collections.Generic;
using System.IO;
using Clearcore2.SecurityManagement;
using Clearcore2.UI.Infrastructure.Events;
using Clearcore2.VerticalApplications.Security.ViewModels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using NUnit.Framework;
using OFX.Core;
using OFX.Presentation.Dialogs;
using OFX.Security.RoleBasedRepository;
using OFX.Security.RoleBasedRepository.Commands;
using Assert = NUnit.Framework.Assert;

namespace Framework.UnitTests.Public.ClientAPI.Clearcore2.VerticalApplications.Security.ViewModel
{    
    [TestFixture]
    [Category(TestCategories.GeneralB)]
    public class RolePermissionCategoriesModelUnitTests : ImportExportDatabaseUnitTestsBase
    {
        [SetUp]
        public void SetUp()
        {
            var mockDialog = new Mock<IMessageDialog>();
            OFXApp.Locator.RegisterInstance(typeof(IMessageDialog), mockDialog.Object);
        }

        [Test]
        public void RolesAndPermissionsAreUpdatedOnSecurityImportedEventTest()
        {
            var rolePermissionCategoriesModel = new RolePermissionCategoriesModel();
            var privateRolePermissionModel = new PrivateObject(rolePermissionCategoriesModel);
            OFXApp.SecurityContext.SecurityRepositories.RunCommand(new ImportDatabaseCommand(Path.GetFullPath(@"..\..\LargeFiles\TestData\SecurityDatabase\Valid.data"), false));
            Assert.That(((List<RoleEntity>) privateRolePermissionModel.GetField("_allRoles")).Count, Is.EqualTo(0));
            Assert.That(rolePermissionCategoriesModel.RolePermissionCategoryList.Count, Is.EqualTo(0));


            OFXApp.EventBroker.GetEventChannel<SecurityDatabaseImportedEvent>(EventChannels.DefaultLocal).Publish(new SecurityDatabaseImportedEvent());
            Assert.That(((List<RoleEntity>)privateRolePermissionModel.GetField("_allRoles")).Count, Is.EqualTo(4));
            Assert.That(rolePermissionCategoriesModel.RolePermissionCategoryList.Count, Is.EqualTo(11));
        }

        private int counter = 0;
        [Test]
        public void RolesChangingEventIsTriggeredWhenSecurityDatabaseImportedEventIsTriggeredTest()
        {
            var rolePermissionCategoriesModel = new RolePermissionCategoriesModel();
            try
            {
                counter = 0;
                rolePermissionCategoriesModel.RolesChanging += RolePermissionCategoriesModel_RolesChanging;

                OFXApp.EventBroker.GetEventChannel<SecurityDatabaseImportedEvent>(EventChannels.DefaultLocal).Publish(new SecurityDatabaseImportedEvent());
                Assert.AreEqual(counter, 1);

                OFXApp.EventBroker.GetEventChannel<SecurityDatabaseImportedEvent>(EventChannels.DefaultLocal).Publish(new SecurityDatabaseImportedEvent());
                Assert.AreEqual(counter, 2);
            }
            finally
            {
                rolePermissionCategoriesModel.RolesChanging -= RolePermissionCategoriesModel_RolesChanging;
            }

        }

        private void RolePermissionCategoriesModel_RolesChanging(object sender, System.EventArgs e)
        {
            counter++;
        }
    }
}
Leave a Comment