public void FixList_FjernerDuplicates_ListCount0_Success()
        {
            // Arrange
            var listInsertOrUpdate = new List <Ejerlav>();
            var listDelete         = new List <Ejerlav>();


            int expectedDelCount    = 0;
            int expectedUpsertCount = 0;

            //int expectedTxid = 0;

            // Act
            UpdateEntityHelper.ProcessOperationLists(listDelete, listInsertOrUpdate);

            // Assert
            int delCount    = listDelete.Count();
            int upsertCount = listInsertOrUpdate.Count();

            //int txid = listInsertOrUpdate[0].EntityTxid;


            Assert.AreEqual(expectedDelCount, delCount);
            Assert.AreEqual(expectedUpsertCount, upsertCount);
            //Assert.AreEqual(expectedTxid, txid);
        }
        public void FixUpdList_FjernerDuplicates_ListCount3_Success()
        {
            // Arrange
            var listInsertOrUpdate = new List <Ejerlav>();
            var listDelete         = new List <Ejerlav>();

            JObject update1item = JObject.Parse(TxidEjerlav1u);

            Ejerlav update1 = new Ejerlav()
            {
                Kode = 101
            };

            update1.SetEntityFields(update1item);

            JObject update2item = JObject.Parse(TxidEjerlav2u);

            Ejerlav update2 = new Ejerlav()
            {
                Kode = 101
            };

            update2.SetEntityFields(update2item);

            JObject update3item = JObject.Parse(TxidEjerlav3u);

            Ejerlav update3 = new Ejerlav()
            {
                Kode = 101
            };

            update3.SetEntityFields(update3item);

            listInsertOrUpdate.Add(update1);
            listInsertOrUpdate.Add(update2);
            listInsertOrUpdate.Add(update3);

            int expectedDelCount    = 0;
            int expectedUpsertCount = 1;
            int expectedTxid        = 8;

            // Act
            UpdateEntityHelper.ProcessOperationLists(listDelete, listInsertOrUpdate);

            // Assert
            int  delCount    = listDelete.Count();
            int  upsertCount = listInsertOrUpdate.Count();
            long txid        = listInsertOrUpdate[0].EntityTxid;


            Assert.AreEqual(expectedDelCount, delCount);
            Assert.AreEqual(expectedUpsertCount, upsertCount);
            Assert.AreEqual(expectedTxid, txid);
        }
示例#3
0
        /// <summary>
        /// Get data from DAWA WebApi and update database.
        /// </summary>
        /// <param name="dawaProcessInfo">Latest id from Dawa.</param>
        /// <returns>Count of all deleted, inserted and updated rows.</returns>
        /// <typeparam name="T">Any entity class in the JOInformatik.DawaReplication.DataAccess namespace.</typeparam>
        public static int Update <T>(DawaProcessInfo dawaProcessInfo, DateTime starttime)
            where T : ReplicationBase
        {
            if (dawaProcessInfo == null)
            {
                throw new ArgumentNullException(nameof(dawaProcessInfo));
            }

            if (dawaProcessInfo.Txid < 1)
            {
#pragma warning disable CA2208 // Instantiate argument exceptions correctly
                throw new ArgumentOutOfRangeException(nameof(dawaProcessInfo.Txid));
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
            }

            var    methodName = LoggingUtils.GetMethodName();
            string entityName = typeof(T).Name;
            Logger?.Info($"{methodName}: Processing entity {entityName}");
            var  fixInfo            = fixInfoList.Find(x => x.TableName == entityName);
            var  listInsertOrUpdate = new List <T>();
            var  listDelete         = new List <T>();
            long txidfra            = EntityStateHelper.GetTxid(DBContext, entityName).Value + 1;
            var  stopwatch          = System.Diagnostics.Stopwatch.StartNew();

            // Always use "&noformat" for increased performance:
            using (var httpClient = new HttpClient())
            {
                var stream = httpClient.GetStreamAsync($"{DawaApiUri}replikering/haendelser?entitet={entityName.ToLowerInvariant()}&txidfra={txidfra}&txidtil={dawaProcessInfo.Txid}&noformat").Result;
                stream.ReadTimeout = ReadTimeoutInSeconds * 1000;
                using (var reader = new JsonTextReader(new StreamReader(stream)))
                {
                    while (reader.Read())
                    {
                        if (reader.TokenType == JsonToken.StartObject)
                        {
                            var item      = JObject.Load(reader);
                            var operation = item.Property("operation").Value.ToString();
                            if (operation != null && (operation == "update" || operation == "insert" || operation == "delete"))
                            {
                                var itemData = item.Property("data").First.ToString(Formatting.None);
                                if (fixInfo != null)
                                {
                                    itemData = itemData.Replace(fixInfo.DataValueBad, fixInfo.DataValueValid);
                                }

                                T itemAsObject = JsonConvert.DeserializeObject <T>(itemData, UserJsonSerializerSettings);
                                itemAsObject.SetEntityFields(item);
                                switch (operation)
                                {
                                case "insert":
                                case "update":
                                    listInsertOrUpdate.Add(itemAsObject);
                                    break;

                                case "delete":
                                    listDelete.Add(itemAsObject);
                                    break;
                                }
                            }
                            else
                            {
                                Logger?.Warn($"{methodName}: Unrecognized operation '{operation}' for {entityName}. Txid = {dawaProcessInfo.Txid}");
                            }
                        }
                    }
                }
            }

            UpdateEntityHelper.ProcessOperationLists(listDelete, listInsertOrUpdate);
            int deleteCount       = listDelete.Count;
            int insertUpdateCount = listInsertOrUpdate.Count;

            if (insertUpdateCount == 0 && deleteCount == 0)
            {
                Logger?.Info($"{methodName}: {entityName}: No changes");
                var finishtime = EntityStateHelper.SetEntityStateDone(DBContext, entityName, true, dawaProcessInfo.Txid, 0);
                EntityStateHelper.SetEntityStateHistoryDone(DBContext, entityName, true, starttime, finishtime, txidfra, insertUpdateCount, deleteCount);
            }
            else
            {
                Logger?.Info($"{methodName}: {entityName}: Inserted and Updated = {insertUpdateCount}, Deleted={deleteCount}");
                int totalCount = insertUpdateCount + deleteCount;

                DBContext.Database.BeginTransaction();
                try
                {
                    DBContext.BulkInsertOrUpdate(listInsertOrUpdate);
                    DBContext.BulkDelete(listDelete);
                    var finishtime = EntityStateHelper.SetEntityStateDone(DBContext, entityName, true, dawaProcessInfo.Txid, totalCount);
                    EntityStateHelper.SetEntityStateHistoryDone(DBContext, entityName, true, starttime, finishtime, txidfra, insertUpdateCount, deleteCount);
                    DBContext.Database.CommitTransaction();

                    if (UseMSApplicationInsights)
                    {
                        TelemetryHelper.AddTelemetryForEntity(EntityProcessMode.Update, entityName, stopwatch);
                    }
                }
                catch (Exception ex)
                {
                    if (DBContext.Database.CurrentTransaction != null)
                    {
                        DBContext.Database.RollbackTransaction();
                    }

                    var exception  = ex.InnerException ?? ex;
                    var finishtime = EntityStateHelper.SetEntityStateDone(DBContext, entityName, false, txidfra, totalCount, exception.Message);
                    EntityStateHelper.SetEntityStateHistoryDone(DBContext, entityName, false, starttime, finishtime, txidfra, null, null, exception.Message);
                    throw;
                }
            }

            return(listInsertOrUpdate.Count + listDelete.Count);
        }
        public void FixList_NoDuplicates_Success()
        {
            // Arrange
            var listInsertOrUpdate = new List <Ejerlav>();
            var listDelete         = new List <Ejerlav>();

            JObject update1item = JObject.Parse(TxidEjerlav1u);

            Ejerlav update1 = new Ejerlav()
            {
                Kode = 101
            };

            update1.SetEntityFields(update1item);

            JObject update2item = JObject.Parse(TxidEjerlav2u);

            Ejerlav update2 = new Ejerlav()
            {
                Kode = 102
            };

            update2.SetEntityFields(update2item);

            JObject update3item = JObject.Parse(TxidEjerlav3u);

            Ejerlav update3 = new Ejerlav()
            {
                Kode = 103
            };

            update3.SetEntityFields(update3item);

            JObject delete1item = JObject.Parse(TxidEjerlav3d);

            Ejerlav delete1 = new Ejerlav()
            {
                Kode = 104
            };

            delete1.SetEntityFields(delete1item);

            JObject delete2item = JObject.Parse(TxidEjerlav4d);

            Ejerlav delete2 = new Ejerlav()
            {
                Kode = 105
            };

            delete2.SetEntityFields(delete2item);

            JObject delete3item = JObject.Parse(TxidEjerlav5d);

            Ejerlav delete3 = new Ejerlav()
            {
                Kode = 106
            };

            delete3.SetEntityFields(delete3item);


            listInsertOrUpdate.Add(update1);
            listInsertOrUpdate.Add(update2);
            listDelete.Add(delete1);
            listDelete.Add(delete2);
            listDelete.Add(delete3);
            listInsertOrUpdate.Add(update3);
            int expectedDelCount    = 3;
            int expectedUpsertCount = 3;
            int expectedTxid        = 8;

            // Act
            UpdateEntityHelper.ProcessOperationLists(listDelete, listInsertOrUpdate);

            // Assert
            int  delCount    = listDelete.Count();
            int  upsertCount = listInsertOrUpdate.Count();
            long txid        = listInsertOrUpdate.Max(f => f.EntityTxid);


            Assert.AreEqual(expectedDelCount, delCount);
            Assert.AreEqual(expectedUpsertCount, upsertCount);
            Assert.AreEqual(expectedTxid, txid);
        }