Ejemplo n.º 1
0
        public static string CreateKeyString(KeyExpression keyExp, bool binaryFormat)
        {
            Workspace       w       = keyExp.ResourceContainer.Workspace;
            UriQueryBuilder builder = new UriQueryBuilder(w, w.ServiceUri);

            builder.UseBinaryFormatForDates  = binaryFormat;
            builder.CleanUpSpecialCharacters = true;

            return(builder.CreateKeyString(keyExp));
        }
Ejemplo n.º 2
0
        public static AstoriaRequest BuildGet(Workspace workspace, KeyExpression key, HttpStatusCode expectedStatusCode, SerializationFormatKind format)
        {
            QueryNode query = ContainmentUtil.BuildCanonicalQuery(key);

            string keyString = UriQueryBuilder.CreateKeyString(key, false);

            if ((expectedStatusCode == System.Net.HttpStatusCode.OK) && (keyString.Contains("/") || keyString.Contains(Uri.EscapeDataString("/"))))
            {
                expectedStatusCode = System.Net.HttpStatusCode.BadRequest;
            }

            return(BuildGet(workspace, query, expectedStatusCode, format));
        }
Ejemplo n.º 3
0
        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;
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        public static string CreateKeyString(KeyExpression keyExp, bool binaryFormat)
        {
            Workspace w = keyExp.ResourceContainer.Workspace;
            UriQueryBuilder builder = new UriQueryBuilder(w, w.ServiceUri);
            builder.UseBinaryFormatForDates = binaryFormat;
            builder.CleanUpSpecialCharacters = true;

            return builder.CreateKeyString(keyExp);
        }
Ejemplo n.º 7
0
        private static bool compareKeyURI(string uriFound, KeyExpression keyExpected)
        {
            UriQueryBuilder builder = new UriQueryBuilder(keyExpected.ResourceContainer.Workspace, keyExpected.ResourceContainer.Workspace.ServiceUri);

            builder.EscapeUriValues          = true;
            builder.CleanUpSpecialCharacters = false;
            builder.UseBinaryFormatForDates  = false;

            switch (keyExpected.IncludeInUri.Count(i => i))
            {
            case 0:
                AstoriaTestLog.FailAndThrow("Cannot compare KeyExpression to URI, key has no included values");
                return(false);

            case 1:
                // TODO: stop ignoring case
                QueryNode query    = ContainmentUtil.BuildCanonicalQuery(keyExpected);
                string    expected = builder.Build(query);

                expected = expected.Replace(".0f", "f");     //this is kinda a hack, but TypeData.FormatForKey is going to add the .0, so we need to remove it
                expected = expected.Replace(".0D", "D");     //this is kinda a hack, but TypeData.FormatForKey is going to add the .0, so we need to remove it
                bool match = uriFound.Equals(expected, StringComparison.InvariantCultureIgnoreCase);
                if (!match)
                {
                    AstoriaTestLog.WriteLineIgnore("Link did not match key, expected '" + expected + "'");
                }
                return(match);

            default:
                QueryNode setQuery = ContainmentUtil.BuildCanonicalQuery(keyExpected, true);

                Workspace w      = keyExpected.ResourceContainer.Workspace;
                string    setUri = builder.Build(setQuery);

                string keySegment = uriFound.Substring(setUri.Length);

                string expectedKeySegment = "(" + UriQueryBuilder.CreateKeyString(keyExpected, false) + ")";

                if (keySegment.Equals(expectedKeySegment, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(true);
                }

                // if not explicitely equal, need to make sure its not due to a re-ordering of the properties
                //
                List <KeyValuePair <string, int> > predicateLocations = new List <KeyValuePair <string, int> >();
                for (int i = 0; i < keyExpected.Values.Length; i++)
                {
                    string predicate = builder.CreateKeyStringPair(keyExpected.Properties[i].Name, keyExpected.Values[i].ClrValue);
                    int    offset    = keySegment.IndexOf(predicate);
                    if (offset < 0)
                    {
                        return(false);
                    }

                    predicateLocations.Add(new KeyValuePair <string, int>(predicate, offset));
                }

                predicateLocations.Sort(delegate(KeyValuePair <string, int> pair1, KeyValuePair <string, int> pair2)
                {
                    return(pair1.Value.CompareTo(pair2.Value));
                });

                expectedKeySegment = "(" + String.Join(",", predicateLocations.Select(pair => pair.Key).ToArray()) + ")";

                return(keySegment.Equals(expectedKeySegment, StringComparison.InvariantCultureIgnoreCase));
            }
        }