public static KeyedResourceInstance CreateKeyedResourceInstance(KeyExpression exp, ResourceContainer container, params ResourceInstanceProperty[] properties) { ResourceType resType = exp.Properties.OfType <ResourceProperty>().First().ResourceType; ResourceInstanceKey instanceKey = ResourceInstanceKey.ConstructResourceInstanceKey(exp); return(new KeyedResourceInstance(instanceKey, properties)); }
public static ResourceInstanceProperty CreateRefInstanceProperty(KeyExpression keyExp, ResourceContainer container, ResourceProperty navProperty) { ResourceType navResourceType = navProperty.Type as ResourceType; ResourceInstanceKey resourceInstanceKey = ResourceInstanceKey.ConstructResourceInstanceKey(keyExp); return(new ResourceInstanceNavRefProperty(navProperty.Name, new AssociationResourceInstance(resourceInstanceKey, AssociationOperation.Add))); }
public KeyedResourceInstance(ResourceInstanceKey key, params ResourceInstanceProperty[] properties) : base(key.ResourceTypeName, properties) { TestUtil.CheckArgumentElementsNotNull(properties, "properties"); ResourceInstanceKey = key; ResourceSetName = key.ResourceSetName; IncludeTypeMetadataHint = true; }
public override bool Equals(object obj) { ResourceInstanceKey otherKey = obj as ResourceInstanceKey; if (otherKey != null) { return(this.Equals(otherKey)); } return(false); }
internal static KeyedResourceInstance CreateKeyedResourceInstanceFromPayloadObject(ResourceContainer container, ResourceType resourceType, PayloadObject payloadObject) { List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>(); List <ResourceInstanceProperty> keyProperties = new List <ResourceInstanceProperty>(); foreach (ResourceProperty property in resourceType.Properties.OfType <ResourceProperty>()) { if (property.IsNavigation) { continue; } if (property.IsComplexType) { PayloadComplexProperty fromPayload = payloadObject[property.Name] as PayloadComplexProperty; properties.Add(ConvertComplexPayloadObjectToComplexProperty(property, fromPayload)); } else { string stringValue; if (payloadObject.PayloadProperties.Any(p => p.Name == property.Name)) { PayloadSimpleProperty fromPayload = payloadObject[property.Name] as PayloadSimpleProperty; stringValue = fromPayload.Value; } else { if (!payloadObject.CustomEpmMappedProperties.TryGetValue(property.Name, out stringValue)) { stringValue = null; } } ResourceInstanceProperty newProperty = null; object val = CommonPayload.DeserializeStringToObject(stringValue, property.Type.ClrType, false, payloadObject.Format); newProperty = new ResourceInstanceSimpleProperty(property.Name, new NodeValue(val, property.Type)); if (property.PrimaryKey != null) { keyProperties.Add(newProperty); } else { properties.Add(newProperty); } } } ResourceInstanceKey key = new ResourceInstanceKey(container, resourceType, keyProperties.ToArray()); return(new KeyedResourceInstance(key, properties.ToArray())); }
internal static KeyedResourceInstance CreateKeyedResourceInstanceByExactClone(ResourceContainer container, ResourceType resourceType, object dataObject) { List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>(); List <ResourceInstanceProperty> keyProperties = new List <ResourceInstanceProperty>(); properties.AddRange(CloneObjectToResourceInstanceProperties(resourceType.Properties.OfType <ResourceProperty>().Where(rp => rp.PrimaryKey == null && rp.IsNavigation == false), dataObject)); keyProperties.AddRange(CloneObjectToResourceInstanceProperties(resourceType.Properties.OfType <ResourceProperty>().Where(rp => rp.PrimaryKey != null && rp.IsNavigation == false), dataObject)); ResourceInstanceKey key = new ResourceInstanceKey(container, resourceType, keyProperties.ToArray()); KeyedResourceInstance keyResourceInstance = new KeyedResourceInstance(key, properties.ToArray()); return(keyResourceInstance); }
public static KeyedResourceInstance CreateKeyedResourceInstanceByClone(ResourceContainer container, ResourceType resourceType, bool excludeRelationships) { Workspace workspace = container.Workspace; //Clone for an existing resource, and update its key KeyExpression keyExpression = workspace.GetRandomExistingKey(container, resourceType); if (keyExpression == null) { return(null); } KeyedResourceInstance dataObject = workspace.GetSingleResourceByKey(keyExpression); if (dataObject == null) { return(null); } ResourceInstanceKey key = null; // if there are any non-server-generated key properties, then we need to build the key if (keyExpression.ResourceType.Properties.Any(p => p.PrimaryKey != null && !p.Facets.ServerGenerated)) { ResourceType newResourceType = container.ResourceTypes.Where(rt => rt.Name == dataObject.TypeName).FirstOrDefault(); key = CreateUniqueKey(container, newResourceType); } //Foreach property in dataObject create a ResourceProperty List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>(); properties.AddRange(dataObject.Properties.OfType <ResourceInstanceSimpleProperty>().ToArray()); properties.AddRange(dataObject.Properties.OfType <ResourceInstanceComplexProperty>().ToArray()); if (!excludeRelationships) { properties.AddRange(CloneRequiredRelationships(container, keyExpression.ResourceType, keyExpression)); } KeyedResourceInstance keyResourceInstance = null; if (key != null) { keyResourceInstance = new KeyedResourceInstance(key, properties.ToArray()); } else { keyResourceInstance = new KeyedResourceInstance(dataObject.ResourceSetName, dataObject.TypeName, properties.ToArray()); } return(keyResourceInstance); }
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 ConvertToKeyExpression(ResourceInstanceKey key, Workspace workspace) { ResourceContainer container = workspace.ServiceContainer.ResourceContainers[key.ResourceSetName]; if (container == null) { return(null); } ResourceType type = container.ResourceTypes.FirstOrDefault(rt => rt.Name == key.ResourceTypeName); if (type == null) { return(null); } return(key.CreateKeyExpression(container, type)); }
public static ResourceInstanceProperty CreateCollectionInstanceProperty(KeyExpressions keyExps, ResourceContainer container, ResourceProperty navProperty) { ResourceType navResourceType = (navProperty.Type as CollectionType).SubType as ResourceType; List <AssociationResourceInstance> associationNodes = new List <AssociationResourceInstance>(); foreach (KeyExpression associatedKey in keyExps) { ResourceInstanceKey resourceInstanceKey = ResourceInstanceKey.ConstructResourceInstanceKey(associatedKey); associationNodes.Add(new AssociationResourceInstance(resourceInstanceKey, AssociationOperation.Add)); } if (associationNodes.Count > 0) { ResourceInstanceNavColProperty navigationProperty = new ResourceInstanceNavColProperty(navProperty.Name, associationNodes.ToArray()); return(navigationProperty); } return(null); }
public bool Equals(ResourceInstanceKey key) { foreach (ResourceInstanceSimpleProperty sp in key.KeyProperties) { bool partialKeyValueFound = false; foreach (ResourceInstanceSimpleProperty sp2 in this.KeyProperties) { if (sp2.Name == sp.Name && sp.PropertyValue.Equals(sp2.PropertyValue)) { partialKeyValueFound = true; break; } } if (!partialKeyValueFound) { return(false); } } return(true); }
internal static ResourceInstanceKey CreateUniqueKey(ResourceContainer container, ResourceType resType, KeyExpressions relatedForeignKeys, KeyExpressions existingKeys) { KeyExpressions possibleRelatedForeignKeys = new KeyExpressions(); Workspace workspace = container.Workspace; int keysGenerated = 0; bool keyTrying = true; ResourceInstanceKey resourceInstanceKey = null; do { possibleRelatedForeignKeys = new KeyExpressions(); resourceInstanceKey = TryCreateUniqueResourceInstanceKey(container, resType, possibleRelatedForeignKeys); KeyExpression keyExpression = resourceInstanceKey.CreateKeyExpression(container, resType); // need to make sure its not a duplicate // if (existingKeys == null) { KeyedResourceInstance o = workspace.GetSingleResourceByKey(keyExpression); if (o == null) { keyTrying = false; } } else { keyTrying = existingKeys.Contains(keyExpression); } keysGenerated++; if (keysGenerated > 25) { throw new Microsoft.Test.ModuleCore.TestFailedException("Unable to create a unique key"); } }while (keyTrying); relatedForeignKeys.Add(possibleRelatedForeignKeys); return(resourceInstanceKey); }
protected string CreateCanonicalUri(ResourceInstanceKey key) { UriQueryBuilder builder = new UriQueryBuilder(Workspace, Workspace.ServiceUri); builder.UseBinaryFormatForDates = false; builder.CleanUpSpecialCharacters = false; KeyExpression keyExpression = ResourceInstanceUtil.ConvertToKeyExpression(key, Workspace); if (keyExpression != null) { QueryNode query = ContainmentUtil.BuildCanonicalQuery(keyExpression); return(builder.Build(query)); } IEnumerable <ResourceInstanceSimpleProperty> properties = key.KeyProperties.OfType <ResourceInstanceSimpleProperty>(); string keyString = builder.CreateKeyString(properties.Select(p => p.Name).ToArray(), properties.Select(p => p.PropertyValue).ToArray()); string uri = Workspace.ServiceUri + "/" + key.ResourceSetName + "(" + keyString + ")"; return(uri); }
public static AstoriaRequest BuildUpdate(Workspace workspace, KeyExpression modifiedKey, bool replace, HttpStatusCode expectedStatusCode, SerializationFormatKind format) { if (modifiedKey == null) { return(null); } ResourceContainer container = modifiedKey.ResourceContainer; ResourceType resourceType = modifiedKey.ResourceType; if (replace && resourceType.Properties.Any(p => p.Facets.IsIdentity)) { return(null); } string keyString = UriQueryBuilder.CreateKeyString(modifiedKey, false); if (expectedStatusCode == HttpStatusCode.NoContent && (keyString.Contains("/") || keyString.Contains(Uri.EscapeDataString("/")))) { expectedStatusCode = HttpStatusCode.BadRequest; } QueryNode query = ContainmentUtil.BuildCanonicalQuery(modifiedKey); List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>(); string[] propertiesToSkip; //Skip because setting the birthdate to a random Datetime won't work due to contraints //if (resourceType.Name == "Employees") // propertiesToSkip = new string[] { "BirthDate" }; ////Skipping because it has some weird constraint on it //else if (resourceType.Name == "Order_Details") // propertiesToSkip = new string[] { "Discount" }; //else // propertiesToSkip = new string[] { }; foreach (ResourceProperty resourceProperty in resourceType.Properties.OfType <ResourceProperty>() .Where(p => !p.IsNavigation && p.PrimaryKey == null && !p.Facets.IsIdentity)) //&& !p.IsComplexType //&& !propertiesToSkip.Contains(p.Name))) { properties.Add(resourceProperty.CreateRandomResourceInstanceProperty()); } if (!properties.Any()) { return(null); } KeyedResourceInstance resourceInstance = new KeyedResourceInstance( ResourceInstanceKey.ConstructResourceInstanceKey(modifiedKey), properties.ToArray()); AstoriaRequest request = workspace.CreateRequest(); request.Verb = replace ? RequestVerb.Put : RequestVerb.Patch; request.Query = query; request.UpdateTree = resourceInstance; request.ExpectedStatusCode = expectedStatusCode; request.Format = format; if (modifiedKey.ResourceType.Properties.Any(p => p.Facets.ConcurrencyModeFixed)) { request.Headers[ConcurrencyUtil.IfMatchHeader] = modifiedKey.ETag; request.ETagHeaderExpected = true; } return(request); }
public AssociationResourceInstance(ResourceInstanceKey key, AssociationOperation operation) : base(key) { _operation = operation; }
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 KeyedResourceInstance(ResourceContainer container, ResourceType resType, params ResourceInstanceProperty[] properties) : this(container.Name, resType.Name, properties.Where(p => !resType.Key.Properties.Any(keyProp => keyProp.Name == p.Name)).ToArray()) { ResourceInstanceKey = new ResourceInstanceKey(container, resType, properties.Where(p => resType.Key.Properties.Any(keyProp => keyProp.Name == p.Name)).ToArray()); }
internal KeyedResourceInstance CreateRandomResource(ResourceContainer container, KeyExpressions existingKeys, bool populateNavProps) { KeyExpressions relatedForeignKeys = new KeyExpressions(); ResourceInstanceKey key = ResourceInstanceUtil.CreateUniqueKey(container, this, relatedForeignKeys, existingKeys); List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>(); // populate the non-key, non-navigation properties // foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation == false && rp.PrimaryKey == null)) { if (p.Facets.IsIdentity) { continue; } ResourceInstanceProperty property = p.CreateRandomResourceInstanceProperty(); properties.Add(property); } if (populateNavProps) { // populate the navigation properties, but don't go by key, as some foreign keys MAY NOT HAVE ASSOCIATED NAVIGATION PROPERTIES // foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation)) { // find a key for this navigation property KeyExpression navPropKey = null; foreach (KeyExpression keyExp in relatedForeignKeys) { //if (p.Type.Equals(keyExp.ResourceType) // || p.Type is ResourceType && (p.Type as ResourceType).BaseTypes.Contains(keyExp.ResourceType)) if (p.OtherAssociationEnd.ResourceType.Equals(keyExp.ResourceType)) { navPropKey = keyExp; break; } } ResourceContainer associatedContainer = container.FindDefaultRelatedContainer(p); if (navPropKey == null) { if (p.OtherAssociationEnd.ResourceType.Key.Properties.OfType <ResourceProperty>() .Where(rp => rp.ForeignKeys.Any()) .Any(rp => rp.ForeignKeys.First().PrimaryKey.Properties.OfType <ResourceProperty>().First().ResourceType.Equals(this))) { // this association has a fk back to the current type, so it cannot be based on any existing entity AstoriaTestLog.WriteLineIgnore("Skipping nav prop '{0}.{1}' due to foreign key constraint on entity being created", this.Name, p.Name); continue; } else { navPropKey = associatedContainer.Workspace.GetRandomExistingKey(associatedContainer, p.OtherAssociationEnd.ResourceType); } } if (navPropKey != null) { if (p.OtherAssociationEnd.Multiplicity == Multiplicity.Many) { properties.Add(ResourceInstanceUtil.CreateCollectionInstanceProperty(new KeyExpressions(navPropKey), navPropKey.ResourceContainer, p)); } else { properties.Add(ResourceInstanceUtil.CreateRefInstanceProperty(navPropKey, navPropKey.ResourceContainer, p)); } } } } return(ResourceInstanceUtil.CreateKeyedResourceInstance(key.CreateKeyExpression(container, this), container, properties.ToArray())); }