public void SendChangedItemsToRemoteReplica_BothParmsHaveChanges_ChangesAggregated()
        {
            IReplica replica   = new Replica("Test");
            var      testItem1 = new TestItem();
            var      testItem2 = new TestItem();

            var requestSender = Mocking.MockAndBind <ISyncRequestSender <TestItem, Guid> >(_container);
            var repository    = Mocking.MockAndBind <IRepository <TestItem, Guid> >(_container);

            Mock.Arrange(() => repository.Find(testItem2.Id)).Returns(testItem2);
            var syncResult = new SyncResult <TestItem, Guid>(_container)
            {
                ChangesRequest = new List <Guid> {
                    testItem2.Id
                }
            };
            var conflictResolutions =
                new ConflictResolutionsToSend <TestItem, Guid> {
                new Resolution <TestItem, Guid>(replica, testItem1)
            };

            var target = new SendingSynchronizer <TestItem, Guid>(_container);

            target.SendChangedItemsToRemoteReplica(syncResult.ChangesRequest, conflictResolutions);

            Mock.Assert(() => requestSender.SendChangedItems(
                            Arg.Matches <IEnumerable <TestItem> >(items => items.Contains(testItem1) && items.Contains(testItem2))));
        }
        public void SendChangedItemsToRemoteReplica__SendsChangesViaRequestSenderForType()
        {
            var requestSender = Mocking.MockAndBind <ISyncRequestSender <TestItem, Guid> >(_container);
            var syncResult    = Mock.Create <ISyncResult <TestItem, Guid> >();
            var target        = new SendingSynchronizer <TestItem, Guid>(_container);
            ConflictResolutionsToSend <TestItem, Guid> conflictResolutions = new ConflictResolutionsToSend <TestItem, Guid>();

            target.SendChangedItemsToRemoteReplica(syncResult.ChangesRequest, conflictResolutions);

            Mock.Assert(() => requestSender.SendChangedItems(Arg.IsAny <IEnumerable <TestItem> >()), Occurs.Once());
        }
        internal void SendChangedItemsToRemoteReplica(IEnumerable <TId> changesRequested,
                                                      ConflictResolutionsToSend <TItem, TId> conflictResolutions)
        {
            var sender = _container.Resolve <ISyncRequestSender <TItem, TId> >();
            var localChangesRequested = changesRequested
                                        .Select(id => _context.Repository.Find(id))
                                        .Where(found => found != null);
            var toSend = localChangesRequested
                         .Union(conflictResolutions.Select(r => r.Item));

            sender.SendChangedItems(toSend);
        }
        public ConflictResolutionsToSend <TestItem, Guid> Resolve(IEnumerable <TestItem> conflictingItems)
        {
            var resolutions = new ConflictResolutionsToSend <TestItem, Guid>();

            foreach (var conflictingItem in conflictingItems)
            {
                if (conflictingItem.Conflicts.Count() > 1)
                {
                    throw new ApplicationException(string.Format("Item {0} with ID {1} has more than one conflict.  This is not supported by this resolver.", conflictingItem, conflictingItem.Id));
                }
                var conflict1 = conflictingItem.Conflicts.First();
                // Find one with the greatest timestamp
                Conflict <Guid> .ConflictItem winner = null;
                foreach (Conflict <Guid> .ConflictItem conflictItem in conflict1.ConflictItems)
                {
                    if (winner == null)
                    {
                        winner = conflictItem;
                    }
                    else
                    {
                        if (((TestItem)conflictItem.Item).LastChangedTime > ((TestItem)winner.Item).LastChangedTime)
                        {
                            winner = conflictItem;
                        }
                    }
                }
                if (winner == null)
                {
                    throw new ApplicationException("conflict.ConflictItems must have at least 2 items.");
                }

                if (winner.Replica.Id != LocalReplica.Id)
                {
                    resolutions.Add(new Resolution <TestItem, Guid>(LocalReplica, (TestItem)winner.Item));
                }
            }
            return(resolutions);
        }