예제 #1
0
        /// <summary>
        /// Build a merged list of safes.
        /// </summary>
        /// <param name="leftItems">List of safes from the left repository, whose order has precedence.</param>
        /// <param name="rightItems">List of safes from the right side repository.</param>
        /// <returns>List of safes for the new merged repository.</returns>
        private static SafeListModel BuildMergedListOfSafes(SafeListModel leftItems, SafeListModel rightItems)
        {
            SafeListModel result = new SafeListModel();

            var map = OuterJoin(leftItems, rightItems, item => item.Id);

            foreach (Tuple <SafeModel, SafeModel> pair in map)
            {
                if (pair.Item1 == null)
                {
                    // Only available on the right side
                    result.Add(pair.Item2.Clone());
                }
                else if (pair.Item2 == null)
                {
                    // Only available on the left side
                    result.Add(pair.Item1.Clone());
                }
                else
                {
                    // Take the more recent
                    SafeModel lastModifiedItem = ChooseLastModified(
                        pair.Item1, pair.Item2, item => item.ModifiedAt, null);
                    result.Add(lastModifiedItem.Clone());
                }
            }
            return(result);
        }
        public void FindOldestOpenSafeWorksCorrectly()
        {
            SafeListModel list = new SafeListModel();

            byte[] testKey = new byte[0];

            // Search in empty list
            Assert.IsNull(list.FindOldestOpenSafe());

            // Search with only one element
            SafeModel safe2002 = new SafeModel {
                CreatedAt = new DateTime(2002, 02, 02), Key = testKey
            };

            list.Add(safe2002);
            Assert.AreSame(safe2002, list.FindOldestOpenSafe());

            // Add newer element
            SafeModel safe2003 = new SafeModel {
                CreatedAt = new DateTime(2003, 03, 03), Key = testKey
            };

            list.Add(safe2003);
            Assert.AreSame(safe2002, list.FindOldestOpenSafe());

            // Add closed safe
            SafeModel safe2000 = new SafeModel {
                CreatedAt = new DateTime(2000, 01, 01), Key = null
            };

            list.Add(safe2000);
            Assert.AreSame(safe2002, list.FindOldestOpenSafe());

            // Add older element
            SafeModel safe2001 = new SafeModel {
                CreatedAt = new DateTime(2001, 01, 01), Key = testKey
            };

            list.Add(safe2001);
            Assert.AreSame(safe2001, list.FindOldestOpenSafe());
        }