public void TestSubstitution()
        {
            var sub = new DefaultRouteValueSubstitution();
            Expression<Func<ControllerSample, ModelSample, ModelSample>> lambda = (c, m)
                => c.ControllerMethod(m.Id, m.Name, QueryParameter.Is<string>(), QueryParameter.Is<int>());

            var methodCallExpression = (MethodCallExpression)lambda.Body;

            var apiExplorerMoq = new Mock<IApiExplorer>();
            apiExplorerMoq.Setup(_ => _.ApiDescriptions).Returns(new Collection<ApiDescription>()
            {
                new ApiDescription()
                {
                }
            });

            var mr = new MappingRule(methodCallExpression, apiExplorerMoq.Object);

            var payload = new ModelSample()
            {
                Id = 1,
                Name = "test &?{}<>",
                Price = 3.2
            };

            var result = sub.Substitute("/Product/{id}/Details?query={query}&skip={skip}&displayname={name}", mr, payload);

            Assume.That(result, Is.EqualTo("/Product/1/Details?query=:query&skip=:skip&displayname=test+%26%3f%7b%7d%3c%3e"));
        }
        public void TestSubstitution()
        {
            var sub = new DefaultRouteValueSubstitution();
            Expression <Func <ControllerSample, ModelSample, ModelSample> > lambda = (c, m)
                                                                                     => c.ControllerMethod(m.Id, m.Name, QueryParameter.Is <string>(), QueryParameter.Is <int>());

            var methodCallExpression = (MethodCallExpression)lambda.Body;

            var apiExplorerMoq = new Mock <IApiExplorer>();

            apiExplorerMoq.Setup(_ => _.ApiDescriptions).Returns(new Collection <ApiDescription>()
            {
                new ApiDescription()
                {
                }
            });

            var mr = new MappingRule(methodCallExpression, apiExplorerMoq.Object);

            var payload = new ModelSample()
            {
                Id    = 1,
                Name  = "test &?{}<>",
                Price = 3.2
            };

            var result = sub.Substitute("/Product/{id}/Details?query={query}&skip={skip}&displayname={name}", mr, payload);

            Assume.That(result, Is.EqualTo("/Product/1/Details?query=:query&skip=:skip&displayname=test+%26%3f%7b%7d%3c%3e"));
        }
Ejemplo n.º 3
0
        public void GetData(Action <ModelSample, Exception> callback)
        {
            // Use this to create design time data

            var item = new ModelSample("Welcome to MVVM Light [design]");

            callback(item, null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Run a read/write example with simple model executing
        /// code on incoming data to generate outgoing data
        /// </summary>
        private static void RunAModel()
        {
            Console.WriteLine("Running model");
            var model = new ModelSample();

            model.Run();
            Console.WriteLine("Running model finished");
        }
Ejemplo n.º 5
0
        public void ComplexTest()
        {
            var routeRelations = new Dictionary<string, List<string>>();
            var payload = new ModelSample() {Id = 1, Name = "test", Price = 5.0, EMailAddress = "*****@*****.**"};

            routeRelations.Add("GET/api/{id}?query={query}", new List<string>()
            {
                "get-query"
            });
            routeRelations.Add("PUT/api/prod/{id}", new List<string>()
            {
                "put-prod"
            });
            var links = LinksGenerator.Generate(_actionConfiguration, routeRelations, payload);

            var serialized = JsonConvert.SerializeObject(links);
            Assume.That(serialized, Is.EqualTo("[{\"rel\":[\"get-query\",\"__query\"],\"href\":\"http://localhost/api/1?query=:query\"}]"));
        }
Ejemplo n.º 6
0
        public void ComplexTest()
        {
            var routeRelations = new Dictionary<string, List<string>>();
            var payload = new ModelSample() {Id = 1, Name = "test", Price = 5.0, EMailAddress = "*****@*****.**"};

            routeRelations.Add("GET/api/{id}?query={query}", new List<string>()
            {
                "get-query"
            });
            routeRelations.Add("PUT/api/prod/{id}", new List<string>()
            {
                "put-prod"
            });
            var actions = ActionsGenerator.Generate(_actionConfiguration, routeRelations, payload);

            var serialized = JsonConvert.SerializeObject(actions);
            Assume.That(serialized, Is.EqualTo("[{\"name\":\"get-query\",\"class\":[\"__query\"],\"method\":\"GET\",\"href\":\"http://localhost/api/1?query=:query\",\"fields\":[{\"name\":\"id\",\"value\":\"1\"},{\"name\":\"name\",\"value\":\"test\"},{\"name\":\"query\"},{\"name\":\"skip\"}]},{\"name\":\"put-prod\",\"method\":\"PUT\",\"href\":\"http://localhost/api/prod/1\",\"fields\":[{\"name\":\"Id\",\"value\":\"1\"},{\"name\":\"Name\",\"value\":\"test\"},{\"name\":\"Price\",\"value\":\"5\"},{\"name\":\"email_address\",\"type\":\"email\",\"value\":\"[email protected]\"}]}]"));
        }
Ejemplo n.º 7
0
        public void ComplexTest()
        {
            var routeRelations = new Dictionary <string, List <string> >();
            var payload        = new ModelSample()
            {
                Id = 1, Name = "test", Price = 5.0, EMailAddress = "*****@*****.**"
            };

            routeRelations.Add("GET/api/{id}?query={query}", new List <string>()
            {
                "get-query"
            });
            routeRelations.Add("PUT/api/prod/{id}", new List <string>()
            {
                "put-prod"
            });
            var links = LinksGenerator.Generate(_actionConfiguration, routeRelations, payload);

            var serialized = JsonConvert.SerializeObject(links);

            Assume.That(serialized, Is.EqualTo("[{\"rel\":[\"get-query\",\"__query\"],\"href\":\"http://localhost/api/1?query=:query\"}]"));
        }
Ejemplo n.º 8
0
        public void ComplexTest()
        {
            var routeRelations = new Dictionary <string, List <string> >();
            var payload        = new ModelSample()
            {
                Id = 1, Name = "test", Price = 5.0, EMailAddress = "*****@*****.**"
            };

            routeRelations.Add("GET/api/{id}?query={query}", new List <string>()
            {
                "get-query"
            });
            routeRelations.Add("PUT/api/prod/{id}", new List <string>()
            {
                "put-prod"
            });
            var actions = ActionsGenerator.Generate(_actionConfiguration, routeRelations, payload);

            var serialized = JsonConvert.SerializeObject(actions);

            Assume.That(serialized, Is.EqualTo("[{\"name\":\"get-query\",\"class\":[\"__query\"],\"method\":\"GET\",\"href\":\"http://localhost/api/1?query=:query\",\"fields\":[{\"name\":\"id\",\"value\":\"1\"},{\"name\":\"name\",\"value\":\"test\"},{\"name\":\"query\"},{\"name\":\"skip\"}]},{\"name\":\"put-prod\",\"method\":\"PUT\",\"href\":\"http://localhost/api/prod/1\",\"fields\":[{\"name\":\"Id\",\"value\":\"1\"},{\"name\":\"Name\",\"value\":\"test\"},{\"name\":\"Price\",\"value\":\"5\"},{\"name\":\"email_address\",\"type\":\"email\",\"value\":\"[email protected]\"}]}]"));
        }
        public void Complex()
        {
            Expression<Func<ControllerSample, ModelSample, ModelSample>> lambda = (test, model)
                => test.ControllerMethod(model.Id, model.Name, QueryParameter.Is<string>(), QueryParameter.Is<int>());
            var methodCallExpression = (MethodCallExpression)lambda.Body;

            var httpControllerDescriptor = _fixture.CreateAnonymous<HttpControllerDescriptor>();

            Expression<Func<ControllerSample, int>> lambda2 = (test) => test.FakeMethodWithAttribute();

            var apiExplorerMoq = new Mock<IApiExplorer>();
            apiExplorerMoq.Setup(_ => _.ApiDescriptions).Returns(new Collection<ApiDescription>()
            {
                new ApiDescription()
                {
                    ActionDescriptor = new ReflectedHttpActionDescriptor(httpControllerDescriptor, methodCallExpression.Method),
                    HttpMethod = HttpMethod.Get,
                    RelativePath = "/api"
                },
                new ApiDescription()
                {
                    ActionDescriptor = new ReflectedHttpActionDescriptor(httpControllerDescriptor, ((MethodCallExpression)lambda2.Body).Method),
                    HttpMethod = HttpMethod.Post,
                    RelativePath = "/api/test"

                }
            });

            _actionConfiguration.AddMappingRule(new MappingRule(methodCallExpression, apiExplorerMoq.Object));

            var rule = new MappingRule((MethodCallExpression)lambda2.Body, apiExplorerMoq.Object)
            {
                Type = MappingRule.RuleType.ActionRule
            };
            rule.Names.Add("action-name");
            _actionConfiguration.AddMappingRule(rule);

            _actionConfiguration.UseSirenSpecification();
            _actionConfiguration.Configure();
            var originalType = typeof(ModelSample);
            var strategy = _defaultStrategyFactory.Build(_actionConfiguration, originalType);
            Assume.That(strategy.ClassKey(originalType), Is.StringContaining("_NHateoas.Tests.ModelSample_PP"));

            var typeBuilder = new TypeBuilder(originalType, strategy);
            var type = typeBuilder.BuildType();
            Assume.That(type.Name, Is.EqualTo(originalType.Name));
            Assume.That(type.FullName, Is.StringContaining("_NHateoas.Tests.ModelSample_PP"));

            var props = type.GetProperties();
            Assume.That(props, Is.Not.Empty);

            var propNames = new List<string>();
            props.ToList().ForEach(p =>
            {   propNames.Add(p.Name);

                if (p.PropertyType == typeof(string))
                    return;

                if (p.PropertyType.IsArray && p.PropertyType.GetElementType() == typeof(string))
                    return;

                var pt = (p.PropertyType.BaseType != null && p.PropertyType.BaseType.IsGenericType) ?
                    p.PropertyType.BaseType.GetGenericArguments()[0] : p.PropertyType;
                pt.GetProperties().ToList().ForEach(sp => propNames.Add(sp.Name));
            });

            Assume.That(propNames, Is.EquivalentTo(new[] { "properties", "Id", "Name", "Price", "EMailAddress", "class", "href","rel", "links", "RelList", "Href", "actions", "ActionName", "Class", "Title", "Method", "Href", "ContentType", "ActionFields" }));
            var propTypes = props.Where(p => !p.PropertyType.IsArray && p.PropertyType != typeof(string)).ToList().ConvertAll(p => p.PropertyType.Name);
            Assume.That(propTypes, Is.EquivalentTo(new[] { "ModelSample", "Links", "Actions"}));

            var instance = Activator.CreateInstance(type);
            var original = new ModelSample()
            {
                Id = 1,
                Name = "test",
                Price = 3.0,
                EMailAddress = "aa.bb@ccc"
            };
            strategy.ActivateInstance(instance, original, _actionConfiguration);

            var result = JsonConvert.SerializeObject(instance);
            Assume.That(result, Is.EqualTo("{\"properties\":{\"Id\":1,\"Name\":\"test\",\"Price\":3.0,\"EMailAddress\":\"aa.bb@ccc\"},\"links\":[{\"rel\":[\"get_modelsample_by_id_name_query_skip\"],\"href\":\"http://localhost/api\"}],\"actions\":[{\"name\":\"rel-name\",\"method\":\"POST\",\"href\":\"http://localhost/api/test\",\"type\":\"application/x-www-form-urlencoded\"}]}"));
        }
        public void Complex()
        {
            Expression <Func <ControllerSample, ModelSample, ModelSample> > lambda = (test, model)
                                                                                     => test.ControllerMethod(model.Id, model.Name, QueryParameter.Is <string>(), QueryParameter.Is <int>());
            var methodCallExpression = (MethodCallExpression)lambda.Body;


            var httpControllerDescriptor = _fixture.CreateAnonymous <HttpControllerDescriptor>();

            Expression <Func <ControllerSample, int> > lambda2 = (test) => test.FakeMethodWithAttribute();

            var apiExplorerMoq = new Mock <IApiExplorer>();

            apiExplorerMoq.Setup(_ => _.ApiDescriptions).Returns(new Collection <ApiDescription>()
            {
                new ApiDescription()
                {
                    ActionDescriptor = new ReflectedHttpActionDescriptor(httpControllerDescriptor, methodCallExpression.Method),
                    HttpMethod       = HttpMethod.Get,
                    RelativePath     = "/api"
                },
                new ApiDescription()
                {
                    ActionDescriptor = new ReflectedHttpActionDescriptor(httpControllerDescriptor, ((MethodCallExpression)lambda2.Body).Method),
                    HttpMethod       = HttpMethod.Post,
                    RelativePath     = "/api/test"
                }
            });

            _actionConfiguration.AddMappingRule(new MappingRule(methodCallExpression, apiExplorerMoq.Object));


            var rule = new MappingRule((MethodCallExpression)lambda2.Body, apiExplorerMoq.Object)
            {
                Type = MappingRule.RuleType.ActionRule
            };

            rule.Names.Add("action-name");
            _actionConfiguration.AddMappingRule(rule);

            _actionConfiguration.UseSirenSpecification();
            _actionConfiguration.Configure();
            var originalType = typeof(ModelSample);
            var strategy     = _defaultStrategyFactory.Build(_actionConfiguration, originalType);

            Assume.That(strategy.ClassKey(originalType), Is.StringContaining("_NHateoas.Tests.ModelSample_PP"));

            var typeBuilder = new TypeBuilder(originalType, strategy);
            var type        = typeBuilder.BuildType();

            Assume.That(type.Name, Is.EqualTo(originalType.Name));
            Assume.That(type.FullName, Is.StringContaining("_NHateoas.Tests.ModelSample_PP"));

            var props = type.GetProperties();

            Assume.That(props, Is.Not.Empty);

            var propNames = new List <string>();

            props.ToList().ForEach(p =>
                                   { propNames.Add(p.Name);

                                     if (p.PropertyType == typeof(string))
                                     {
                                         return;
                                     }

                                     if (p.PropertyType.IsArray && p.PropertyType.GetElementType() == typeof(string))
                                     {
                                         return;
                                     }

                                     var pt = (p.PropertyType.BaseType != null && p.PropertyType.BaseType.IsGenericType) ?
                                              p.PropertyType.BaseType.GetGenericArguments()[0] : p.PropertyType;
                                     pt.GetProperties().ToList().ForEach(sp => propNames.Add(sp.Name)); });

            Assume.That(propNames, Is.EquivalentTo(new[] { "properties", "Id", "Name", "Price", "EMailAddress", "class", "href", "rel", "links", "RelList", "Href", "actions", "ActionName", "Class", "Title", "Method", "Href", "ContentType", "ActionFields" }));
            var propTypes = props.Where(p => !p.PropertyType.IsArray && p.PropertyType != typeof(string)).ToList().ConvertAll(p => p.PropertyType.Name);

            Assume.That(propTypes, Is.EquivalentTo(new[] { "ModelSample", "Links", "Actions" }));


            var instance = Activator.CreateInstance(type);
            var original = new ModelSample()
            {
                Id           = 1,
                Name         = "test",
                Price        = 3.0,
                EMailAddress = "aa.bb@ccc"
            };

            strategy.ActivateInstance(instance, original, _actionConfiguration);

            var result = JsonConvert.SerializeObject(instance);

            Assume.That(result, Is.EqualTo("{\"properties\":{\"Id\":1,\"Name\":\"test\",\"Price\":3.0,\"EMailAddress\":\"aa.bb@ccc\"},\"links\":[{\"rel\":[\"get_modelsample_by_id_name_query_skip\"],\"href\":\"http://localhost/api\"}],\"actions\":[{\"name\":\"rel-name\",\"method\":\"POST\",\"href\":\"http://localhost/api/test\",\"type\":\"application/x-www-form-urlencoded\"}]}"));
        }