コード例 #1
0
ファイル: ProcessController.cs プロジェクト: mparsin/Elements
        public async Task<IHttpActionResult> GetProcesses(SearchParameters parameters)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (parameters == null)
            {
                return BadRequest();
            }

            try
            {
                var searchCommand = SearchCommandFactory.CreateSearchCommand(parameters.ProcessName);

                searchCommand.IncludeProcessMetadata = parameters.IncludeProcessMetadata;
                searchCommand.IncludeLayoutMetadata = parameters.IncludeLayoutMetadata;
                searchCommand.IncludeMetricsMetadata = parameters.IncludeMetricsMetadata;

                if (parameters.PageNumber != null)
                {
                    searchCommand.PageNumber = parameters.PageNumber.Value;
                }

                if (parameters.PageSize != null)
                {
                    searchCommand.PageSize = parameters.PageSize.Value;
                }

                searchCommand.LayoutId = parameters.LayoutId;

                searchCommand.FilterId = parameters.FilterId;
                searchCommand.FilterParameters.AddRange(parameters.FilterParameters);
                searchCommand.ApplyDefaultFilter = parameters.ApplyDefaultFilter;

                searchCommand.SearchText = parameters.SearchText;

                searchCommand.GroupColumn = parameters.GroupColumn;

                searchCommand.SortDescriptors.AddRange(parameters.SortDescriptors);
                searchCommand.ApplyLayoutSortDescriptors = parameters.ApplyLayoutSortDescriptors;

                searchCommand.IncludeKpiMetadata = parameters.IncludeKpiMetadata;
                searchCommand.MetricParameters = parameters.MetricParameters;
                searchCommand.KpiParameters = parameters.KpiParameters;

                var result = await searchCommand.ExecuteAsync();

                return Ok(SearchResultFormatters.DefaultFormatter.FormatResult(result));
            }
            catch (FilterNotFoundException)
            {
                return NotFound();
            }
            catch (LayoutNotFoundException)
            {
                return NotFound();
            }
            catch (ProcessNotFoundException)
            {
                return NotFound();
            }
            catch (Exception ex)
            {
                Logger.Log(LogSeverity.Error, LoggerSource, ex);
                return InternalServerError();
            }
        }
コード例 #2
0
        public void WhenErrorOccurs_GetProcesses_ReturnsInternalServerErrorResult()
        {
            // Arrange.
            var searchCommand = Mock.Create<ISearchCommand>(Behavior.Loose);
            Mock.Arrange(() => searchCommand.ExecuteAsync()).Returns(TaskHelper.FromException<SearchCommandResult>(new Exception()));

            Mock.Arrange(() => _searchCommandFactory.CreateSearchCommand(ProcessName)).Returns(searchCommand);

            var controller = CreateProcessController();
            var searchParameters = new SearchParameters(ProcessName);

            // Act.
            var result = controller.GetProcesses(searchParameters).Result as InternalServerErrorResult;

            // Assert.
            Assert.IsNotNull(result);
        }
コード例 #3
0
        public void WhenModelStateIsInvalid_GetProcesses_ReturnsInvalidModelStateResult()
        {
            // Arrange.
            var controller = CreateProcessController();
            controller.ModelState.AddModelError("key", "error");

            var searchParameters = new SearchParameters(ProcessName);

            // Act.
            var result = controller.GetProcesses(searchParameters).Result as InvalidModelStateResult;

            // Assert.
            Assert.IsNotNull(result);
        }
コード例 #4
0
        public void GetProcesses_Returns_FormattedResult()
        {
            // Arrange.
            const int PageNumber = 2;
            const int PageSize = 20;
            const string SearchText = "Search text";

            var searchResult = new SearchCommandResult();

            var searchCommand = Mock.Create<ISearchCommand>(Behavior.Loose);
            Mock.Arrange(() => searchCommand.FilterParameters).Returns(new Collection<object>());
            Mock.Arrange(() => searchCommand.SortDescriptors).Returns(new Collection<ISortDescriptor>());
            Mock.Arrange(() => searchCommand.ExecuteAsync()).Returns(Task.FromResult(searchResult));
            Mock.Arrange(() => _searchCommandFactory.CreateSearchCommand(ProcessName)).Returns(searchCommand);

            var formattedResult = Mock.Create<ISearchResult>(Behavior.Loose);
            Mock.Arrange(() => _searchResultFormatter.FormatResult(searchResult)).Returns(formattedResult);

            var controller = CreateProcessController();
            var searchParameters = new SearchParameters(ProcessName)
                                       {
                                           IncludeProcessMetadata = true,
                                           IncludeLayoutMetadata = true,
                                           PageNumber = PageNumber,
                                           PageSize = PageSize,
                                           SearchText = SearchText,
                                           LayoutId = LayoutId,
                                           FilterId = FilterId,
                                           ApplyDefaultFilter = false,
                                           FilterParameters = { "abc" },
                                           SortDescriptors = { new SortDescriptor("Date", SortDirection.Descending) },
                                           ApplyLayoutSortDescriptors = false
                                       };

            // Act.
            var result = controller.GetProcesses(searchParameters).Result as OkNegotiatedContentResult<ISearchResult>;

            // Assert.
            Assert.IsNotNull(result);
            Assert.AreSame(formattedResult, result.Content);

            Assert.IsTrue(searchCommand.IncludeProcessMetadata);
            Assert.IsTrue(searchCommand.IncludeLayoutMetadata);
            Assert.AreEqual(PageNumber, searchCommand.PageNumber);
            Assert.AreEqual(PageSize, searchCommand.PageSize);
            Assert.AreEqual(SearchText, searchCommand.SearchText);
            Assert.AreEqual(LayoutId, searchCommand.LayoutId);

            Assert.AreEqual(FilterId, searchCommand.FilterId);
            Assert.IsFalse(searchCommand.ApplyDefaultFilter);
            Assert.AreEqual(1, searchCommand.FilterParameters.Count);
            Assert.AreEqual("abc", searchCommand.FilterParameters[0]);

            Assert.AreEqual(1, searchCommand.SortDescriptors.Count);
            Assert.AreEqual("Date", searchCommand.SortDescriptors[0].ColumnName);
            Assert.AreEqual(SortDirection.Descending, searchCommand.SortDescriptors[0].Direction);
            Assert.IsFalse(searchCommand.ApplyLayoutSortDescriptors);
        }