Пример #1
0
        public async Task SaveDataset_GivenViewModelIsNotNullButResponseContainsModelErrors_ReturnsBadRequest()
        {
            // Arrange
            ValidatedApiResponse <NewDatasetVersionResponseModel> response = new ValidatedApiResponse <NewDatasetVersionResponseModel>(HttpStatusCode.BadRequest)
            {
                ModelState = new Dictionary <string, IEnumerable <string> >
                {
                    {
                        "Name", new[]
                        {
                            "Invalid name"
                        }
                    }
                }
            };

            CreateDatasetViewModel viewModel = new CreateDatasetViewModel
            {
                Description      = "Description",
                Filename         = "Filename.xlsx",
                Name             = "Name",
                DataDefinitionId = "0123456789",
                FundingStreamId  = "DSG"
            };

            _apiClient
            .CreateNewDataset(Arg.Any <CreateNewDatasetModel>())
            .Returns(response);

            // Act
            IActionResult result = await _controller.SaveDataset(viewModel);

            // Assert
            _logger
            .Received(1)
            .Warning(Arg.Is("Invalid model provided"));

            result
            .Should()
            .BeOfType <BadRequestObjectResult>();
        }
Пример #2
0
        public async Task <IActionResult> SaveDataset([FromBody] CreateDatasetViewModel vm)
        {
            Guard.ArgumentNotNull(vm.Name, nameof(vm.Name));
            Guard.ArgumentNotNull(vm.Description, nameof(vm.Description));
            Guard.ArgumentNotNull(vm.Filename, nameof(vm.Filename));
            Guard.ArgumentNotNull(vm.FundingStreamId, nameof(vm.FundingStreamId));
            Guard.ArgumentNotNull(vm.DataDefinitionId, nameof(vm.DataDefinitionId));

            ValidatedApiResponse <NewDatasetVersionResponseModel> response = await _datasetApiClient.CreateNewDataset(
                new CreateNewDatasetModel
            {
                Name            = vm.Name,
                DefinitionId    = vm.DataDefinitionId,
                Description     = vm.Description,
                Filename        = vm.Filename,
                FundingStreamId = vm.FundingStreamId
            });

            if (response.IsBadRequest(out BadRequestObjectResult badRequest))
            {
                _logger.Warning("Invalid model provided");

                return(badRequest);
            }
            else
            {
                if (!response.StatusCode.IsSuccess())
                {
                    int statusCode = (int)response.StatusCode;

                    _logger.Error($"Error when posting data set with status code: {statusCode}");

                    return(new StatusCodeResult(statusCode));
                }
            }

            return(new OkObjectResult(response.Content));
        }