public void TestThatGetUserDispatches()
    {
        var postRequest            = Request.From(ConsumerByteBuffer(PostJohnDoeUserMessage));
        var postCompletes          = new MockCompletesEventuallyResponse();
        var postCompletesWithCalls = postCompletes.ExpectWithTimes(1);

        Dispatcher.DispatchFor(new Context(postRequest, postCompletes));
        postCompletesWithCalls.ReadFrom <int>("completed");
        Assert.NotNull(postCompletes.Response);

        var getUserMessage        = $"GET {postCompletes.Response.Get().HeaderOf(ResponseHeader.Location).Value} HTTP/1.1\nHost: vlingo.io\n\n";
        var getRequest            = Request.From(ConsumerByteBuffer(getUserMessage));
        var getCompletes          = new MockCompletesEventuallyResponse();
        var getCompletesWithCalls = getCompletes.ExpectWithTimes(1);

        Dispatcher.DispatchFor(new Context(getRequest, getCompletes));
        getCompletesWithCalls.ReadFrom <int>("completed");
        Assert.NotNull(getCompletes.Response);
        Assert.Equal(ResponseStatus.Ok, getCompletes.Response.Get().Status);
        var getUserData = JsonSerialization.Deserialized <UserData>(getCompletes.Response.Get().Entity.Content, _settings);

        Assert.NotNull(getUserData);
        Assert.Equal(JohnDoeUserData.NameData.Given, getUserData.NameData.Given);
        Assert.Equal(JohnDoeUserData.NameData.Family, getUserData.NameData.Family);
        Assert.Equal(JohnDoeUserData.ContactData.EmailAddress, getUserData.ContactData.EmailAddress);
        Assert.Equal(JohnDoeUserData.ContactData.TelephoneNumber, getUserData.ContactData.TelephoneNumber);
    }
    public void TestThatPostRegisterUserDispatches()
    {
        var request   = Request.From(ConsumerByteBuffer(PostJohnDoeUserMessage));
        var completes = new MockCompletesEventuallyResponse();

        var withCalls = completes.ExpectWithTimes(1);

        Dispatcher.DispatchFor(new Context(request, completes));
        withCalls.ReadFrom <int>("completed");

        Assert.NotNull(completes.Response);

        Assert.Equal(ResponseStatus.Created, completes.Response.Get().Status);
        Assert.Equal(2, completes.Response.Get().Headers.Count);
        Assert.Equal(ResponseHeader.Location, completes.Response.Get().Headers[0].Name);
        Assert.StartsWith("/users/", completes.Response.Get().HeaderOf(ResponseHeader.Location).Value);
        Assert.NotNull(completes.Response.Get().Entity);

        var createdUserData = (UserData)JsonSerialization.Deserialized(completes.Response.Get().Entity.Content, typeof(UserData), _settings);

        Assert.NotNull(createdUserData);
        Assert.Equal(JohnDoeUserData.NameData.Given, createdUserData.NameData.Given);
        Assert.Equal(JohnDoeUserData.NameData.Family, createdUserData.NameData.Family);
        Assert.Equal(JohnDoeUserData.ContactData.EmailAddress, createdUserData.ContactData.EmailAddress);
        Assert.Equal(JohnDoeUserData.ContactData.TelephoneNumber, createdUserData.ContactData.TelephoneNumber);
    }
    public void TestThatDispatchesThroughPool()
    {
        for (var count = 0; count < 3; ++count)
        {
            var request   = Request.From(ToConsumerByteBuffer(_postDataMessage));
            var completes = new MockCompletesEventuallyResponse();

            var outcomes = completes.ExpectWithTimes(1);
            _dispatcher.DispatchFor(new Context(request, completes));
            var responseCount = outcomes.ReadFrom <int>("completed");
            var response      = outcomes.ReadFrom <Response>("response");
            Assert.Equal(1, responseCount);

            var responseData = JsonSerialization.Deserialized <Data>(response.Entity.Content);

            Assert.Equal(TestData1, responseData);

            _output.WriteLine("previousResourceHandlerId={0} resourceHandlerId={1}", _previousResourceHandlerId,
                              responseData.ResourceHandlerId);

            Assert.NotEqual(_previousResourceHandlerId, responseData.ResourceHandlerId);

            _previousResourceHandlerId = responseData.ResourceHandlerId;
        }
    }
    public void TestThatPatchNameWorks()
    {
        _output.WriteLine("TestThatPatchNameWorks()");
        var postRequest1            = Request.From(ConsumerByteBuffer(PostJohnDoeUserMessage));
        var postCompletes1          = new MockCompletesEventuallyResponse();
        var postCompletes1WithCalls = postCompletes1.ExpectWithTimes(1);

        Dispatcher.DispatchFor(new Context(postRequest1, postCompletes1));
        postCompletes1WithCalls.ReadFrom <int>("completed");

        Assert.NotNull(postCompletes1.Response);
        _output.WriteLine("1");

        var postRequest2   = Request.From(ConsumerByteBuffer(PostJaneDoeUserMessage));
        var postCompletes2 = new MockCompletesEventuallyResponse();

        var postCompletes2WithCalls = postCompletes2.ExpectWithTimes(1);

        Dispatcher.DispatchFor(new Context(postRequest2, postCompletes2));
        postCompletes2WithCalls.ReadFrom <int>("completed");

        Assert.NotNull(postCompletes2.Response);
        _output.WriteLine("2");

        // John Doe and Jane Doe marry and change their family name to, of course, Doe-Doe
        var johnNameData            = NameData.From("John", "Doe-Doe");
        var johnNameSerialized      = JsonSerialization.Serialized(johnNameData);
        var patchJohnDoeUserMessage =
            $"PATCH {postCompletes1.Response.Get().HeaderOf(ResponseHeader.Location).Value}" +
            $"/name HTTP/1.1\nHost: vlingo.io\nContent-Length: {johnNameSerialized.Length}" +
            $"\n\n{johnNameSerialized}";

        _output.WriteLine($"2.0: {patchJohnDoeUserMessage}");
        var patchRequest1   = Request.From(ConsumerByteBuffer(patchJohnDoeUserMessage));
        var patchCompletes1 = new MockCompletesEventuallyResponse();

        var patchCompletes1WithCalls = patchCompletes1.ExpectWithTimes(1);

        Dispatcher.DispatchFor(new Context(patchRequest1, patchCompletes1));
        patchCompletes1WithCalls.ReadFrom <int>("completed");

        Assert.NotNull(patchCompletes1.Response);
        Assert.Equal(ResponseStatus.Ok, patchCompletes1.Response.Get().Status);
        var getJohnDoeDoeUserData = JsonSerialization.Deserialized <UserData>(patchCompletes1.Response.Get().Entity.Content, _settings);

        Assert.Equal(johnNameData.Given, getJohnDoeDoeUserData.NameData.Given);
        Assert.Equal(johnNameData.Family, getJohnDoeDoeUserData.NameData.Family);
        Assert.Equal(JohnDoeUserData.ContactData.EmailAddress, getJohnDoeDoeUserData.ContactData.EmailAddress);
        Assert.Equal(JohnDoeUserData.ContactData.TelephoneNumber, getJohnDoeDoeUserData.ContactData.TelephoneNumber);

        var janeNameData            = NameData.From("Jane", "Doe-Doe");
        var janeNameSerialized      = JsonSerialization.Serialized(janeNameData);
        var patchJaneDoeUserMessage =
            $"PATCH {postCompletes2.Response.Get().HeaderOf(ResponseHeader.Location).Value}" +
            $"/name HTTP/1.1\nHost: vlingo.io\nContent-Length: {janeNameSerialized.Length}" +
            $"\n\n{janeNameSerialized}";

        var patchRequest2   = Request.From(ConsumerByteBuffer(patchJaneDoeUserMessage));
        var patchCompletes2 = new MockCompletesEventuallyResponse();

        var patchCompletes2WithCalls = patchCompletes2.ExpectWithTimes(1);

        Dispatcher.DispatchFor(new Context(patchRequest2, patchCompletes2));
        patchCompletes2WithCalls.ReadFrom <int>("completed");

        Assert.NotNull(patchCompletes2.Response);
        Assert.Equal(ResponseStatus.Ok, patchCompletes2.Response.Get().Status);
        var getJaneDoeDoeUserData = JsonSerialization.Deserialized <UserData>(patchCompletes2.Response.Get().Entity.Content);

        Assert.Equal(janeNameData.Given, getJaneDoeDoeUserData.NameData.Given);
        Assert.Equal(janeNameData.Family, getJaneDoeDoeUserData.NameData.Family);
        Assert.Equal(JaneDoeUserData.ContactData.EmailAddress, getJaneDoeDoeUserData.ContactData.EmailAddress);
        Assert.Equal(JaneDoeUserData.ContactData.TelephoneNumber, getJaneDoeDoeUserData.ContactData.TelephoneNumber);
    }