private bool IsValid(UDFMetadata metadata, out IEnumerable <UDFMetadataError> errorMessages) { var errors = new List <UDFMetadataError>(); if (string.IsNullOrWhiteSpace(metadata.Name) || metadata.Name.Equals("new", StringComparison.InvariantCultureIgnoreCase)) { errors.Add(new UDFMetadataError() { Field = UDFMetadataField.Name, Message = "Cannot be empty or 'new'" }); } else if (!this.fieldNamePattern.IsMatch(metadata.Name)) { errors.Add(new UDFMetadataError() { Field = UDFMetadataField.Name, Message = "Not allowed (must start with a letter and len >= 3)" }); } if (string.IsNullOrWhiteSpace(metadata.Display)) { errors.Add(new UDFMetadataError() { Field = UDFMetadataField.Display, Message = "Cannot be empty" }); } errorMessages = errors; return(errors.Count == 0); }
public bool AddFieldFor(string tenant, Type modelType, UDFMetadata metadata, out IEnumerable <UDFMetadataError> errorMessages) { if (modelType.GetInterface("IUDFModel") == null) { throw new InvalidOperationException("udfModelType should implement IUDFModel interface"); } if (!this.IsValid(metadata, out errorMessages)) { return(false); } var tenantData = this.tenantStore.GetTenant(tenant); if (tenantData.ExtensionDictionary == null) { tenantData.ExtensionDictionary = new Dictionary <string, List <UDFMetadata> >(); } List <UDFMetadata> udfs; if (!tenantData.ExtensionDictionary.TryGetValue(modelType.Name, out udfs)) { tenantData.ExtensionDictionary.Add(modelType.Name, new List <UDFMetadata>() { metadata }); } else { if (udfs.FirstOrDefault(i => i.Name.Equals(metadata.Name, StringComparison.InvariantCultureIgnoreCase)) != null) { errorMessages = new List <UDFMetadataError>() { new UDFMetadataError() { Field = UDFMetadataField.Name, Message = "* Field name already used in this model" } }; return(false); } else { udfs.Add(metadata); } } this.tenantStore.SaveTenant(tenantData); errorMessages = null; return(true); }
public override void CopyFrom(UDFMetadata item) { base.CopyFrom(item); if (item is UDFItem) { this.Value = (item as UDFItem).Value; } else { this.SetUDFValue(item.DefaultValue); } }
public void CopyFromReturnsUpdatedMetadataProperties() { // Setup var metadata = new UDFMetadata { Type = UDFType.String, Name = "name", Display = "display", DefaultValue = "default", Mandatory = true }; // Act var newMetadata = new UDFMetadata(); newMetadata.CopyFrom(metadata); // Assert Assert.AreEqual(metadata.Type, newMetadata.Type); Assert.AreEqual(metadata.Name, newMetadata.Name); Assert.AreEqual(metadata.Display, newMetadata.Display); Assert.AreEqual(metadata.Mandatory, newMetadata.Mandatory); }
public void AddFieldsForReturnsErrorWhenEmptyFieldNameIsAdded() { // Setup const string Tenant = "tenant"; var mockTenant = new Mock<Tenant>(); var metadata = new UDFMetadata { Name = string.Empty, Display = "custom" }; var mockTenantStore = new Mock<ITenantStore>(); mockTenantStore.Setup(m => m.GetTenant(Tenant)).Returns(mockTenant.Object); IEnumerable<UDFMetadataError> errors = new List<UDFMetadataError>(); var udfDictionary = new UDFDictionary(mockTenantStore.Object); // Act var result = udfDictionary.AddFieldFor<TestExtension>(Tenant, metadata, out errors); // Assert Assert.IsFalse(result); Assert.IsTrue(errors.Count() > 0); Assert.AreEqual(UDFMetadataField.Name, errors.ToList()[0].Field); Assert.AreEqual("Cannot be empty or 'new'", errors.ToList()[0].Message); }
public void AddFieldsForReturnsErrorWhenDuplicateFieldIsAdded() { // Setup const string Tenant = "tenant"; var mockTenant = new Mock<Tenant>(); var metadata = new UDFMetadata { Name = "custom", Display = "custom" }; var duplicateMetadata = new UDFMetadata { Name = "custom", Display = "custom" }; var mockTenantStore = new Mock<ITenantStore>(); mockTenantStore.Setup(m => m.GetTenant(Tenant)).Returns(mockTenant.Object); IEnumerable<UDFMetadataError> errors = new List<UDFMetadataError>(); var udfDictionary = new UDFDictionary(mockTenantStore.Object); udfDictionary.AddFieldFor<TestExtension>(Tenant, metadata, out errors); // Act var result = udfDictionary.AddFieldFor<TestExtension>(Tenant, duplicateMetadata, out errors); // Assert Assert.IsFalse(result); Assert.IsTrue(errors.Count() > 0); Assert.IsTrue(errors.ToList()[0].Field == UDFMetadataField.Name); }
public void RemoveFieldForRemovesCustomField() { // Setup const string Tenant = "tenant"; var mockTenant = new Tenant { Name = Tenant, SubscriptionKind = Shared.Models.SubscriptionKind.Premium }; var metadata = new UDFMetadata { Name = "custom", Display = "display" }; var mockTenantStore = new Mock<ITenantStore>(); mockTenantStore.Setup(m => m.GetTenant(Tenant)).Returns(mockTenant); var udfDictionary = new UDFDictionary(mockTenantStore.Object); IEnumerable<UDFMetadataError> errors = new List<UDFMetadataError>(); var result = udfDictionary.AddFieldFor<TestExtension>(Tenant, metadata, out errors); var f1 = udfDictionary.GetFieldsFor<TestExtension>(Tenant); // Act udfDictionary.RemoveFieldFor<TestExtension>(Tenant, "custom"); var fields = udfDictionary.GetFieldsFor<TestExtension>(Tenant); // Assert mockTenantStore.Verify(m => m.SaveTenant(mockTenant)); Assert.AreEqual(0, fields.Count()); }
public void GetFieldsForReturnsNullWhenEmptyList() { // Setup const string Tenant = "tenant"; var mockTenant = new Tenant { Name = Tenant, SubscriptionKind = Shared.Models.SubscriptionKind.Premium }; var metadata = new UDFMetadata { Name = "custom", Display = "display" }; var mockTenantStore = new Mock<ITenantStore>(); mockTenantStore.Setup(m => m.GetTenant(Tenant)).Returns(mockTenant); var udfDictionary = new UDFDictionary(mockTenantStore.Object); // Act var fields = udfDictionary.GetFieldsFor<TestExtension>(Tenant); // Assert Assert.IsNull(fields); }
public void AreValidForReturnsTrueWhenExtensionListMatchesModelList() { // Setup const string Tenant = "tenant"; var mockTenant = new Tenant { Name = Tenant, SubscriptionKind = Shared.Models.SubscriptionKind.Premium }; var list = new List<UDFItem> { new UDFItem { DefaultValue = "defaultvalue", Name = "name", Value = "value", Display = "display" } }; var testModel = new TestExtension(); var mockTenantStore = new Mock<ITenantStore>(); mockTenantStore.Setup(m => m.GetTenant(Tenant)).Returns(mockTenant); var metadata = new UDFMetadata { Name = "custom", Display = "display" }; var udfDictionary = new UDFDictionary(mockTenantStore.Object); IEnumerable<UDFMetadataError> errors = new List<UDFMetadataError>(); udfDictionary.AddFieldFor<TestExtension>(Tenant, metadata, out errors); // Act var result = udfDictionary.AreValidFor<TestExtension>(Tenant, list); // Assert Assert.IsTrue(result); }
public void AddFieldsForReturnsTrue() { // Setup const string Tenant = "tenant"; var mockTenant = new Mock<Tenant>(); var metadata = new UDFMetadata { Name = "custom", Display = "display" }; var mockTenantStore = new Mock<ITenantStore>(); mockTenantStore.Setup(m => m.GetTenant(Tenant)).Returns(mockTenant.Object); // Act var udfDictionary = new UDFDictionary(mockTenantStore.Object); IEnumerable<UDFMetadataError> errors = new List<UDFMetadataError>(); var result = udfDictionary.AddFieldFor<TestExtension>(Tenant, metadata, out errors); // Assert Assert.IsTrue(result); }
private void TestGetEdmType(string type, UDFType udfType) { // Setup var metadata = new UDFMetadata { Type = udfType }; // Act var result = metadata.GetEdmType(); // Assert Assert.AreEqual(type, result); }
public bool AddFieldFor <T>(string tenant, UDFMetadata metadata, out IEnumerable <UDFMetadataError> errorMessages) where T : IUDFModel { return(this.AddFieldFor(tenant, typeof(T), metadata, out errorMessages)); }
public UDFItem(UDFMetadata source) { this.CopyFrom(source); }
private void TestSetUdfType(string type) { // Setup var metadata = new UDFMetadata(); // Act metadata.SetUDFType(type); // Assert Assert.AreEqual(type, metadata.GetEdmType()); }
public ActionResult UdfNew(string tenant, string model, UDFMetadata metadata) { IEnumerable<UDFMetadataError> errorMessages; if (!this.udfDictionary.AddFieldFor(this.TenantName, this.extendableTypes[model], metadata, out errorMessages)) { foreach (var error in errorMessages) { switch (error.Field) { case UDFMetadataField.Name: this.ModelState.AddModelError( "ContentModel.Name", string.Format(CultureInfo.InvariantCulture, "* {0}", error.Message)); break; case UDFMetadataField.Display: this.ModelState.AddModelError( "ContentModel.Display", string.Format(CultureInfo.InvariantCulture, "* {0}", error.Message)); break; } } } if (!this.ModelState.IsValid) { var mvcModel = this.CreateTenantUdfPageViewData(model, metadata); mvcModel.Title = string.Format("New User Defined Field for {0} model : Error!", model); return this.View(mvcModel); } this.ViewBag.Model = model; return this.RedirectToAction("UdfIndex"); }