public void ShouldStartNewProcessManagerWithConsumerContext()
        {
            // Arrange
            _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IMessageHandler <FakeMessage1>), typeof(IAsyncMessageHandler <FakeMessage1>))).Returns(new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeProcessManager1),
                    MessageType = typeof(FakeMessage1)
                }
            });
            _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IStartProcessManager <FakeMessage1>), typeof(IStartAsyncProcessManager <FakeMessage1>))).Returns(new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeProcessManager1),
                    MessageType = typeof(FakeMessage1)
                }
            });

            var processManager = new FakeProcessManager1();

            _mockContainer.Setup(x => x.GetInstance(typeof(FakeProcessManager1))).Returns(processManager);

            var data = new FakeProcessManagerData
            {
                User = "******"
            };
            var mockPersistanceData = new Mock <IPersistanceData <FakeProcessManagerData> >();

            mockPersistanceData.Setup(x => x.Data).Returns(data);
            _mockProcessManagerFinder.Setup(x => x.FindData <FakeProcessManagerData>(It.IsAny <IProcessManagerPropertyMapper>(), It.IsAny <Message>())).Returns(mockPersistanceData.Object);

            var processManagerProcessor = new ProcessManagerProcessor(_mockProcessManagerFinder.Object, _mockContainer.Object, _mockLogger.Object);

            var context = new ConsumeContext();

            // Act
            processManagerProcessor.ProcessMessage <FakeMessage1>(JsonConvert.SerializeObject(new FakeMessage1(Guid.NewGuid())
            {
                Username = "******"
            }), context).GetAwaiter().GetResult();

            // Assert
            // Data.User is set by the ProcessManagers Execute method
            Assert.Equal(context, processManager.Context);
        }
        public void ShouldGetCorrectProcessManagerReferencesFromContainer()
        {
            // Arrange
            _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IMessageHandler <FakeMessage1>), typeof(IAsyncMessageHandler <FakeMessage1>))).Returns(new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeProcessManager1),
                    MessageType = typeof(FakeMessage1)
                }
            });
            _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IStartProcessManager <FakeMessage1>), typeof(IStartAsyncProcessManager <FakeMessage1>))).Returns(new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeProcessManager1),
                    MessageType = typeof(FakeMessage1)
                }
            });

            _mockContainer.Setup(x => x.GetInstance(typeof(FakeProcessManager1))).Returns(new FakeProcessManager1());

            var data = new FakeProcessManagerData
            {
                User = "******"
            };
            var mockPersistanceData = new Mock <IPersistanceData <FakeProcessManagerData> >();

            mockPersistanceData.Setup(x => x.Data).Returns(data);
            //_mockProcessManagerFinder.Setup(x => x.FindData<FakeProcessManagerData>(It.IsAny<Guid>())).Returns(mockPersistanceData.Object);
            //_mockProcessManagerFinder.Setup(x => x.FindData<FakeProcessManagerData>(It.IsAny<Guid>())).Returns(mockPersistanceData.Object);
            _mockProcessManagerFinder.Setup(x => x.FindData <FakeProcessManagerData>(It.IsAny <IProcessManagerPropertyMapper>(), It.IsAny <Message>())).Returns(mockPersistanceData.Object);

            var processManagerProcessor = new ProcessManagerProcessor(_mockProcessManagerFinder.Object, _mockContainer.Object, _mockLogger.Object);

            // Act
            processManagerProcessor.ProcessMessage <FakeMessage1>(JsonConvert.SerializeObject(new FakeMessage1(Guid.NewGuid())
            {
                Username = "******"
            }), new ConsumeContext()).GetAwaiter().GetResult();

            // Assert
            _mockContainer.Verify(x => x.GetHandlerTypes(typeof(IMessageHandler <FakeMessage1>), typeof(IAsyncMessageHandler <FakeMessage1>)), Times.Once);
            _mockContainer.Verify(x => x.GetHandlerTypes(typeof(IStartProcessManager <FakeMessage1>), typeof(IStartAsyncProcessManager <FakeMessage1>)), Times.Once);
        }
        public void ShouldRemoveProcessManagerDataIfProcessManagerIsComplete()
        {
            // Arrange
            var id      = Guid.NewGuid();
            var message = new FakeMessage2(id)
            {
                Email = "*****@*****.**"
            };

            var data = new FakeProcessManagerData
            {
                User = "******"
            };

            var mockPersistanceData = new Mock <IPersistanceData <FakeProcessManagerData> >();

            mockPersistanceData.Setup(x => x.Data).Returns(data);
            _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IMessageHandler <FakeMessage2>), typeof(IAsyncMessageHandler <FakeMessage2>))).Returns(new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeProcessManager1),
                    MessageType = typeof(FakeMessage2)
                }
            });

            var processManager = new FakeProcessManager1
            {
                Complete = true
            };

            _mockContainer.Setup(x => x.GetInstance(typeof(FakeProcessManager1))).Returns(processManager);
            _mockProcessManagerFinder.Setup(x => x.FindData <FakeProcessManagerData>(It.IsAny <ProcessManagerPropertyMapper>(), It.Is <FakeMessage2>(m => m.CorrelationId == id))).Returns(mockPersistanceData.Object);

            _mockProcessManagerFinder.Setup(x => x.UpdateData(It.IsAny <FakePersistanceData>()));

            var processManagerProcessor = new ProcessManagerProcessor(_mockProcessManagerFinder.Object, _mockContainer.Object, _mockLogger.Object);

            // Act
            processManagerProcessor.ProcessMessage <FakeMessage2>(JsonConvert.SerializeObject(message), new ConsumeContext()).GetAwaiter().GetResult();

            // Assert
            _mockProcessManagerFinder.Verify(x => x.DeleteData(It.Is <IPersistanceData <FakeProcessManagerData> >(y => y.Data.Email == "*****@*****.**" && y.Data.User == "Tim Watson")), Times.Once);
        }
        public void ShouldPersistNewProcessManagerWithExistingPersistanceData()
        {
            // Arrange
            _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IMessageHandler <FakeMessage1>))).Returns(new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeProcessManager1),
                    MessageType = typeof(FakeMessage1)
                }
            });
            _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IStartProcessManager <FakeMessage1>))).Returns(new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeProcessManager1),
                    MessageType = typeof(FakeMessage1)
                }
            });

            var processManager = new FakeProcessManager1();

            _mockContainer.Setup(x => x.GetInstance(typeof(FakeProcessManager1))).Returns(processManager);

            var data = new FakeProcessManagerData
            {
                User = "******"
            };
            var mockPersistanceData = new Mock <IPersistanceData <FakeProcessManagerData> >();

            mockPersistanceData.Setup(x => x.Data).Returns(data);
            _mockProcessManagerFinder.Setup(x => x.FindData <FakeProcessManagerData>(It.IsAny <IProcessManagerPropertyMapper>(), It.IsAny <Message>())).Returns(mockPersistanceData.Object);

            var processManagerProcessor = new ProcessManagerProcessor(_mockProcessManagerFinder.Object, _mockContainer.Object);

            // Act
            processManagerProcessor.ProcessMessage <FakeMessage1>(JsonConvert.SerializeObject(new FakeMessage1(Guid.NewGuid())
            {
                Username = "******"
            }), new ConsumeContext());

            // Assert
            _mockProcessManagerFinder.Verify(x => x.UpdateData(It.Is <IPersistanceData <FakeProcessManagerData> >(y => y.Data.User == "Jakub Pachansky")), Times.Exactly(2));
        }
        public void ShouldStartExistingProcessManagerWithConsumerContext()
        {
            // Arrange
            var id      = Guid.NewGuid();
            var message = new FakeMessage2(id)
            {
                Email = "*****@*****.**"
            };

            var data = new FakeProcessManagerData
            {
                User = "******"
            };

            var mockPersistanceData = new Mock <IPersistanceData <FakeProcessManagerData> >();

            mockPersistanceData.Setup(x => x.Data).Returns(data);
            _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IMessageHandler <FakeMessage2>), typeof(IAsyncMessageHandler <FakeMessage2>))).Returns(new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeProcessManager1),
                    MessageType = typeof(FakeMessage2)
                }
            });

            var processManager = new FakeProcessManager1();

            _mockContainer.Setup(x => x.GetInstance(typeof(FakeProcessManager1))).Returns(processManager);
            _mockProcessManagerFinder.Setup(x => x.FindData <FakeProcessManagerData>(It.IsAny <ProcessManagerPropertyMapper>(), It.Is <FakeMessage2>(m => m.CorrelationId == id))).Returns(mockPersistanceData.Object);

            var processManagerProcessor = new ProcessManagerProcessor(_mockProcessManagerFinder.Object, _mockContainer.Object, _mockLogger.Object);

            var context = new ConsumeContext();

            // Act
            processManagerProcessor.ProcessMessage <FakeMessage2>(JsonConvert.SerializeObject(message), context).GetAwaiter().GetResult();

            // Assert
            Assert.Equal(context, processManager.Context);
        }
        public void ShouldStartProcessManagerWithExistingData()
        {
            // Arrange
            var id      = Guid.NewGuid();
            var message = new FakeMessage2(id)
            {
                Email = "*****@*****.**"
            };

            var data = new FakeProcessManagerData
            {
                User = "******"
            };

            var mockPersistanceData = new Mock <IPersistanceData <FakeProcessManagerData> >();

            mockPersistanceData.Setup(x => x.Data).Returns(data);
            _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IMessageHandler <FakeMessage2>))).Returns(new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeProcessManager1),
                    MessageType = typeof(FakeMessage2)
                }
            });

            var processManager = new FakeProcessManager1();

            _mockContainer.Setup(x => x.GetInstance(typeof(FakeProcessManager1))).Returns(processManager);
            _mockProcessManagerFinder.Setup(x => x.FindData <FakeProcessManagerData>(It.IsAny <ProcessManagerPropertyMapper>(), It.Is <FakeMessage2>(m => m.CorrelationId == id))).Returns(mockPersistanceData.Object);

            var processManagerProcessor = new ProcessManagerProcessor(_mockProcessManagerFinder.Object, _mockContainer.Object);

            // Act
            processManagerProcessor.ProcessMessage <FakeMessage2>(JsonConvert.SerializeObject(message), new ConsumeContext());

            // Assert
            Assert.Equal("Tim Watson", processManager.Data.User);   // Can only be this if Data was set on process manager
            Assert.Equal("*****@*****.**", processManager.Data.Email); // Can only be this if execute was called
        }
        public void ShouldFindExistingProcessManagerInstance()
        {
            // Arrange
            var id      = Guid.NewGuid();
            var message = new FakeMessage2(id);

            _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IMessageHandler <FakeMessage2>))).Returns(new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeProcessManager1),
                    MessageType = typeof(FakeMessage2)
                }
            });

            var processManager = new FakeProcessManager1();

            _mockContainer.Setup(x => x.GetInstance(typeof(FakeProcessManager1))).Returns(processManager);

            var data = new FakeProcessManagerData
            {
                User = "******"
            };
            var mockPersistanceData = new Mock <IPersistanceData <FakeProcessManagerData> >();

            mockPersistanceData.Setup(x => x.Data).Returns(data);
            _mockProcessManagerFinder.Setup(x => x.FindData <FakeProcessManagerData>(It.IsAny <ProcessManagerPropertyMapper>(), message)).Returns(mockPersistanceData.Object);

            var processManagerProcessor = new ProcessManagerProcessor(_mockProcessManagerFinder.Object, _mockContainer.Object);

            // Act
            processManagerProcessor.ProcessMessage <FakeMessage2>(JsonConvert.SerializeObject(message), new ConsumeContext());

            // Assert
            _mockContainer.Verify(x => x.GetInstance(typeof(FakeProcessManager1)), Times.Once);
            _mockProcessManagerFinder.Verify(x => x.FindData <FakeProcessManagerData>(It.IsAny <ProcessManagerPropertyMapper>(), It.Is <FakeMessage2>(m => m.CorrelationId == id)), Times.Once);
        }