Beispiel #1
0
        public override KeyExpressions GetAllExistingKeysOfType(ResourceContainer resourceContainer, ResourceType resourceType)
        {
            if (!DataGenerator.Done)
            {
                return(DataGenerator.GetAllGeneratedKeys(resourceContainer, resourceType));
            }

            bool            rightsChanged = false;
            EntitySetRights oldRights     = this.DataService.ConfigSettings.GetEntitySetAccessRule(resourceContainer.Name);

            if ((EntitySetRights.ReadMultiple & oldRights) == 0)
            {
                rightsChanged = true;
                this.DataService.ConfigSettings.SetEntitySetAccessRule(resourceContainer.Name, EntitySetRights.All);
            }

            KeyExpressions keys = SocketExceptionHandler.Execute <KeyExpressions>(
                () => GetAllExistingKeysBase(resourceContainer));

            if (rightsChanged)
            {
                this.DataService.ConfigSettings.SetEntitySetAccessRule(resourceContainer.Name, oldRights);
            }

            if (resourceType == null)
            {
                return(keys);
            }

            return(new KeyExpressions(keys.Where(k => k.ResourceType.Equals(resourceType))));
        }
        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");
                    }
                }
            }
        }
Beispiel #3
0
        public void GetKeyExpression()
        {
            if (_resType.Name.Contains("Order_Details_Extended") || _resourceContainer.Name.Contains("Order_Details_Extended"))
            {
                this.Result = null;
                return;
            }

            if (_resType.Name != _resourceContainer.BaseType.Name)
            {
                KeyExpressions keyExp2 = _resourceContainer.Workspace.GetExistingAssociatedKeys(_resourceContainer, _prop, _key);
                if (null == keyExp2 || keyExp2.Count == 0) /* no keys for resource type*/ return {
                    ;
                }
                foreach (KeyExpression ke in keyExp2)
                {
                    bool bSpecial = SpecialChars(ke);
                    if (bSpecial)
                    {
                        this.Result = null;
                        break;
                    }
                    else
                    {
                        this.Result = ke;
                        AstoriaTestLog.WriteLineIgnore("Predicate from container :" + _resType.Name);
                        break;
                    }
                }
            }
Beispiel #4
0
        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);
        }
Beispiel #5
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));
        }
        private static List <ResourceInstanceProperty> CloneRequiredRelationships(ResourceContainer container, ResourceType resourceType, KeyExpression keyExpression)
        {
            List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>();
            //Foreach Navigation Property in dataObject create a bind
            Dictionary <ResourceProperty, KeyExpressions> navigationProperties = GetAllAssociatedKeys(container, resourceType, keyExpression);

            foreach (ResourceProperty navProperty in navigationProperties.Keys)
            {
                //ResourceAssociationEnd otherEnd = navProperty.ResourceAssociation.GetOtherEnd(navProperty);
                ResourceType      navPropertyResourceType = (navProperty.Type is CollectionType ? (navProperty.Type as CollectionType).SubType : navProperty.Type) as ResourceType;
                ResourceContainer otherContainer          = container.FindDefaultRelatedContainer(navProperty);

                bool foreignKeyViolation = false;
                foreach (ResourceProperty otherProperty in otherContainer.BaseType.Key.Properties)
                {
                    if (otherProperty.ForeignKeys.Count() > 0)
                    {
                        ResourceType otherType = otherProperty.ForeignKeys.First().PrimaryKey.Properties.OfType <ResourceProperty>().First().ResourceType;
                        if (otherType == container.BaseType)
                        {
                            foreignKeyViolation = true;
                            break;
                        }
                    }
                }
                if (foreignKeyViolation)
                {
                    continue;
                }

                KeyExpressions keyExpressions = navigationProperties[navProperty];
                if (navProperty.Type is ResourceType)
                {
                    if (keyExpressions.Count > 0)
                    {
                        properties.Add(CreateRefInstanceProperty(keyExpressions[0], otherContainer, navProperty));
                    }
                }
                else
                {
                    ResourceInstanceProperty property = CreateCollectionInstanceProperty(keyExpressions, otherContainer, navProperty);
                    if (property != null)
                    {
                        properties.Add(property);
                    }
                }
            }
            return(properties);
        }
        public static Dictionary <ResourceProperty, KeyExpressions> GetAllAssociatedKeys(ResourceContainer container, ResourceType resourceType, KeyExpression keyExpression)
        {
            Workspace workspace = container.Workspace;
            Dictionary <ResourceProperty, KeyExpressions> listOfKeyExpressions = new Dictionary <ResourceProperty, KeyExpressions>();

            foreach (ResourceProperty resourceProperty in resourceType.Properties)
            {
                if (resourceProperty.IsNavigation)
                {
                    KeyExpressions keys = workspace.GetExistingAssociatedKeys(container, resourceProperty, keyExpression);
                    listOfKeyExpressions.Add(resourceProperty, keys);
                }
            }
            return(listOfKeyExpressions);
        }
        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);
        }
Beispiel #9
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);
        }
        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);
        }
Beispiel #11
0
        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);
        }
Beispiel #13
0
 public KeyedResourceInstance CreateRandomResource(ResourceContainer container, KeyExpressions existingKeys)
 {
     return(CreateRandomResource(container, existingKeys, true));
 }
Beispiel #14
0
 public static KeyExpressions Except(this KeyExpressions keys, IEnumerable <KeyExpression> toOmit)
 {
     return(new KeyExpressions(keys.Where(k => !toOmit.Contains(k))));
 }
Beispiel #15
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()));
        }
Beispiel #16
0
        public static AstoriaRequest BuildDelete(Workspace workspace, KeyExpressions existingKeys, HttpStatusCode expectedStatusCode, SerializationFormatKind format)
        {
            KeyExpression deletedKey;

            return(BuildDelete(workspace, existingKeys, expectedStatusCode, format, out deletedKey));
        }
Beispiel #17
0
 public static AstoriaRequest BuildDelete(Workspace workspace, KeyExpressions existingKeys, HttpStatusCode expectedStatusCode)
 {
     return(BuildDelete(workspace, existingKeys, expectedStatusCode, SerializationFormatKind.Default));
 }
 internal static ResourceInstanceKey CreateUniqueKey(ResourceContainer container, ResourceType resType, KeyExpressions relatedForeignKeys)
 {
     return(CreateUniqueKey(container, resType, relatedForeignKeys, null));
 }