Exemplo n.º 1
0
 public ODataEntryMetadataContextTest()
 {
     this.entry = new ODataResource {
         TypeName = ActualEntityType.FullName()
     };
     this.typeContext = new TestFeedAndEntryTypeContext();
     this.entryMetadataContextWithoutModel = ODataResourceMetadataContext.Create(this.entry, this.typeContext, new ODataResourceSerializationInfo(), /*actualEntityType*/ null, new TestMetadataContext(), new SelectedPropertiesNode(SelectedPropertiesNode.SelectionType.EntireSubtree), null);
     this.entryMetadataContextWithModel    = ODataResourceMetadataContext.Create(this.entry, this.typeContext, /*serializationInfo*/ null, ActualEntityType, new TestMetadataContext(), new SelectedPropertiesNode(SelectedPropertiesNode.SelectionType.EntireSubtree), null);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Generates an open entity set containing entities with different amounts of primitive open properties.
        /// </summary>
        /// <param name="model">The entity model. The method will modify the model and call Fixup().</param>
        /// <param name="withTypeNames">True if the payloads should specify type names.</param>
        /// <param name="primitiveValueFilter">A function for excluding primitives which are unsupported for open properties.</param>
        /// <returns>The open entity set containing entities with primitive open properties.</returns>
        public static ODataPayloadElement CreateOpenEntitySetInstance(EdmModel model, bool withTypeNames, Func <PrimitiveValue, bool> primitiveValueFilter)
        {
            ExceptionUtilities.CheckArgumentNotNull(model, "model");
            ExceptionUtilities.CheckArgumentNotNull(primitiveValueFilter, "primitiveValueFilter");

            var edmEntityType = new EdmEntityType("TestModel", "OpenEntityType", baseType: null, isAbstract: false, isOpen: true);

            model.AddElement(edmEntityType);
            edmEntityType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String, true);

            var container = new EdmEntityContainer("TestModel", "DefaultContainer");

            container.AddEntitySet("OpenEntityTypes", edmEntityType);
            model.AddElement(container);

            var feed = PayloadBuilder.EntitySet().WithTypeAnnotation(edmEntityType);

            string entityTypeName = withTypeNames ? edmEntityType.FullName() : null;

            var primitiveValues = TestValues.CreatePrimitiveValuesWithMetadata(true).Where(p => primitiveValueFilter(p));

            int idx = 0;

            for (int i = 0; i <= primitiveValues.Count(); ++i)
            {
                var entityInstance = PayloadBuilder.Entity(entityTypeName).PrimitiveProperty("Name", Guid.NewGuid().ToString());
                foreach (var property in primitiveValues.Take(i))
                {
                    entityInstance.PrimitiveProperty("Property" + (idx++), property.ClrValue);
                }
                feed.Add(entityInstance);
            }
            return(feed);
        }
Exemplo n.º 3
0
 public void TestInit()
 {
     this.entry = new ODataEntry {
         TypeName = ActualEntityType.FullName()
     };
     this.typeContext = new TestFeedAndEntryTypeContext();
     this.entryMetadataContextWithoutModel = ODataEntryMetadataContext.Create(this.entry, this.typeContext, new ODataFeedAndEntrySerializationInfo(), /*actualEntityType*/ null, new TestMetadataContext(), SelectedPropertiesNode.EntireSubtree);
     this.entryMetadataContextWithModel    = ODataEntryMetadataContext.Create(this.entry, this.typeContext, /*serializationInfo*/ null, ActualEntityType, new TestMetadataContext(), SelectedPropertiesNode.EntireSubtree);
 }
Exemplo n.º 4
0
 public IEdmSchemaType FindDeclaredType(string qualifiedName)
 {
     if (qualifiedName == _entityType.FullName())
     {
         return(_entityType);
     }
     else
     {
         return(_source.FindDeclaredType(qualifiedName));
     }
 }
Exemplo n.º 5
0
        private OeEntryFactory(Type clrEntityType, IEdmEntitySetBase entitySet, OePropertyAccessor[] accessors)
        {
            Array.Sort(accessors, AccessorByNameComparer.Instance);
            ClrEntityType = clrEntityType;
            EntitySet     = entitySet;
            _allAccessors = accessors;
            Accessors     = GetAccessorsWithoutSkiptoken(accessors);

            EdmEntityType   = entitySet.EntityType();
            NavigationLinks = Array.Empty <OeEntryFactory>();
            _typeName       = EdmEntityType.FullName();
        }
Exemplo n.º 6
0
        public OeEntryFactory(IEdmEntitySetBase entitySet, OePropertyAccessor[] accessors, OePropertyAccessor[] skipTokenAccessors)
        {
            Array.Sort(accessors, AccessorByNameComparer.Instance);
            EntitySet          = entitySet;
            _allAccessors      = accessors;
            Accessors          = GetAccessorsWithoutSkiptoken(accessors);
            SkipTokenAccessors = skipTokenAccessors;

            EdmEntityType   = entitySet.EntityType();
            NavigationLinks = Array.Empty <OeEntryFactory>();
            _typeName       = EdmEntityType.FullName();

            IsTuple = accessors.Length == 0 ? false : OeExpressionHelper.IsTupleType(accessors[0].PropertyExpression.Expression.Type);
        }
Exemplo n.º 7
0
        public OeEntryFactory(
            IEdmEntitySetBase entitySet,
            OePropertyAccessor[] accessors,
            OePropertyAccessor[]?skipTokenAccessors)
        {
            Array.Sort(accessors, AccessorByNameComparer.Instance);
            EntitySet          = entitySet;
            _allAccessors      = accessors;
            Accessors          = GetAccessorsWithoutSkiptoken(accessors);
            SkipTokenAccessors = skipTokenAccessors ?? Array.Empty <OePropertyAccessor>();

            EdmEntityType   = entitySet.EntityType();
            NavigationLinks = Array.Empty <OeNavigationEntryFactory>();
            _typeName       = EdmEntityType.FullName();

            if (accessors.Length != 0 && accessors[0].PropertyExpression is MemberExpression propertyExpression)
            {
                IsTuple = OeExpressionHelper.IsTupleType(propertyExpression.Expression !.Type);
            }
        }
Exemplo n.º 8
0
        /// <summary>Adds a new entity type (without any properties).</summary>
        /// <param name="name">The name of the type.</param>
        /// <param name="instanceType">The instance type or null if this should be untyped resource.</param>
        /// <param name="baseType">The base type.</param>
        /// <param name="isAbstract">If the type should be abstract.</param>
        /// <param name="nameSpace">The namespace of the entity type</param>
        /// <returns>The newly created entity type.</returns>
        public IEdmEntityType AddEntityType(string name, Type instanceType, IEdmEntityType baseType, bool isAbstract, string nameSpace, bool isOpen = false)
        {
            EdmEntityType entityType = new EdmEntityType(
                this.namespaceName,
                name,
                baseType,
                isAbstract,
                isOpen);

            this.entityTypes.Add(entityType.FullName(), entityType);

            if (entityType.BaseType != null)
            {
                List <IEdmStructuredType> derivedTypes;
                if (!this.derivedTypeMappings.TryGetValue(entityType.BaseType, out derivedTypes))
                {
                    derivedTypes = new List <IEdmStructuredType>();
                    this.derivedTypeMappings[entityType.BaseType] = derivedTypes;
                }
            }

            return(entityType);
        }
Exemplo n.º 9
0
        public void EdmPathExpressionWithInvalidNonInheritedTypeCastSegmentShouldError()
        {
            EdmEntityType entityType      = new EdmEntityType("ds.s", "entityType");
            EdmEntityType otherEntityType = new EdmEntityType("ds.s", "otherEntityType");
            EdmModel      model           = new EdmModel();

            model.AddElement(entityType);
            model.AddElement(otherEntityType);

            EdmFunction function = new EdmFunction("ns", "GetStuff", new EdmEntityTypeReference(entityType, true), true /*isBound*/, new EdmPathExpression("bindingEntity/ds.s.otherEntityType"), false);

            function.AddParameter("bindingEntity", new EdmEntityTypeReference(entityType, false));
            model.AddElement(function);

            ValidateErrorInList(
                model,
                function,
                EdmErrorCode.InvalidPathInvalidTypeCastSegment,
                Strings.EdmModel_Validator_Semantic_InvalidEntitySetPathInvalidTypeCastSegment("EntitySetPath", "bindingEntity/ds.s.otherEntityType", entityType.FullName(), otherEntityType.FullName()));
        }
Exemplo n.º 10
0
        public void WriterStreamPropertiesTests()
        {
            Uri baseUri             = new Uri("http://www.odata.org/", UriKind.Absolute);
            Uri relativeReadLinkUri = new Uri("readlink", UriKind.RelativeOrAbsolute);
            Uri relativeEditLinkUri = new Uri("editlink", UriKind.RelativeOrAbsolute);
            Uri absoluteReadLinkUri = new Uri(baseUri, relativeReadLinkUri.OriginalString);
            Uri absoluteEditLinkUri = new Uri(baseUri, relativeEditLinkUri.OriginalString);

            string contentType        = "application/binary";
            string etag               = "\"myetagvalue\"";
            string streamPropertyName = "stream1";

            var namedStreamProperties = new[]
            {
                // with only read link
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        ReadLink = relativeReadLinkUri
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        ReadLink = relativeReadLinkUri, ContentType = contentType
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        ReadLink = absoluteReadLinkUri
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        ReadLink = absoluteReadLinkUri, ContentType = contentType
                    }
                },
                // with only edit link
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        EditLink = relativeEditLinkUri
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        EditLink = relativeEditLinkUri, ContentType = contentType
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        EditLink = relativeEditLinkUri, ContentType = contentType, ETag = etag
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        EditLink = absoluteEditLinkUri
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        EditLink = absoluteEditLinkUri, ContentType = contentType
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        EditLink = absoluteEditLinkUri, ContentType = contentType, ETag = etag
                    }
                },
                // with both edit and read link
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        ReadLink = relativeReadLinkUri, EditLink = relativeEditLinkUri
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        ReadLink = relativeReadLinkUri, EditLink = relativeEditLinkUri, ContentType = contentType
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        ReadLink = relativeReadLinkUri, EditLink = relativeEditLinkUri, ContentType = contentType, ETag = etag
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        ReadLink = absoluteReadLinkUri, EditLink = relativeEditLinkUri
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        ReadLink = absoluteReadLinkUri, EditLink = relativeEditLinkUri, ContentType = contentType
                    }
                },
                new ODataProperty {
                    Name = streamPropertyName, Value = new ODataStreamReferenceValue {
                        ReadLink = absoluteReadLinkUri, EditLink = relativeEditLinkUri, ContentType = contentType, ETag = etag
                    }
                },
            };

            var testCases = namedStreamProperties.Select(property =>
            {
                var propertyName         = property.Name;
                var streamReferenceValue = (ODataStreamReferenceValue)property.Value;
                return(new StreamPropertyTestCase
                {
                    NamedStreamProperty = property,
                    GetExpectedAtomPayload = (testConfiguration) =>
                    {
                        return
                        (streamReferenceValue.ReadLink == null
                                    ? string.Empty
                                    : (
                             "<link rel=\"http://docs.oasis-open.org/odata/ns/mediaresource/" + property.Name + "\" " +
                             (streamReferenceValue.ContentType == null ? string.Empty : "type=\"" + streamReferenceValue.ContentType + "\" ") +
                             "title=\"" + property.Name + "\" " +
                             "href=\"" + (((ODataStreamReferenceValue)property.Value).ReadLink.IsAbsoluteUri ? absoluteReadLinkUri.OriginalString : relativeReadLinkUri.OriginalString) + "\" " +
                             "xmlns=\"" + TestAtomConstants.AtomNamespace + "\" />")) +

                        (streamReferenceValue.EditLink == null
                                    ? string.Empty
                                    : (
                             "<link rel=\"http://docs.oasis-open.org/odata/ns/edit-media/" + property.Name + "\" " +
                             (streamReferenceValue.ContentType == null ? string.Empty : "type=\"" + streamReferenceValue.ContentType + "\" ") +
                             "title=\"" + property.Name + "\" " +
                             "href=\"" + (((ODataStreamReferenceValue)property.Value).EditLink.IsAbsoluteUri ? absoluteEditLinkUri.OriginalString : relativeEditLinkUri.OriginalString) + "\" " +
                             (streamReferenceValue.ETag == null ? string.Empty : "m:etag=\"" + streamReferenceValue.ETag.Replace("\"", "&quot;") + "\" xmlns:m=\"" + TestAtomConstants.ODataMetadataNamespace + "\" ") +
                             "xmlns=\"" + TestAtomConstants.AtomNamespace + "\" />"));
                    },
                    GetExpectedJsonLightPayload = (testConfiguration) =>
                    {
                        return JsonLightWriterUtils.CombineProperties(
                            (streamReferenceValue.EditLink == null ? string.Empty : ("\"" + JsonLightUtils.GetPropertyAnnotationName(propertyName, JsonLightConstants.ODataMediaEditLinkAnnotationName) + "\":\"" + absoluteEditLinkUri.OriginalString + "\"")),
                            (streamReferenceValue.ReadLink == null ? string.Empty : ("\"" + JsonLightUtils.GetPropertyAnnotationName(propertyName, JsonLightConstants.ODataMediaReadLinkAnnotationName) + "\":\"" + absoluteReadLinkUri.OriginalString + "\"")),
                            (streamReferenceValue.ContentType == null ? string.Empty : ("\"" + JsonLightUtils.GetPropertyAnnotationName(propertyName, JsonLightConstants.ODataMediaContentTypeAnnotationName) + "\":\"" + streamReferenceValue.ContentType + "\"")),
                            (streamReferenceValue.ETag == null ? string.Empty : ("\"" + JsonLightUtils.GetPropertyAnnotationName(propertyName, JsonLightConstants.ODataMediaETagAnnotationName) + "\":\"" + streamReferenceValue.ETag.Replace("\"", "\\\"") + "\"")));
                    },
                });
            });

            var testDescriptors = testCases.SelectMany(testCase =>
            {
                EdmModel model = new EdmModel();

                EdmEntityType edmEntityType = new EdmEntityType("TestModel", "StreamPropertyEntityType");
                EdmStructuralProperty edmStructuralProperty = edmEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false);
                edmEntityType.AddKeys(new IEdmStructuralProperty[] { edmStructuralProperty });
                model.AddElement(edmEntityType);

                EdmEntityContainer edmEntityContainer = new EdmEntityContainer("TestModel", "DefaultContainer");
                model.AddElement(edmEntityContainer);

                EdmEntitySet edmEntitySet = new EdmEntitySet(edmEntityContainer, "StreamPropertyEntitySet", edmEntityType);
                edmEntityContainer.AddElement(edmEntitySet);

                ODataEntry entry = new ODataEntry()
                {
                    Id       = ObjectModelUtils.DefaultEntryId,
                    ReadLink = ObjectModelUtils.DefaultEntryReadLink,
                    TypeName = edmEntityType.FullName()
                };

                var streamReference = (ODataStreamReferenceValue)testCase.NamedStreamProperty.Value;
                bool needBaseUri    = (streamReference.ReadLink != null && !streamReference.ReadLink.IsAbsoluteUri) || (streamReference.EditLink != null && !streamReference.EditLink.IsAbsoluteUri);
                entry.Properties    = new ODataProperty[] { testCase.NamedStreamProperty };

                var resultDescriptor = new PayloadWriterTestDescriptor <ODataItem>(
                    this.Settings,
                    entry,
                    (testConfiguration) =>
                {
                    if (testConfiguration.Format == ODataFormat.Atom)
                    {
                        return(new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
                        {
                            Xml = "<NamedStream>" + testCase.GetExpectedAtomPayload(testConfiguration) + "</NamedStream>",
                            FragmentExtractor = result => result,
                        });
                    }
                    else if (testConfiguration.Format == ODataFormat.Json)
                    {
                        return(new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
                        {
                            Json = string.Join(
                                "$(NL)",
                                "{",
                                testCase.GetExpectedJsonLightPayload(testConfiguration),
                                "}"),
                            FragmentExtractor = result => result.RemoveAllAnnotations(true),
                        });
                    }
                    else
                    {
                        throw new NotSupportedException("Unsupported ODataFormat found: " + testConfiguration.Format.ToString());
                    }
                })
                {
                    Model = model,
                    PayloadEdmElementContainer = edmEntityContainer,
                    PayloadEdmElementType      = edmEntityType,
                };

                var resultTestCases = new List <StreamPropertyTestDescriptor>();
                if (needBaseUri)
                {
                    resultTestCases.Add(new StreamPropertyTestDescriptor {
                        BaseUri = baseUri, TestDescriptor = resultDescriptor
                    });
                }
                else
                {
                    resultTestCases.Add(new StreamPropertyTestDescriptor {
                        BaseUri = null, TestDescriptor = resultDescriptor
                    });
                    resultTestCases.Add(new StreamPropertyTestDescriptor {
                        BaseUri = baseUri, TestDescriptor = resultDescriptor
                    });
                    resultTestCases.Add(new StreamPropertyTestDescriptor {
                        BaseUri = new Uri("http://mybaseuri/", UriKind.Absolute), TestDescriptor = resultDescriptor
                    });
                }

                return(resultTestCases);
            });

            var testDescriptorBaseUriPairSet = testDescriptors.SelectMany(descriptor =>
                                                                          WriterPayloads.NamedStreamPayloads(descriptor.TestDescriptor).Select(namedStreamPayload =>
                                                                                                                                               new Tuple <PayloadWriterTestDescriptor <ODataItem>, Uri>(namedStreamPayload, descriptor.BaseUri)));

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptorBaseUriPairSet,
                this.WriterTestConfigurationProvider.ExplicitFormatConfigurationsWithIndent,
                (testDescriptorBaseUriPair, testConfiguration) =>
            {
                var testDescriptor = testDescriptorBaseUriPair.Item1;

                if (testDescriptor.IsGeneratedPayload && testConfiguration.Format == ODataFormat.Json)
                {
                    return;
                }

                ODataMessageWriterSettings settings = testConfiguration.MessageWriterSettings.Clone();
                settings.PayloadBaseUri             = testDescriptorBaseUriPair.Item2;
                settings.SetServiceDocumentUri(ServiceDocumentUri);

                WriterTestConfiguration config =
                    new WriterTestConfiguration(testConfiguration.Format, settings, testConfiguration.IsRequest, testConfiguration.Synchronous);

                if (testConfiguration.IsRequest)
                {
                    ODataEntry payloadEntry           = (ODataEntry)testDescriptor.PayloadItems[0];
                    ODataProperty firstStreamProperty = payloadEntry.Properties.Where(p => p.Value is ODataStreamReferenceValue).FirstOrDefault();
                    this.Assert.IsNotNull(firstStreamProperty, "firstStreamProperty != null");

                    testDescriptor = new PayloadWriterTestDescriptor <ODataItem>(testDescriptor)
                    {
                        ExpectedResultCallback = tc => new WriterTestExpectedResults(this.Settings.ExpectedResultSettings)
                        {
                            ExpectedException2 = ODataExpectedExceptions.ODataException("WriterValidationUtils_StreamPropertyInRequest", firstStreamProperty.Name)
                        }
                    };
                }

                TestWriterUtils.WriteAndVerifyODataPayload(testDescriptor, config, this.Assert, this.Logger);
            });
        }