예제 #1
0
        async public Task <IActionResult> ValidateDataset([FromBody] ValidateDatasetModel vm)
        {
            Guard.ArgumentNotNull(vm, nameof(vm));

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ValidatedApiResponse <DatasetValidationStatusModel> apiResponse = await _datasetApiClient.ValidateDataset(vm);

            if (apiResponse == null)
            {
                _logger.Warning("Validate Dataset API response was null");
                return(new InternalServerErrorResult("Validate Dataset API response was null"));
            }

            if (!apiResponse.StatusCode.IsSuccess())
            {
                _logger.Warning("Failed to validate dataset with status code: {statusCode}", apiResponse.StatusCode);

                if (apiResponse.StatusCode == HttpStatusCode.BadRequest && !apiResponse.ModelState.Values.IsNullOrEmpty())
                {
                    return(new BadRequestObjectResult(apiResponse.ModelState));
                }

                return(new InternalServerErrorResult("Validate Dataset API response failed with status code: {statusCode}" + apiResponse.StatusCode));
            }

            DatasetValidationStatusViewModel result = _mapper.Map <DatasetValidationStatusViewModel>(apiResponse.Content);

            return(new OkObjectResult(result));
        }
예제 #2
0
        public async Task <IActionResult> GetDatasetValidateStatus([FromRoute] string operationId)
        {
            if (string.IsNullOrWhiteSpace(operationId))
            {
                return(new BadRequestObjectResult("Missing operationId"));
            }

            ApiResponse <DatasetValidationStatusModel> statusResponse =
                await _datasetApiClient.GetValidateDatasetStatus(operationId);

            if (statusResponse.StatusCode == HttpStatusCode.NotFound)
            {
                return(new NotFoundObjectResult("Validation status not found"));
            }
            else if (statusResponse.StatusCode == HttpStatusCode.OK)
            {
                DatasetValidationStatusViewModel result =
                    _mapper.Map <DatasetValidationStatusViewModel>(statusResponse.Content);
                return(new OkObjectResult(result));
            }
            else
            {
                _logger.Error(
                    $"{nameof(GetDatasetValidateStatus)} returned unexpected HTTP status {statusResponse.StatusCode}");
                return(new InternalServerErrorResult("Unable to query Validate Status"));
            }
        }
예제 #3
0
        public async Task <IActionResult> ValidateDataset([FromBody] ValidateDatasetModel vm)
        {
            Guard.ArgumentNotNull(vm, nameof(vm));

            FundingStreamPermission permissions = await _authorizationHelper.GetUserFundingStreamPermissions(User, vm.FundingStreamId);

            if (permissions?.CanUploadDataSourceFiles != true)
            {
                _logger.Error($"User [{User?.Identity?.Name}] has insufficient permissions to upload a dataset file for {vm.FundingStreamId}");
                return(Forbid(new AuthenticationProperties()));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ValidatedApiResponse <DatasetValidationStatusModel> apiResponse = await _datasetApiClient.ValidateDataset(
                new GetDatasetBlobModel
            {
                DatasetId            = vm.DatasetId,
                Version              = vm.Version,
                Filename             = vm.Filename,
                Description          = vm.Description,
                Comment              = vm.Comment,
                FundingStreamId      = vm.FundingStreamId,
                MergeExistingVersion = vm.MergeExistingVersion
            });

            if (apiResponse == null)
            {
                _logger.Warning("Validate Dataset API response was null");
                return(new InternalServerErrorResult("Validate Dataset API response was null"));
            }

            if (!apiResponse.StatusCode.IsSuccess())
            {
                _logger.Warning("Failed to validate dataset with status code: {statusCode}", apiResponse.StatusCode);

                if (apiResponse.IsBadRequest(out BadRequestObjectResult badRequest))
                {
                    return(badRequest);
                }

                return(new InternalServerErrorResult(
                           "Validate Dataset API response failed with status code: {statusCode}" + apiResponse.StatusCode));
            }

            DatasetValidationStatusViewModel
                result = _mapper.Map <DatasetValidationStatusViewModel>(apiResponse.Content);

            return(new OkObjectResult(result));
        }
예제 #4
0
        public async Task ValidateDataset_GivenViewModelAndResponseIsSuccess_ReturnsStatusModel()
        {
            string fundingStreamId = Guid.NewGuid().ToString();

            // Arrange
            ValidateDatasetModel viewModel = new ValidateDatasetModel
            {
                FundingStreamId = fundingStreamId
            };

            GivenTheUserHasPermissionToUploadDataSourceFilesForFundingStream(fundingStreamId, new FundingStreamPermission
            {
                CanUploadDataSourceFiles = true
            });

            DatasetValidationStatusModel statusModel = new DatasetValidationStatusModel
            {
                ValidationFailures = new Dictionary <string, IEnumerable <string> >(),
                CurrentOperation   = DatasetValidationStatus.Processing,
                DatasetId          = "datasetId",
                ErrorMessage       = "errorMessage",
                OperationId        = "operationId"
            };

            ValidatedApiResponse <DatasetValidationStatusModel> response = new ValidatedApiResponse <DatasetValidationStatusModel>(HttpStatusCode.OK, statusModel);

            DatasetValidationStatusViewModel resultViewModel = new DatasetValidationStatusViewModel
            {
                ValidationFailures = new Dictionary <string, IEnumerable <string> >(),
                CurrentOperation   = DatasetValidationStatusOperationViewModel.Processing,
                DatasetId          = "datasetId",
                ErrorMessage       = "errorMessage",
                OperationId        = "operationId"
            };

            _apiClient
            .ValidateDataset(Arg.Any <GetDatasetBlobModel>())
            .Returns(response);

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

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeEquivalentTo(resultViewModel);
        }
        public async Task ValidateDataset_GivenViewModelAndResponseIsSuccess_ReturnsStatusModel()
        {
            // Arrange
            ValidateDatasetModel viewModel = new ValidateDatasetModel();


            DatasetValidationStatusModel statusModel = new DatasetValidationStatusModel()
            {
                ValidationFailures = new Dictionary <string, IEnumerable <string> >(),
                CurrentOperation   = DatasetValidationStatusOperation.Processing,
                DatasetId          = "datasetId",
                ErrorMessage       = "errorMessage",
                OperationId        = "operationId",
            };

            ValidatedApiResponse <DatasetValidationStatusModel> response = new ValidatedApiResponse <DatasetValidationStatusModel>(HttpStatusCode.OK, statusModel);

            DatasetValidationStatusViewModel resultViewModel = new DatasetValidationStatusViewModel()
            {
                ValidationFailures = new Dictionary <string, IEnumerable <string> >(),
                CurrentOperation   = DatasetValidationStatusOperationViewModel.Processing,
                DatasetId          = "datasetId",
                ErrorMessage       = "errorMessage",
                OperationId        = "operationId",
            };

            IDatasetsApiClient apiClient = CreateApiClient();

            apiClient
            .ValidateDataset(Arg.Is(viewModel))
            .Returns(response);

            ILogger logger = CreateLogger();

            DatasetController controller = CreateController(apiClient, logger);

            // Act
            IActionResult result = await controller.ValidateDataset(viewModel);

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeEquivalentTo(resultViewModel);
        }