public void LoadContactReadOnly_ContactExistsAndLockedByDifferentOwner_ReturnsContact(
     IElasticAnalyticsContactService contactService,
     ElasticContact eContact,
     ISystemContext ctx)
 {
     var res = contactService.LoadForReadOnly(eContact.Id, ctx);
     res.Should().NotBeNull();
     res.Id.Should().Be(eContact.Id);
 }
        public void DeleteContact_ContactExistsWithActiveLeaseBySameOwner_ContactDeleted(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner us,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, us, false, ctx).Should().BeTrue();

                // deletes seem to be brutal - they don't care about locks
                contactService.Delete(contact.Id, ctx);

                contactService
                    .LoadForReadOnly(contact.Id, ctx)
                    .Should()
                    .BeNull();
            }
        }
        public void DeleteContact_ContactExistsWithActiveLeaseByDifferentOwner_DeleteStillHonoured(
            IElasticAnalyticsContactService contactService,
            ILeaseService leaseService,
            ElasticContact contact,
            ElasticLeaseOwner us,
            ElasticLeaseOwner them,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, them, false, ctx).Should().BeTrue();

                // deletes seem to be brutal
                contactService.Delete(contact.Id, ctx);

                contactService
                    .LoadForReadOnly(contact.Id, ctx)
                    .Should()
                    .BeNull();

                // lease is deleted so if we wanted, we can just go and create another contact with the
                // same id straight away...
                contact.Lease = null;
                contactService.Save(contact, us, false, ctx).Should().BeTrue();
            }
        }
        public void DeleteContact_ContactExistsWithNoLease_ContactDeleted(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner us,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService
                    .Save(contact, us, true, ctx)
                    .Should().BeTrue();

                contactService.Delete(contact.Id, ctx);

                contactService
                    .LoadForReadOnly(contact.Id, ctx)
                    .Should()
                    .BeNull();
            }
        }
        public void DeleteContact_ContactExistsWithExpiredLeaseByDifferentOwner_ContactDeleted(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner them,
            ISystemContext ctx,
            IDateTimeController dateTime,
            TestIndexUtils contactIndex) 
        {
            using (contactIndex)
            {
                // arrange contact with expired lease from another owner...
                var timeTraveller = (DateTimeTimeTraveller)dateTime;

                using (timeTraveller.NewJourney(-24))
                {
                    contactService.Save(contact, them, false, ctx).Should().BeTrue();
                }

                // deletes don't have an owner, at this level at least.
                contactService.Delete(contact.Id, ctx);

                contactService
                    .LoadForReadOnly(contact.Id, ctx)
                    .Should()
                    .BeNull();
            }
        }
        public void SaveContact_ContactDoesntExistShouldNotBeReleased_ContactCreatedWithLock(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner us,
            ElasticLeaseOwner them,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, them, false, ctx)
                    .Should().Be(true);

                var res = contactService.TryLoadAndLock(contact.Id, us, TimeSpan.FromMinutes(1), ctx);

                res.Status.Should().Be(LockAttemptStatus.AlreadyLocked);
                res.LockedObject.ShouldBeEquivalentToContact(contact);

                contactService.LoadForReadOnly(contact.Identification.Identity, ctx)
                    .ShouldBeEquivalentToContact(contact, l => l == null);
            }
        }
        public void SaveContact_ContactExistsWithIdentityNotLocked_ContactUpdated(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner us,
            ElasticLeaseOwner them,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contact.Identification.IdentityLevel.Should().Be(IdentificationLevel.Known);

                contactService.Save(contact, them, true, ctx)
                    .Should().Be(true);

                //contact.SystemInfo.VisitCount = 999; // make a change

                contactService.Save(contact, us, false, ctx)
                    .Should().Be(true);

                // Assert
                var res = contactService.TryLoadAndLock(contact.Id, us, TimeSpan.FromMinutes(1), ctx);
                res.Status.Should().Be(LockAttemptStatus.Success);
                res.LockedObject.ShouldBeEquivalentToContact(contact, l => l.Expires > DateTime.UtcNow && l.IsOwnedBy(us) && l.Version == 4);

                contactService.LoadForReadOnly(contact.Identification.Identity, ctx)
                    .ShouldBeEquivalentToContact(contact, l => l == null);
            }
        }
        public void SaveContact_ContactDoesntExistShouldBeReleased_ContactCreatedLockFree(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner them,
            ElasticLeaseOwner us,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, them, true, ctx)
                    .Should().Be(true);

                var res = contactService.TryLoadAndLock(contact.Id, us, TimeSpan.FromMinutes(1), ctx);

                res.Status.Should().Be(LockAttemptStatus.Success);
                res.LockedObject.ShouldBeEquivalentToContact(contact, l => l.Expires > DateTime.UtcNow && l.IsOwnedBy(us) && l.Version == 3);

                contactService.LoadForReadOnly(contact.Identification.Identity, ctx)
                    .ShouldBeEquivalentToContact(contact);
            }
        }
        public void LoadForReadOnlyByIdentity_ContactObsolete_NullReturned(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticContact successor,
            ElasticLeaseOwner owner,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, owner, true, ctx)
                    .Should().Be(true);

                contactService.Obsolete(contact.Id, successor.Id, owner, ctx);
                contactService.Save(successor, owner, true, ctx);

                contactService.LoadForReadOnly(contact.Identification.Identity, ctx)
                    .Should().BeNull();
            }
        }
        public void LoadForReadOnlyByIdentity_ContactExistsLockedByDifferentOwner_ContactReturned(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner them,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, them, false, ctx)
                    .Should().Be(true);

                contact.Lease = null;
                contactService.LoadForReadOnly(contact.Identification.Identity, ctx)
                    .ShouldBeEquivalentToContact(contact);
            }
        }
        public void LoadForReadOnlyById_ContactObsolete_SuccessorReturned(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticContact successor,
            ElasticLeaseOwner owner,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, owner, true, ctx)
                    .Should().Be(true);

                contactService.Obsolete(contact.Id, successor.Id, owner, ctx);
                contactService.Save(successor, owner, true, ctx);

                contactService.LoadForReadOnly(contact.Id, ctx)
                    .ShouldBeEquivalentToContact(successor, l => l == null);
            }
        }
        public void LoadForReadOnlyById_ContactExistsNotLocked_ContactReturned(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner them,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, them, true, ctx)
                    .Should().Be(true);

                contactService.LoadForReadOnly(contact.Id, ctx)
                    .ShouldBeEquivalentToContact(contact, l => l == null);
            }
        }