public void GetFirstNonTypeCastSegment_WorksForSelectPathWithFirstNonTypeSegmentAtMiddle()
        {
            // Arrange
            EdmEntityType          entityType = new EdmEntityType("NS", "Customer");
            IEdmStructuralProperty property   = entityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.String);

            ODataPathSegment firstTypeSegment      = new TypeSegment(entityType, null);
            ODataPathSegment secondTypeSegment     = new TypeSegment(entityType, null);
            ODataPathSegment firstPropertySegment  = new PropertySegment(property);
            ODataPathSegment secondPropertySegment = new PropertySegment(property);
            ODataSelectPath  selectPath            = new ODataSelectPath(
                firstTypeSegment,
                secondTypeSegment,
                firstPropertySegment, // here
                secondPropertySegment);

            // Act
            IList <ODataPathSegment> remainingSegments;
            ODataPathSegment         firstNonTypeSegment = selectPath.GetFirstNonTypeCastSegment(out remainingSegments);

            // Assert
            Assert.NotNull(firstNonTypeSegment);
            Assert.Same(firstPropertySegment, firstNonTypeSegment);

            Assert.NotNull(remainingSegments);
            Assert.Same(secondPropertySegment, Assert.Single(remainingSegments));
        }
        public void GetFirstNonTypeCastSegment_SelectPath_ThrowsArgumentNull()
        {
            // Arrange & Act
            ODataSelectPath          selectPath = null;
            IList <ODataPathSegment> remainingSegments;

            // Assert
            ExceptionAssert.ThrowsArgumentNull(() => selectPath.GetFirstNonTypeCastSegment(out remainingSegments), "selectPath");
        }
        public void GetFirstNonTypeCastSegment_ThrowsForUnsupportedMiddleSegmentInSelectPath()
        {
            // Arrange & Act
            ODataSelectPath          selectPath = new ODataSelectPath(new DynamicPathSegment("abc"), new DynamicPathSegment("xyz"));
            IList <ODataPathSegment> remainingSegments;

            // Assert
            ExceptionAssert.Throws <ODataException>(() => selectPath.GetFirstNonTypeCastSegment(out remainingSegments),
                                                    String.Format(SRResources.InvalidSegmentInSelectExpandPath, "DynamicPathSegment"));
        }
        public void GetFirstNonTypeCastSegment_ThrowsForUnsupportedLastSegmentInSelectPath()
        {
            // Arrange & Act
            IEdmType                 stringType = EdmCoreModel.Instance.GetString(false).Definition;
            ODataSelectPath          selectPath = new ODataSelectPath(new TypeSegment(stringType, null), new TypeSegment(stringType, null));
            IList <ODataPathSegment> remainingSegments;

            // Assert
            ExceptionAssert.Throws <ODataException>(() => selectPath.GetFirstNonTypeCastSegment(out remainingSegments),
                                                    String.Format(SRResources.InvalidLastSegmentInSelectExpandPath, "TypeSegment"));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Build the $select item, it maybe $select=complex/abc, $select=abc, $select=nav, etc.
        /// </summary>
        /// <param name="pathSelectItem">The expanded reference select item.</param>
        /// <param name="currentLevelPropertiesInclude">The current properties to include at current level.</param>
        /// <param name="structuralTypeInfo">The structural type properties.</param>
        private void BuildSelectItem(PathSelectItem pathSelectItem,
                                     IDictionary <IEdmStructuralProperty, SelectExpandIncludedProperty> currentLevelPropertiesInclude,
                                     EdmStructuralTypeInfo structuralTypeInfo)
        {
            Contract.Assert(pathSelectItem != null && pathSelectItem.SelectedPath != null);
            Contract.Assert(currentLevelPropertiesInclude != null);
            Contract.Assert(structuralTypeInfo != null);

            // Verify and process the $select=abc/xyz/....
            ODataSelectPath          selectPath = pathSelectItem.SelectedPath;
            IList <ODataPathSegment> remainingSegments;
            ODataPathSegment         segment = selectPath.GetFirstNonTypeCastSegment(out remainingSegments);

            PropertySegment firstPropertySegment = segment as PropertySegment;

            if (firstPropertySegment != null)
            {
                if (structuralTypeInfo.IsStructuralPropertyDefined(firstPropertySegment.Property))
                {
                    // $select=abc/xyz/...
                    SelectExpandIncludedProperty newPropertySelectItem;
                    if (!currentLevelPropertiesInclude.TryGetValue(firstPropertySegment.Property, out newPropertySelectItem))
                    {
                        newPropertySelectItem = new SelectExpandIncludedProperty(firstPropertySegment);
                        currentLevelPropertiesInclude[firstPropertySegment.Property] = newPropertySelectItem;
                    }

                    newPropertySelectItem.AddSubSelectItem(remainingSegments, pathSelectItem);
                }

                return;
            }

            // If the first segment is not a property segment,
            // that segment must be the last segment, so the remainging segments should be null.
            Contract.Assert(remainingSegments == null);

            NavigationPropertySegment navigationSegment = segment as NavigationPropertySegment;

            if (navigationSegment != null)
            {
                // for example: $select=NavigationProperty or $select=NS.VipCustomer/VipNav
                if (structuralTypeInfo.IsNavigationPropertyDefined(navigationSegment.NavigationProperty))
                {
                    if (SelectedNavigationProperties == null)
                    {
                        SelectedNavigationProperties = new HashSet <IEdmNavigationProperty>();
                    }

                    SelectedNavigationProperties.Add(navigationSegment.NavigationProperty);
                }

                return;
            }

            OperationSegment operationSegment = segment as OperationSegment;

            if (operationSegment != null)
            {
                // for example: $select=NS.Operation, or, $select=NS.VipCustomer/NS.Operation
                AddOperations(operationSegment, structuralTypeInfo.AllActions, structuralTypeInfo.AllFunctions);
                return;
            }

            DynamicPathSegment dynamicPathSegment = segment as DynamicPathSegment;

            if (dynamicPathSegment != null)
            {
                if (SelectedDynamicProperties == null)
                {
                    SelectedDynamicProperties = new HashSet <string>();
                }

                SelectedDynamicProperties.Add(dynamicPathSegment.Identifier);
                return;
            }

            // In fact, we should never be here, because it's verified above
            throw new ODataException(Error.Format(SRResources.SelectionTypeNotSupported, segment.GetType().Name));
        }