private static void VerifyLinksPayload(Workspace w, CommonPayload payload, LinqQueryBuilder linqBuilder) { ArrayList expectedEntities = CommonPayload.CreateList(linqBuilder.QueryResult); if (payload == null) { AstoriaTestLog.AreEqual(expectedEntities.Count, 0, "Unexpected null $ref payload"); } else { KeyExpressions expectedKeys = new KeyExpressions(); foreach (object o in expectedEntities) { KeyExpression keyExp = w.CreateKeyExpressionFromProviderObject(o); expectedKeys.Add(keyExp); } List <string> linksFound = new List <string>(); if (payload.Resources == null) { linksFound.Add(payload.Value); } else { foreach (PayloadObject o in (payload.Resources as List <PayloadObject>)) { if (o.PayloadProperties.Any(p => p.Name == "uri")) { linksFound.Add((o["uri"] as PayloadSimpleProperty).Value); } } } AstoriaTestLog.AreEqual(expectedKeys.Count, linksFound.Count, "Number of expected entities does not match number of links found"); foreach (string link in linksFound) { KeyExpression match = null; foreach (KeyExpression expectedKey in expectedKeys) { if (compareKeyURI(link, expectedKey)) { match = expectedKey; break; } } if (match != null) { expectedKeys.Remove(match); } else { AstoriaTestLog.WriteLineIgnore("Unexpected URI: '" + link + "'"); AstoriaTestLog.FailAndThrow("Unexpected URI found in $ref payload"); } } } }
public override KeyExpressions GetExistingAssociatedKeys(ResourceContainer resourceContainer, ResourceProperty property, KeyExpression keyExpression) { bool pageSizeChanged = false; int originalPageSize = this.DataService.ConfigSettings.GetEntitySetPageSize(resourceContainer.Name); if (originalPageSize < 1000) { pageSizeChanged = true; this.DataService.ConfigSettings.SetEntitySetPageSize(resourceContainer.Name, 100000); } UriQueryBuilder uriQueryBuilder = new UriQueryBuilder(this, this.ServiceUri); ExpNode query = Query.From( Exp.Variable(resourceContainer)) .Where(keyExpression) //.OfType(property.ResourceType) .Nav(new PropertyExpression(property)) .Select(); string uri = uriQueryBuilder.Build(query); ResourceType associatedResourceType = property.Type as ResourceType; if (property.Type is CollectionType) { associatedResourceType = (property.Type as CollectionType).SubType as ResourceType; } Type type = this._resourceTypeToWorkspaceTypeList[associatedResourceType]; MethodInfo method = this.GetType().GetMethod("ClientExecuteWrapper", new Type[] { typeof(string), typeof(ResourceContainer) }); MethodInfo genMethod = method.MakeGenericMethod(new Type[] { type }); IEnumerable o = (IEnumerable)genMethod.Invoke(this, new object[] { uri, resourceContainer.FindDefaultRelatedContainer(property) }); KeyExpressions keys = new KeyExpressions(); //IEnumerator enumerator = o.GetEnumerator(); foreach (object current in o) { if (current != null) { //Type t = enumerator.Current.GetType(); Type t = current.GetType(); IEnumerable <ResourceType> typesWithName = this.ServiceContainer.ResourceTypes.Where(rt => (t.Name.Equals(rt.Name))); IEnumerable <ResourceType> typesWithNamespace = typesWithName.Where(rt2 => rt2.Namespace == t.Namespace).ToList(); ResourceType instanceType = typesWithNamespace.First(); ResourceContainer relatedContainer = resourceContainer.FindDefaultRelatedContainer(property); keys.Add(GetKeyExpression(relatedContainer, instanceType, current)); } } if (pageSizeChanged) { this.DataService.ConfigSettings.SetEntitySetPageSize(resourceContainer.Name, originalPageSize); } return(keys); }
public static KeyExpressions GetAllExistingKeys(Workspace w, ResourceContainer resourceContainer, ResourceType resourceType) { IEnumerable <ContainmentAttribute> attributes = GetContainmentAttributes(w.ServiceContainer, ca => ca.ChildContainer == resourceContainer); // should we pick one path at random? just use the first? determine which is shortest? ContainmentAttribute att = attributes.FirstOrDefault(); if (att == null) { QueryNode query = Query.From( Exp.Variable(resourceContainer)) .Select(); if (resourceType != null) { query = query.OfType(resourceType); } return(w.GetAllExistingKeys(query, resourceContainer)); } // recursively get all the parent container's keys // since we don't know the exact parent type, don't use one // KeyExpressions parentKeys = GetAllExistingKeys(w, att.ParentContainer, null); // we're doing some extra work by re-determining the parent key's access path, // but it would be hard to keep it around // KeyExpressions childKeys = new KeyExpressions(); foreach (KeyExpression parentKey in parentKeys) { // we don't necessarily need a canonical path QueryNode q = BuildQuery(parentKey, false); q = q.Nav(att.ParentNavigationProperty.Property()).Select(); if (resourceType != null) { q = q.OfType(resourceType); } foreach (KeyExpression childKey in w.GetAllExistingKeys(q, resourceContainer)) { childKeys.Add(childKey); } } return(childKeys); }
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); }
public KeyExpressions Diff(KeyExpressions keyExpressions) { KeyExpressions diffKeyExpressions = new KeyExpressions(); foreach (KeyExpression keyExp in keyExpressions) { bool found = false; foreach (KeyExpression keyExp2 in this) { if (keyExp2.Equals(keyExp)) { found = true; break; } } if (found == false) { diffKeyExpressions.Add(keyExp); } } return(diffKeyExpressions); }
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); }