public TaxonomyValidatorTest() { // prep a validator with an existing title _validator = new TaxonomyValidator(new List <string> { "Technology" }); }
public TaxonomyValidatorTests() { var loggerMock = new Mock <ILogger <TaxonomyService> >(); var validationServiceMock = new Mock <IValidationService>(); var metadataServiceMock = new Mock <IMetadataService>(); var cacheServiceMock = new Mock <ICacheService>(); var mathematicalTaxonomy = new TaxonomySchemeBuilder().GenerateSampleMathematicalTaxonomyList().Build(); var informationClassificationTaxonomy = new TaxonomySchemeBuilder().GenerateSampleInformationClassificationTaxonomyList().Build(); var taxonomyRepoMock = new Mock <ITaxonomyRepository>(); taxonomyRepoMock.Setup(t => t.GetTaxonomies(Graph.Metadata.Constants.Resource.Type.MathematicalModelCategory)).Returns(mathematicalTaxonomy); taxonomyRepoMock.Setup(t => t.GetTaxonomies(Graph.Metadata.Constants.Resource.Type.InformationClassification)).Returns(informationClassificationTaxonomy); cacheServiceMock.Setup(t => t.GetOrAdd(It.IsAny <string>(), It.IsAny <Func <IList <Taxonomy> > >())).Returns( (string key, Func <IList <Taxonomy> > function) => function()); cacheServiceMock.Setup(t => t.GetOrAdd(It.IsAny <string>(), It.IsAny <Func <IList <TaxonomyResultDTO> > >())).Returns( (string key, Func <IList <TaxonomyResultDTO> > function) => function()); var pidUriTemplateServiceMock = new Mock <IPidUriTemplateService>(); pidUriTemplateServiceMock.Setup(t => t.GetFlatPidUriTemplateByPidUriTemplate(It.IsAny <Entity>())) .Returns(null as PidUriTemplateFlattened); pidUriTemplateServiceMock.Setup(t => t.GetFlatPidUriTemplateByPidUriTemplate(It.IsAny <Entity>())) .Returns(null as PidUriTemplateFlattened); pidUriTemplateServiceMock.Setup(t => t.FormatPidUriTemplateName(It.IsAny <PidUriTemplateFlattened>())) .Returns(string.Empty); var serviceCollection = new ServiceCollection(); serviceCollection.AddTransient <TaxonomyNameResolver>(t => new TaxonomyNameResolver(pidUriTemplateServiceMock.Object)); var serviceProvider = serviceCollection.BuildServiceProvider(); var config = new MapperConfiguration(cfg => { cfg.ConstructServicesUsing(serviceProvider.GetService); cfg.AddProfile(new TaxonomyProfile()); }); var mapper = config.CreateMapper(); var taxonomyService = new TaxonomyService(mapper, metadataServiceMock.Object, validationServiceMock.Object, taxonomyRepoMock.Object, loggerMock.Object, cacheServiceMock.Object); _validator = new TaxonomyValidator(taxonomyService); _metadata = new MetadataBuilder() .GenerateSampleHasInformationClassifikation() .GenerateSampleMathematicalCategory() .Build(); }
/// <summary> /// Prepares a category or tag for create or update, making sure its title and slug are valid. /// </summary> /// <param name="tax">A category or tag.</param> /// <param name="createOrUpdate"></param> /// <returns></returns> private async Task <ITaxonomy> PrepTaxonomyAsync(ITaxonomy tax, ECreateOrUpdate createOrUpdate) { // get existing titles and slugs List <string> existingTitles = null; List <string> existingSlugs = null; ETaxonomyType type = ETaxonomyType.Category; ITaxonomy origTax = tax; if (tax is Category cat) { if (cat.Id != 0) { origTax = await _catRepo.GetAsync(cat.Id); } var allCats = await GetCategoriesAsync(); existingTitles = allCats.Select(c => c.Title).ToList(); existingSlugs = allCats.Select(c => c.Slug).ToList(); } else { var tag = (Tag)tax; if (tag.Id != 0) { origTax = await _tagRepo.GetAsync(tag.Id); } var allTags = await GetTagsAsync(); existingTitles = allTags.Select(c => c.Title).ToList(); existingSlugs = allTags.Select(c => c.Slug).ToList(); type = ETaxonomyType.Tag; } // remove self if it is update if (createOrUpdate == ECreateOrUpdate.Update) { existingTitles.Remove(origTax.Title); existingSlugs.Remove(origTax.Slug); } // html encode title and description tax.Title = Util.CleanHtml(tax.Title); tax.Description = Util.CleanHtml(tax.Description); // validator var validator = new TaxonomyValidator(existingTitles); ValidationResult result = await validator.ValidateAsync(tax); if (!result.IsValid) { throw new FanException($"Failed to {createOrUpdate.ToString().ToLower()} {type}.", result.Errors); } // slug always updated according to title origTax.Slug = BlogUtil.FormatTaxonomySlug(tax.Title, existingSlugs); origTax.Title = tax.Title; origTax.Description = tax.Description; origTax.Count = tax.Count; _logger.LogDebug(createOrUpdate + " {@Taxonomy}", origTax); return(origTax); }