示例#1
0
 public override void ToPrecedenceFreeEPL(TextWriter writer)
 {
     writer.Write(TableName);
     if (KeyExpressions != null && !KeyExpressions.IsEmpty())
     {
         writer.Write("[");
         ToPrecedenceFreeEPL(KeyExpressions, writer);
         writer.Write("]");
     }
     if (OptionalColumn != null)
     {
         writer.Write(".");
         writer.Write(OptionalColumn);
     }
     if (OptionalAggregate != null)
     {
         writer.Write(".");
         OptionalAggregate.ToEPL(writer, ExpressionPrecedenceEnum.MINIMUM);
     }
 }
示例#2
0
        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());
        }
示例#3
0
 public KeyedResourceInstance CreateRandomResource(ResourceContainer container, KeyExpressions existingKeys)
 {
     return CreateRandomResource(container, existingKeys, true);
 }
 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;
 }
        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;
        }
        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;
        }
 internal static ResourceInstanceKey CreateUniqueKey(ResourceContainer container, ResourceType resType, KeyExpressions relatedForeignKeys)
 {
     return CreateUniqueKey(container, resType, relatedForeignKeys, null);
 }
示例#8
0
        public virtual KeyExpressions GetAllExistingKeys(ExpNode query, ResourceContainer resourceContainer)
        {
            KeyExpressions keys = new KeyExpressions();
            IQueryable objects = this.GetUnderlyingProviderQueryResults(query);
            IEnumerator enumerator = null;
            try
            {
                enumerator = objects.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    Type t = enumerator.Current.GetType();
                    if (AstoriaTestProperties.EdmObjectLayer == ServiceEdmObjectLayer.PocoWithProxy && AstoriaTestProperties.DataLayerProviderKinds.Contains(Astoria.DataLayerProviderKind.Edm))
                        t = t.BaseType;
                    List<ResourceType> types = resourceContainer.ResourceTypes.Where(rt => rt.Name.Equals(t.Name) && rt.Namespace.Equals(t.Namespace)).ToList();

                    ResourceType actualResourceType = types.Single();
                    keys.Add(GetKeyExpression(resourceContainer, actualResourceType, enumerator.Current));
                }
            }
            catch (NullReferenceException exc)
            {
                //TODO: Due to a client bug
            }
            finally
            {
                IDisposable disposable = (enumerator as IDisposable);
                if (null != disposable)
                {
                    disposable.Dispose();
                }
            }
            return keys;
        }
示例#9
0
        public virtual KeyExpressions GetExistingAssociatedKeys(ResourceContainer resourceContainer, ResourceProperty property, KeyExpression keyExpression)
        {
            LinqQueryBuilder linqBuilder = new LinqQueryBuilder(this);
            ExpNode query = Query.From(
                                 Exp.Variable(resourceContainer))
                                .Where(keyExpression)
                                .OfType(property.ResourceType)
                                .Nav(new PropertyExpression(property))
                                .Select();

            linqBuilder.Build(query);

            ResourceType associatedType = property.Type as ResourceType;
            if (property.Type is ResourceCollection)
            {
                associatedType = (property.Type as ResourceCollection).SubType as ResourceType;
            }
            IQueryable queryable = linqBuilder.QueryResult;
            KeyExpressions keys = new KeyExpressions();
            IEnumerator enumerator = queryable.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (enumerator.Current != null)
                {
                    Type t = enumerator.Current.GetType();
                    if (AstoriaTestProperties.EdmObjectLayer == ServiceEdmObjectLayer.PocoWithProxy)
                        t = t.BaseType;

                    IEnumerable<ResourceType> typesWithName = resourceContainer.Workspace.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, enumerator.Current));
                }
            }
            return keys;
        }
示例#10
0
        protected virtual KeyExpression GetRandomExistingKey(ResourceContainer resourceContainer, ResourceType resourceType, KeyExpression partialKey)
        {
            LinqQueryBuilder linqBuilder = new LinqQueryBuilder(this);
            QueryNode query = Query.From(Exp.Variable(resourceContainer));

            //TODO: Figure out how to make OfType work with astoria dataquerycontext
            if (resourceType != null && resourceContainer.Workspace.DataLayerProviderKind != DataLayerProviderKind.InMemoryLinq && resourceType.BaseType != null)
            {
                query = query.OfType(resourceType);
            }
            else if (partialKey != null)
            {
                query = query.Where(partialKey);
            }
            query = query.Select().Top(50);

            linqBuilder.Build(query);

            int count = 0;
            KeyExpressions keys = new KeyExpressions();

            IQueryable objects = linqBuilder.QueryResult;
            IEnumerator enumerator = null;
            try
            {
                enumerator = objects.GetEnumerator();
                while (enumerator.MoveNext() && count < 50)
                {
                    if (enumerator.Current != null)
                    {
                        Type t = enumerator.Current.GetType();
                        if (AstoriaTestProperties.EdmObjectLayer == ServiceEdmObjectLayer.PocoWithProxy)
                            t = t.BaseType;
                        List<ResourceType> types = resourceContainer.ResourceTypes.Where(rt => rt.Name.Equals(t.Name) && rt.Namespace.Equals(t.Namespace)).ToList();
                        if (types.Count > 0)
                        {
                            ResourceType actualResourceType = types.Single();

                            if (resourceType != null && resourceType != actualResourceType)
                                continue;

                            KeyExpression keyExp = GetKeyExpression(resourceContainer, actualResourceType, enumerator.Current);
                            //If key is something that has approx value
                            //Then its really a nonfunctional key, excluding it as 
                            //we only want valid keys here
                            if (!keyExp.IsApproxKeyValue())
                            {
                                keys.Add(keyExp);
                                count++;
                            }
                        }
                    }
                }
            }
            finally
            {
                IDisposable disposable = (enumerator as IDisposable);
                if (null != disposable)
                {
                    disposable.Dispose();
                }
            }
            return keys.Choose();
        }
示例#11
0
 public static AstoriaRequest BuildDelete(Workspace workspace, KeyExpressions existingKeys, HttpStatusCode expectedStatusCode)
 {
     return BuildDelete(workspace, existingKeys, expectedStatusCode, SerializationFormatKind.Default);
 }
示例#12
0
 public static AstoriaRequest BuildDelete(Workspace workspace, KeyExpressions existingKeys, HttpStatusCode expectedStatusCode, SerializationFormatKind format)
 {
     KeyExpression deletedKey;
     return BuildDelete(workspace, existingKeys, expectedStatusCode, format, out deletedKey);
 }
示例#13
0
        public static AstoriaRequest BuildDelete(Workspace workspace, KeyExpressions existingKeys, HttpStatusCode expectedStatusCode, SerializationFormatKind format, out KeyExpression deletedKey)
        {
            deletedKey = existingKeys.Choose();
            if (deletedKey == null)
            {
                AstoriaTestLog.WriteLine("Cannot build DELETE request, no existing keys");
                return null;
            }
            existingKeys.Remove(deletedKey);

            return BuildDelete(workspace, deletedKey, expectedStatusCode, format);
        }
示例#14
0
        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");
                    }
                }
            }
        }
示例#15
0
        private KeyExpressions GetKeyExpressionsFromPayload(ResourceContainer container, AstoriaResponse response)
        {
            CommonPayload payload = response.CommonPayload;

            KeyExpressions keys = new KeyExpressions();

            //IEnumerable<ResourceProperty> keyProperties = container.BaseType.Properties.OfType<ResourceProperty>().Where(rp => rp.PrimaryKey != null);

            if (payload.Resources != null)
            {
                foreach (PayloadObject entity in (List<PayloadObject>)payload.Resources)
                {
                    keys.Add(ConcurrencyUtil.ConstructKey(container, entity));
                }
            }

            return keys;
        }
示例#16
0
        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;
        }