public void Validate_GivenNameAlreadyExists_ValidIsFalse() { //Arrange CreateNewDatasetModel model = CreateModel(); IEnumerable <Dataset> datasets = new[] { new Dataset() }; IDatasetRepository repository = CreateDatasetsRepository(true, true); repository .GetDatasetsByQuery(Arg.Any <Expression <Func <DocumentEntity <Dataset>, bool> > >()) .Returns(datasets); CreateNewDatasetModelValidator validator = CreateValidator(repository); //Act ValidationResult result = validator.Validate(model); //Assert result .IsValid .Should() .BeFalse(); result .Errors .Count .Should() .Be(1); }
public async Task <IActionResult> GetDataSourcesByRelationshipId(string relationshipId) { if (string.IsNullOrWhiteSpace(relationshipId)) { _logger.Error("The relationshipId id was not provided to GetDataSourcesByRelationshipId"); return(new BadRequestObjectResult("No relationship id was provided")); } SelectDatasourceModel selectDatasourceModel = new SelectDatasourceModel(); DefinitionSpecificationRelationship relationship = await _datasetRepository.GetDefinitionSpecificationRelationshipById(relationshipId); if (relationship == null) { return(new StatusCodeResult(412)); } selectDatasourceModel.RelationshipId = relationship.Id; selectDatasourceModel.RelationshipName = relationship.Name; selectDatasourceModel.SpecificationId = relationship.Specification.Id; selectDatasourceModel.SpecificationName = relationship.Specification.Name; selectDatasourceModel.DefinitionId = relationship.DatasetDefinition.Id; selectDatasourceModel.DefinitionName = relationship.DatasetDefinition.Name; IEnumerable <Dataset> datasets = await _datasetRepository.GetDatasetsByQuery(m => m.Content.Definition.Id == relationship.DatasetDefinition.Id); if (!datasets.IsNullOrEmpty()) { if (selectDatasourceModel.Datasets == null) { selectDatasourceModel.Datasets = Enumerable.Empty <DatasetVersions>(); } foreach (Dataset dataset in datasets) { selectDatasourceModel.Datasets = selectDatasourceModel.Datasets.Concat(new[] { new DatasetVersions { Id = dataset.Id, Name = dataset.Name, Description = dataset.Description, SelectedVersion = (relationship.DatasetVersion != null && relationship.DatasetVersion.Id == dataset.Id) ? relationship.DatasetVersion.Version : null as int?, Versions = dataset.History.Select(m => new DatasetVersionModel { Id = m.Id, Version = m.Version, Date = m.Date, Author = m.Author, Comment = m.Comment }) } }); } } return(new OkObjectResult(selectDatasourceModel)); }
static IDatasetRepository CreateDatasetsRepository(bool hasDataset = false) { IDatasetRepository repository = Substitute.For <IDatasetRepository>(); repository .GetDatasetsByQuery(Arg.Any <Expression <Func <Dataset, bool> > >()) .Returns(hasDataset ? new[] { new Dataset() } : new Dataset[0]); return(repository); }
public async Task <IActionResult> GetDataSourcesByRelationshipId(HttpRequest request) { request.Query.TryGetValue("relationshipId", out Microsoft.Extensions.Primitives.StringValues relId); string relationshipId = relId.FirstOrDefault(); if (string.IsNullOrWhiteSpace(relationshipId)) { _logger.Error("The relationshipId id was not provided to GetDataSourcesByRelationshipId"); return(new BadRequestObjectResult("No relationship id was provided")); } SelectDatasourceModel selectDatasourceModel = new SelectDatasourceModel(); DefinitionSpecificationRelationship relationship = await _datasetRepository.GetDefinitionSpecificationRelationshipById(relationshipId); if (relationship == null) { return(new StatusCodeResult(412)); } selectDatasourceModel.RelationshipId = relationship.Id; selectDatasourceModel.RelationshipName = relationship.Name; selectDatasourceModel.SpecificationId = relationship.Specification.Id; selectDatasourceModel.SpecificationName = relationship.Specification.Name; selectDatasourceModel.DefinitionId = relationship.DatasetDefinition.Id; selectDatasourceModel.DefinitionName = relationship.DatasetDefinition.Name; IEnumerable <Dataset> datasets = await _datasetRepository.GetDatasetsByQuery(m => m.Definition.Id == relationship.DatasetDefinition.Id); if (!datasets.IsNullOrEmpty()) { if (selectDatasourceModel.Datasets == null) { selectDatasourceModel.Datasets = Enumerable.Empty <DatasetVersions>(); } foreach (Dataset dataset in datasets) { selectDatasourceModel.Datasets = selectDatasourceModel.Datasets.Concat(new[] { new DatasetVersions { Id = dataset.Id, Name = dataset.Name, SelectedVersion = (relationship.DatasetVersion != null && relationship.DatasetVersion.Id == dataset.Id) ? relationship.DatasetVersion.Version : null as int?, Versions = dataset.History.Select(m => m.Version) } }); } } return(new OkObjectResult(selectDatasourceModel)); }
public async Task RegenerateProviderSourceDatasets_GivenSpecificationId_ThenCallJobServiceToProcess() { // Arrange IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(SpecificationId) }, }); HttpRequest request = Substitute.For <HttpRequest>(); request.Query.Returns(queryStringValues); IDatasetRepository datasetRepository = CreateDatasetsRepository(); datasetRepository .GetDefinitionSpecificationRelationshipsByQuery(Arg.Any <Expression <Func <DefinitionSpecificationRelationship, bool> > >()) .Returns(new List <DefinitionSpecificationRelationship> { new DefinitionSpecificationRelationship { DatasetVersion = new DatasetRelationshipVersion { Id = "DSRV1", Version = 1 }, Specification = new Common.Models.Reference { Id = SpecificationId, Name = "SpecAbc" } } }); datasetRepository .GetDatasetsByQuery(Arg.Any <Expression <Func <Dataset, bool> > >()) .Returns(new List <Dataset> { new Dataset { Id = "DS1" } }); IJobsApiClient jobsApiClient = CreateJobsApiClient(); DatasetService service = CreateDatasetService(datasetRepository: datasetRepository, jobsApiClient: jobsApiClient); // Act await service.RegenerateProviderSourceDatasets(request); // Assert await jobsApiClient .Received(1) .CreateJob(Arg.Is <JobCreateModel>(j => j.JobDefinitionId == "MapDatasetJob" && j.Properties.ContainsKey("session-id") && j.Properties["session-id"] == SpecificationId)); }
public CreateNewDatasetModelValidator(IDatasetRepository datasetsRepository) { _datasetsRepository = datasetsRepository; RuleFor(model => model.DefinitionId) .NotEmpty() .WithMessage("You must provide a valid dataset schema"); RuleFor(model => model.Description) .NotEmpty() .WithMessage("You must give a description for the dataset"); RuleFor(model => model.Filename) .Custom((name, context) => { CreateNewDatasetModel model = context.ParentContext.InstanceToValidate as CreateNewDatasetModel; if (string.IsNullOrWhiteSpace(model.Filename)) { context.AddFailure("You must provide a filename"); } else if (!validExtensions.Contains(Path.GetExtension(model.Filename.ToLower()))) { context.AddFailure("Check you have the right file format"); } }); RuleFor(model => model.Name) .Custom((name, context) => { CreateNewDatasetModel model = context.ParentContext.InstanceToValidate as CreateNewDatasetModel; if (string.IsNullOrWhiteSpace(model.Name)) { context.AddFailure("Use a descriptive unique name other users can understand"); } else { IEnumerable <Dataset> datasets = _datasetsRepository.GetDatasetsByQuery(m => m.Name.ToLower() == model.Name.ToLower()).Result; if (datasets != null && datasets.Any()) { context.AddFailure("Use a descriptive unique name other users can understand"); } } }); }
static IDatasetRepository CreateDatasetsRepository(bool hasDataset = false, bool hasDatasetDefinitionForFundingStream = false) { IDatasetRepository repository = Substitute.For <IDatasetRepository>(); repository .GetDatasetsByQuery(Arg.Any <Expression <Func <DocumentEntity <Dataset>, bool> > >()) .Returns(hasDataset ? new[] { new Dataset() } : new Dataset[0]); repository .GetDatasetDefinitionsByFundingStreamId(Arg.Is(FundingStreamId)) .Returns(hasDatasetDefinitionForFundingStream ? new[] { new Models.Datasets.Schema.DatasetDefinationByFundingStream() { Id = DefinitionId } } : Enumerable.Empty <Models.Datasets.Schema.DatasetDefinationByFundingStream>()); return(repository); }
public async Task RegenerateProviderSourceDatasets_GivenSpecificationId_ThenCallJobServiceToProcess() { // Arrange IDatasetRepository datasetRepository = CreateDatasetsRepository(); datasetRepository .GetDefinitionSpecificationRelationshipsByQuery(Arg.Any <Expression <Func <DocumentEntity <DefinitionSpecificationRelationship>, bool> > >()) .Returns(new List <DefinitionSpecificationRelationship> { new DefinitionSpecificationRelationship { DatasetVersion = new DatasetRelationshipVersion { Id = "DSRV1", Version = 1 }, Specification = new Common.Models.Reference { Id = SpecificationId, Name = "SpecAbc" } } }); datasetRepository .GetDatasetsByQuery(Arg.Any <Expression <Func <DocumentEntity <Dataset>, bool> > >()) .Returns(new List <Dataset> { new Dataset { Id = "DS1" } }); IJobManagement jobManagement = CreateJobManagement(); DatasetService service = CreateDatasetService(datasetRepository: datasetRepository, jobManagement: jobManagement); // Act await service.RegenerateProviderSourceDatasets(SpecificationId, null, null); // Assert await jobManagement .Received(1) .QueueJob(Arg.Is <JobCreateModel>(j => j.JobDefinitionId == "MapDatasetJob" && j.Properties.ContainsKey("session-id") && j.Properties["session-id"] == SpecificationId)); }
public CreateNewDatasetModelValidator( IDatasetRepository datasetsRepository, IPolicyRepository policyRepository) { _datasetsRepository = datasetsRepository; _policyRepository = policyRepository; RuleFor(model => model.Description) .NotEmpty() .WithMessage("You must give a description for the dataset"); RuleFor(model => model.Filename) .Custom((name, context) => { CreateNewDatasetModel model = context.ParentContext.InstanceToValidate as CreateNewDatasetModel; if (string.IsNullOrWhiteSpace(model.Filename)) { context.AddFailure("You must provide a filename"); } else if (!validExtensions.Contains(Path.GetExtension(model.Filename.ToLower()))) { context.AddFailure("Check you have the right file format"); } }); RuleFor(model => model.Name) .Custom((name, context) => { CreateNewDatasetModel model = context.ParentContext.InstanceToValidate as CreateNewDatasetModel; if (string.IsNullOrWhiteSpace(model.Name)) { context.AddFailure("Use a descriptive unique name other users can understand"); } else { IEnumerable <Dataset> datasets = _datasetsRepository.GetDatasetsByQuery(m => m.Content.Name.ToLower() == model.Name.ToLower()).Result; if (datasets != null && datasets.Any()) { context.AddFailure("Use a descriptive unique name other users can understand"); } } }); RuleFor(model => model.FundingStreamId) .Custom((name, context) => { CreateNewDatasetModel model = context.ParentContext.InstanceToValidate as CreateNewDatasetModel; if (string.IsNullOrWhiteSpace(model.FundingStreamId)) { context.AddFailure("You must give a Funding Stream Id for the dataset"); } else { IEnumerable <PoliciesApiModels.FundingStream> fundingStreams = _policyRepository.GetFundingStreams().Result; if (fundingStreams != null && !fundingStreams.Any(_ => _.Id == model.FundingStreamId)) { context.AddFailure($"Unable to find given funding stream ID: {model.FundingStreamId}"); } } }); RuleFor(model => model.DefinitionId) .Custom((name, context) => { CreateNewDatasetModel model = context.ParentContext.InstanceToValidate as CreateNewDatasetModel; if (string.IsNullOrWhiteSpace(model.DefinitionId)) { context.AddFailure("You must provide a valid dataset schema"); } else if (!string.IsNullOrWhiteSpace(model.FundingStreamId)) { IEnumerable <DatasetDefinationByFundingStream> datasetDefinitions = _datasetsRepository.GetDatasetDefinitionsByFundingStreamId(model.FundingStreamId).Result; if (datasetDefinitions?.Any(d => d.Id == model.DefinitionId) == false) { context.AddFailure($"Dataset definition not found for funding stream ID: {model.FundingStreamId}"); } } }); }