Exemple #1
0
        /// <summary>
        /// Verifies the default stream.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="queryEntityValue">The query entity value.</param>
        /// <param name="ed">The entity descriptor.</param>
        /// <param name="continuation">The stream descriptor continuation.</param>
        private void VerifyDefaultStream(DataServiceContext dataContext, QueryStructuralValue queryEntityValue, EntityDescriptor ed, IAsyncContinuation continuation)
        {
            var expectedStreamValue = queryEntityValue.GetDefaultStreamValue();

            this.VerifyStreamDescriptorValues(expectedStreamValue, null, null, ed.StreamETag, ed.EditStreamUri, ed.ReadStreamUri);

            var expectedReadStreamUri = GetExpectedReadStreamUri(expectedStreamValue);

            Uri readStreamUri = dataContext.GetReadStreamUri(ed.Entity);

            this.Assert.AreEqual(expectedReadStreamUri, readStreamUri, "Read stream uri did not match for default stream");

            dataContext.GetReadStream(
                continuation,
                this.isAsynchronous,
                ed.Entity,
                null,
                new DataServiceRequestArgs()
            {
            },
                response =>
            {
                UpdateStreamValueFromHeaders(expectedStreamValue, response);

                this.VerifyStreamDescriptorValues(expectedStreamValue, null, null, ed.StreamETag, ed.EditStreamUri, ed.ReadStreamUri);

                // skip this verification when using payload driven verification since we don't have the expected content for streams
                if (!this.DataProviderSettings.UsePayloadDrivenVerification)
                {
                    this.Assert.IsTrue(this.VerifyStreams(expectedStreamValue, response), "Failed to compare the default stream");
                }

                continuation.Continue();
            });
        }
        private string CalculateExpectedETagForEntityOrStream(ODataUri uri, QueryStructuralValue entity)
        {
            if (uri.IsMediaResource())
            {
                return(entity.GetDefaultStreamValue().GetExpectedETag());
            }

            if (uri.IsNamedStream())
            {
                var streamSegment = uri.Segments.OfType <NamedStreamSegment>().Last();
                return(entity.GetStreamValue(streamSegment.Name).GetExpectedETag());
            }

            return(this.LiteralConverter.ConstructWeakETag(entity));
        }
Exemple #3
0
        private void VerifyStoreData(ODataRequest request, ODataResponse response, QueryStructuralValue storeValue)
        {
            string contentType;

            ExceptionUtilities.Assert(request.Headers.TryGetValue(HttpHeaders.ContentType, out contentType), "Could not get Content-Type header from request");

            if (request.Uri.IsNamedStream())
            {
                string streamName  = request.Uri.Segments.OfType <NamedStreamSegment>().Last().Name;
                var    streamValue = storeValue.GetStreamValue(streamName);
                this.VerifyStream(streamValue, contentType, request, response);
                return;
            }

            bool isInsert        = request.GetEffectiveVerb() == HttpVerb.Post;
            bool isMediaResource = request.Uri.IsMediaResource();

            if (isInsert)
            {
                EntitySet expectedEntitySet;
                if (request.Uri.TryGetExpectedEntitySet(out expectedEntitySet))
                {
                    isMediaResource = expectedEntitySet.EntityType.HasStream();
                }
            }

            if (isMediaResource)
            {
                var streamValue = storeValue.GetDefaultStreamValue();
                this.VerifyStream(streamValue, contentType, request, response);
            }
            else
            {
                if (isInsert)
                {
                    this.VerifyTypeNameForInsert(request, response, storeValue);
                }

                var formatStrategy    = this.FormatSelector.GetStrategy(contentType, request.Uri);
                var primitiveComparer = formatStrategy.GetPrimitiveComparer();

                // TODO: verify relationships
                // TODO: verify PUT vs PATCH semantics
                this.VerifyPropertyValues(request, primitiveComparer, storeValue);
            }
        }
        private string CalculateExpectedETagForEntityOrStream(ODataUri uri, QueryStructuralValue entity)
        {
            if (uri.IsMediaResource())
            {
                return entity.GetDefaultStreamValue().GetExpectedETag();
            }

            if (uri.IsNamedStream())
            {
                var streamSegment = uri.Segments.OfType<NamedStreamSegment>().Last();
                return entity.GetStreamValue(streamSegment.Name).GetExpectedETag();
            }

            return this.LiteralConverter.ConstructWeakETag(entity);
        }
            /// <summary>
            /// Verify ETag and Id values of given payload element.
            /// </summary>
            /// <param name="entityType">type of the element</param>
            /// <param name="payloadElement">payload element to verify</param>
            /// <param name="value">expected values</param>
            private void VerifyEntityMetadata(QueryEntityType entityType, EntityInstance payloadElement, QueryStructuralValue value)
            {
                if (this.parent.ExpectedPayloadOptions.HasFlag(ODataPayloadOptions.IncludeETags))
                {
                    if (entityType.EntityType.HasETag())
                    {
                        // TODO: are ETags always based on property values?
                        var expectedETag = this.parent.LiteralConverter.ConstructWeakETag(value);
                        this.parent.Assert.AreEqual(expectedETag, payloadElement.ETag, "Entity's ETag did not match");
                    }
                    else
                    {
                        this.parent.Assert.IsNull(payloadElement.ETag, "Entity should not have had an ETag");
                    }
                }

                if (this.parent.ExpectedPayloadOptions.HasFlag(ODataPayloadOptions.IncludeEntityIdentifier))
                {
                    this.parent.Assert.IsNotNull(payloadElement.Id, "Entity's ID unexpectedly null");

                    if (this.parent.ExpectedPayloadOptions.HasFlag(ODataPayloadOptions.UseConventionBasedIdentifiers))
                    {
                        var expectedId = this.parent.LinkGenerator.GenerateEntityId(value);

                        this.parent.Assert.AreEqual(expectedId, payloadElement.Id, "Entity's ID did not match");
                    }
                }
                else
                {
                    this.parent.Assert.IsNull(payloadElement.Id, "Entity's ID unexpectedly non-null");
                }

                if (this.parent.ExpectedPayloadOptions.HasFlag(ODataPayloadOptions.UseConventionBasedLinks))
                {
                    var expectedEditLink = this.parent.LinkGenerator.GenerateEntityEditLink(value);
                    this.CompareUri(expectedEditLink, payloadElement.EditLink, "Entity's edit-link did not match");
                }

                if (entityType.EntityType.GetBaseTypesAndSelf().Any(t => t.HasStream()))
                {
                    using (this.parent.Assert.WithMessage("Entity's stream metadata did not match"))
                    {
                        var defaultStreamValue = value.GetDefaultStreamValue();
                        this.parent.Assert.AreEqual(defaultStreamValue.ContentType, payloadElement.StreamContentType, "Content type did not match");

                        this.CompareStreamETag(defaultStreamValue, payloadElement.StreamETag);

                        if (this.parent.ExpectedPayloadOptions.HasFlag(ODataPayloadOptions.IncludeMediaResourceEditLinks))
                        {
                            this.CompareUri(defaultStreamValue.EditLink, payloadElement.StreamEditLink, "Edit link did not match");
                        }
                        else
                        {
                            this.parent.Assert.IsNull(payloadElement.StreamEditLink, "Edit link unexpectedly non-null");
                        }

                        if (this.parent.ExpectedPayloadOptions.HasFlag(ODataPayloadOptions.IncludeMediaResourceSourceLinks))
                        {
                            this.CompareUri(defaultStreamValue.SelfLink, payloadElement.StreamSourceLink, "Source link did not match");
                        }
                        else
                        {
                            this.parent.Assert.IsNull(payloadElement.StreamSourceLink, "Source link unexpectedly non-null");
                        }
                    }
                }
            }
        private QueryStructuralValue CloneStructuralValue(Dictionary<QueryStructuralValue, QueryStructuralValue> identityMap, QueryStructuralValue qsv)
        {
            if (qsv.IsNull)
            {
                return qsv;
            }

            QueryStructuralValue clonedValue;

            if (identityMap.TryGetValue(qsv, out clonedValue))
            {
                return clonedValue;
            }

            clonedValue = qsv.Type.CreateNewInstance();
            identityMap.Add(qsv, clonedValue);
      
            foreach (var m in qsv.Type.Properties)
            {
                // copy scalar properties
                if (m.PropertyType is QueryScalarType)
                {
                    clonedValue.SetValue(m.Name, qsv.GetScalarValue(m.Name));
                    continue;
                }

                // copy stream properties
                if (m.PropertyType is AstoriaQueryStreamType)
                {
                    if (m.Name.Contains("DefaultStream"))
                    {
                        clonedValue.SetDefaultStreamValue(qsv.GetDefaultStreamValue());
                    }
                    else
                    {
                        clonedValue.SetStreamValue(m.Name, qsv.GetStreamValue(m.Name));
                    }

                    continue;
                }

                var qst = m.PropertyType as QueryStructuralType;
                if (m.PropertyType is QueryStructuralType)
                {
                    if (!qst.IsValueType)
                    {
                        // skip reference types, clone everything else
                        continue;
                    }

                    clonedValue.SetValue(m.Name, this.CloneStructuralValue(identityMap, qsv.GetStructuralValue(m.Name)));
                }

                var qct = m.PropertyType as QueryCollectionType;
                if (qct != null)
                {
                    var elementStructuralType = qct.ElementType as QueryStructuralType;
                    if (elementStructuralType != null)
                    {
                        if (!elementStructuralType.IsValueType)
                        {
                            // skip collections of reference types, clone everything else
                            continue;
                        }
                    }

                    clonedValue.SetValue(m.Name, this.CloneCollectionValue(identityMap, qsv.GetCollectionValue(m.Name)));
                }
            }

            return clonedValue;
        }
        private void VerifyStoreData(ODataRequest request, ODataResponse response, QueryStructuralValue storeValue)
        {
            string contentType;
            ExceptionUtilities.Assert(request.Headers.TryGetValue(HttpHeaders.ContentType, out contentType), "Could not get Content-Type header from request");

            if (request.Uri.IsNamedStream())
            {
                string streamName = request.Uri.Segments.OfType<NamedStreamSegment>().Last().Name;
                var streamValue = storeValue.GetStreamValue(streamName);
                this.VerifyStream(streamValue, contentType, request, response);
                return;
            }

            bool isInsert = request.GetEffectiveVerb() == HttpVerb.Post;
            bool isMediaResource = request.Uri.IsMediaResource();
            if (isInsert)
            {
                EntitySet expectedEntitySet;
                if (request.Uri.TryGetExpectedEntitySet(out expectedEntitySet))
                {
                    isMediaResource = expectedEntitySet.EntityType.HasStream();
                }
            }

            if (isMediaResource)
            {
                var streamValue = storeValue.GetDefaultStreamValue();
                this.VerifyStream(streamValue, contentType, request, response);
            }
            else
            {
                if (isInsert)
                {
                    this.VerifyTypeNameForInsert(request, response, storeValue);
                }

                var formatStrategy = this.FormatSelector.GetStrategy(contentType, request.Uri);
                var primitiveComparer = formatStrategy.GetPrimitiveComparer();

                // TODO: verify relationships
                // TODO: verify PUT vs PATCH semantics
                this.VerifyPropertyValues(request, primitiveComparer, storeValue);
            }
        }