Exemplo n.º 1
0
        public void CloneCopiesAllProperties()
        {
            NoteModel note1 = new NoteModel
            {
                Id                 = Guid.NewGuid(),
                NoteType           = NoteType.Checklist,
                HtmlContent        = "<html>",
                BackgroundColorHex = "#000000",
                InRecyclingBin     = true,
                CreatedAt          = new DateTime(2000, 10, 22, 18, 55, 30),
                ModifiedAt         = new DateTime(2001, 10, 22, 18, 55, 30),
                MaintainedAt       = new DateTime(2002, 10, 22, 18, 55, 30),
                SafeId             = new Guid("10000000000000000000000000000000"),
            };
            NoteModel note2 = note1.Clone();

            Assert.AreEqual(note1.Id, note2.Id);
            Assert.AreEqual(note1.NoteType, note2.NoteType);
            Assert.AreEqual(note1.HtmlContent, note2.HtmlContent);
            Assert.AreEqual(note1.BackgroundColorHex, note2.BackgroundColorHex);
            Assert.AreEqual(note1.InRecyclingBin, note2.InRecyclingBin);
            Assert.AreEqual(note1.CreatedAt, note2.CreatedAt);
            Assert.AreEqual(note1.ModifiedAt, note2.ModifiedAt);
            Assert.AreEqual(note1.MaintainedAt, note2.MaintainedAt);
            Assert.AreEqual(note1.SafeId, note2.SafeId);
        }
Exemplo n.º 2
0
        public void UseOrderCorrectlyIfNoNewNotes()
        {
            NoteRepositoryModel clientRepo = new NoteRepositoryModel();
            NoteModel           note102    = new NoteModel();
            NoteModel           note104    = new NoteModel();

            clientRepo.Notes.Add(note102);
            clientRepo.Notes.Add(note104);
            NoteRepositoryModel serverRepo = new NoteRepositoryModel();
            NoteModel           note202    = note102.Clone();
            NoteModel           note204    = note104.Clone();

            serverRepo.Notes.Add(note204);
            serverRepo.Notes.Add(note202);

            // Take order of client
            NoteRepositoryMerger merger = new NoteRepositoryMerger();

            clientRepo.OrderModifiedAt = new DateTime(2000, 01, 02); // newer
            serverRepo.OrderModifiedAt = new DateTime(2000, 01, 01); // older
            NoteRepositoryModel result = merger.Merge(clientRepo, serverRepo);

            Assert.AreEqual(2, result.Notes.Count);
            Assert.AreEqual(note102.Id, result.Notes[0].Id);
            Assert.AreEqual(note104.Id, result.Notes[1].Id);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Build a merged list of notes.
        /// </summary>
        /// <param name="leftItems">List of notes from the left repository, whose order has precedence.</param>
        /// <param name="rightItems">List of notes from the right side repository.</param>
        /// <returns>List of notes for the new merged repository.</returns>
        private static NoteListModel BuildMergedListOfNotes(NoteListModel leftItems, NoteListModel rightItems)
        {
            NoteListModel result = new NoteListModel();

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

            foreach (Tuple <NoteModel, NoteModel> 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
                    NoteModel lastModifiedItem = ChooseLastModified(
                        pair.Item1, pair.Item2, item => item.ModifiedAt, item => item.MetaModifiedAt);
                    result.Add(lastModifiedItem.Clone());
                }
            }
            return(result);
        }
Exemplo n.º 4
0
        public void KeepPinnedNotesToTheTop()
        {
            NoteRepositoryModel serverRepo = new NoteRepositoryModel();
            NoteModel           note101    = new NoteModel {
                IsPinned = true
            };
            NoteModel note102 = new NoteModel();
            NoteModel note103 = new NoteModel();
            NoteModel note104 = new NoteModel();

            serverRepo.Notes.Add(note101);
            serverRepo.Notes.Add(note102);
            serverRepo.Notes.Add(note103);
            serverRepo.Notes.Add(note104);

            NoteRepositoryModel clientRepo = new NoteRepositoryModel();
            NoteModel           note201    = note101.Clone(); // IsPinned == true
            NoteModel           note202    = note102.Clone();
            NoteModel           note203    = note103.Clone();
            NoteModel           note204    = note104.Clone();

            note204.IsPinned = true;
            note204.RefreshModifiedAt();
            clientRepo.Notes.Add(note204); // User pinned note so it went to the top
            clientRepo.Notes.Add(note201); // Was already pinned
            clientRepo.Notes.Add(note203); // The new order of 202/203 where changed, but the remote repo has a newer order
            clientRepo.Notes.Add(note202);

            // Take order of server, but keep pinned to the top
            NoteRepositoryMerger merger = new NoteRepositoryMerger();

            serverRepo.OrderModifiedAt = new DateTime(2000, 01, 02); // newer
            clientRepo.OrderModifiedAt = new DateTime(2000, 01, 01);
            NoteRepositoryModel result = merger.Merge(clientRepo, serverRepo);

            Assert.AreEqual(note204.Id, result.Notes[0].Id);
            Assert.AreEqual(note201.Id, result.Notes[1].Id);
            Assert.AreEqual(note202.Id, result.Notes[2].Id);
            Assert.AreEqual(note203.Id, result.Notes[3].Id);
        }
        /// <summary>
        /// Build a combined list of notes, using the map.
        /// </summary>
        /// <param name="orderedNotes">Same ordered repository used to build the map.</param>
        /// <param name="otherNotes">Same other repository used to build the map.</param>
        /// <param name="map">Map with corresponding indexes of the notes of both repositories.</param>
        /// <returns>List of notes for the new merged repository.</returns>
        private static NoteListModel CreateNoteList(NoteListModel orderedNotes, NoteListModel otherNotes, Map map)
        {
            NoteListModel result = new NoteListModel();

            foreach (var pair in map)
            {
                if (pair.Item1 == -1)
                {
                    // Only available in otherNotes
                    NoteModel otherNote = otherNotes[pair.Item2];
                    result.Add(otherNote.Clone());
                }
                else if (pair.Item2 == -1)
                {
                    // Only available in orderedNotes
                    NoteModel orderedNote = orderedNotes[pair.Item1];
                    result.Add(orderedNote.Clone());
                }
                else
                {
                    // Take the more recent note
                    NoteModel orderedNote = orderedNotes[pair.Item1];
                    NoteModel otherNote   = otherNotes[pair.Item2];
                    NoteModel newNote;
                    if (orderedNote.ModifiedAt >= otherNote.ModifiedAt)
                    {
                        newNote = orderedNote.Clone();
                    }
                    else
                    {
                        newNote = otherNote.Clone();
                    }
                    result.Add(newNote);
                }
            }
            return(result);
        }
Exemplo n.º 6
0
        public void UseOrderCorrectly()
        {
            NoteRepositoryModel clientRepo = new NoteRepositoryModel();
            NoteModel           note101    = new NoteModel();
            NoteModel           note102    = new NoteModel();
            NoteModel           note103    = new NoteModel();
            NoteModel           note104    = new NoteModel();
            NoteModel           note105    = new NoteModel();

            clientRepo.Notes.Add(note101);
            clientRepo.Notes.Add(note102);
            clientRepo.Notes.Add(note103);
            clientRepo.Notes.Add(note104);
            clientRepo.Notes.Add(note105);
            NoteRepositoryModel serverRepo = new NoteRepositoryModel();
            NoteModel           note201    = new NoteModel();
            NoteModel           note202    = note103.Clone();
            NoteModel           note203    = new NoteModel();
            NoteModel           note204    = note102.Clone();
            NoteModel           note205    = new NoteModel();
            NoteModel           note206    = note104.Clone();

            serverRepo.Notes.Add(note201);
            serverRepo.Notes.Add(note202);
            serverRepo.Notes.Add(note203);
            serverRepo.Notes.Add(note204);
            serverRepo.Notes.Add(note205);
            serverRepo.Notes.Add(note206);

            // Take order of client
            NoteRepositoryMerger merger = new NoteRepositoryMerger();

            clientRepo.OrderModifiedAt = new DateTime(2000, 01, 02); // newer
            serverRepo.OrderModifiedAt = new DateTime(2000, 01, 01); // older
            NoteRepositoryModel result = merger.Merge(clientRepo, serverRepo);

            Assert.AreEqual(8, result.Notes.Count);
            Assert.AreEqual(note101.Id, result.Notes[0].Id);
            Assert.AreEqual(note201.Id, result.Notes[1].Id);
            Assert.AreEqual(note102.Id, result.Notes[2].Id);
            Assert.AreEqual(note205.Id, result.Notes[3].Id);
            Assert.AreEqual(note103.Id, result.Notes[4].Id);
            Assert.AreEqual(note203.Id, result.Notes[5].Id);
            Assert.AreEqual(note104.Id, result.Notes[6].Id);
            Assert.AreEqual(note105.Id, result.Notes[7].Id);

            // Take order of server
            clientRepo.OrderModifiedAt = new DateTime(2000, 01, 01); // older
            serverRepo.OrderModifiedAt = new DateTime(2000, 01, 02); // newer
            result = merger.Merge(clientRepo, serverRepo);

            Assert.AreEqual(8, result.Notes.Count);
            Assert.AreEqual(note201.Id, result.Notes[0].Id);
            Assert.AreEqual(note101.Id, result.Notes[1].Id);
            Assert.AreEqual(note202.Id, result.Notes[2].Id);
            Assert.AreEqual(note203.Id, result.Notes[3].Id);
            Assert.AreEqual(note204.Id, result.Notes[4].Id);
            Assert.AreEqual(note205.Id, result.Notes[5].Id);
            Assert.AreEqual(note206.Id, result.Notes[6].Id);
            Assert.AreEqual(note105.Id, result.Notes[7].Id);
        }