public override Object Clone() { NodeProperty clone = (NodeProperty)base.Clone(); //Note: We purposely don't clone types, so we can always do identity checking return(clone); }
public KeyExpression(ResourceContainer container, ResourceType resourceType, IDictionary <string, object> properties) : base(null) { // populate key and ETag from properties List <NodeProperty> keyProperties = resourceType.Key.Properties.ToList(); _properties = new PropertyExpression[keyProperties.Count]; _values = new ConstantExpression[keyProperties.Count]; _include = new bool[keyProperties.Count]; _resourceType = resourceType; _resourceContainer = container; // originally, this would not happen if using constructor without type for (int i = 0; i < keyProperties.Count; i++) { NodeProperty property = keyProperties[i]; _properties[i] = new PropertyExpression(property); _values[i] = new ConstantExpression(new NodeValue(properties[property.Name], property.Type)); _include[i] = true; } if (resourceType.Properties.Any(p => p.Facets.ConcurrencyModeFixed)) { ETag = ConcurrencyUtil.ConstructETag(container, properties); } else { ETag = null; } this.KnownPropertyValues = properties; }
public static ResourceInstanceKey ConstructResourceInstanceKey(KeyExpression keyExpression) { List <ResourceInstanceSimpleProperty> keyProperties = new List <ResourceInstanceSimpleProperty>(); for (int i = 0; i < keyExpression.Properties.Count(); i++) { NodeProperty p = keyExpression.Properties.ElementAt(i); NodeValue nodeVal = keyExpression.Values.ElementAt(i); keyProperties.Add(new ResourceInstanceSimpleProperty(p.Name, nodeVal.ClrValue)); } ResourceInstanceKey instanceKey = new ResourceInstanceKey(keyExpression.ResourceContainer, keyExpression.ResourceType, keyProperties.ToArray()); return(instanceKey); }
public static KeyExpression GetContainingKey(this ContainmentAttribute att, KeyExpression childKey, ResourceType parentType, bool abbreviate) { AstoriaTestLog.Compare(childKey.ResourceContainer == att.ChildContainer, String.Format("ChildKey does not belong to expected set (Expected '{0}', got '{1}'", att.ChildContainer.Name, childKey.ResourceContainer.Name)); List <PropertyExpression> parentProperties = new List <PropertyExpression>(); List <ConstantExpression> parentValues = new List <ConstantExpression>(); foreach (NodeProperty p_prop in att.ParentContainer.BaseType.Key.Properties) { string c_name; if (!att.KeyMapping.TryGetValue(p_prop.Name, out c_name)) { AstoriaTestLog.FailAndThrow(String.Format("Parent key property {0} does not appear in derived key mapping", p_prop.Name)); } // need to get the offset now int c_offset = 0; for (; c_offset < childKey.Properties.Length; c_offset++) { if (childKey.Properties[c_offset].Name == c_name) { break; } } if (c_offset >= childKey.Properties.Length) { AstoriaTestLog.FailAndThrow(String.Format("Could not find property '{0}' in child key", c_name)); } NodeProperty c_prop = childKey.Properties[c_offset]; parentProperties.Add(p_prop.Property()); parentValues.Add(new ConstantExpression(childKey.Values[c_offset])); if (abbreviate) { childKey.IncludeInUri[c_offset] = false; } } return(new KeyExpression(att.ParentContainer, parentType, parentProperties.ToArray(), parentValues.ToArray())); }
protected void ComparePropertyValues(NodeType type, PayloadProperty first, PayloadProperty second, bool checkValue) { if (type is ComplexType) { if (first.IsNull) { if (!second.IsNull) { AstoriaTestLog.FailAndThrow("Second property is unexpectedly non-null"); } return; } else if (second.IsNull) { AstoriaTestLog.FailAndThrow("Second property is unexpectedly null"); } PayloadComplexProperty firstComplex = first as PayloadComplexProperty; PayloadComplexProperty secondComplex = second as PayloadComplexProperty; if (firstComplex == null) { AstoriaTestLog.FailAndThrow("First property was not a complex property"); } if (secondComplex == null) { AstoriaTestLog.FailAndThrow("Second property was not a complex property"); } foreach (string propertyName in firstComplex.PayloadProperties.Keys.Union(secondComplex.PayloadProperties.Keys)) { // TODO: verify typing if (propertyName == "__metadata") { continue; } PayloadProperty firstProperty; bool firstHadProperty = firstComplex.PayloadProperties.TryGetValue(propertyName, out firstProperty); PayloadProperty secondProperty; bool secondHadProperty = secondComplex.PayloadProperties.TryGetValue(propertyName, out secondProperty); // so that we can ignore this case later on, check it now if (!firstHadProperty && !secondHadProperty) { AstoriaTestLog.FailAndThrow("Property list contained property '" + propertyName + "' despite neither complex property containing it"); } // since a complex type is never open, there shouldnt be any unexpected properties NodeProperty property = (type as ComplexType).Properties.SingleOrDefault(p => p.Name == propertyName); if (property == null) { if (firstHadProperty && secondHadProperty) { AstoriaTestLog.FailAndThrow("Both complex properties contained sub-property '" + propertyName + "' which is not defined on type '" + type.Name + "'"); } else if (firstHadProperty) { AstoriaTestLog.FailAndThrow("First complex property contained sub-property '" + propertyName + "' which is not defined on type '" + type.Name + "'"); } else if (secondHadProperty) { AstoriaTestLog.FailAndThrow("Second complex property contained sub-property '" + propertyName + "' which is not defined on type '" + type.Name + "'"); } } if (!firstHadProperty) { AstoriaTestLog.FailAndThrow("First complex property property missing sub-property '" + propertyName + "'"); } else if (!secondHadProperty) { AstoriaTestLog.FailAndThrow("Second complex property property missing sub-property '" + propertyName + "'"); } ComparePropertyValues(property, firstProperty, secondProperty, checkValue && !property.Facets.ServerGenerated); } } else { PayloadSimpleProperty firstSimple = first as PayloadSimpleProperty; PayloadSimpleProperty secondSimple = second as PayloadSimpleProperty; if (firstSimple == null) { AstoriaTestLog.FailAndThrow("First property was not a simple property"); } if (secondSimple == null) { AstoriaTestLog.FailAndThrow("Second property was not a simple property"); } object expectedObject = CommonPayload.DeserializeStringToObject(firstSimple.Value, type.ClrType, false, first.ParentObject.Format); if (checkValue) { CommonPayload.ComparePrimitiveValuesObjectAndString(expectedObject, type.ClrType, secondSimple.Value, false, second.ParentObject.Format, true); } else { CommonPayload.DeserializeStringToObject(secondSimple.Value, type.ClrType, false, second.ParentObject.Format); } } }
protected void ComparePropertyValues(NodeProperty property, PayloadProperty first, PayloadProperty second, bool checkValue) { // in case we need to do any facet-based logic ComparePropertyValues(property.Type, first, second, checkValue); }
public NullablePropertyExpression(NodeProperty property, bool valueOnly) : base(property, valueOnly) { }
private IEnumerable<ResourceAttribute> GetAttributesFromProperty(NodeProperty property) { List<ResourceAttribute> atts = property.Facets.Attributes.ToList(); if (property is ResourceProperty && (property as ResourceProperty).IsComplexType) atts.AddRange((property.Type as ComplexType).Properties.SelectMany<NodeProperty, ResourceAttribute>(GetAttributesFromProperty)); return atts; }
public static void CompareMetadata(IEdmModel expectedItems, IEdmModel actualItems, bool isReflectionBasedProvider, string defaultEntityContainerName, ServiceContainer serviceContainer) { // // Verify the metadata version is correct, the number of entity containers is 1, the entity // container names match, and the number of entity types are the same // AstoriaTestLog.AreEqual(1.1, actualItems.GetEdmVersion(), "The metadata version was not correct"); AstoriaTestLog.AreEqual(serviceContainer.Workspace.ContextTypeName, actualItems.EntityContainer.Name, "Entity Container names do not match"); if (!isReflectionBasedProvider) { AstoriaTestLog.AreEqual(expectedItems.SchemaElements.OfType <IEdmEntityType>().Count(), actualItems.SchemaElements.OfType <IEdmEntityType>().Count(), "Entity Type Counts do not match"); } foreach (IEdmOperationImport metadataExposedFunction in actualItems.EntityContainer.OperationImports()) { AstoriaTestLog.TraceInfo("--> " + metadataExposedFunction.Name); } // // For each entity type exposed through the metadata endpoint, verify the following // 1. Verify it has an equivalent definition in the resource container // 2. Verify the entity type name is correct // 3. Verify that no navigation property is exposed in the entity type // 4. Verify that the property count in the entity type // IEnumerable <ComplexType> rtTypeCollection = serviceContainer.AllTypes.Where(a => a.GetType().Name.Equals("ResourceType")); AstoriaTestLog.TraceInfo(rtTypeCollection.Count().ToString()); foreach (IEdmEntityType metadataExposedEntityType in actualItems.SchemaElements.OfType <IEdmEntityType>()) { ResourceType resourceContainerEntityType = serviceContainer.AllTypes.Where(b => b.GetType().Name.Equals("ResourceType") & (b.FullName.Equals(metadataExposedEntityType.FullName()))).FirstOrDefault() as ResourceType; if (resourceContainerEntityType == null) { continue; } AstoriaTestLog.IsNotNull(resourceContainerEntityType, "Did not find entity type in resource container"); AstoriaTestLog.AreEqual(resourceContainerEntityType.FullName, metadataExposedEntityType.FullName(), "Entity Type name mismatch"); // // Verify the name, type, and nullable attribute values // ResourceContainer rc = serviceContainer.ResourceContainers.Where(a => a.BaseType.Name.Equals(metadataExposedEntityType.Name)).FirstOrDefault(); if (rc == null) { continue; } int navCount = rc.BaseType.Properties.Cast <NodeProperty>().Cast <ResourceProperty>().Where(a => a.IsNavigation).Count(); AstoriaTestLog.TraceInfo(rc.Name); AstoriaTestLog.TraceInfo(navCount.ToString()); AstoriaTestLog.AreEqual(resourceContainerEntityType.Properties.Count - navCount, metadataExposedEntityType.Properties().Count(), "Edm Property count mismatch"); foreach (IEdmProperty metadataExposedProperty in metadataExposedEntityType.Properties()) { string errorStringEntityTypeProperty = metadataExposedEntityType.FullName() + " : " + metadataExposedProperty.Name; NodeProperty resourceContainerProperty = resourceContainerEntityType.Properties.Where(a => a.Name.Equals(metadataExposedProperty.Name)).FirstOrDefault(); AstoriaTestLog.IsNotNull(resourceContainerProperty, "Did not find property -->" + errorStringEntityTypeProperty + "<-- in resource container"); if (resourceContainerProperty == null) { continue; } if (metadataExposedProperty.Type.IsBinary()) { if (AstoriaTestProperties.DataLayerProviderKinds.Contains(DataLayerProviderKind.LinqToSql)) { AstoriaTestLog.AreEqual("System.Data.Linq.Binary", resourceContainerProperty.Type.ClrType.FullName, errorStringEntityTypeProperty + " type is incorrect"); } else { AstoriaTestLog.AreEqual("System.Byte[]", resourceContainerProperty.Type.ClrType.FullName, errorStringEntityTypeProperty + " type is incorrect"); } } else { AstoriaTestLog.AreEqual(metadataExposedProperty.Type.FullName(), resourceContainerProperty.Type.FullName, errorStringEntityTypeProperty + " type is incorrect"); } } } }
//Constructor public PropertyExpression(NodeProperty property, bool valueOnly) : base(property.Name) { _property = property; _valueOnly = valueOnly; }
//Constructor public PropertyExpression(NodeProperty property) : base(property.Name) { _property = property; }
public static string ConstructEquivalentETag(ResourceType type, string ETag) { string[] pieces = SplitETag(ETag); List <NodeProperty> etagProperties = type.Properties.Where(p => p.Facets.ConcurrencyModeFixed).ToList(); for (int i = 0; i < pieces.Length; i++) { string piece = pieces[i]; if (piece == Null) { continue; } NodeProperty property = etagProperties[i]; NodeType propertyType = etagProperties[i].Type; if (propertyType == Clr.Types.String && property.Facets.FixedLength) { piece = piece.Trim('\''); if (piece.Length < property.Facets.MaxSize) { piece = piece + " "; } else if (piece.EndsWith(" ")) { piece = piece.Remove(piece.Length - 1); } piece = "'" + piece + "'"; } else if (propertyType.IsNumeric) { if (piece.Contains("INF") || piece.Contains("NaN")) { continue; } else if (piece.ToLower().Contains("e")) //must be a floating point { // add 0's and flip capitalization piece = piece.Replace("E%2b", "0e%2B0"); piece = piece.Replace("e%2b", "0E%2B0"); piece = piece.Replace("E%2B", "0e%2b0"); piece = piece.Replace("e%2B", "0E%2b0"); piece = piece.Replace("E+", "0e+0"); piece = piece.Replace("e+", "0E+0"); } else if (propertyType.ClrType == typeof(double) && !(piece.EndsWith("D") || piece.EndsWith("d"))) { if (!piece.Contains('.')) { piece = piece + ".0"; } else { piece = piece + "0"; } piece = piece + "E+0"; } else if (propertyType.ClrType == typeof(float) || propertyType.ClrType == typeof(Single) || propertyType.ClrType == typeof(double)) { if (!piece.Contains('.')) { piece = piece.Insert(piece.Length - 1, ".0"); //just before the 'f' or 'D' } else { piece = piece.Insert(piece.Length - 1, "0"); //just before the 'f' or 'D' } piece = piece.Insert(piece.Length - 1, "E+0"); } else if (propertyType.ClrType == typeof(decimal)) { if (!piece.Contains('.')) { piece = piece.Insert(piece.Length - 1, ".0"); //just before the 'M' } else { piece = piece.Insert(piece.Length - 1, "0"); //just before the 'M' } } if (piece.StartsWith("-")) { piece = piece.Insert(1, "0"); } else { piece = "0" + piece; } } pieces[i] = piece; } return(ConstructETag(pieces)); }
public static KeyExpression Key(ResourceContainer container, ResourceType type, NodeProperty[] properties, NodeValue[] values) { ConstantExpression[] constants = values.Select(v => Constant(v)).ToArray(); PropertyExpression[] propExps = properties.Select(p => Property(p)).ToArray(); return new KeyExpression(container, type, propExps, constants); }
public static PropertyExpression[] Properties(NodeProperty[] properties) { return properties.Select(p => Exp.Property(p)).ToArray(); }
//Constructor public NestedPropertyExpression(NodeProperty property, PropertyExpression[] propertyExpressions) : base(property) { _propertyExpressions = propertyExpressions; }
//Constructor public NestedPropertyExpression(NodeProperty property, PropertyExpression[] propertyExpressions, bool valueOnly) : base(property, valueOnly) { _propertyExpressions = propertyExpressions; }
private static ResourceInstanceKey TryCreateUniqueResourceInstanceKey(ResourceContainer container, ResourceType resType, KeyExpressions relatedForeignKeys) { Workspace workspace = container.Workspace; List <ResourceInstanceSimpleProperty> keyProperties = new List <ResourceInstanceSimpleProperty>(); Dictionary <ResourceProperty, ResourceProperty> foreignKeyMap = resType.Key.Properties.OfType <ResourceProperty>() .Where(p => p.ForeignKeys.Any()) .ToDictionary(p => p, p => p.ForeignKeys.First().PrimaryKey.Properties.OfType <ResourceProperty>().First()); Dictionary <string, ResourceProperty> reverseForeignKeyMap = new Dictionary <string, ResourceProperty>(); foreach (KeyValuePair <ResourceProperty, ResourceProperty> pair in foreignKeyMap) { reverseForeignKeyMap[pair.Value.Name] = pair.Key; } Dictionary <ResourceProperty, object> propertyValues = new Dictionary <ResourceProperty, object>(); List <ResourceProperty> constrainedProperties = new List <ResourceProperty>(); foreach (ResourceProperty property in resType.Key.Properties.OfType <ResourceProperty>()) { if (foreignKeyMap.ContainsKey(property)) { constrainedProperties.Add(property); } else { NodeValue obj = (property.Type as PrimitiveType).CreateRandomValueForFacets(property.Facets); propertyValues[property] = obj.ClrValue; } } foreach (ResourceProperty currentProperty in constrainedProperties) { if (propertyValues.ContainsKey(currentProperty)) { continue; } ResourceProperty foreignProperty = foreignKeyMap[currentProperty]; ResourceContainer foreignContainer = container.FindDefaultRelatedContainer(foreignProperty.ResourceType); KeyExpression foreignKey = relatedForeignKeys.Where(k => k.ResourceContainer == foreignContainer).FirstOrDefault(); if (foreignKey == null) { KeyExpressions foreignKeys = workspace.GetAllExistingKeysOfType(foreignContainer, foreignProperty.ResourceType); while (foreignKey == null && foreignKeys.Any()) { foreignKey = foreignKeys.Choose(); // ensure that for every property in the key, it matches any local values for (int i = 0; i < foreignKey.Properties.Length; i++) { string keyPropertyName = foreignKey.Properties[i].Name; ResourceProperty localProperty; if (reverseForeignKeyMap.TryGetValue(keyPropertyName, out localProperty)) { object keyValue = foreignKey.Values[i].ClrValue; object localValue; if (propertyValues.TryGetValue(localProperty, out localValue)) { if (localValue != keyValue) { foreignKeys.Remove(foreignKey); foreignKey = null; break; } } } } } if (foreignKey == null) { AstoriaTestLog.FailAndThrow("Could not find an appropriate foreign key"); } relatedForeignKeys.Add(foreignKey); } for (int i = 0; i < foreignKey.Properties.Length; i++) { NodeProperty p = foreignKey.Properties[i]; if (p.Name == foreignProperty.Name) { propertyValues[currentProperty] = foreignKey.Values[i].ClrValue; } else if (p.ForeignKeys.Count() > 0) { string foreign = p.ForeignKeys.First().PrimaryKey.Properties.OfType <ResourceProperty>().First().Name; ResourceProperty localProperty; if (reverseForeignKeyMap.TryGetValue(foreign, out localProperty)) { propertyValues[localProperty] = foreignKey.Values[i].ClrValue; } } } } foreach (ResourceProperty property in resType.Key.Properties.OfType <ResourceProperty>()) { if (!propertyValues.ContainsKey(property)) { propertyValues[property] = (object)null; } } foreach (KeyValuePair <ResourceProperty, object> pair in propertyValues) { keyProperties.Add(new ResourceInstanceSimpleProperty(pair.Key.Facets, pair.Key.Name, pair.Value)); } ResourceInstanceKey resourceInstanceKey = new ResourceInstanceKey(container, resType, keyProperties.ToArray()); return(resourceInstanceKey); }
public NullablePropertyExpression(NodeProperty property) : base(property) { }
public static PropertyExpression Property(this NodeProperty property) { return(new PropertyExpression(property)); }
private void CompareResetProperty(NodeProperty property, PayloadProperty value) { bool complexType = property.Type is ComplexType; Workspace w = value.ParentObject.Payload.Workspace; bool expectNull = false; if (w.DataLayerProviderKind == DataLayerProviderKind.NonClr && (!property.Facets.IsClrProperty || complexType)) { expectNull = true; } if (w.DataLayerProviderKind == DataLayerProviderKind.InMemoryLinq && complexType) { expectNull = true; } if (!property.Facets.IsDeclaredProperty) { expectNull = true; } if (property.Facets.Nullable) { expectNull = true; } if (property.Type is ComplexType) { if (value.IsNull) { if (!expectNull) { AstoriaTestLog.FailAndThrow("Complex property '" + property.Name + " is unexpectedly null after reset"); } } else { if (!(value is PayloadComplexProperty)) { AstoriaTestLog.FailAndThrow("Property '" + property.Name + "' is complex, but a simple property was found instead"); } ComplexType ct = property.Type as ComplexType; PayloadComplexProperty complexProperty = value as PayloadComplexProperty; foreach (NodeProperty subProperty in ct.Properties) { PayloadProperty subValue; if (!complexProperty.PayloadProperties.TryGetValue(subProperty.Name, out subValue)) { AstoriaTestLog.FailAndThrow("Property of complex type '" + ct.Name + "' missing sub-property '" + subProperty.Name + "'"); } CompareResetProperty(subProperty, subValue); } } } else { PayloadSimpleProperty simpleProperty = value as PayloadSimpleProperty; if (simpleProperty == null) { AstoriaTestLog.FailAndThrow("Property was unexpectedly not a simple property"); } Type propertyType = property.Type.ClrType; object expected = null; if (!expectNull) { expected = DefaultValue(propertyType); } CommonPayload.ComparePrimitiveValuesObjectAndString(expected, propertyType, simpleProperty.Value, false, value.ParentObject.Format, true); } }