private static ComplexInstance GenerateSimilarComplexInstance(IRandomNumberGenerator random, ComplexInstance currentInstance, bool randomizePropertyValues = false)
        {
            ComplexInstance instance = ((ComplexInstance)currentInstance.DeepCopy());

            if (!randomizePropertyValues)
            {
                return(instance);
            }

            foreach (var property in instance.Properties)
            {
                PrimitiveProperty primitive = property as PrimitiveProperty;
                if (primitive != null)
                {
                    primitive.Value = TestValues.GetDifferentPrimitiveValue(primitive.Value);
                }

                ComplexProperty complex = property as ComplexProperty;
                if (complex != null)
                {
                    complex.Value = GenerateSimilarComplexInstance(random, complex.Value);
                }

                PrimitiveMultiValueProperty pmultival = property as PrimitiveMultiValueProperty;
                if (pmultival != null)
                {
                    pmultival.Value = GenerateSimilarPrimitiveMultiValue(random, pmultival.Value);
                }

                ComplexMultiValueProperty cmultival = property as ComplexMultiValueProperty;
                if (cmultival != null)
                {
                    cmultival.Value = GenerateSimilarComplexMultiValue(random, cmultival.Value);
                }
            }

            return(instance);
        }
Example #2
0
        public void ParameterReaderJsonLightTest()
        {
            EdmModel model = new EdmModel();

            model.Fixup();

            EdmEntityContainer container = model.EntityContainer as EdmEntityContainer;

            EdmFunction function = new EdmFunction(container.Namespace, "f1", EdmCoreModel.Instance.GetInt32(true));

            function.AddParameter("p1", EdmCoreModel.Instance.GetInt32(false) as EdmTypeReference);
            function.AddParameter("p2", EdmCoreModel.Instance.GetString(false) as EdmTypeReference);
            model.AddElement(function);
            EdmOperationImport f1 = container.FunctionImport(function);

            ComplexInstance f1Params = PayloadBuilder.ComplexValue().PrimitiveProperty("p1", 42).PrimitiveProperty("p2", "Vienna")
                                       .ExpectedFunctionImport(f1);

            var testCases = new[]
            {
                new
                {
                    DebugDescription  = "Custom property annotation for a valid parameter property (before property) - should work.",
                    Json              = "{\"" + JsonLightUtils.GetPropertyAnnotationName("p1", "my.custom") + "\":42, \"p1\":42, \"p2\":\"Vienna\"}",
                    ExpectedException = (ExpectedException)null,
                },
                new
                {
                    DebugDescription  = "Custom property annotation for a valid parameter property (after property) - should fail.",
                    Json              = "{\"p1\":42, \"" + JsonLightUtils.GetPropertyAnnotationName("p1", "my.custom") + "\":42, \"p2\":\"Vienna\"}",
                    ExpectedException = ODataExpectedExceptions.ODataException("DuplicatePropertyNamesChecker_PropertyAnnotationAfterTheProperty", "my.custom", "p1"),
                },
                new
                {
                    DebugDescription  = "OData property annotation for a valid parameter property (before property) - should work.",
                    Json              = "{\"" + JsonLightUtils.GetPropertyAnnotationName("p1", JsonLightConstants.ODataTypeAnnotationName) + "\":42, \"p1\":42, \"p2\":\"Vienna\"}",
                    ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightParameterDeserializer_PropertyAnnotationForParameters"),
                },
                new
                {
                    DebugDescription  = "OData property annotation for a valid parameter property (after property) - should fail.",
                    Json              = "{\"p1\":42, \"" + JsonLightUtils.GetPropertyAnnotationName("p1", JsonLightConstants.ODataTypeAnnotationName) + "\":42, \"p2\":\"Vienna\"}",
                    ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightParameterDeserializer_PropertyAnnotationForParameters"),
                },
                new
                {
                    DebugDescription  = "Custom property annotation for an invalid parameter property - should fail.",
                    Json              = "{\"" + JsonLightUtils.GetPropertyAnnotationName("p0", "my.custom") + "\":42, \"p1\":42, \"p2\":\"Vienna\"}",
                    ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightParameterDeserializer_PropertyAnnotationWithoutPropertyForParameters", "p0"),
                },
                new
                {
                    DebugDescription  = "Custom instance annotation - should work.",
                    Json              = "{\"@my.custom\":42, \"p1\":42, \"p2\":\"Vienna\"}",
                    ExpectedException = (ExpectedException)null,
                },
                new
                {
                    DebugDescription  = "OData instance annotation - should fail.",
                    Json              = "{\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataTypeAnnotationName + "\":\"Edm.Int32\", \"p1\":42, \"p2\":\"Vienna\"}",
                    ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightPropertyAndValueDeserializer_UnexpectedAnnotationProperties", JsonLightConstants.ODataTypeAnnotationName),
                },
            };

            var testDescriptors = testCases.Select(testCase =>
                                                   new PayloadReaderTestDescriptor(this.Settings)
            {
                DebugDescription  = testCase.DebugDescription,
                PayloadElement    = f1Params.DeepCopy().JsonRepresentation(testCase.Json),
                PayloadEdmModel   = model,
                PayloadKind       = ODataPayloadKind.Parameter,
                ExpectedException = testCase.ExpectedException,
            });

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.ReaderTestConfigurationProvider.JsonLightFormatConfigurations.Where(c => c.IsRequest),
                (testDescriptor, testConfiguration) =>
            {
                testDescriptor.ExpectedResultNormalizers.Add(tc => ParameterReaderTests.FixupExpectedCollectionParameterPayloadElement);

                // These descriptors are already tailored specifically for Json Light and
                // do not require normalization.
                testDescriptor.TestDescriptorNormalizers.Clear();
                testDescriptor.RunTest(testConfiguration);
            });
        }
        private static ComplexInstance GenerateSimilarComplexInstance(IRandomNumberGenerator random, ComplexInstance currentInstance, bool randomizePropertyValues = false)
        {
            ComplexInstance instance = ((ComplexInstance)currentInstance.DeepCopy());
            if (!randomizePropertyValues)
            {
                return instance;
            }

            foreach (var property in instance.Properties)
            {
                PrimitiveProperty primitive = property as PrimitiveProperty;
                if (primitive != null)
                {
                    primitive.Value = TestValues.GetDifferentPrimitiveValue(primitive.Value);
                }

                ComplexProperty complex = property as ComplexProperty;
                if (complex != null)
                {
                    complex.Value = GenerateSimilarComplexInstance(random, complex.Value);
                }

                PrimitiveMultiValueProperty pmultival = property as PrimitiveMultiValueProperty;
                if (pmultival != null)
                {
                    pmultival.Value = GenerateSimilarPrimitiveMultiValue(random, pmultival.Value);
                }

                ComplexMultiValueProperty cmultival = property as ComplexMultiValueProperty;
                if (cmultival != null)
                {
                    cmultival.Value = GenerateSimilarComplexMultiValue(random, cmultival.Value);
                }
            }

            return instance;
        }
Example #4
0
        /// <summary>
        /// Create Complex values with the same type as the given complex Value but different property values
        /// </summary>
        /// <param name="complexValue">Complex value to create other values</param>
        /// <returns>An IEnumerable of complex values</returns>
        public static ComplexInstance GetComplexValueWithDifferentValues(ComplexInstance complexValue)
        {
            if (complexValue == null)
            {
                return null;
            }
            List<PropertyInstance> newPropertyValues = new List<PropertyInstance>();
            foreach (var prop in complexValue.Properties)
            {
                var primProp = prop as PrimitiveProperty;
                if (primProp != null)
                {
                    var newProp = primProp.DeepCopy();
                    newProp.Value = GetDifferentPrimitiveValue(primProp.Value);
                    newPropertyValues.Add(newProp);
                }

                var complexProperty = prop as ComplexProperty;
                if (complexProperty != null)
                {
                    newPropertyValues.Add(GetComplexPropertyWithDifferentPrimitiveValues(complexProperty));
                }

                var complexCollectionProp = prop as ComplexMultiValueProperty;
                if (complexCollectionProp != null)
                {
                    var newProp = complexCollectionProp.DeepCopy();
                    if (complexCollectionProp.Value != null)
                    {
                        newProp.Value.Clear();
                        foreach (var complexPropValue in complexCollectionProp.Value)
                        {
                            newProp.Value.Add(GetComplexValueWithDifferentValues(complexPropValue));
                        }
                    }
                    newPropertyValues.Add(newProp);
                }

                var primCollectionProperty = prop as PrimitiveMultiValueProperty;
                if (primCollectionProperty != null)
                {
                    var newProp = primCollectionProperty.DeepCopy();
                    if (primCollectionProperty.Value != null)
                    {
                        newProp.Value.Clear();
                        List<PrimitiveValue> usedValues = new List<PrimitiveValue>();
                        foreach (var primitiveVal in primCollectionProperty.Value)
                        {
                            var newPrimValue = GetDifferentPrimitiveValue(primitiveVal, usedValues);
                            newProp.Value.Add(newPrimValue);
                            usedValues.Add(newPrimValue);
                        }
                    }
                    newPropertyValues.Add(newProp);
                }
            }

            var newValue = complexValue.DeepCopy();
            newValue.Properties = newPropertyValues;

            return newValue;
        }