public void ShouldThrowWhenUpdatingTwoInstancesOfSameDataAtTheSameTime()
        {
            // Arrange
            var correlationId        = Guid.NewGuid();
            IProcessManagerData data = new TestSqlServerData {
                CorrelationId = correlationId, Name = "TestDataUpdated"
            };
            var       xmlSerializer = new XmlSerializer(data.GetType());
            var       sww           = new StringWriter();
            XmlWriter writer        = XmlWriter.Create(sww);

            xmlSerializer.Serialize(writer, data);
            var dataXml = sww.ToString();

            SetupTestDbData(new List <TestDbRow> {
                new TestDbRow {
                    Id = correlationId.ToString(), DataXml = dataXml, Version = 1
                }
            });

            IProcessManagerFinder processManagerFinder = new SqlServerProcessManagerFinder(_connectionString, string.Empty, 1);

            var foundData1 = processManagerFinder.FindData <TestSqlServerData>(_mapper, new Message(correlationId));
            var foundData2 = processManagerFinder.FindData <TestSqlServerData>(_mapper, new Message(correlationId));

            processManagerFinder.UpdateData(foundData1); // first update should be fine

            // Act / Assert
            Assert.Throws <ArgumentException>(() => processManagerFinder.UpdateData(foundData2)); // second update should fail
        }
        public void ShouldFindData()
        {
            // Arrange
            var correlationId = Guid.NewGuid();

            var data = new TestSqlServerData {
                CorrelationId = correlationId, Name = "TestData"
            };
            var       xmlSerializer = new XmlSerializer(data.GetType());
            var       sww           = new StringWriter();
            XmlWriter writer        = XmlWriter.Create(sww);

            xmlSerializer.Serialize(writer, data);
            var dataXml = sww.ToString();

            SetupTestDbData(new List <TestDbRow> {
                new TestDbRow {
                    Id = correlationId.ToString(), DataXml = dataXml, Version = 1
                }
            });
            IProcessManagerFinder processManagerFinder = new SqlServerProcessManagerFinder(_connectionString, string.Empty);

            // Act
            var result = processManagerFinder.FindData <TestSqlServerData>(_mapper, new Message(correlationId));

            // Assert
            Assert.Equal("TestData", result.Data.Name);

            // Teardown - complete transaction
            processManagerFinder.UpdateData(result);
        }
        public void ShouldUpdateWhenInsertingDataWithExistingId()
        {
            // Arrange
            var correlationId = Guid.NewGuid();

            SetupTestDbData(new List <TestDbRow> {
                new TestDbRow {
                    Id = correlationId.ToString(), DataXml = "FakeJsonData"
                }
            });

            IProcessManagerData data = new TestSqlServerData {
                CorrelationId = correlationId, Name = "TestData"
            };
            IProcessManagerFinder processManagerFinder = new SqlServerProcessManagerFinder(_connectionString, string.Empty);

            // Act
            processManagerFinder.InsertData(data);

            // Assert
            var results = GetTestDbData(correlationId);

            Assert.Equal(1, results.Count);
            Assert.Equal(correlationId.ToString(), results[0].Id);
            Assert.NotEqual("FakeJsonData", results[0].DataXml);
            Assert.True(results[0].DataXml.Contains("TestData"));
        }
        public void ShouldInsertData()
        {
            // Arrange
            var correlationId = Guid.NewGuid();
            IProcessManagerData data = new TestSqlServerData { CorrelationId = correlationId, Name = "TestData" };
            IProcessManagerFinder processManagerFinder = new SqlServerProcessManagerFinder(_connectionString, string.Empty);

            // Act
            processManagerFinder.InsertData(data);

            // Assert
            var results = GetTestDbData(correlationId);
            Assert.Equal(1, results.Count);
            Assert.Equal(correlationId.ToString(), results[0].Id);
            Assert.True(results[0].DataXml.Contains("TestData"));
        }
        public void ShouldUpdateData()
        {
            // Arrange
            var correlationId = Guid.NewGuid();
            var testDataJson  = "{\"CorrelationId\":\"e845f0a0-4af0-4d1e-a324-790d49d540ae\",\"Name\":\"TestDataOriginal\"}";

            IProcessManagerData data = new TestSqlServerData {
                CorrelationId = correlationId, Name = "TestDataOriginal"
            };
            var       xmlSerializer = new XmlSerializer(data.GetType());
            var       sww           = new StringWriter();
            XmlWriter writer        = XmlWriter.Create(sww);

            xmlSerializer.Serialize(writer, data);
            var dataXml = sww.ToString();

            SetupTestDbData(new List <TestDbRow> {
                new TestDbRow {
                    Id = correlationId.ToString(), DataXml = dataXml
                }
            });

            IProcessManagerData updatedData = new TestSqlServerData {
                CorrelationId = correlationId, Name = "TestDataUpdated"
            };
            var sqlServerData = new SqlServerData <IProcessManagerData> {
                Data = updatedData, Id = correlationId
            };

            IProcessManagerFinder processManagerFinder = new SqlServerProcessManagerFinder(_connectionString, string.Empty);

            // Act
            //processManagerFinder.FindData<TestSqlServerData>(correlationId);
            processManagerFinder.FindData <TestSqlServerData>(_mapper, new Message(correlationId));
            processManagerFinder.UpdateData(sqlServerData);

            // Assert
            var results = GetTestDbData(correlationId);

            Assert.Equal(1, results.Count);
            Assert.Equal(correlationId.ToString(), results[0].Id);
            Assert.False(results[0].DataXml.Contains("TestDataOriginal"));
            Assert.True(results[0].DataXml.Contains("TestDataUpdated"));
        }
        public void ShouldInsertData()
        {
            // Arrange
            var correlationId        = Guid.NewGuid();
            IProcessManagerData data = new TestSqlServerData {
                CorrelationId = correlationId, Name = "TestData"
            };
            IProcessManagerFinder processManagerFinder = new SqlServerProcessManagerFinder(_connectionString, string.Empty);

            // Act
            processManagerFinder.InsertData(data);

            // Assert
            var results = GetTestDbData(correlationId);

            Assert.Equal(1, results.Count);
            Assert.Equal(correlationId.ToString(), results[0].Id);
            Assert.True(results[0].DataXml.Contains("TestData"));
        }
        public void ShouldDeleteData()
        {
            // Arrange
            var correlationId = Guid.NewGuid();

            IProcessManagerData data = new TestSqlServerData {
                CorrelationId = correlationId, Name = "TestDataUpdated"
            };
            var       xmlSerializer = new XmlSerializer(data.GetType());
            var       sww           = new StringWriter();
            XmlWriter writer        = XmlWriter.Create(sww);

            xmlSerializer.Serialize(writer, data);
            var dataXml = sww.ToString();

            SetupTestDbData(new List <TestDbRow> {
                new TestDbRow {
                    Id = correlationId.ToString(), DataXml = dataXml, Version = 1
                }
            });

            IProcessManagerFinder processManagerFinder = new SqlServerProcessManagerFinder(_connectionString, string.Empty);

            var sqlServerDataToBeDeleted = new SqlServerData <IProcessManagerData> {
                Data = data, Id = correlationId, Version = 1
            };

            // Act
            processManagerFinder.FindData <TestData>(_mapper, new Message(correlationId));
            processManagerFinder.DeleteData(sqlServerDataToBeDeleted);

            // Assert
            var results = GetTestDbData(correlationId);

            Assert.Equal(0, results.Count);
        }
        public void ShouldUpdateWhenInsertingDataWithExistingId()
        {
            // Arrange
            var correlationId = Guid.NewGuid();
            SetupTestDbData(new List<TestDbRow> { new TestDbRow { Id = correlationId.ToString(), DataXml = "FakeJsonData" } });

            IProcessManagerData data = new TestSqlServerData { CorrelationId = correlationId, Name = "TestData" };
            IProcessManagerFinder processManagerFinder = new SqlServerProcessManagerFinder(_connectionString, string.Empty);

            // Act
            processManagerFinder.InsertData(data);

            // Assert
            var results = GetTestDbData(correlationId);
            Assert.Equal(1, results.Count);
            Assert.Equal(correlationId.ToString(), results[0].Id);
            Assert.NotEqual("FakeJsonData", results[0].DataXml);
            Assert.True(results[0].DataXml.Contains("TestData"));
        }
        public void ShouldDeleteData()
        {
            // Arrange
            var correlationId = Guid.NewGuid();

            IProcessManagerData data = new TestSqlServerData { CorrelationId = correlationId, Name = "TestDataUpdated" };
            var xmlSerializer = new XmlSerializer(data.GetType());
            var sww = new StringWriter();
            XmlWriter writer = XmlWriter.Create(sww);
            xmlSerializer.Serialize(writer, data);
            var dataXml = sww.ToString();

            SetupTestDbData(new List<TestDbRow> { new TestDbRow { Id = correlationId.ToString(), DataXml = dataXml, Version = 1 } });

            IProcessManagerFinder processManagerFinder = new SqlServerProcessManagerFinder(_connectionString, string.Empty);

            var sqlServerDataToBeDeleted = new SqlServerData<IProcessManagerData> { Data = data, Id = correlationId, Version = 1};

            // Act
            processManagerFinder.FindData<TestData>(_mapper, new Message(correlationId));
            processManagerFinder.DeleteData(sqlServerDataToBeDeleted);

            // Assert
            var results = GetTestDbData(correlationId);
            Assert.Equal(0, results.Count);
        }
        public void ShouldThrowWhenUpdatingTwoInstancesOfSameDataAtTheSameTime()
        {
            // Arrange
            var correlationId = Guid.NewGuid();
            IProcessManagerData data = new TestSqlServerData { CorrelationId = correlationId, Name = "TestDataUpdated" };
            var xmlSerializer = new XmlSerializer(data.GetType());
            var sww = new StringWriter();
            XmlWriter writer = XmlWriter.Create(sww);
            xmlSerializer.Serialize(writer, data);
            var dataXml = sww.ToString();

            SetupTestDbData(new List<TestDbRow> { new TestDbRow { Id = correlationId.ToString(), DataXml = dataXml, Version = 1 } });

            IProcessManagerFinder processManagerFinder = new SqlServerProcessManagerFinder(_connectionString, string.Empty, 1);

            var foundData1 = processManagerFinder.FindData<TestSqlServerData>(_mapper, new Message(correlationId));
            var foundData2 = processManagerFinder.FindData<TestSqlServerData>(_mapper, new Message(correlationId));

            processManagerFinder.UpdateData(foundData1); // first update should be fine

            // Act / Assert
            Assert.Throws<ArgumentException>(() => processManagerFinder.UpdateData(foundData2)); // second update should fail
        }
        public void ShouldUpdateData()
        {
            // Arrange
            var correlationId = Guid.NewGuid();
            var testDataJson = "{\"CorrelationId\":\"e845f0a0-4af0-4d1e-a324-790d49d540ae\",\"Name\":\"TestDataOriginal\"}";

            IProcessManagerData data = new TestSqlServerData { CorrelationId = correlationId, Name = "TestDataOriginal" };
            var xmlSerializer = new XmlSerializer(data.GetType());
            var sww = new StringWriter();
            XmlWriter writer = XmlWriter.Create(sww);
            xmlSerializer.Serialize(writer, data);
            var dataXml = sww.ToString();
            SetupTestDbData(new List<TestDbRow> { new TestDbRow { Id = correlationId.ToString(), DataXml = dataXml } });

            IProcessManagerData updatedData = new TestSqlServerData { CorrelationId = correlationId, Name = "TestDataUpdated" };
            var sqlServerData = new SqlServerData<IProcessManagerData> { Data = updatedData, Id = correlationId };

            IProcessManagerFinder processManagerFinder = new SqlServerProcessManagerFinder(_connectionString, string.Empty);

            // Act
            //processManagerFinder.FindData<TestSqlServerData>(correlationId);
            processManagerFinder.FindData<TestSqlServerData>(_mapper, new Message(correlationId));
            processManagerFinder.UpdateData(sqlServerData);

            // Assert
            var results = GetTestDbData(correlationId);
            Assert.Equal(1, results.Count);
            Assert.Equal(correlationId.ToString(), results[0].Id);
            Assert.False(results[0].DataXml.Contains("TestDataOriginal"));
            Assert.True(results[0].DataXml.Contains("TestDataUpdated"));
        }
        public void ShouldFindData()
        {
            // Arrange
            var correlationId = Guid.NewGuid();

            var data = new TestSqlServerData {CorrelationId = correlationId, Name = "TestData"};
            var xmlSerializer = new XmlSerializer(data.GetType());
            var sww = new StringWriter();
            XmlWriter writer = XmlWriter.Create(sww);
            xmlSerializer.Serialize(writer, data);
            var dataXml = sww.ToString();

            SetupTestDbData(new List<TestDbRow> { new TestDbRow { Id = correlationId.ToString(), DataXml = dataXml, Version = 1} });
            IProcessManagerFinder processManagerFinder = new SqlServerProcessManagerFinder(_connectionString, string.Empty);

            // Act
            var result = processManagerFinder.FindData<TestSqlServerData>(_mapper, new Message(correlationId));

            // Assert
            Assert.Equal("TestData", result.Data.Name);

            // Teardown - complete transaction
            processManagerFinder.UpdateData(result);
        }