示例#1
0
 public IHttpActionResult SetDefaultProcessLayout(string process, Layout layout)
 {
     try
     {
         LayoutRepository.MakeDefault(process, layout.Id);
         return Ok();
     }
     catch (LayoutNotFoundException)
     {
         return NotFound();
     }
     catch (Exception ex)
     {
         Logger.Log(LogSeverity.Error, LoggerSource, ex);
         return InternalServerError();
     }
 }
        public void WhenErrorOccurs_PostProcessLayout_ReturnsInternalServerErrorResult()
        {
            // Arrange.
            var layout = new Layout();
            Mock.Arrange(() => _layoutRepository.InsertLayout(layout)).Throws<Exception>();

            var controller = CreateProcessController();

            // Act.
            var result = controller.PostProcessLayout(ProcessName, layout) as InternalServerErrorResult;

            // Assert.
            Assert.IsNotNull(result);
            Mock.Assert(() => _logger.Log(LogSeverity.Error, Arg.AnyString, Arg.IsAny<Exception>()), Occurs.Once());
        }
        public void PutProcessLayout_Updates_ProcessLayout()
        {
            // Arrange.
            var layout = new Layout();
            var savedLayout = new Layout();

            Mock.Arrange(() => _layoutRepository.UpdateLayout(layout)).Returns(l => l.Process == ProcessName && l.Id == LayoutId ? savedLayout : null);

            var controller = CreateProcessController();

            // Act.
            var result = controller.PutProcessLayout(ProcessName, LayoutId, layout) as OkNegotiatedContentResult<Layout>;

            // Assert.
            Assert.IsNotNull(result);
            Assert.AreSame(savedLayout, result.Content);
        }
        public void PostProcessLayout_Creates_NewProcessLayout()
        {
            // Arrange.
            var layout = new Layout();
            var savedLayout = new Layout { Id = LayoutId };

            Mock.Arrange(() => _layoutRepository.InsertLayout(layout)).Returns(l => l.Process == ProcessName ? savedLayout : null);

            var controller = CreateProcessController();

            // Act.
            var result = controller.PostProcessLayout(ProcessName, layout) as CreatedAtRouteNegotiatedContentResult<Layout>;

            // Assert.
            Assert.IsNotNull(result);
            Assert.AreSame(savedLayout, result.Content);
        }
        public void WhenProcessIsNotFound_PostProcessLayout_ReturnsNotFoundResult()
        {
            // Arrange.
            var layout = new Layout();
            Mock.Arrange(() => _layoutRepository.InsertLayout(layout)).Throws<ProcessNotFoundException>();

            var controller = CreateProcessController();

            // Act.
            var result = controller.PostProcessLayout(ProcessName, layout) as NotFoundResult;

            // Assert.
            Assert.IsNotNull(result);
        }
示例#6
0
        /// <summary>
        /// Updates an existing layout.
        /// </summary>
        /// <param name="layout">
        /// The layout.
        /// </param>
        /// <returns>
        /// The saved layout.
        /// </returns>
        public Layout UpdateLayout(Layout layout)
        {
            if (layout == null)
            {
                throw new ArgumentNullException("layout");
            }

            var processMetadata = ProcessRepository.GetProcessMetadata(layout.Process);
            var layoutEdit = LayoutManager.GetLayoutEdit(layout.Id);
            if (layoutEdit == null || layoutEdit.Id == 0)
            {
                throw ErrorUtils.LayoutNotFound(layout.Id);
            }

            if (layoutEdit.IsAdminLayout == true)
            {
                throw new InvalidOperationException("Cannot update admin layout.");
            }

            layoutEdit.Name = layout.Name;
            if (string.IsNullOrEmpty(layoutEdit.Guid))
            {
                layoutEdit.Guid = Guid.NewGuid().ToString();
            }

            layoutEdit.LayoutDefinition = GetLayoutDefinition(layout, processMetadata);
            layoutEdit = (ILayoutEdit)layoutEdit.Save();

            return CreateLayout(layoutEdit);
        }
        public void WhenLayoutIsNotFound_SetDefaultProcessLayout_ReturnsNotFoundResult()
        {
            // Arrange.
            Mock.Arrange(() => _layoutRepository.MakeDefault(ProcessName, LayoutId)).Throws<LayoutNotFoundException>();

            var layout = new Layout { Id = LayoutId };
            var controller = CreateProcessController();

            // Act.
            var result = controller.SetDefaultProcessLayout(ProcessName, layout) as NotFoundResult;

            // Assert.
            Assert.IsNotNull(result);
        }
示例#8
0
        public void InsertLayout_Inserts_NewLayout()
        {
            // Arrange.
            const string LayoutName = "Test Layout";
            const string ProcessName = "TestProcess_p";
            const int AccountId = 111;
            const int LayoutId = 222;

            Mock.Arrange(() => _utils.CurrentUserAccountId).Returns(AccountId);

            var layoutEdit = Mock.Create<ILayoutEdit>(Behavior.Loose);
            Mock.Arrange(() => layoutEdit.Save()).DoInstead(
                () =>
                    {
                        Assert.AreEqual(LayoutName, layoutEdit.Name);
                        Assert.AreEqual(ProcessName, layoutEdit.ProcessSystemName);

                        Guid layoutGuid;
                        Assert.IsTrue(Guid.TryParse(layoutEdit.Guid ?? string.Empty, out layoutGuid));

                        Assert.AreEqual(AccountId, layoutEdit.Account);
                        Assert.AreEqual(false, layoutEdit.IsDefault);
                        Assert.AreEqual(false, layoutEdit.IsAdminLayout);
                        Assert.AreEqual(false, layoutEdit.IsAdminDefault);
                    }).Returns(
                        () =>
                            {
                                var savedLayoutEdit = CloneLayout(layoutEdit);
                                savedLayoutEdit.Id = LayoutId;

                                return savedLayoutEdit;
                            });

            Mock.Arrange(() => _layoutManager.NewLayoutEdit()).Returns(layoutEdit);

            var processMetadata = CreateProcessMetadata(ProcessName);
            Mock.Arrange(() => _processRepository.GetProcessMetadata(ProcessName)).Returns(processMetadata);

            var layout = new Layout { Name = LayoutName, Process = ProcessName };
            layout.Columns.Add(new Column { SystemName = "Field1_f", Width = 50, SortLevel = 1, SortDirection = SortDirection.Ascending });
            layout.Columns.Add(new Column { SystemName = "Field2_f", Width = 100, GroupLevel = 1 });

            var layoutRepository = CreateLayoutRepository();

            // Act.
            var savedLayout = layoutRepository.InsertLayout(layout);

            // Assert.
            Assert.IsNotNull(savedLayout);
            Assert.AreEqual(LayoutId, savedLayout.Id);
            Assert.AreEqual(LayoutName, savedLayout.Name);
            Assert.AreEqual(ProcessName, savedLayout.Process);

            Assert.AreEqual(2, savedLayout.Columns.Count);

            Assert.AreEqual("Field1_f", savedLayout.Columns[0].SystemName);
            Assert.AreEqual(50, savedLayout.Columns[0].Width);
            Assert.AreEqual(1, savedLayout.Columns[0].SortLevel);
            Assert.AreEqual(SortDirection.Ascending, savedLayout.Columns[0].SortDirection);
            Assert.IsNull(savedLayout.Columns[0].GroupLevel);

            Assert.AreEqual("Field2_f", savedLayout.Columns[1].SystemName);
            Assert.AreEqual(100, savedLayout.Columns[1].Width);
            Assert.IsNull(savedLayout.Columns[1].SortLevel);
            Assert.AreEqual(SortDirection.None, savedLayout.Columns[1].SortDirection);
            Assert.AreEqual(1, savedLayout.Columns[1].GroupLevel);
        }
        public void WhenLayoutIsAdmin_PutProcessLayout_ReturnsForbiddenStatus()
        {
            // Arrange.
            var layout = new Layout();
            Mock.Arrange(() => _layoutRepository.UpdateLayout(layout)).Throws<InvalidOperationException>();

            var controller = CreateProcessController();

            // Act.
            var result = controller.PutProcessLayout(ProcessName, LayoutId, layout) as StatusCodeResult;

            // Assert.
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.Forbidden, result.StatusCode);
        }
示例#10
0
        public void WhenLayoutIsNotFound_UpdateLayout_ThrowsException()
        {
            // Arrange.
            const int LayoutId = 111;
            const string LayoutName = "New Layout";
            const string ProcessName = "TestProcess_p";

            var processMetadata = CreateProcessMetadata(ProcessName);
            Mock.Arrange(() => _processRepository.GetProcessMetadata(ProcessName)).Returns(processMetadata);

            Mock.Arrange(() => _layoutManager.GetLayoutEdit(LayoutId)).Returns(() => null);

            var layout = new Layout { Id = LayoutId, Name = LayoutName, Process = ProcessName };
            var layoutRepository = CreateLayoutRepository();

            // Act.
            layoutRepository.UpdateLayout(layout);
        }
示例#11
0
        public void WhenLayoutIsAdmin_UpdateLayout_ThrowsException()
        {
            // Arrange.
            const int LayoutId = 111;
            const string LayoutName = "New Layout";
            const string ProcessName = "TestProcess_p";

            var processMetadata = CreateProcessMetadata(ProcessName);
            Mock.Arrange(() => _processRepository.GetProcessMetadata(ProcessName)).Returns(processMetadata);

            var layoutEdit = CreateLayoutEdit(LayoutId);
            Mock.Arrange(() => layoutEdit.ProcessSystemName).Returns(ProcessName);
            Mock.Arrange(() => layoutEdit.IsAdminLayout).Returns(true);
            Mock.Arrange(() => layoutEdit.Save()).Returns(() => CloneLayout(layoutEdit));
            Mock.Arrange(() => _layoutManager.GetLayoutEdit(LayoutId)).Returns(layoutEdit);

            var layout = new Layout { Id = LayoutId, Name = LayoutName, Process = ProcessName };
            var layoutRepository = CreateLayoutRepository();

            // Act.
            layoutRepository.UpdateLayout(layout);
        }
示例#12
0
        public void UpdateLayout_Updates_ExistingLayout()
        {
            // Arrange.
            const int LayoutId = 111;
            const string LayoutName = "New Layout";
            const string ProcessName = "TestProcess_p";

            var processMetadata = CreateProcessMetadata(ProcessName);
            Mock.Arrange(() => _processRepository.GetProcessMetadata(ProcessName)).Returns(processMetadata);

            var layoutEdit = CreateLayoutEdit(LayoutId);
            Mock.Arrange(() => layoutEdit.ProcessSystemName).Returns(ProcessName);
            Mock.Arrange(() => layoutEdit.Save()).Returns(() => CloneLayout(layoutEdit));
            Mock.Arrange(() => _layoutManager.GetLayoutEdit(LayoutId)).Returns(layoutEdit);

            var layout = new Layout { Id = LayoutId, Name = LayoutName, Process = ProcessName };
            layout.Columns.Add(new Column { SystemName = "Field1_f" });

            var layoutRepository = CreateLayoutRepository();

            // Act.
            var savedLayout = layoutRepository.UpdateLayout(layout);

            // Assert.
            Assert.IsNotNull(savedLayout);

            Assert.AreEqual(LayoutId, savedLayout.Id);
            Assert.AreEqual(LayoutName, savedLayout.Name);
            Assert.AreEqual(ProcessName, savedLayout.Process);
            Assert.AreEqual(1, savedLayout.Columns.Count);

            Assert.AreEqual("Field1_f", savedLayout.Columns[0].SystemName);

            Mock.Assert(() => layoutEdit.Save(), Occurs.Once());
        }
示例#13
0
        private Layout CreateLayout(ILayoutEdit layoutEdit)
        {
            var layout = new Layout { Id = layoutEdit.Id, Name = layoutEdit.Name, Process = layoutEdit.ProcessSystemName };

            foreach (var gridColumn in GetGridColumns(layoutEdit.LayoutDefinition))
            {
                layout.Columns.Add(CreateColumn(gridColumn));
            }

            return layout;
        }
示例#14
0
        private static string GetLayoutDefinition(Layout layout, IProcessMetadata processMetadata)
        {
            if (layout.Columns.Count == 0)
            {
                return null;
            }

            return JsonConvert.SerializeObject(layout.Columns.Select(column => CreateGridColumn(column, processMetadata)));
        }
示例#15
0
        public IHttpActionResult PostProcessLayout(string process, Layout layout)
        {
            if (layout == null)
            {
                return BadRequest();
            }

            try
            {
                layout.Process = process;

                layout = LayoutRepository.InsertLayout(layout);

                return CreatedAtRoute(GetProcessLayoutRouteName, new { layoutId = layout.Id }, layout);
            }
            catch (ProcessNotFoundException)
            {
                return NotFound();
            }
            catch (Exception ex)
            {
                Logger.Log(LogSeverity.Error, LoggerSource, ex);
                return InternalServerError();
            }
        }
示例#16
0
        public void SetDefaultProcessLayout_Changes_DefaultLayout()
        {
            // Arrange.
            var layout = new Layout { Id = LayoutId };
            var controller = CreateProcessController();

            // Act.
            var result = controller.SetDefaultProcessLayout(ProcessName, layout) as OkResult;

            // Assert.
            Assert.IsNotNull(result);
            Mock.Assert(() => _layoutRepository.MakeDefault(ProcessName, LayoutId), Occurs.Once());
        }
示例#17
0
        public IHttpActionResult PutProcessLayout(string process, int layoutId, Layout layout)
        {
            if (layout == null)
            {
                return BadRequest();
            }

            try
            {
                layout.Id = layoutId;
                layout.Process = process;
                layout = LayoutRepository.UpdateLayout(layout);

                return Ok(layout);
            }
            catch (ProcessNotFoundException)
            {
                return NotFound();
            }
            catch (LayoutNotFoundException)
            {
                return NotFound();
            }
            catch (InvalidOperationException ex)
            {
                Logger.Log(LogSeverity.Error, LoggerSource, ex);
                return StatusCode(HttpStatusCode.Forbidden);
            }
            catch (Exception ex)
            {
                Logger.Log(LogSeverity.Error, LoggerSource, ex);
                return InternalServerError();
            }
        }
示例#18
0
        /// <summary>
        /// Inserts a new layout.
        /// </summary>
        /// <param name="layout">
        /// The layout.
        /// </param>
        /// <returns>
        /// The saved layout.
        /// </returns>
        public Layout InsertLayout(Layout layout)
        {
            if (layout == null)
            {
                throw new ArgumentNullException("layout");
            }

            var processMetadata = ProcessRepository.GetProcessMetadata(layout.Process);
            var layoutEdit = LayoutManager.NewLayoutEdit();

            layoutEdit.Name = layout.Name;
            layoutEdit.ProcessSystemName = processMetadata.Name;
            layoutEdit.Guid = Guid.NewGuid().ToString();
            layoutEdit.Account = ToNullable(Utils.CurrentUserAccountId);
            layoutEdit.IsDefault = false;
            layoutEdit.IsAdminLayout = false;
            layoutEdit.IsAdminDefault = false;
            layoutEdit.LayoutDefinition = GetLayoutDefinition(layout, processMetadata);

            layoutEdit = (ILayoutEdit)layoutEdit.Save();

            return CreateLayout(layoutEdit);
        }