public void Serializing_messages_with_json_dot_net()
        {
            _message = new TestMessage
            {
                Name    = "Joe",
                Details = new List <TestMessageDetail>
                {
                    new TestMessageDetail
                    {
                        Item  = "A",
                        Value = 27.5d
                    },
                    new TestMessageDetail
                    {
                        Item  = "B",
                        Value = 13.5d
                    }
                },
                EnumDetails = new List <TestMessageDetail>
                {
                    new TestMessageDetail
                    {
                        Item  = "1",
                        Value = 42.0d
                    }
                }
            };

            _envelope = new Envelope {
                Message = _message
            };

            _envelope.MessageType.Add(_message.GetType().ToMessageName());
            _envelope.MessageType.Add(typeof(MessageA).ToMessageName());
            _envelope.MessageType.Add(typeof(MessageB).ToMessageName());

            _envelope.Headers.Add("Simple", "Value");
            _envelope.Host = new BusHostInfo(true);

            _serializer   = JsonMessageSerializer.Serializer;
            _deserializer = JsonMessageSerializer.Deserializer;

            using (var memoryStream = new MemoryStream())
                using (var writer = new StreamWriter(memoryStream))
                    using (var jsonWriter = new JsonTextWriter(writer))
                    {
                        jsonWriter.Formatting = Formatting.Indented;
                        _serializer.Serialize(jsonWriter, _envelope);

                        writer.Flush();
                        _body = Encoding.UTF8.GetString(memoryStream.ToArray());
                    }

            _xml = JsonConvert.DeserializeXNode(_body, "envelope");
        }
Exemplo n.º 2
0
        public async Task Should_dispatch_to_state_machine()
        {
            var testMessage = new TestMessage()
            {
                TestId = Guid.NewGuid()
            };
            var fakeStateMachine = A.Fake <ISaga <TestState, Guid> >(
                x => x.Implements <IMachineMessageHandler <TestMessage> >()
                );

            A.CallTo(() => ((ISaga)fakeStateMachine).FindKey(A <IIntegrationMessage> .Ignored, A <MessageMetaData> .Ignored))
            .Returns(Guid.NewGuid());

            var fakeSagaPersistence = A.Fake <ISagaPersistence <TestState, Guid> >();

            MessagingMap.AddSaga(new SagaProperties(
                                     fakeStateMachine.GetType(), typeof(Guid),
                                     typeof(TestState),
                                     fakeSagaPersistence.GetType(),
                                     new List <Type> {
                typeof(TestMessage)
            },
                                     true /* custom handler for state machines*/)
                                 );

            var messageContext = new MessageContext(null, this.FakeEndpoint, this.MessagingService, new MessageTypeProperties()
            {
                SagaType = fakeStateMachine.GetType(),
                MessageTypeIdentifier = testMessage.MessageType,
                Type = testMessage.GetType()
            });

            A.CallTo(() => this.FakeServiceFactory.CreateScope()).Returns(this.FakeServiceFactory);
            A.CallTo(() => this.FakeServiceFactory.GetInstance(typeof(IOutboxSession))).Returns(this.FakeOutboxSession);
            A.CallTo(() => this.FakeServiceFactory.GetInstance(fakeSagaPersistence.GetType())).Returns(fakeSagaPersistence);
            A.CallTo(() => this.FakeServiceFactory.GetInstance(fakeStateMachine.GetType())).Returns(fakeStateMachine);
            //A.CallTo(() => this.FakeServiceFactory.GetInstance(typeof(ICustomMessageHandler<TestMessage>))).Returns(fakeStateMachine as ICustomMessageHandler<TestMessage>);
            //A.CallTo(() => this.FakeServiceFactory.GetInstance(typeof(IMessageHandler<TestMessage>))).Returns(fakeStateMachine as IMessageHandler<TestMessage>);

            MessageDispatcher dispatcher = new MessageDispatcher(this.FakeServiceFactory);
            await dispatcher.Dispatch(testMessage, messageContext);

            A.CallTo(() => ((ISagaPersistence)fakeSagaPersistence).LoadOrCreateState(fakeStateMachine, A <object> .Ignored))
            .MustHaveHappenedOnceExactly();

            A.CallTo(() => ((IMachineMessageHandler <TestMessage>)fakeStateMachine).Handle(testMessage, messageContext))
            .MustHaveHappenedOnceExactly();

            A.CallTo(() => ((ISagaPersistence)fakeSagaPersistence).AddOrUpdateState(fakeStateMachine))
            .MustHaveHappenedOnceExactly();
        }
        public void Serializing_messages_with_json_dot_net()
        {
            _message = new TestMessage
            {
                Name    = "Joe",
                Details = new List <TestMessageDetail>
                {
                    new TestMessageDetail {
                        Item = "A", Value = 27.5d
                    },
                    new TestMessageDetail {
                        Item = "B", Value = 13.5d
                    },
                },
                DateTimeProperty       = new DateTime(2014, 03, 05, 01, 53, 04, 193),
                DateTimeStringProperty = "2014-03-05T01:53:04.193",
                Dictionary             = new Dictionary <string, string>()
                {
                    { "string-datetime-property", "2014-03-05T01:53:04.193" }
                }
            };

            _envelope = new Envelope
            {
                Message = _message,
            };

            _envelope.MessageType.Add(_message.GetType().ToMessageName());
            _envelope.MessageType.Add(typeof(MessageA).ToMessageName());
            _envelope.MessageType.Add(typeof(MessageB).ToMessageName());

            _serializer   = JsonMessageSerializer.Serializer;
            _deserializer = JsonMessageSerializer.Deserializer;

            using (var memoryStream = new MemoryStream())
                using (var writer = new StreamWriter(memoryStream))
                    using (var jsonWriter = new JsonTextWriter(writer))
                    {
                        jsonWriter.Formatting = Formatting.Indented;
                        _serializer.Serialize(jsonWriter, _envelope);

                        writer.Flush();
                        _body = Encoding.UTF8.GetString(memoryStream.ToArray());
                    }

            _xml = JsonConvert.DeserializeXNode(_body, "envelope");
        }
Exemplo n.º 4
0
        public void MessageEnvelop_SerializesMessageContent()
        {
            var testMessage = new TestMessage
            {
                Greeting    = "Hello world!",
                PickANumber = 42,
                BirthDay    = new System.DateTime(1970, 4, 14)
            };

            testMessage.Id.Should().NotBeEmpty();

            var envelope = new MessageEnvelope(testMessage);

            envelope.MessageContent.Should().NotBeNullOrEmpty();
            envelope.MessageContent.Should().ContainAll(testMessage.Id.ToString(), testMessage.Greeting, testMessage.PickANumber.ToString(), testMessage.BirthDay.ToString("yyyy-MM-dd"));
            envelope.MessageType.Should().Contain(testMessage.GetType().FullName);
        }
Exemplo n.º 5
0
        public void Should_throw_when_untyped_deserialization_with_incompatible_options()
        {
            //Arrange
            var sut      = new NewtonsoftJsonMessageSerDes(Mock.Of <IMessageTypeRegistry>(x => x.ResolveType(It.IsAny <string>(), It.IsAny <IEnumerable <Assembly> >()) == typeof(TestMessage)));
            var @event   = new TestMessage(11232, 1122312, "something");
            var envelope = new MessagingEnvelope(new Dictionary <string, string>
            {
                [MessagingHeaders.MessageType] = @event.GetType().Name
            }, @event);

            var json = sut.SerializeMessageEnvelope(envelope);

            //Act
            void Action() => sut.DeserializeMessageEnvelope(json, new MessageSerDesOptions {
                DeserializationType = DeserializationType.TypeSafe
            });

            //Assert

            ((Action)Action).Should().Throw <Exception>().WithMessage("*TypeSafe*");
        }
Exemplo n.º 6
0
        public void Should_deserialize_with_json_payload()
        {
            //Arrange
            var sut      = new NewtonsoftJsonMessageSerDes(Mock.Of <IMessageTypeRegistry>(x => x.ResolveType(It.IsAny <string>(), It.IsAny <IEnumerable <Assembly> >()) == typeof(TestMessage)));
            var @event   = new TestMessage(11232, 1122312, "something");
            var envelope = new MessagingEnvelope(new Dictionary <string, string>
            {
                [MessagingHeaders.MessageType] = @event.GetType().Name
            }, @event);

            var json = sut.SerializeMessageEnvelope(envelope);

            //Act
            var deserialized = sut.DeserializeMessageEnvelope(json,
                                                              new MessageSerDesOptions {
                DeserializationType = DeserializationType.HeadersOnly
            });

            //Assert
            deserialized.Should().NotBeNull();
            deserialized.Payload.GetType().Should().Be(typeof(string));
            (deserialized.Payload as string).Should().ContainAll(@event.ContractId.ToString(), @event.PartnerId.ToString(), @event.Details);
        }
        public void Serializing_messages_with_json_dot_net()
        {
            _message = new TestMessage
            {
                Name = "Joe",
                Details = new List<TestMessageDetail>
                {
                    new TestMessageDetail {Item = "A", Value = 27.5d},
                    new TestMessageDetail {Item = "B", Value = 13.5d},
                },
            };

            _envelope = new Envelope
            {
                Message = _message,
            };

            _envelope.MessageType.Add(_message.GetType().ToMessageName());
            _envelope.MessageType.Add(typeof(MessageA).ToMessageName());
            _envelope.MessageType.Add(typeof(MessageB).ToMessageName());

            _serializer = JsonMessageSerializer.Serializer;
            _deserializer = JsonMessageSerializer.Deserializer;

            using (var memoryStream = new MemoryStream())
            using (var writer = new StreamWriter(memoryStream))
            using (var jsonWriter = new JsonTextWriter(writer))
            {
                jsonWriter.Formatting = Formatting.Indented;
                _serializer.Serialize(jsonWriter, _envelope);

                writer.Flush();
                _body = Encoding.UTF8.GetString(memoryStream.ToArray());
            }

            _xml = JsonConvert.DeserializeXNode(_body, "envelope");
        }
Exemplo n.º 8
0
        public async Task Should_dispatch_to_handler_class()
        {
            var testMessage = new TestMessage()
            {
                TestId = Guid.NewGuid()
            };
            var fakeHandlerClass = A.Fake <IMessageHandler <TestMessage> >();

            var messageContext = new MessageContext(null, this.FakeEndpoint, this.MessagingService, new MessageTypeProperties()
            {
                MessageTypeIdentifier = testMessage.MessageType,
                Type = testMessage.GetType()
            });

            A.CallTo(() => this.FakeServiceFactory.CreateScope()).Returns(this.FakeServiceFactory);
            A.CallTo(() => this.FakeServiceFactory.GetInstance(typeof(IOutboxSession))).Returns(this.FakeOutboxSession);
            A.CallTo(() => this.FakeServiceFactory.GetInstance(typeof(IMessageHandler <TestMessage>))).Returns(fakeHandlerClass as IMessageHandler <TestMessage>);

            MessageDispatcher dispatcher = new MessageDispatcher(this.FakeServiceFactory);
            await dispatcher.Dispatch(testMessage, messageContext);

            A.CallTo(() => fakeHandlerClass.Handle(testMessage, messageContext))
            .MustHaveHappenedOnceExactly();
        }