<commit_before>﻿namespace Georadix.WebApi
{
    using Georadix.WebApi.Filters;
    using Georadix.WebApi.Testing;
    using Newtonsoft.Json;
    using SimpleInjector;
    using System.ComponentModel.DataAnnotations;
    using System.Diagnostics.CodeAnalysis;
    using System.Net;
    using System.Net.Http;
    using System.Threading.Tasks;
    using System.Web.Http;
    using Xunit;

    public class ValidateModelAttributeFixture
    {
        [Fact]
        public async Task ExecuteActionWithInvalidModelReturnsModelState()
        {
            using (var server = new InMemoryServer(new Container(), this.ConfigureServer))
            {
                server.Container.Register<ValidateModelAttributeFixtureController>();

                var model = new ValidateModelAttributeFixtureModel() { Name = string.Empty };

                var response = await server.Client.PostAsJsonAsync("entities", model);

                Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);

                var responseContent = await response.Content.ReadAsStringAsync();

                Assert.Contains("The name cannot be empty", responseContent);
            }
        }

        [Fact]
        public async Task ExecuteActionWithValidModelReturnsContent()
        {
            using (var server = new InMemoryServer(new Container(), this.ConfigureServer))
            {
                server.Container.Register<ValidateModelAttributeFixtureController>();

                var model = new ValidateModelAttributeFixtureModel() { Name = "test" };

                var response = await server.Client.PostAsJsonAsync("entities", model);

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);

                var responseContent = await response.Content.ReadAsStringAsync();

                Assert.Contains(model.Name, responseContent);
            }
        }

        private void ConfigureServer(InMemoryServer server)
        {
            server.Configuration.Filters.Add(new ValidateModelAttribute());

            server.Configuration.MapHttpAttributeRoutes();
        }
    }

    [SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass",
        Justification = "Embedding this controller class inside the fixture causes routing to its methods to fail.")]
    [RoutePrefix("entities")]
    public class ValidateModelAttributeFixtureController : ApiController
    {
        [Route("")]
        public string Post(ValidateModelAttributeFixtureModel model)
        {
            return model.Name;
        }
    }

    public class ValidateModelAttributeFixtureModel
    {
        [Required(AllowEmptyStrings = false, ErrorMessage = "The name cannot be empty.")]
        public string Name { get; set; }
    }
}<commit_msg>Reorganized the server container setup.<commit_after>﻿namespace Georadix.WebApi
{
    using Georadix.WebApi.Filters;
    using Georadix.WebApi.Testing;
    using Newtonsoft.Json;
    using SimpleInjector;
    using System.ComponentModel.DataAnnotations;
    using System.Diagnostics.CodeAnalysis;
    using System.Net;
    using System.Net.Http;
    using System.Threading.Tasks;
    using System.Web.Http;
    using Xunit;

    public class ValidateModelAttributeFixture
    {
        [Fact]
        public async Task ExecuteActionWithInvalidModelReturnsModelState()
        {
            using (var server = this.ServerFactory())
            {
                var model = new ValidateModelAttributeFixtureModel() { Name = string.Empty };

                var response = await server.Client.PostAsJsonAsync("entities", model);

                Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);

                var responseContent = await response.Content.ReadAsStringAsync();

                Assert.Contains("The name cannot be empty", responseContent);
            }
        }

        [Fact]
        public async Task ExecuteActionWithValidModelReturnsContent()
        {
            using (var server = this.ServerFactory())
            {
                var model = new ValidateModelAttributeFixtureModel() { Name = "test" };

                var response = await server.Client.PostAsJsonAsync("entities", model);

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);

                var responseContent = await response.Content.ReadAsStringAsync();

                Assert.Contains(model.Name, responseContent);
            }
        }

        private void ConfigureServer(InMemoryServer server)
        {
            server.Configuration.Filters.Add(new ValidateModelAttribute());

            server.Configuration.MapHttpAttributeRoutes();
        }

        private InMemoryServer ServerFactory()
        {
            var container = new Container();

            container.Register<ValidateModelAttributeFixtureController>();

            var server = new InMemoryServer(container, this.ConfigureServer);

            return server;
        }
    }

    [SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass",
        Justification = "Embedding this controller class inside the fixture causes routing to its methods to fail.")]
    [RoutePrefix("entities")]
    public class ValidateModelAttributeFixtureController : ApiController
    {
        [Route("")]
        public string Post(ValidateModelAttributeFixtureModel model)
        {
            return model.Name;
        }
    }

    public class ValidateModelAttributeFixtureModel
    {
        [Required(AllowEmptyStrings = false, ErrorMessage = "The name cannot be empty.")]
        public string Name { get; set; }
    }
}
{'fraction_non_alphanumeric': 0.08136667840789011, 'fraction_numerical': 0.0014089468122578373, 'mean_word_length': 2.4861878453038675, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6343447', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1370, 'n_words': 309}