public async Task SetAsyncRequestHandlerOverrideTrue()
        {
            // Setup: Create two mock request handlers
            var requestHandler1 = new Mock <Func <CommonObjects.TestMessageContents, RequestContext <CommonObjects.TestMessageContents>, Task> >();
            var requestHandler2 = new Mock <Func <CommonObjects.TestMessageContents, RequestContext <CommonObjects.TestMessageContents>, Task> >();

            // If:
            // ... I assign a request handler on the JSON RPC host
            // ... And I reassign the request handler with an override
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);

            jh.SetAsyncRequestHandler(CommonObjects.RequestType, requestHandler1.Object);
            jh.SetAsyncRequestHandler(CommonObjects.RequestType, requestHandler2.Object, true);

            // Then: There should only be one request handler
            Assert.Single(jh.requestHandlers);
            Assert.Contains(CommonObjects.RequestType.MethodName, jh.requestHandlers.Keys);

            // If: I call the stored request handler
            await jh.requestHandlers[CommonObjects.RequestType.MethodName](CommonObjects.RequestMessage);

            // Then: The correct request handler should have been called
            requestHandler2.Verify(a => a(
                                       It.Is <CommonObjects.TestMessageContents>(p => p.Equals(CommonObjects.TestMessageContents.DefaultInstance)),
                                       It.Is <RequestContext <CommonObjects.TestMessageContents> >(p => p.messageQueue == jh.outputQueue && p.requestMessage == CommonObjects.RequestMessage)
                                       ), Times.Once);
            requestHandler1.Verify(a => a(
                                       It.IsAny <CommonObjects.TestMessageContents>(),
                                       It.IsAny <RequestContext <CommonObjects.TestMessageContents> >()
                                       ), Times.Never);
        }
        public void SetAsyncRequestHandlerOverrideFalse()
        {
            // Setup: Create a mock request handler
            var requestHandler = new Mock <Func <CommonObjects.TestMessageContents, RequestContext <CommonObjects.TestMessageContents>, Task> >();

            // If:
            // ... I assign a request handler on the JSON RPC host
            // ... And I reassign the request handler without overriding
            // Then: I should get an exception
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);

            jh.SetAsyncRequestHandler(CommonObjects.RequestType, requestHandler.Object);
            Assert.ThrowsAny <Exception>(() => jh.SetAsyncRequestHandler(CommonObjects.RequestType, requestHandler.Object));
        }
        public async Task SetAsyncRequestHandler(bool nullContents)
        {
            // Setup: Create a mock request handler
            var requestHandler = new Mock <Func <CommonObjects.TestMessageContents, RequestContext <CommonObjects.TestMessageContents>, Task> >();
            var message        = nullContents
                ? Message.CreateRequest(CommonObjects.RequestType, CommonObjects.MessageId, null)
                : CommonObjects.RequestMessage;

            // If: I assign a request handler on the JSON RPC host
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);

            jh.SetAsyncRequestHandler(CommonObjects.RequestType, requestHandler.Object);

            // Then: It should be the only request handler set
            Assert.Single(jh.requestHandlers);
            Assert.Contains(CommonObjects.RequestType.MethodName, jh.requestHandlers.Keys);

            // If: I call the stored request handler
            await jh.requestHandlers[CommonObjects.RequestType.MethodName](message);
            await jh.requestHandlers[CommonObjects.RequestType.MethodName](message);

            // Then: The request handler should have been called with the params and a proper request context
            var expectedContents = nullContents
                ? null
                : CommonObjects.TestMessageContents.DefaultInstance;

            requestHandler.Verify(a => a(
                                      It.Is <CommonObjects.TestMessageContents>(p => p == expectedContents),
                                      It.Is <RequestContext <CommonObjects.TestMessageContents> >(rc => rc.messageQueue == jh.outputQueue && rc.requestMessage == message)
                                      ), Times.Exactly(2));
        }
        public void SetAsyncRequestHandlerNullRequestHandler()
        {
            // If: I assign a request handler on the JSON RPC host with a null request handler
            // Then: I should get an exception
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);

            Assert.Throws <ArgumentNullException>(() => jh.SetAsyncRequestHandler(CommonObjects.RequestType, null));
        }
        public void SetAsyncRequestHandlerNullRequestType()
        {
            // If: I assign a request handler on the JSON RPC host with a null request type
            // Then: I should get an exception
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);

            Assert.Throws <ArgumentNullException>(() =>
                                                  jh.SetAsyncRequestHandler <object, object>(null, (a, b) => Task.FromResult(false)));
        }
        public async Task DispatchMessageRequestException()
        {
            // Setup: Create a JSON RPC host with a request handler that throws an unhandled exception every time
            var jh          = new JsonRpcHost(GetChannelBase(null, null).Object);
            var mockHandler = new Mock <Func <CommonObjects.TestMessageContents, RequestContext <CommonObjects.TestMessageContents>, Task> >();

            mockHandler.Setup(f => f(
                                  It.IsAny <CommonObjects.TestMessageContents>(),
                                  It.IsAny <RequestContext <CommonObjects.TestMessageContents> >()
                                  ))
            .Returns(Task.FromException(new Exception()));
            jh.SetAsyncRequestHandler(CommonObjects.RequestType, mockHandler.Object);

            // If: I dispatch a message whose handler throws
            // Then: I should get an exception
            await Assert.ThrowsAsync <Exception>(() => jh.DispatchMessage(CommonObjects.RequestMessage));
        }
        public async Task DispatchMessageRequestWithHandler(Task result)
        {
            // Setup: Create a JSON RPC host with a request handler setup
            var jh          = new JsonRpcHost(GetChannelBase(null, null).Object);
            var mockHandler = new Mock <Func <CommonObjects.TestMessageContents, RequestContext <CommonObjects.TestMessageContents>, Task> >();

            mockHandler.Setup(f => f(
                                  It.Is <CommonObjects.TestMessageContents>(m => m == CommonObjects.TestMessageContents.DefaultInstance),
                                  It.Is <RequestContext <CommonObjects.TestMessageContents> >(rc => rc.messageQueue == jh.outputQueue)
                                  )).Returns(result);
            jh.SetAsyncRequestHandler(CommonObjects.RequestType, mockHandler.Object);

            // If: I dispatch a request
            await jh.DispatchMessage(CommonObjects.RequestMessage);

            // Then: The request handler should have been called
            mockHandler.Verify(f => f(
                                   It.Is <CommonObjects.TestMessageContents>(m => m == CommonObjects.TestMessageContents.DefaultInstance),
                                   It.IsAny <RequestContext <CommonObjects.TestMessageContents> >()
                                   ), Times.Once);
        }