public async Task CreateAsync_GivenAlreadyUsedName_ThrowsItemNotProcessableException()
        {
            var functionRepository    = Substitute.For <IFunctionRepository>();
            var permissionRepository  = Substitute.For <IPermissionRepository>();
            var applicationRepository = Substitute.For <IApplicationRepository>();
            var subRealmRepository    = Substitute.For <ISubRealmRepository>();

            applicationRepository.GetByIdAsync(mockedFunctionModel.Application.Id)
            .Returns(mockedFunctionModel.Application);
            permissionRepository.GetByIdWithApplicationAsync(mockedFunctionModel.FunctionPermissions[0].PermissionId)
            .Returns(mockedFunctionModel.FunctionPermissions[0].Permission);
            functionRepository.GetByIdAsync(mockedFunctionModel.Id).Returns(mockedFunctionModel);
            functionRepository.GetByNameAsync(mockedFunctionSubmitModel.Name).Returns(mockedFunctionModel);
            functionRepository.CreateAsync(Arg.Any <FunctionModel>()).Returns(mockedFunctionModel);

            var functionService = new FunctionService(functionRepository, permissionRepository, applicationRepository, subRealmRepository, mapper);

            // Act
            Exception caughEx = null;

            try
            {
                var functionResource = await functionService.CreateAsync(mockedFunctionSubmitModel, Guid.NewGuid());
            }
            catch (Exception ex)
            {
                caughEx = ex;
            }

            // Assert
            Assert.True(caughEx is ItemNotProcessableException, "Attempted create with an already used name must throw an ItemNotProcessableException.");
        }
        public async Task CreateAsync_GivenUnlinkedPermissionAndApplication_ThrowsItemNotProcessableException()
        {
            // Arrange
            var functionRepository    = Substitute.For <IFunctionRepository>();
            var permissionRepository  = Substitute.For <IPermissionRepository>();
            var applicationRepository = Substitute.For <IApplicationRepository>();
            var subRealmRepository    = Substitute.For <ISubRealmRepository>();

            // Change ApplicationId to break link between the permission and Application
            mockedFunctionModel.FunctionPermissions[0].Permission.ApplicationFunctionPermissions[0].ApplicationFunction.Application.Id = Guid.NewGuid();

            applicationRepository.GetByIdAsync(Arg.Any <Guid>())
            .Returns(mockedFunctionModel.Application);
            permissionRepository.GetByIdWithApplicationAsync(mockedFunctionModel.FunctionPermissions[0].PermissionId)
            .Returns(mockedFunctionModel.FunctionPermissions[0].Permission);
            functionRepository.CreateAsync(Arg.Any <FunctionModel>()).Returns(mockedFunctionModel);

            var functionService = new FunctionService(functionRepository, permissionRepository, applicationRepository, subRealmRepository, mapper);

            // Act
            Exception caughEx = null;

            try
            {
                var functionResource = await functionService.CreateAsync(mockedFunctionSubmitModel, Guid.NewGuid());
            }
            catch (Exception ex)
            {
                caughEx = ex;
            }

            // Assert
            Assert.True(caughEx is ItemNotProcessableException, "Unlinked permissions and applications must throw an ItemNotProcessableException.");
        }
        public async Task CreateAsync_GivenFullProcessableModel_ReturnsCreatedModel()
        {
            // Arrange
            var functionRepository    = Substitute.For <IFunctionRepository>();
            var permissionRepository  = Substitute.For <IPermissionRepository>();
            var applicationRepository = Substitute.For <IApplicationRepository>();
            var subRealmRepository    = Substitute.For <ISubRealmRepository>();

            applicationRepository.GetByIdAsync(mockedFunctionModel.Application.Id)
            .Returns(mockedFunctionModel.Application);
            permissionRepository.GetByIdWithApplicationAsync(mockedFunctionModel.FunctionPermissions[0].PermissionId)
            .Returns(mockedFunctionModel.FunctionPermissions[0].Permission);
            functionRepository.CreateAsync(Arg.Any <FunctionModel>()).Returns(mockedFunctionModel);

            var functionService = new FunctionService(functionRepository, permissionRepository, applicationRepository, subRealmRepository, mapper);

            // Act
            var functionResource = await functionService.CreateAsync(mockedFunctionSubmitModel, Guid.NewGuid());

            // Assert
            Assert.NotNull(functionResource);
            Assert.True(functionResource.Name == mockedFunctionSubmitModel.Name, $"Function Resource name: '{functionResource.Name}' not the expected value: '{mockedFunctionSubmitModel.Name}'");
            Assert.True(functionResource.ApplicationId == mockedFunctionSubmitModel.ApplicationId, $"Function Resource name: '{functionResource.ApplicationId}' not the expected value: '{mockedFunctionSubmitModel.ApplicationId}'");
            Assert.True(functionResource.Permissions.Count == mockedFunctionSubmitModel.Permissions.Count, $"Function Resource Permission Count: '{functionResource.Permissions.Count}' not the expected value: '{mockedFunctionSubmitModel.Permissions.Count}'");
        }
        public async Task CreateAsync_GivenUnfindablePermission_ThrowsItemNotFoundException()
        {
            // Arrange
            var functionRepository    = Substitute.For <IFunctionRepository>();
            var permissionRepository  = Substitute.For <IPermissionRepository>();
            var applicationRepository = Substitute.For <IApplicationRepository>();
            var subRealmRepository    = Substitute.For <ISubRealmRepository>();

            applicationRepository.GetByIdAsync(mockedFunctionModel.Application.Id)
            .Returns(mockedFunctionModel.Application);

            var functionService = new FunctionService(functionRepository, permissionRepository, applicationRepository, subRealmRepository, mapper);

            // Act
            Exception caughEx = null;

            try
            {
                var functionResource = await functionService.CreateAsync(mockedFunctionSubmitModel, Guid.NewGuid());
            }
            catch (Exception ex)
            {
                caughEx = ex;
            }

            // Assert
            Assert.True(caughEx is ItemNotFoundException, "Unfindable Permissions must throw an ItemNotFoundException.");
        }