public async Task SaveDataset_GivenViewModelIsNotNullButResponseContainsModelErrors_ReturnsBadRequest()
        {
            // Arrange
            ValidatedApiResponse <NewDatasetVersionResponseModel> response = new ValidatedApiResponse <NewDatasetVersionResponseModel>(HttpStatusCode.BadRequest);

            response.ModelState = new Dictionary <string, IEnumerable <string> >
            {
                { "Name", new [] { "Invalid name" } }
            };

            CreateDatasetViewModel viewModel = new CreateDatasetViewModel();

            IDatasetsApiClient apiClient = CreateApiClient();

            apiClient
            .CreateDataset(Arg.Any <CreateNewDatasetModel>())
            .Returns(response);

            ILogger logger = CreateLogger();

            DatasetController controller = CreateController(apiClient, logger);

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

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

            result
            .Should()
            .BeOfType <BadRequestObjectResult>();
        }
        public async Task ValidateDataset_GivenViewModelButResponseIsBadRequest_ReturnsStatusCode400()
        {
            // Arrange
            ValidateDatasetModel viewModel = new ValidateDatasetModel();

            ValidatedApiResponse <DatasetValidationStatusModel> response = new ValidatedApiResponse <DatasetValidationStatusModel>(HttpStatusCode.BadRequest);

            response.ModelState = new Dictionary <string, IEnumerable <string> >();

            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 <InternalServerErrorResult>();

            logger
            .Received(1)
            .Warning(Arg.Is("Failed to validate dataset with status code: {statusCode}"), Arg.Is(HttpStatusCode.BadRequest));
        }
        public void SaveDataset_GivenViewModelIsNull_ThowsArgumentNullException()
        {
            // Arrange
            CreateDatasetViewModel viewModel = null;

            DatasetController controller = CreateController();

            // Act
            Func <Task> test = async() => await controller.SaveDataset(viewModel);

            // Assert
            test
            .Should()
            .ThrowExactly <ArgumentNullException>();
        }
        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);
        }
        public async Task SaveDataset_GivenResponseIsSuccess_ReturnsSuccess()
        {
            // Arrange
            NewDatasetVersionResponseModel responseModel = new NewDatasetVersionResponseModel
            {
                DatasetId = "dataset-id"
            };

            ValidatedApiResponse <NewDatasetVersionResponseModel> response = new ValidatedApiResponse <NewDatasetVersionResponseModel>(HttpStatusCode.OK, responseModel);

            CreateDatasetViewModel viewModel = new CreateDatasetViewModel();

            IDatasetsApiClient apiClient = CreateApiClient();

            apiClient
            .CreateDataset(Arg.Any <CreateNewDatasetModel>())
            .Returns(response);

            ILogger logger = CreateLogger();

            DatasetController controller = CreateController(apiClient, logger);

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

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            OkObjectResult objectResult = result as OkObjectResult;

            NewDatasetVersionResponseModel content = objectResult.Value as NewDatasetVersionResponseModel;

            content
            .DatasetId
            .Should()
            .Be("dataset-id");
        }
        public async Task SaveDataset_GivenResponseIsInternalServerError_ReturnsStatusCode500()
        {
            // Arrange
            ValidatedApiResponse <NewDatasetVersionResponseModel> response = new ValidatedApiResponse <NewDatasetVersionResponseModel>(HttpStatusCode.InternalServerError);

            CreateDatasetViewModel viewModel = new CreateDatasetViewModel();

            IDatasetsApiClient apiClient = CreateApiClient();

            apiClient
            .CreateDataset(Arg.Any <CreateNewDatasetModel>())
            .Returns(response);

            ILogger logger = CreateLogger();

            DatasetController controller = CreateController(apiClient, logger);

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

            // Assert
            logger
            .Received(1)
            .Error(Arg.Is($"Error when posting data set with status code: 500"));

            result
            .Should()
            .BeOfType <StatusCodeResult>();

            StatusCodeResult statusCodeResult = result as StatusCodeResult;

            statusCodeResult
            .StatusCode
            .Should()
            .Be(500);
        }