示例#1
0
        public void InvalidCasesTest()
        {
            ResourceType rt = (ResourceType)ResourceTypeUtils.GetTestInstance(typeof(ResourceType));

            ResourceType complexType = new ResourceType(typeof(object), ResourceTypeKind.ComplexType, null, "Namespace", "Address", false);
            ResourceType entityType  = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Namespace", "Order", false);
            ResourceType primitiveOrComplexCollectionType = ResourceType.GetCollectionResourceType(complexType);
            ResourceType entityCollectionType             = ResourceType.GetEntityCollectionResourceType(entityType);
            ResourceType namedStreamType = ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream));

            AstoriaTestNS.TestUtil.RunCombinations(
                GetPropertyKindValues(),
                new ResourceType[] { entityType, complexType, primitiveOrComplexCollectionType, entityCollectionType }.Concat(ResourceTypeUtils.GetPrimitiveResourceTypes()),
                (kind, type) =>
            {
                bool invalidCase = true;

                if (IsValidValue(kind))
                {
                    if ((kind.HasFlag(ResourcePropertyKind.Primitive) && type.ResourceTypeKind == ResourceTypeKind.Primitive && type != namedStreamType) ||
                        (kind.HasFlag(ResourcePropertyKind.ComplexType) && type.ResourceTypeKind == ResourceTypeKind.ComplexType) ||
                        (kind.HasFlag(ResourcePropertyKind.ResourceReference) && type.ResourceTypeKind == ResourceTypeKind.EntityType) ||
                        (kind.HasFlag(ResourcePropertyKind.ResourceSetReference) && type.ResourceTypeKind == ResourceTypeKind.EntityType) ||
                        (kind.HasFlag(ResourcePropertyKind.Collection) && type.ResourceTypeKind == ResourceTypeKind.Collection) ||
                        (kind.HasFlag(ResourcePropertyKind.Stream) && type == namedStreamType))
                    {
                        invalidCase = false;
                    }

                    if ((kind & ResourcePropertyKind.Key) == ResourcePropertyKind.Key &&
                        type.InstanceType.IsGenericType && type.InstanceType.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        invalidCase = true;
                    }
                }

                if (invalidCase)
                {
                    ExceptionUtils.ThrowsException <ArgumentException>(
                        () => new ResourceProperty("TestProperty", kind, type),
                        string.Format("resource property constructor test case. Kind: '{0}', Type: '{1}: {2}'", kind, type.ResourceTypeKind, type.InstanceType));
                }
                else
                {
                    ResourceProperty p = new ResourceProperty("TestProperty", kind, type);
                    Assert.AreEqual("TestProperty", p.Name, "Name was not stored properly");
                    Assert.AreEqual(kind, p.Kind, "Kind was not stored properly");
                    Assert.AreEqual(type, p.ResourceType, "Type was not stored properly");
                    Assert.AreEqual(p.Kind.HasFlag(ResourcePropertyKind.Stream) ? false : true, p.CanReflectOnInstanceTypeProperty, "CanReflectOnInstanceTypeProperty should be true by default on non-NamedStreams, and false on NamedStreams.");
                    Assert.IsNull(p.MimeType, "MimeType should be null by default.");

                    if (p.Kind.HasFlag(ResourcePropertyKind.Stream))
                    {
                        ExceptionUtils.ThrowsException <InvalidOperationException>(
                            () => { p.CanReflectOnInstanceTypeProperty = false; },
                            "CanReflectOnInstanceTypeProperty should be settable on non-namedstreams, and not settable on namedstreams");
                    }
                    else
                    {
                        p.CanReflectOnInstanceTypeProperty = false;
                    }

                    Assert.AreEqual(false, p.CanReflectOnInstanceTypeProperty, "CanReflectOnInstanceTypeProperty should be false.");

                    bool shouldFail = true;
                    if ((kind & ResourcePropertyKind.Primitive) == ResourcePropertyKind.Primitive)
                    {
                        shouldFail = false;
                    }

                    if (shouldFail)
                    {
                        ExceptionUtils.ThrowsException <InvalidOperationException>(
                            () => p.MimeType = "plain/text",
                            string.Format("Setting MimeType on non-primitive property should fail. Kind: '{0}', Type: '{1}: {2}'", kind, type.ResourceTypeKind, type.InstanceType));
                    }
                    else
                    {
                        p.MimeType = "plain/text";
                    }
                }
            });
        }
        public void CollectionTypeValidation()
        {
            ResourceType entityType   = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "foo", "Order", false);
            ResourceType complexType  = new ResourceType(typeof(object), ResourceTypeKind.ComplexType, null, "foo", "bar", false);
            ResourceType complexType2 = new ResourceType(typeof(object), ResourceTypeKind.ComplexType, null, "foo", "bar2", false);

            complexType2.AddProperty(new ResourceProperty("CollectionProperty", ResourcePropertyKind.Collection, ResourceType.GetCollectionResourceType(complexType)));

            var itemTypes = new ResourceType[] { complexType, complexType2 }.Concat(ResourceTypeUtils.GetPrimitiveResourceTypes());
            var collectionTypes = itemTypes.Except(new[] { ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)) }).Select(it => new { ItemType = it, CollectionType = ResourceType.GetCollectionResourceType(it) });

            AstoriaTestNS.TestUtil.RunCombinations(
                collectionTypes,
                c =>
            {
                Assert.AreEqual(c.ItemType, c.CollectionType.ItemType, "The item type of the collection doesn't match the one specified upon creation.");
                Assert.AreEqual("Collection(" + c.ItemType.FullName + ")", c.CollectionType.FullName, "The full name of a collection type is wrong.");
                Assert.AreEqual("Collection(" + c.ItemType.FullName + ")", c.CollectionType.Name, "The name of a collection type is wrong.");
                Assert.AreEqual("", c.CollectionType.Namespace, "The namespace of a collection type should be empty.");
                Assert.IsTrue(c.CollectionType.IsReadOnly, "The collection type is always read-only.");
                Assert.IsFalse(c.CollectionType.IsAbstract, "Collection type is never abstract.");
                Assert.IsFalse(c.CollectionType.IsOpenType, "Collection type is never open.");
                Assert.IsFalse(c.CollectionType.IsMediaLinkEntry, "Collection type is never an MLE.");
                Assert.AreEqual(typeof(IEnumerable <>).MakeGenericType(c.ItemType.InstanceType), c.CollectionType.InstanceType, "The instance type of the collection type is wrong.");
                Assert.IsTrue(c.CollectionType.CanReflectOnInstanceType, "Collection type has CanReflectOnInstanceType always true.");
                Assert.IsNull(c.CollectionType.BaseType, "Collection type has never a base type.");
                Assert.AreEqual(ResourceTypeKind.Collection, c.CollectionType.ResourceTypeKind, "The kind of a collection type is always Collection.");
                Assert.AreEqual(0, c.CollectionType.PropertiesDeclaredOnThisType.Count(), "Collection type has no properties.");
                Assert.AreEqual(0, c.CollectionType.Properties.Count(), "Collection type has no properties.");
                Assert.AreEqual(0, c.CollectionType.KeyProperties.Count(), "Collection type has no properties.");
                Assert.AreEqual(0, c.CollectionType.ETagProperties.Count(), "Collection type has no properties.");
                Assert.IsNull(c.CollectionType.CustomState, "Custom state should be null by default.");

                ExceptionUtils.ThrowsException <InvalidOperationException>(
                    () => c.CollectionType.IsMediaLinkEntry = true,
                    "Setting MLE on collection type should fail.");
                ExceptionUtils.ThrowsException <InvalidOperationException>(
                    () => c.CollectionType.IsOpenType = true,
                    "Setting IsOpenType on collection type should fail.");
                ExceptionUtils.ThrowsException <InvalidOperationException>(
                    () => c.CollectionType.CanReflectOnInstanceType = false,
                    "Setting CanReflectOnInstanceType on collection type should fail.");
                // Setting a custom state should still work
                c.CollectionType.CustomState = "some value";
                Assert.AreEqual("some value", c.CollectionType.CustomState, "Custom state doesn't persist its value.");

                ExceptionUtils.ThrowsException <InvalidOperationException>(
                    () => c.CollectionType.AddProperty(new ResourceProperty("ID", ResourcePropertyKind.ComplexType, complexType)),
                    "Adding a property on collection type should fail.");

                c.CollectionType.SetReadOnly();
                Assert.IsTrue(c.CollectionType.IsReadOnly, "The collection type is always read-only.");
            });

            // Verify that only primitive and complex types are allowed as items in a collection
            ResourceType collectionType = ResourceType.GetCollectionResourceType(complexType);

            foreach (var t in new ResourceType[] { entityType, collectionType })
            {
                Exception exception = AstoriaTestNS.TestUtil.RunCatching(() => ResourceType.GetCollectionResourceType(t));
                Assert.IsTrue(exception is ArgumentException, "Exception of a wrong type");
                Assert.AreEqual(
                    "Only collection properties that contain primitive types or complex types are supported.",
                    exception.Message, "Wrong exception message.");
            }
        }
示例#3
0
        public void ServiceOperationConstructorInvalidTests()
        {
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "The 'resultType' parameter must be null when the 'resultKind' parameter value is 'Void', however the 'resultType' parameter cannot be null when the 'resultKind' parameter is of any value other than 'Void'. Please make sure that the 'resultKind' parameter value is set according to the 'resultType' parameter value.",
                "op", ServiceOperationResultKind.Void, ResourceType.GetPrimitiveResourceType(typeof(int)), null, "GET", null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "The 'resultType' parameter must be null when the 'resultKind' parameter value is 'Void', however the 'resultType' parameter cannot be null when the 'resultKind' parameter is of any value other than 'Void'. Please make sure that the 'resultKind' parameter value is set according to the 'resultType' parameter value.",
                "op", ServiceOperationResultKind.DirectValue, null, null, "GET", null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "A parameter with the name 'p1' already exists. Please make sure that every parameter has a unique name.",
                "op", ServiceOperationResultKind.Void, null, null, "GET",
                new ServiceOperationParameter[] {
                new ServiceOperationParameter("p1", ResourceType.GetPrimitiveResourceType(typeof(int))),
                new ServiceOperationParameter("p1", ResourceType.GetPrimitiveResourceType(typeof(string)))
            });
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "The resource type 'Collection(foo.Customer)' is not a type that can be returned by a service operation. A service operation can only return values of an entity type, a complex type or any primitive type, other than the stream type.",
                "op", ServiceOperationResultKind.QueryWithMultipleResults, ResourceType.GetEntityCollectionResourceType(new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "foo", "Customer", false)), null, "GET", null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "The resource type 'Edm.Stream' is not a type that can be returned by a service operation. A service operation can only return values of an entity type, a complex type or any primitive type, other than the stream type.",
                "op", ServiceOperationResultKind.DirectValue, ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)), null, "GET", null);

            ResourceProperty p            = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int)));
            ResourceType     customerType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "NoBaseType", false);

            customerType.AddProperty(p);
            customerType.SetReadOnly();
            ResourceSet  customerSet = new ResourceSet("Customers", customerType);
            ResourceType orderType   = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "NoBaseType", false);

            orderType.AddProperty(p);
            orderType.SetReadOnly();

            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "'resultSet' must be null when 'resultType' is null or not an EntityType.",
                "op", ServiceOperationResultKind.Void, null, customerSet, "GET", null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "'resultSet' must be null when 'resultType' is null or not an EntityType.",
                "op", ServiceOperationResultKind.DirectValue, ResourceType.GetPrimitiveResourceType(typeof(int)), customerSet, "GET", null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "When 'resultType' is an entity type, 'resultSet' cannot be null and the resource type of 'resultSet' must be assignable from 'resultType'.",
                "op", ServiceOperationResultKind.QueryWithMultipleResults, customerType, null, "GET", null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "When 'resultType' is an entity type, 'resultSet' cannot be null and the resource type of 'resultSet' must be assignable from 'resultType'.",
                "op", ServiceOperationResultKind.QueryWithMultipleResults, orderType, customerSet, "GET", null);

            ExceptionUtils.ThrowsException <InvalidOperationException>(
                () =>
            {
                ServiceOperation op = new ServiceOperation("op", ServiceOperationResultKind.Void, null, null, "GET", null);
                op.MimeType         = null;
            },
                "MimeType cannot be set to null");

            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "Value cannot be null or empty.\r\nParameter name: method",
                "op", ServiceOperationResultKind.DirectValue, ResourceType.GetPrimitiveResourceType(typeof(int)), null, null, null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "Value cannot be null or empty.\r\nParameter name: method",
                "op", ServiceOperationResultKind.DirectValue, ResourceType.GetPrimitiveResourceType(typeof(int)), null, string.Empty, null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "Value cannot be null or empty.\r\nParameter name: name",
                null, ServiceOperationResultKind.DirectValue, ResourceType.GetPrimitiveResourceType(typeof(int)), null, "GET", null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "Value cannot be null or empty.\r\nParameter name: name",
                string.Empty, ServiceOperationResultKind.DirectValue, ResourceType.GetPrimitiveResourceType(typeof(int)), null, "GET", null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "An invalid HTTP method 'PUT' was specified for the service operation 'op'. Only the HTTP 'POST' and 'GET' methods are supported for service operations.",
                "op", ServiceOperationResultKind.Void, null, null, "PUT", null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "An invalid HTTP method 'PATCH' was specified for the service operation 'op'. Only the HTTP 'POST' and 'GET' methods are supported for service operations.",
                "op", ServiceOperationResultKind.Void, null, null, "PATCH", null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "An invalid HTTP method 'DELETE' was specified for the service operation 'op'. Only the HTTP 'POST' and 'GET' methods are supported for service operations.",
                "op", ServiceOperationResultKind.Void, null, null, "DELETE", null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "An invalid HTTP method 'HEAD' was specified for the service operation 'op'. Only the HTTP 'POST' and 'GET' methods are supported for service operations.",
                "op", ServiceOperationResultKind.Void, null, null, "HEAD", null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ServiceOperation),
                "An invalid HTTP method 'Some Method' was specified for the service operation 'op'. Only the HTTP 'POST' and 'GET' methods are supported for service operations.",
                "op", ServiceOperationResultKind.Void, null, null, "Some Method", null);
        }