private static IEdmModel GetEdmModelFromEdmxStream(Stream s)
        {
            // Update the EdmModel on the workspace
            IEdmModel model = null;
            IEnumerable <EdmError> errors = null;

#if !WINDOWS_PHONE && !SILVERLIGHT
            using (var xmlTextReader = XmlTextReader.Create(s, new XmlReaderSettings()))
            {
#else
            using (var xmlTextReader = XmlReader.Create(s))
            {
#endif
                ExceptionUtilities.Assert(CsdlReader.TryParse(xmlTextReader, out model, out errors), "Cannot read csdl model");
                ExceptionUtilities.Assert(!errors.Any(), "Errors on parsing csdl");
            }

            return(model);
        }
        private QueryValue EvaluateJoinExpression(LinqJoinExpressionBase expression, bool isGroupJoin)
        {
            var outer = this.EvaluateCollection(expression.Outer);
            var inner = this.EvaluateCollection(expression.Inner);

            var outerKeys = this.EvaluateKeysForJoin(outer, expression.OuterKeySelector);
            var innerKeys = this.EvaluateKeysForJoin(inner, expression.InnerKeySelector);

            // Dictionary: inner key -> a list of inner values with this key
            Dictionary <QueryScalarValue, IList <QueryValue> > innerKeyLookup = this.BuildInnerKeyLookup(inner, innerKeys);

            List <QueryValue> joinResultElements = new List <QueryValue>();

            for (int outerIndex = 0; outerIndex < outer.Elements.Count; outerIndex++)
            {
                ExceptionUtilities.Assert(outerKeys.Elements[outerIndex] is QueryScalarValue, "For now we only support join on primitive keys.");
                var outerKey = (QueryScalarValue)outerKeys.Elements[outerIndex];

                IList <QueryValue> innerMatches;
                if (!innerKeyLookup.TryGetValue(outerKey, out innerMatches))
                {
                    innerMatches = new List <QueryValue>();
                }

                if (isGroupJoin)
                {
                    QueryValue result = this.EvaluateLambda <QueryValue>(expression.ResultSelector, outer.Elements[outerIndex], inner.Type.CreateCollectionWithValues(innerMatches));
                    joinResultElements.Add(result);
                }
                else
                {
                    foreach (var innerValue in innerMatches)
                    {
                        QueryValue result = this.EvaluateLambda <QueryValue>(expression.ResultSelector, outer.Elements[outerIndex], innerValue);
                        joinResultElements.Add(result);
                    }
                }
            }

            var resultType = expression.ResultSelector.Body.ExpressionType;

            return(new QueryCollectionValue(resultType.CreateCollectionType(), resultType.EvaluationStrategy, QueryError.GetErrorFromValues(joinResultElements), joinResultElements));
        }
Beispiel #3
0
        private void WriteEntry(ODataWriter writer, Lazy <ODataReader> lazyReader, ODataResource entry)
        {
            this.WriteStart(writer, entry);
            var annotation = entry.GetAnnotation <ODataEntryNavigationLinksObjectModelAnnotation>();
            ODataNestedResourceInfo navLink = null;

            if (annotation != null)
            {
                for (int i = 0; i < annotation.Count; ++i)
                {
                    bool found = annotation.TryGetNavigationLinkAt(i, out navLink);
                    ExceptionUtilities.Assert(found, "Navigation links should be ordered sequentially for writing");
                    this.WriteNavigationLink(writer, lazyReader, navLink);
                }
            }

            this.WriteEnd(writer, ODataReaderState.ResourceEnd);
            this.Read(lazyReader);
        }
Beispiel #4
0
        /// <summary>
        /// Converts an entity data type to a entity type.
        /// </summary>
        /// <param name="entity">The entity data type to convert.</param>
        /// <returns>The corresponding entity type.</returns>
        private static IEdmEntityTypeReference GetEntityType(IEdmModel model, EntityDataType entityDataType)
        {
            Debug.Assert(entityDataType != null, "entityDataType != null");

            IEdmSchemaType edmType = model.FindType(entityDataType.Definition.FullName);

            ExceptionUtilities.Assert(
                edmType != null,
                "The expected entity type '{0}' was not found in the entity model for this test.",
                entityDataType.Definition.FullName);

            IEdmEntityType entityType = edmType as IEdmEntityType;

            ExceptionUtilities.Assert(
                entityType != null,
                "The expected entity type '{0}' is not defined as entity type in the test's metadata.",
                entityDataType.Definition.FullName);
            return((IEdmEntityTypeReference)entityType.ToTypeReference(entityDataType.IsNullable));
        }
Beispiel #5
0
        /// <summary>
        /// Calculates an entity id based on the base uri, entity set resolver, set name, and entity key values
        /// </summary>
        /// <param name="contextData">The context data</param>
        /// <param name="entitySetName">the entity set name</param>
        /// <param name="entity">The entity to generate an id for</param>
        /// <returns>The id for the given entity</returns>
        public Uri CalculateEntityId(DataServiceContextData contextData, string entitySetName, object entity)
        {
            ExceptionUtilities.CheckArgumentNotNull(contextData, "contextData");
            ExceptionUtilities.CheckStringArgumentIsNotNullOrEmpty(entitySetName, "entitySetName");
            ExceptionUtilities.CheckArgumentNotNull(entity, "entity");

            if (contextData.BaseUri != null)
            {
                ExceptionUtilities.Assert(contextData.BaseUri.IsAbsoluteUri, "Base uri must be absolute. Uri was: '{0}'", contextData.BaseUri.OriginalString);
                return(new Uri(UriHelpers.ConcatenateUriSegments(contextData.BaseUri.AbsoluteUri, Uri.EscapeDataString(entitySetName) + this.CalculateEntityKey(entity))));
            }
            else
            {
                ExceptionUtilities.CheckObjectNotNull(contextData.ResolveEntitySet, "Entity set resolver cannot be null if base uri is null");
                var resolvedUri = contextData.ResolveEntitySet(entitySetName);
                ExceptionUtilities.CheckObjectNotNull(resolvedUri, "Entity set resolver returned null for set name '{0}'", entitySetName);
                return(new Uri(resolvedUri.AbsoluteUri + this.CalculateEntityKey(entity)));
            }
        }
Beispiel #6
0
        /// <summary>
        /// Visits a QueryExpression tree whose root node is a Function Call Expression
        /// </summary>
        /// <param name="expression">Query Custom Function Call Expression</param>
        /// <returns>A uri with a Action in it</returns>
        public override string Visit(QueryCustomFunctionCallExpression expression)
        {
            var serviceOperation = expression.Function.Annotations.OfType <ServiceOperationAnnotation>().SingleOrDefault();

            // We are ignoring the other parameters associated with the function call expression because other than a binding parameter
            // these will be added into the payload that is sent
            if (serviceOperation != null && serviceOperation.BindingKind.IsBound())
            {
                // Calculate the uri previous to the action query then append the function name after it
                ExceptionUtilities.Assert(expression.Arguments.Count > 0, "Action Function Expression where IsBinding = true must have at least one argment");
                var argumentExpression = expression.Arguments[0];
                return(string.Format(CultureInfo.InvariantCulture, "{0}/{1}", this.ComputeUriInternal(argumentExpression), expression.Function.Name));
            }
            else
            {
                // return the function name because its at the root whether its a legacy serviceoperation or an action
                return(expression.Function.Name);
            }
        }
 /// <summary>
 /// Disposes the inner message reader.
 /// </summary>
 public void Dispose()
 {
     this.messageReader.Dispose();
     // Calling IDisposable.Dispose  twice should not throw.
     this.messageReader.Dispose();
     if (this.message != null && this.message.TestStream != null && this.message.StreamRetrieved)
     {
         bool       enableMessageStreamDisposal = testConfiguration.MessageReaderSettings.EnableMessageStreamDisposal;
         TestStream messageStream = this.message.TestStream;
         if (enableMessageStreamDisposal == false && messageStream != null)
         {
             ExceptionUtilities.Assert(messageStream.DisposeCount == 0, "Dispose method on the stream must not be called when EnableMessageStreamDisposal is set to false.");
         }
         else
         {
             ExceptionUtilities.Assert(messageStream.DisposeCount == 1, "Dispose method on the stream must be called exactly once when EnableMessageStreamDisposal is set to true.");
         }
     }
 }
Beispiel #8
0
        /// <summary>
        /// Uses the given target type to invoke the appropriate deserialize method
        /// Throws if the given type is not valid
        /// </summary>
        /// <param name="value">The wire representation of a primitive value</param>
        /// <param name="targetType">The type to deserialize into</param>
        /// <returns>A clr instance of the primitive value</returns>
        public virtual object DeserializePrimitive(TWire value, Type targetType)
        {
            ExceptionUtilities.CheckArgumentNotNull(targetType, "targetType");

            bool nullableType = false;

            // if its nullable, just get the generic argument
            if (targetType.IsGenericType() &&
                targetType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                nullableType = true;
                targetType   = Nullable.GetUnderlyingType(targetType);
            }

            if ((nullableType || !targetType.IsValueType()) && this.ValueIsNull(value, targetType))
            {
                return(null);
            }

            // find a method with the right return type and a single parameter of type TWire
            MethodInfo method = typeof(PrimitiveConverter <TWire>).GetMethods()
                                .SingleOrDefault(m => m.ReturnType == targetType &&
                                                 m.Name.StartsWith("Deserialize", StringComparison.Ordinal) &&
                                                 m.GetParameters().Count(p => p.ParameterType == typeof(TWire)) == 1);

            ExceptionUtilities.CheckObjectNotNull(method, "Cannot deserialize into target type '{0}'", targetType.FullName);

            // invoke the method
            object result = method.Invoke(this, new object[] { value });

            // ensure its of the right type before returning it
            ExceptionUtilities.Assert(targetType.IsInstanceOfType(result), "Result of deserialization was not of expected type");

            if (nullableType)
            {
                Type            nullable    = typeof(Nullable <>).MakeGenericType(targetType);
                ConstructorInfo constructor = nullable.GetInstanceConstructor(true, new Type[] { targetType });
                ExceptionUtilities.CheckObjectNotNull(constructor, "Cannot find constructor for Nullable<> with type argument: " + targetType.FullName);
                result = constructor.Invoke(new object[] { result });
            }

            return(result);
        }
        private void BuildStructuralTypes(QueryTypeLibrary library, EntityContainer container)
        {
            foreach (var entitySet in container.EntitySets)
            {
                var rootType        = entitySet.EntityType;
                var allDerivedTypes = container.Model.EntityTypes.Where(et => et.IsKindOf(rootType));

                foreach (var entityType in allDerivedTypes)
                {
                    var queryEntityType = library.GetQueryEntityType(entitySet, entityType);

                    var pathToProperty = new PathToProperty(container, entitySet, entityType);
                    this.CreateMembers(library, queryEntityType, entityType.AllProperties, pathToProperty);
                    ExceptionUtilities.Assert(pathToProperty.PathStackWithinEntityType.Count == 0, "Path to property stack is not empty.");

                    this.CreateNavigationMembers(library, queryEntityType, entityType.AllNavigationProperties, container);
                }
            }
        }
Beispiel #10
0
        private VariationTestItem InvokeMatrixCombinationCallback(MethodInfo method, VariationTestItem variation, VariationAttribute attribute)
        {
            var callbackAttr = (TestMatrixCombinationCallbackAttribute)method.GetCustomAttributes(typeof(TestMatrixCombinationCallbackAttribute), false).SingleOrDefault();

            if (callbackAttr != null)
            {
                var callbackMethod = this.GetType().GetMethod(callbackAttr.CallbackMethodName, true, false);
                ExceptionUtilities.CheckObjectNotNull(callbackMethod, "Could not find instance method '{0}' on type '{1}'", callbackAttr.CallbackMethodName, this.GetType());
                ExceptionUtilities.Assert(typeof(VariationTestItem).IsAssignableFrom(callbackMethod.ReturnType), "Matrix combination callback '{0}' returns unexpected type '{1}'", callbackMethod.Name, callbackMethod.ReturnType);

                var arguments = new object[attribute.Parameters.Length + 1];
                arguments[0] = variation;
                Array.Copy(attribute.Parameters, 0, arguments, 1, attribute.Parameters.Length);

                variation = (VariationTestItem)callbackMethod.Invoke(this, arguments);
            }

            return(variation);
        }
Beispiel #11
0
        /// <summary>
        /// Parses the token of type array
        /// </summary>
        /// <returns>Parsed array</returns>
        private JsonArray ParseArray()
        {
            // Array can start only with '['
            ExceptionUtilities.Assert(this.tokenizer.TokenType == JsonTokenType.LeftSquareBracket, "Invalid Token");

            // Should not end before we get ']'
            ExceptionUtilities.Assert(this.tokenizer.HasMoreTokens(), "Invalid End Of Stream");
            this.tokenizer.GetNextToken();

            // Array is an ordered collection of values
            ExceptionUtilities.Assert(this.IsValueType(this.tokenizer.TokenType) || this.tokenizer.TokenType == JsonTokenType.RightSquareBracket, "InvalidToken");
            var result = new JsonArray();

            while (this.tokenizer.HasMoreTokens())
            {
                if (this.IsValueType(this.tokenizer.TokenType))
                {
                    JsonValue v = this.ParseValue();
                    result.Add(v);

                    // Values are separated by , (comma).
                    ExceptionUtilities.Assert(this.tokenizer.TokenType == JsonTokenType.Comma || this.tokenizer.TokenType == JsonTokenType.RightSquareBracket, "Invalid Token");
                }
                else if (this.tokenizer.TokenType == JsonTokenType.RightSquareBracket)
                {
                    if (this.tokenizer.HasMoreTokens())
                    {
                        this.tokenizer.GetNextToken();
                    }

                    return(result);
                }
                else if (this.tokenizer.TokenType == JsonTokenType.Comma)
                {
                    this.tokenizer.GetNextToken();

                    // Last element of the array cannot be followed by a comma.
                    ExceptionUtilities.Assert(this.IsValueType(this.tokenizer.TokenType) || this.tokenizer.TokenType == JsonTokenType.RightSquareBracket, "InvalidToken");
                }
            }

            return(result);
        }
Beispiel #12
0
        /// <summary>
        /// Serializes the given root element to a binary representation
        /// </summary>
        /// <param name="rootElement">The root element</param>
        /// <param name="encodingName">Optional name of an encoding to use if it is relevant to the current format</param>
        /// <returns>The serialized value</returns>
        public override byte[] SerializeToBinary(ODataPayloadElement rootElement, string encodingName)
        {
            ExceptionUtilities.CheckArgumentNotNull(rootElement, "rootElement");
            ExceptionUtilities.Assert(rootElement.ElementType == ODataPayloadElementType.ComplexInstance, "Only complex instances form values can be serialized as html forms. Type was: {0}", rootElement.ElementType);

            var complexInstance = (ComplexInstance)rootElement;

            var queryOpions = new List <KeyValuePair <string, string> >();

            foreach (var property in complexInstance.Properties)
            {
                string value;
                if (property.ElementType == ODataPayloadElementType.PrimitiveProperty)
                {
                    value = this.LiteralConverter.SerializePrimitive(((PrimitiveProperty)property).Value.ClrValue);
                }
                else if (property.ElementType == ODataPayloadElementType.ComplexProperty)
                {
                    value = this.JsonConverter.ConvertToJson(((ComplexProperty)property).Value);
                }
                else if (property.ElementType == ODataPayloadElementType.PrimitiveMultiValueProperty)
                {
                    value = this.JsonConverter.ConvertToJson(((PrimitiveMultiValueProperty)property).Value);
                }
                else
                {
                    ExceptionUtilities.Assert(property.ElementType == ODataPayloadElementType.ComplexMultiValueProperty, "Only primitive, complex, and multi-value properties are supported");
                    value = this.JsonConverter.ConvertToJson(((ComplexMultiValueProperty)property).Value);
                }

                var option = new KeyValuePair <string, string>(property.Name, value);
                queryOpions.Add(option);
            }

            StringBuilder builder = new StringBuilder();

            UriHelpers.ConcatenateQueryOptions(builder, queryOpions);

            var encoding = HttpUtilities.GetEncodingOrDefault(encodingName);

            return(encoding.GetBytes(builder.ToString()));
        }
        internal static IList <KeyValuePair <string, ITypedValue> > ConvertActionArgumentsToTypedValues(this Function function, ServiceOperationAnnotation actionAnnotation, IList <KeyValuePair <string, QueryExpression> > parameters)
        {
            var parameterValues = new List <KeyValuePair <string, ITypedValue> >();

            FunctionParameter boundParameter = null;

            if (actionAnnotation.BindingKind.IsBound())
            {
                boundParameter = function.Parameters[0];
            }

            foreach (var parameterPair in parameters)
            {
                var edmFunctionParameterDefinition = function.Parameters.Where(p => p.Name == parameterPair.Key).SingleOrDefault();
                ExceptionUtilities.CheckObjectNotNull(edmFunctionParameterDefinition, "Cannot find parameter '{0}' defined in function '{1}'", parameterPair.Key, function.Name);
                ExceptionUtilities.Assert(edmFunctionParameterDefinition != boundParameter, "Bound parameters MUST not be passed in as they will have already been built for the ODataUri was built");

                var primitiveDataType  = edmFunctionParameterDefinition.DataType as PrimitiveDataType;
                var collectionDataType = edmFunctionParameterDefinition.DataType as CollectionDataType;

                if (primitiveDataType != null)
                {
                    ExceptionUtilities.CheckObjectNotNull(primitiveDataType, "Expected a primitive data type not '{0}'", primitiveDataType);
                    var primitiveValue = ConvertToPrimitiveValue(parameterPair.Value, primitiveDataType);
                    parameterValues.Add(new KeyValuePair <string, ITypedValue>(edmFunctionParameterDefinition.Name, primitiveValue));
                }
                else if (collectionDataType != null)
                {
                    ExceptionUtilities.CheckObjectNotNull(collectionDataType, "expected a collection data type, actual '{0}", collectionDataType);
                    var collectionValue = parameterPair.Value.ConvertToMultiValue(collectionDataType.ElementDataType);
                    parameterValues.Add(new KeyValuePair <string, ITypedValue>(edmFunctionParameterDefinition.Name, collectionValue));
                }
                else
                {
                    var complexArgumentValue = parameterPair.Value;
                    var complexInstance      = complexArgumentValue.ConvertToComplexInstance();
                    parameterValues.Add(new KeyValuePair <string, ITypedValue>(edmFunctionParameterDefinition.Name, complexInstance));
                }
            }

            return(parameterValues);
        }
Beispiel #14
0
        /// <summary>
        /// Entities returned from FunctionImport only have their scalar properties set. However, in order to properly evaluate results of queries
        /// which project navigation properties of these entities (such as FunctionImportReturningCustomers().Select(c => c.Orders))
        /// we need to associate these entities with ones that are already stored in the data set (which basically is the full graph)
        /// In order to do that, we look through entity associated with the function import for entity with matching keys and return it instead
        /// </summary>
        /// <param name="result">Collection of fixed to fix-up.</param>
        /// <param name="entitySetName">Name of entity set associated with the given function import.</param>
        /// <returns>Collection of fixed-up entities.</returns>
        private QueryValue FixupFunctionImportResultEntities(QueryValue result, string entitySetName)
        {
            var resultCollection = result as QueryCollectionValue;

            ExceptionUtilities.CheckObjectNotNull(resultCollection, "Expecting collection.");
            ExceptionUtilities.Assert(resultCollection.Type.ElementType is QueryEntityType, "Expecting collection of entities.");

            var fixedElements = new List <QueryValue>();

            foreach (var element in resultCollection.Elements.Cast <QueryStructuralValue>())
            {
                var        elementEntityType                  = (QueryEntityType)element.Type;
                var        primaryKeyPropertyNames            = elementEntityType.Properties.Where(p => p.IsPrimaryKey).Select(p => p.Name);
                var        elementKeyPropertyValuesDictionary = primaryKeyPropertyNames.Select(n => new { Name = n, Value = element.GetScalarValue(n) }).ToDictionary(k => k.Name, e => e.Value);
                QueryValue matchingElementInEntitySet         = null;
                foreach (var entitySetElement in this.DataSet[entitySetName].Elements.Cast <QueryStructuralValue>())
                {
                    if (!elementEntityType.EntityType.Equals(((QueryEntityType)entitySetElement.Type).EntityType))
                    {
                        continue;
                    }

                    var elementsMatch = this.AllKeysMatch(entitySetElement, elementKeyPropertyValuesDictionary);
                    if (elementsMatch)
                    {
                        matchingElementInEntitySet = entitySetElement;
                        break;
                    }
                }

                if (matchingElementInEntitySet != null)
                {
                    fixedElements.Add(matchingElementInEntitySet);
                }
                else
                {
                    fixedElements.Add(element);
                }
            }

            return(((QueryCollectionType)result.Type).CreateCollectionWithValues(fixedElements));
        }
        /// <summary>
        /// Normalizes an Atom link property value between an XmlTreeAnnotation and another property.
        /// </summary>
        /// <typeparam name="T">The type of payload element.</typeparam>
        /// <param name="payloadElement">The payload element to normalize.</param>
        /// <param name="relationValue">The relation value of the link to normalize.</param>
        /// <param name="attributeName">The name of the link attribute to normalize.</param>
        /// <param name="getOtherLinkValue">Function for retrieving the other value for the link property.</param>
        /// <param name="setOtherLinkValue">Delegate for setting the other value for the link property.</param>
        private void NormalizeLinkValue <T>(T payloadElement, string relationValue, string attributeName, Func <T, string> getOtherLinkValue, Action <T, string> setOtherLinkValue) where T : ODataPayloadElement
        {
            string            otherLinkValue        = getOtherLinkValue(payloadElement);
            XmlTreeAnnotation linkXmlTreeAnnotation = payloadElement.Annotations
                                                      .OfType <XmlTreeAnnotation>()
                                                      .SingleOrDefault(a => a.LocalName.Equals(TestAtomConstants.AtomLinkElementName) && HasAttribute(a, TestAtomConstants.AtomLinkRelationAttributeName, relationValue));

            if (otherLinkValue != null)
            {
                if (linkXmlTreeAnnotation != null)
                {
                    var linkAttribute = GetAttributeAnnotation(linkXmlTreeAnnotation, attributeName);
                    if (linkAttribute == null)
                    {
                        linkXmlTreeAnnotation.Children.Add(XmlTreeAnnotation.AtomAttribute(attributeName, otherLinkValue));
                    }
                    else if (linkAttribute.PropertyValue != otherLinkValue)
                    {
                        ExceptionUtilities.Assert(otherLinkValue.Equals(linkAttribute.PropertyValue), attributeName + " value for link '" + relationValue + "' does not match value on Xml Tree");
                    }
                }
                else
                {
                    // Add an xml tree annotation to match the value from the other source.
                    payloadElement.Add(
                        XmlTreeAnnotation.Atom(
                            TestAtomConstants.AtomLinkElementName,
                            null,
                            XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkRelationAttributeName, relationValue),
                            XmlTreeAnnotation.AtomAttribute(attributeName, otherLinkValue)));
                }
            }
            else if (linkXmlTreeAnnotation != null)
            {
                // Set the other source to match the atom:link href value of the annotation
                var linkAttribute = GetAttributeAnnotation(linkXmlTreeAnnotation, attributeName);
                if (linkAttribute != null)
                {
                    setOtherLinkValue(payloadElement, linkAttribute.PropertyValue);
                }
            }
        }
Beispiel #16
0
        /// <summary>
        /// Splits the given json payload into multiple lines and adds the indentation marker to it.
        /// </summary>
        /// <param name="jsonString">Json Payload.</param>
        /// <param name="indentDepth">Indentation depth to start with.</param>
        /// <returns>all the lines after indenting the json payload.</returns>
        public static string[] GetJsonLines(string jsonString, int indentDepth = 0)
        {
            string        indentVariable      = "$(Indent)";
            int           originalIndentDepth = indentDepth;
            List <string> jsonLines           = new List <string>();
            StringBuilder stringBuilder       = new StringBuilder();

            for (int i = 0; i < jsonString.Length; i++)
            {
                if (jsonString[i] == '{' || jsonString[i] == '[')
                {
                    stringBuilder.Append(jsonString[i]);
                    jsonLines.Add(stringBuilder.ToString());
                    stringBuilder.Clear();
                    indentDepth++;
                    for (int j = 0; j < indentDepth; j++)
                    {
                        stringBuilder.Append(indentVariable);
                    }
                }
                else if (jsonString[i] == '}' || jsonString[i] == ']')
                {
                    jsonLines.Add(stringBuilder.ToString());
                    stringBuilder.Clear();
                    indentDepth--;
                    for (int j = 0; j < indentDepth; j++)
                    {
                        stringBuilder.Append(indentVariable);
                    }

                    stringBuilder.Append(jsonString[i]);
                }
                else
                {
                    stringBuilder.Append(jsonString[i]);
                }
            }

            jsonLines.Add(stringBuilder.ToString());
            ExceptionUtilities.Assert(originalIndentDepth == indentDepth, "indentDepth must be zero");
            return(jsonLines.ToArray());
        }
        private bool ValuesAreEqual(QueryValue firstValue, QueryValue secondValue)
        {
            var firstValueScalar = firstValue as QueryScalarValue;

            if (firstValueScalar != null)
            {
                var secondValueScalar = secondValue as QueryScalarValue;
                return(secondValueScalar != null && this.ScalarValuesAreEqual(firstValueScalar, secondValueScalar));
            }

            var firstValueStructural = firstValue as QueryStructuralValue;

            if (firstValueStructural != null)
            {
                var secondValueStructural = secondValue as QueryStructuralValue;
                return(secondValueStructural != null && this.StructuralValuesAreEqual(firstValueStructural, secondValueStructural));
            }

            var firstValueReference = firstValue as QueryReferenceValue;

            if (firstValueReference != null)
            {
                var secondValueReference = secondValue as QueryReferenceValue;
                return(secondValueReference != null && this.ReferenceValuesAreEqual(firstValueReference, secondValueReference));
            }

            var firstValueRecord = firstValue as QueryRecordValue;

            if (firstValueRecord != null)
            {
                var secondValueRecord = secondValue as QueryRecordValue;
                return(secondValueRecord != null && this.RecordValuesAreEqual(firstValueRecord, secondValueRecord));
            }

            var firstValueCollection = firstValue as QueryCollectionValue;

            ExceptionUtilities.Assert(firstValueCollection != null, "Now only support query value equality comparison on primitive / structural / collection, not {0}.", firstValue);

            var secondValueCollection = secondValue as QueryCollectionValue;

            return(secondValueCollection != null && this.CollectionValuesAreEqual(firstValueCollection, secondValueCollection));
        }
        /// <summary>
        /// Removes all references to the given entity
        /// </summary>
        /// <param name="type">The type of the entity</param>
        /// <param name="entity">The entity to remove</param>
        private void RemoveAllReferences(QueryEntityType type, QueryStructuralValue entity)
        {
            ExceptionUtilities.Assert(type == entity.Type, "QueryStructuralValue must have the same type as what is provided");

            // get all association sets that touch the entity's set
            var associationSets = this.Model.EntityContainers
                                  .SelectMany(c => c.AssociationSets)
                                  .Where(a => a.Ends.Any(e => e.EntitySet == type.EntitySet && type.EntityType.IsKindOf(e.AssociationEnd.EntityType)))
                                  .ToList();

            // get all the navigation properties for each set. Note that there will only be more than one in the case of a self-reference
            var navigationPropertiesBySet = associationSets
                                            .SelectMany(a => a.Ends)
                                            .ToLookup(
                e => e.EntitySet.Name,
                e => e.AssociationEnd.EntityType.AllNavigationProperties.SingleOrDefault(n => n.FromAssociationEnd == e.AssociationEnd));

            // compute the key of this entity
            var key = this.GetEntityKey(entity);

            // go through each potentially-affected set and remove the entity from the navigations of each element
            foreach (var setNavigations in navigationPropertiesBySet)
            {
                var setName     = setNavigations.Key;
                var setContents = this.QueryDataSet[setName].Elements.Cast <QueryStructuralValue>();

                // go through each element in the set
                foreach (var related in setContents)
                {
                    // and remove the entity from each navigation
                    foreach (var navigation in setNavigations.Where(n => n != null))
                    {
                        // We have associations between sub types in the hierarchy so some instances will not have particular associations so we should filter them out
                        var relatedQueryEntityType = related.Type as QueryEntityType;
                        if (relatedQueryEntityType.EntityType.IsKindOf(navigation.FromAssociationEnd.EntityType))
                        {
                            this.RemoveFromNavigationProperty(key, related, navigation);
                        }
                    }
                }
            }
        }
            /// <summary>
            /// Returns whether or not to expect an error when reading the given payload with the given version.
            /// </summary>
            /// <param name="requestPayload">The payload which will be read.</param>
            /// <param name="version">The version the reader expects.</param>
            /// <param name="error">The error if one is expected.</param>
            /// <returns>
            /// Whether an error is expected
            /// </returns>
            public bool TryCalculateError(ODataPayloadElement requestPayload, DataServiceProtocolVersion version, out ExpectedErrorMessage error)
            {
                error = null;

                if (requestPayload == null)
                {
                    return(false);
                }

                // Simplifying assumption. The rest of this class assumes that the version is < 3 and doesn't check further.
                if (version >= DataServiceProtocolVersion.V4)
                {
                    return(false);
                }

                ExceptionUtilities.Assert(version != DataServiceProtocolVersion.Unspecified, "Version parameter cannot be unspecified");
                this.payloadVersion = version;

                return(error != null);
            }
Beispiel #20
0
        /// <summary>
        /// Initializes a new instance of the ResourceInstance class with the given type and set
        /// </summary>
        /// <param name="type">The type of the instance</param>
        /// <param name="set">The set the instance belongs to</param>
        public ResourceInstance(ResourceType type, ResourceSet set)
            : base()
        {
            ExceptionUtilities.CheckArgumentNotNull(type, "type");

            if (type.ResourceTypeKind == ResourceTypeKind.EntityType)
            {
                ExceptionUtilities.CheckArgumentNotNull(set, "set");
                this.IsEntityType    = true;
                this.ResourceSetName = set.Name;
            }
            else
            {
                ExceptionUtilities.Assert(type.ResourceTypeKind == ResourceTypeKind.ComplexType, "Only entity and complex types can be represented. Type was: '{0}'", type);
                ExceptionUtilities.Assert(set == null, "Complex types cannot have sets");
                this.IsEntityType = false;
            }

            this.ResourceTypeName = type.FullName;
        }
        /// <summary>
        /// Replaces function parameter reference expression with an expression represeting argument for the parameter
        /// </summary>
        /// <param name="expression">The function parameter reference expression</param>
        /// <returns>An expression represeting corresponding argument for the parameter</returns>
        protected QueryExpression ReplaceFunctionParameterReference(QueryFunctionParameterReferenceExpression expression)
        {
            string parameterName = expression.ParameterName;
            var    functionCall  = this.customFunctionsCallStack.Peek();
            var    parameters    = functionCall.Function.Parameters;
            var    arguemnts     = functionCall.Arguments;

            var matchingParameters = parameters.Where(p => p.Name == parameterName).ToList();

            ExceptionUtilities.Assert(
                matchingParameters.Count == 1,
                "Found {0} parameters with name '{1}'. Expected: 1. Function: '{2}'.",
                matchingParameters.Count,
                parameterName,
                functionCall.Function.FullName);

            int index = parameters.IndexOf(matchingParameters[0]);

            return(arguemnts[index]);
        }
Beispiel #22
0
        private IDataGenerator ResolveStringGenerator(IRandomNumberGenerator random, DataGenerationHint[] hints, bool isNullable)
        {
            int minLength;
            int maxLength = this.GetMaxLengthAndCheckLengthRange(hints, out minLength);

            bool ansiString = hints.OfType <AnsiStringHint>().Any();

            IStringGenerator stringGenerator = ansiString ? this.AnsiStringGenerator : this.UnicodeStringGenerator;

            var stringPrefixHint = hints.OfType <StringPrefixHint>().SingleOrDefault();

            if (stringPrefixHint != null)
            {
                string prefix = stringPrefixHint.Value;
                ExceptionUtilities.Assert(prefix.Length < maxLength, "the length of string prefix in data generation exceeds the max length limit");
                return(CreateRandomGenerator <string>(random, (r) => prefix + stringGenerator.GenerateString(r, Math.Max(minLength - prefix.Length, 0), maxLength - prefix.Length), hints));
            }

            return(CreateRandomGenerator <string>(random, (r) => stringGenerator.GenerateString(r, minLength, maxLength), hints));
        }
        private static IEnumerable <T> RecurseBaseTypes <T>(XElement typeElement, IDictionary <string, XElement> typeIndex, Func <XElement, IEnumerable <T> > selectFunction, ICollection <XElement> seenElements)
        {
            string baseTypeValue = string.Empty;

            if (typeElement.TryGetAttributeValue("BaseType", out baseTypeValue))
            {
                ExceptionUtilities.Assert(typeIndex.ContainsKey(baseTypeValue), "Failed to find base type " + baseTypeValue);
                var baseTypeElement = typeIndex[baseTypeValue];
                if (seenElements.Contains(baseTypeElement))
                {
                    // Detect cyclical hierachies
                    return(selectFunction(baseTypeElement));
                }

                seenElements.Add(baseTypeElement);
                return(RecurseBaseTypes(baseTypeElement, typeIndex, selectFunction, seenElements).Concat(selectFunction(typeElement)));
            }

            return(selectFunction(typeElement));
        }
Beispiel #24
0
            /// <summary>
            /// Visits a collection start.
            /// </summary>
            /// <param name="collection">The collection start to visit.</param>
            protected override ODataPayloadElement VisitCollectionStart(ODataCollectionStart collection)
            {
                ExceptionUtilities.CheckArgumentNotNull(collection, "collection");

                // NOTE the Taupo OM does not currently support heterogenous collections; we determine the
                //      type of the collection by looking at the first non-null item
                ODataCollectionItemsObjectModelAnnotation itemsAnnotation = collection.GetAnnotation <ODataCollectionItemsObjectModelAnnotation>();

                ExceptionUtilities.Assert(itemsAnnotation != null, "itemsAnnotation != null");

                PrimitiveCollection primitiveCollection = PayloadBuilder.PrimitiveCollection(collection.Name);

                foreach (object item in itemsAnnotation)
                {
                    PrimitiveValue primitiveValue = (PrimitiveValue)this.Visit(item);
                    primitiveCollection.Add(primitiveValue);
                }

                return(primitiveCollection);
            }
Beispiel #25
0
 private static ODataPayloadElementCollection CreateCollectionForElementType(ODataPayloadElementType elementType)
 {
     if (elementType == ODataPayloadElementType.PrimitiveValue)
     {
         return(new PrimitiveCollection());
     }
     else if (elementType == ODataPayloadElementType.DeferredLink)
     {
         return(new LinkCollection());
     }
     else if (elementType == ODataPayloadElementType.ComplexInstance)
     {
         return(new ComplexInstanceCollection());
     }
     else
     {
         ExceptionUtilities.Assert(elementType == ODataPayloadElementType.EntityInstance, "Element type was unexpected type '{0}'", elementType);
         return(new EntitySetInstance());
     }
 }
Beispiel #26
0
 private static ODataPayloadElement CreateNullElement(ODataPayloadElementType elementType)
 {
     if (elementType == ODataPayloadElementType.PrimitiveValue)
     {
         return(new PrimitiveValue(null, null));
     }
     else if (elementType == ODataPayloadElementType.DeferredLink)
     {
         return(new DeferredLink());
     }
     else if (elementType == ODataPayloadElementType.ComplexInstance)
     {
         return(new ComplexInstance(null, true));
     }
     else
     {
         ExceptionUtilities.Assert(elementType == ODataPayloadElementType.EntityInstance, "Element type was unexpected type '{0}'", elementType);
         return(new EntityInstance(null, true));
     }
 }
Beispiel #27
0
        public virtual object GetResource(IQueryable query, string fullTypeName)
        {
            ExceptionUtilities.CheckArgumentNotNull(query, "query");
            object resource = null;

            foreach (object r in query)
            {
                ExceptionUtilities.Assert(resource == null, "Invalid Uri specified. The query '{0}' must refer to a single resource", new object[] { query.ToString() });
                resource = r;
            }
            if (resource == null)
            {
                return(null);
            }
            if (fullTypeName != null)
            {
                this.ValidateResourceType(resource, fullTypeName);
            }
            return(new UpdatableToken(resource));
        }
        /// <summary>
        /// Gets a data generator for the specified member.
        /// </summary>
        /// <param name="memberName">The member name.</param>
        /// <returns>A data generator for the specified member.</returns>
        public IDataGenerator GetMemberDataGenerator(string memberName)
        {
            string nextMemberName;
            string currenMemberName = MemberValueGenerators.GetCurrentMemberName(memberName, out nextMemberName);
            int    index;

            ExceptionUtilities.Assert(int.TryParse(currenMemberName, out index), "Invalid member name for collection data generator. It should start with index. Member name: '{0}'.", memberName);

            IDataGenerator memberDataGenerator = this.itemDataGenerator;

            if (nextMemberName != null)
            {
                var memberDataGenerators = memberDataGenerator as IMemberDataGenerators;
                ExceptionUtilities.CheckObjectNotNull(memberDataGenerators, "Cannot find data generator for member '{0}'.", memberName);

                memberDataGenerator = memberDataGenerators.GetMemberDataGenerator(nextMemberName);
            }

            return(memberDataGenerator);
        }
Beispiel #29
0
        /// <summary>
        /// Validator for spatial values.
        /// </summary>
        /// <param name="payloadElement">The payload element to validate.</param>
        /// <param name="testConfigLimits">The test configuration limits to modify.</param>
        public static void SpatialValueValidator(ODataPayloadElement payloadElement, TestConfigurationLimits testConfigLimits)
        {
            PrimitiveValue primitiveValue = payloadElement as PrimitiveValue;

            ExceptionUtilities.Assert(primitiveValue != null, "This validator only works on primitive values.");

            if (primitiveValue.ClrValue is ISpatial)
            {
                // Spatial values require V3.
                testConfigLimits.RaiseMinPayloadVersion(ODataVersion.V4);
            }
            else if (primitiveValue.ClrValue == null)
            {
                EntityModelTypeAnnotation typeAnnotation = primitiveValue.GetAnnotation <EntityModelTypeAnnotation>();
                if (typeAnnotation != null && typeAnnotation.EdmModelType.Definition.IsSpatial())
                {
                    testConfigLimits.RaiseMinPayloadVersion(ODataVersion.V4);
                }
            }
        }
        private QueryValue EvaluateQueryMethodWithLambdaExpression <TResult>(
            LinqQueryMethodWithLambdaExpression expression,
            Func <QueryCollectionValue, QueryValue> nolambdaEvaluator,
            Func <QueryCollectionValue, Func <QueryValue, TResult>, QueryValue> lambdaEvaluator)
            where TResult : QueryValue
        {
            var source = this.EvaluateCollection(expression.Source);

            LinqLambdaExpression lambda = expression.Lambda;

            if (lambda == null)
            {
                ExceptionUtilities.Assert(nolambdaEvaluator != null, "noLambdaEvaluator must not be null.");
                return(nolambdaEvaluator(source));
            }
            else
            {
                return(lambdaEvaluator(source, v => this.EvaluateLambda <TResult>(lambda, v)));
            }
        }