protected virtual void SetPropFromProperties <T>( T model, PropertyInfo prop, EntryAttributeDictionary entryProps ) where T : IEntry { var attr = prop.GetCustomAttribute <LdapFieldAttribute>(); if (attr == null) { return; } var ldapAttrName = attr?.Name ?? prop.Name; if (!prop.CanWrite) { throw new ArgumentException( $"Column attribute '{ldapAttrName}' applied to property '{prop.Name}' on type '{typeof(T).FullName}', " + $"but {prop.Name} is not writable."); } try { var val = GetAttributeRawValue <T>(ldapAttrName, prop, entryProps, attr); ValidateTypeConvertAndSet(model, val, prop, ldapAttrName); } catch (Exception ex) { throw new ArgumentException( $"Error converting LDAP attribute '{ldapAttrName}' for property '{prop.Name}' on type '{typeof(T).FullName}'." , ex); } }
/// <summary> /// Creates a new instance of an LDAP Entry model and populates its fields. /// </summary> /// <param name="entryProps">A proxy dictionary of the LDAP entry's fields.</param> /// <param name="dn">DistinguishedName field of this entry.</param> /// <typeparam name="T">The destination IEntry model type.</typeparam> /// <returns>A populated instance of T.</returns> public T Create <T>(EntryAttributeDictionary entryProps, string dn = null) where T : IEntry, new() => Create(new T() { DistinguishedName = dn, Attributes = entryProps }, entryProps);
public void Constructs() { var props = new EntryAttributeDictionary(new Dictionary <string, Core.Proxies.AttributeValueList>()); var path = "something"; var p = new SearchResultProxy(null, props, path); Assert.Equal(path, p.Path); Assert.Equal(props, p.Properties); }
public void Create_ConversionError_InformsOfAttribute() { var props = new Dictionary <string, AttributeValueList>(); var properties = new EntryAttributeDictionary(props); var ex = Assert.Throws <ArgumentException>(() => ModelCreator.Create <MyModel>(properties, "bogus path")); Assert.Contains("mail", ex.Message); Assert.NotNull(ex.InnerException); }
public void Contains_ReturnsMockResults() { var m = new Dictionary <string, Core.Proxies.AttributeValueList>() { { "a key", new Core.Proxies.AttributeValueList(new List <object>()) } }; var p = new EntryAttributeDictionary(m); Assert.True(p.ContainsKey("a key")); Assert.False(p.ContainsKey("another key")); }
public void Count_ReturnsMockResults() { var m = new Dictionary <string, Core.Proxies.AttributeValueList>() { { "a key", new Core.Proxies.AttributeValueList(new List <object>()) }, { "2 key", new Core.Proxies.AttributeValueList(new List <object>()) }, { "3 key", new Core.Proxies.AttributeValueList(new List <object>()) } }; var p = new EntryAttributeDictionary(m); Assert.Equal(3, p.Count); }
public void Create_EmptyManyProp_Optional_YieldsEmptyField() { var path = "test path"; var properties = new EntryAttributeDictionary( new Dictionary <string, Core.Proxies.AttributeValueList>() { { "alt-mails", new string[] { } } } ); var b = Creator.Create <MyTestModel>(properties, path); Assert.Empty(b.AltMails); }
public void Create_EmptyProp_Optional_YieldsNullField() { var path = "test path"; var properties = new EntryAttributeDictionary( new Dictionary <string, Core.Proxies.AttributeValueList>() { { "mail", new string[] { } }, } ); var b = Creator.Create <MyTestModel>(properties, path); Assert.Null(b.Mail); }
public void Create_NullManyProp_Optional_YieldsNullField() { var path = "test path"; var properties = new EntryAttributeDictionary( new Dictionary <string, Core.Proxies.AttributeValueList>() { { "mail", new string[] { "*****@*****.**" } }, // { "alt-mails", new string[] { "*****@*****.**", "*****@*****.**", "*****@*****.**" } } } ); var b = Creator.Create <MyTestModel>(properties, path); Assert.Null(b.AltMails); }
public void Create_CanConvertToSimpleStringFromBytes() { var path = "test path"; var teststr = "*****@*****.**"; var properties = new EntryAttributeDictionary( new Dictionary <string, AttributeValueList>() { { "mail", new AttributeValueList(new object[] { Encoding.UTF8.GetBytes(teststr) }) } } ); var b = Creator.Create <MyTestModel>(properties, path); Assert.Equal(teststr, b.Mail); }
public void Create_CanConvertCustomFieldTypes() { var props = new Dictionary <string, AttributeValueList>() { { "mail", new AttributeValueList("*****@*****.**") }, { "alt-mails", new AttributeValueList("*****@*****.**", "*****@*****.**") }, { "number", new AttributeValueList(123) } }; var properties = new EntryAttributeDictionary(props); var result = ModelCreator.Create <MyModel>(properties, "bogus path"); Assert.Equal(props["mail"][0] as string, result.Mail); Assert.Equal(result.Mail2, result.Mail); Assert.Equal(result.Number2, (int)result.Number); }
/// <summary> /// Given an IEntry class, T, populate its LdapField properties from the given /// collection of directory entry attributes. /// </summary> /// <typeparam name="T">A type implementing IEntry.</typeparam> /// <param name="model">An object whose type implements IEntry.</param> /// <param name="entryProps">A collection of directory entry properties and values.</param> /// <returns></returns> public virtual T Create <T>( T model, EntryAttributeDictionary entryProps ) where T : IEntry { var t = typeof(T); var props = t.GetProperties(); foreach (var prop in props) { SetPropFromProperties(model, prop, entryProps); } return(model); }
protected internal AttributeValueList GetAttributeRawValue <T>( string ldapAttrName, PropertyInfo prop, EntryAttributeDictionary entryProps, LdapFieldAttribute attr) { if (!(entryProps.ContainsKey(ldapAttrName) || attr.Optional)) { throw new ArgumentException( $"LDAP attribute '{attr.Name}' for property '{prop.Name}' on type '{typeof(T).FullName}'" + " is not marked as optional, but was not found in the store."); } if (entryProps.TryGetValue(ldapAttrName, out var val)) { return(val); } return(null); }
public void Create_ResultProperties_To_Model() { var path = "test path"; var properties = new EntryAttributeDictionary( new Dictionary <string, Core.Proxies.AttributeValueList>() { { "dn", new string[] { "ou=some, ou=dn" } }, { "cn", new string[] { "example" } }, { "objectclass", new string[] { "testuser" } }, { "objectsid", new Core.Proxies.AttributeValueList(new List <object> { new byte[] { 0x31, 0x41 } }) }, { "userprincipalname", new string[] { "testuser" } }, { "samaccountname", new string[] { "testuser" } }, { "mail", new string[] { "*****@*****.**" } }, { "alt-mails", new string[] { "*****@*****.**", "*****@*****.**", "*****@*****.**" } } } ); var b = Creator.Create <MyTestModel>(properties, path); Assert.Equal(properties, b.Attributes); Assert.Equal(path, b.DistinguishedName); }
public SearchResultProxy(SearchResult result, EntryAttributeDictionary properties, string path) { Result = result; Properties = properties; Path = path; }