public void ConfigureHypermedia(HttpConfiguration httpConfiguration) { // Set up model-controller mapping. new HypermediaConfigurator <Product, ProductsController>(httpConfiguration) .For((model, controller) => controller.Get(model.Id)) .UseSirenSpecification() .Map((model, controller) => controller.Get(model.Id)) .AsSelfLink() .Map((model, controller) => controller.Get()) .AsParentLink() .Map((model, controller) => controller.Get(QueryParameter.Is <string>(), QueryParameter.Is <int>(), QueryParameter.Is <int>())) .AsAction() .Map((model, controller) => controller.Get(model.Name)) .Map((model, controller) => controller.Post(model)) .Map((model, controller) => controller.Put(model.Id, model)) .Map((model, controller) => controller.Delete(model.Id)) .MapReference <ProductDetailsController>((model, referencedController) => referencedController.GetByProductId(model.Id)) .AsLink() .MapEmbeddedEntity <Models.ProductDetails, ProductDetailsController>(model => model.ProductDetailsFromModel.First(), (model, controller) => controller.GetByProductId(model.Id)).WitRel("details") .MapEmbeddedEntity <Models.Product, ProductsController>(model => model.ThisProduct, (model, controller) => controller.Get(model.Name)).WitRel("product") .For((model, controller) => controller.Get(model.Name)) .UseSirenSpecification() .MapReference <ProductDetailsController>((model, referencedController) => referencedController.GetByProductId(model.Id)) .AsLink() .Map((model, controller) => controller.Get()) .AsParentLink() .Map((model, controller) => controller.Get(QueryParameter.Is <string>(), QueryParameter.Is <int>(), QueryParameter.Is <int>())) .AsAction() .Map((model, controller) => controller.Get(model.Id)) .Map((model, controller) => controller.Get(model.Name)) .AsSelfLink() .Map((model, controller) => controller.Post(model)) .Map((model, controller) => controller.Put(model.Id, model)) .Map((model, controller) => controller.Delete(model.Id)) .For((model, controller) => controller.Get()) .UseSirenSpecification() .Map((model, controller) => controller.Get()) .AsSelfLink() .Map((model, controller) => controller.Get(model.Id)) .MapReference <ProductDetailsController>((model, referencedController) => referencedController.GetByProductId(model.Id)) .AsAction() .For((model, controller) => controller.Post(model)) .UseSirenSpecification() .Map((model, controller) => controller.Get(model.Id)) .MapReference <ProductDetailsController>((model, referencedController) => referencedController.GetByProductId(model.Id)) .For((model, controller) => controller.Get(QueryParameter.Is <string>(), QueryParameter.Is <int>(), QueryParameter.Is <int>())) .UseSirenSpecification() .Map((model, controller) => controller.Get()) .AsSelfLink() .MapReference <ProductDetailsController>((model, referencedController) => referencedController.GetByProductId(model.Id)) .AsAction() .Configure(); }
public void ReturnModel() { 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 mappingRule = new MappingRule(methodCallExpression, null); var result = _routeNameBuilder.Build(mappingRule, "get"); Assume.That(result, Is.EquivalentTo(new[] { "get_modelsample_by_id_name_query_skip" })); }
public void BuildExtended() { Expression <Func <ModelSample, ControllerSample, ModelSample> > expression = (model, controllerFixture) => controllerFixture.ControllerMethod(model.Id, model.Name, QueryParameter.Is <string>(), QueryParameter.Is <int>()); var result = ParametersDelegateBuilder.Build(expression.Body as MethodCallExpression); Assume.That(result.Count, Is.EqualTo(4)); Assume.That(result.Keys, Is.EquivalentTo(new [] { "id", "name", "query", "skip" })); var modelFixture = _fixture.CreateAnonymous <ModelSample>(); Assume.That(result["id"].DynamicInvoke(modelFixture), Is.EqualTo(modelFixture.Id)); Assume.That(result["name"].DynamicInvoke(modelFixture), Is.EqualTo(modelFixture.Name)); Assume.That(result["query"].DynamicInvoke(modelFixture), Is.EqualTo(":query")); Assume.That(result["skip"].DynamicInvoke(modelFixture), Is.EqualTo(":skip")); }
public void BuildWrongExpressionMethod() { Expression <Func <ModelSample, ControllerSample, ModelSample> > expression = (model, controllerFixture) => controllerFixture.ControllerMethod(ControllerSample.SomeMethod(), model.Name, QueryParameter.Is <string>(), QueryParameter.Is <int>()); ParametersDelegateBuilder.Build(expression.Body as MethodCallExpression); }
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 Setup() { _fixture = new Fixture(); _fixture.Customize(new AutoMoqCustomization()); _fixture.Customize(new RandomNumericSequenceCustomization()); Expression <Func <ControllerSample, ModelSample, IEnumerable <ModelSample> > > lambda = (c, m) => c.ControllerQueryMethod(m.Id, m.Name, QueryParameter.Is <string>(), QueryParameter.Is <int>()); Expression <Func <ControllerSample, ModelSample, ModelSample> > lambda2 = (c, m) => c.ControllerMethodPut(m.Id, m); var apiExplorerMoq = new Mock <IApiExplorer>(); apiExplorerMoq.Setup(_ => _.ApiDescriptions).Returns(new Collection <ApiDescription>() { new ApiDescription() { } }); var actionConfiguration = new Mock <IActionConfiguration>(); actionConfiguration.Setup(_ => _.MappingRules).Returns(() => new List <MappingRule> { new MappingRule((MethodCallExpression)lambda.Body, apiExplorerMoq.Object) { ApiDescriptions = new List <ApiDescription>() { new ApiDescription() { RelativePath = "/api/{id}?query={query}", HttpMethod = HttpMethod.Get } }, Type = MappingRule.RuleType.Default }, new MappingRule((MethodCallExpression)lambda2.Body, apiExplorerMoq.Object) { ApiDescriptions = new List <ApiDescription>() { new ApiDescription() { RelativePath = "/api/prod/{id}", HttpMethod = HttpMethod.Put } }, Type = MappingRule.RuleType.Default } }); _actionConfiguration = actionConfiguration.Object; }
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>(); 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" } }); var mappingRule = new MappingRule(methodCallExpression, apiExplorerMoq.Object); _actionConfiguration.AddMappingRule(mappingRule); _actionConfiguration.Configure(); var originalType = typeof(ModelSample); var strategy = _defaultStrategyFactory.Build(_actionConfiguration, originalType); Assume.That(strategy.ClassKey(originalType), Is.StringContaining("_NHateoas.Tests.ModelSample_SP_SR")); 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_SP_SR")); var props = type.GetProperties(); Assume.That(props, Is.Not.Empty); var propNames = props.ToList().ConvertAll(p => p.Name); Assume.That(propNames, Is.EquivalentTo(new[] { "Id", "Name", "Price", "EMailAddress", "get_modelsample_by_id_name_query_skip" })); var propTypes = props.ToList().ConvertAll(p => p.PropertyType.Name); Assume.That(propTypes, Is.EquivalentTo(new[] { "Int32", "String", "Double", "String", "String" })); var instance = Activator.CreateInstance(type); var original = _fixture.CreateAnonymous <ModelSample>(); strategy.ActivateInstance(instance, original, _actionConfiguration); var propValues = props.ToList().ConvertAll(p => p.GetValue(instance).ToString()); Assume.That(propValues, Is.EquivalentTo(new[] { original.Id.ToString(), original.Name, original.Price.ToString(), original.EMailAddress, "/api" })); }
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 TestNotFromBodyGet() { 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() { HttpMethod = HttpMethod.Get } }); var mr = new MappingRule(methodCallExpression, apiExplorerMoq.Object); var payload = _fixture.CreateAnonymous <ModelSample>(); var fields = ActionFieldsGenerator.Generate(mr, apiExplorerMoq.Object.ApiDescriptions[0], payload); Assume.That(fields, Is.Not.Null); var names = fields.ConvertAll(f => f.FieldName); Assume.That(names, Is.EquivalentTo(new [] { "id", "name", "query", "skip" })); var types = fields.ConvertAll(f => f.FieldType); Assume.That(types, Is.All.Null); var values = fields.ConvertAll(f => f.FieldValue); Assume.That(values, Is.EqualTo(new object[] { payload.Id.ToString(), payload.Name, null, null })); }
public void TestNotFromBodyNotGet() { 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 = _fixture.CreateAnonymous <ModelSample>(); var fields = ActionFieldsGenerator.Generate(mr, apiExplorerMoq.Object.ApiDescriptions[0], payload); Assume.That(fields, Is.Null); }