public async Task Find_restores_aggregate_using_memento_if_found()
        {
            // Arrange
            var      user    = new FakeUser(id: Guid.NewGuid(), username: Guid.NewGuid().ToString());
            IMemento memento = user.SaveToMemento();

            user.ChangeUsername(username: Guid.NewGuid().ToString());

            IMementoStore mementoStore = Mock.Of <IMementoStore>();

            Mock.Get(mementoStore)
            .Setup(x => x.Find <FakeUser>(user.Id, default))
            .ReturnsAsync(memento);

            ISqlEventStore eventStore = Mock.Of <ISqlEventStore>();

            Mock.Get(eventStore)
            .Setup(x => x.LoadEvents <FakeUser>(user.Id, 1, default))
            .ReturnsAsync(user.FlushPendingEvents().Skip(1))
            .Verifiable();

            var sut = new SqlEventSourcedRepository <FakeUser>(
                eventStore,
                Mock.Of <ISqlEventPublisher>(),
                mementoStore,
                FakeUser.Factory,
                FakeUser.Factory);

            // Act
            FakeUser actual = await sut.Find(user.Id, default);

            // Assert
            Mock.Get(eventStore).Verify();
            actual.ShouldBeEquivalentTo(user);
        }
        public async Task SaveAndPublish_saves_memento()
        {
            // Arrange
            var    user          = new FakeUser(id: Guid.NewGuid(), username: Guid.NewGuid().ToString());
            string operationId   = Guid.NewGuid().ToString();
            var    correlationId = Guid.NewGuid();
            string contributor   = Guid.NewGuid().ToString();

            IMementoStore mementoStore = Mock.Of <IMementoStore>();

            var sut = new SqlEventSourcedRepository <FakeUser>(
                Mock.Of <ISqlEventStore>(),
                Mock.Of <ISqlEventPublisher>(),
                mementoStore,
                FakeUser.Factory,
                FakeUser.Factory);

            // Act
            await sut.SaveAndPublish(user, operationId, correlationId, contributor);

            // Assert
            Mock.Get(mementoStore).Verify(
                x =>
                x.Save <FakeUser>(
                    user.Id,
                    It.Is <FakeUserMemento>(
                        p =>
                        p.Version == user.Version &&
                        p.Username == user.Username),
                    default),
                Times.Once());
        }
 public AzureEventSourcedRepository(
     IAzureEventStore eventStore,
     IAzureEventPublisher eventPublisher,
     IMementoStore mementoStore,
     Func <Guid, IEnumerable <IDomainEvent>, T> entityFactory,
     Func <Guid, IMemento, IEnumerable <IDomainEvent>, T> mementoEntityFactory)
     : this(eventStore, eventPublisher, entityFactory)
 {
     _mementoStore         = mementoStore ?? throw new ArgumentNullException(nameof(mementoStore));
     _mementoEntityFactory = mementoEntityFactory ?? throw new ArgumentNullException(nameof(mementoEntityFactory));
 }
 public void TestInitialize()
 {
     fixture        = new Fixture().Customize(new AutoMoqCustomization());
     eventStore     = Mock.Of <ISqlEventStore>();
     eventPublisher = Mock.Of <ISqlEventPublisher>();
     mementoStore   = Mock.Of <IMementoStore>();
     sut            = new SqlEventSourcedRepository <FakeUser>(
         eventStore,
         eventPublisher,
         mementoStore,
         FakeUser.Factory,
         FakeUser.Factory);
 }
示例#5
0
 public AzureEventSourcedRepository_features()
 {
     fixture        = new Fixture().Customize(new AutoMoqCustomization());
     eventStore     = Mock.Of <IAzureEventStore>();
     eventPublisher = Mock.Of <IAzureEventPublisher>();
     mementoStore   = Mock.Of <IMementoStore>();
     sut            = new AzureEventSourcedRepository <FakeUser>(
         eventStore,
         eventPublisher,
         mementoStore,
         FakeUser.Factory,
         FakeUser.Factory);
 }
 public void TestInitialize()
 {
     _fixture        = new Fixture().Customize(new AutoMoqCustomization());
     _eventStore     = Mock.Of <IAzureEventStore>();
     _eventPublisher = Mock.Of <IAzureEventPublisher>();
     _mementoStore   = Mock.Of <IMementoStore>();
     _sut            = new AzureEventSourcedRepository <FakeUser>(
         _eventStore,
         _eventPublisher,
         _mementoStore,
         FakeUser.Factory,
         FakeUser.Factory);
 }
        public void SaveAndPublish_does_not_saves_memento_if_fails_to_save_events()
        {
            // Arrange
            var    user          = new FakeUser(id: Guid.NewGuid(), username: Guid.NewGuid().ToString());
            string operationId   = Guid.NewGuid().ToString();
            var    correlationId = Guid.NewGuid();
            string contributor   = Guid.NewGuid().ToString();

            ISqlEventStore eventStore   = Mock.Of <ISqlEventStore>();
            IMementoStore  mementoStore = Mock.Of <IMementoStore>();

            Mock.Get(eventStore)
            .Setup(
                x =>
                x.SaveEvents <FakeUser>(
                    It.IsAny <IEnumerable <IDomainEvent> >(),
                    operationId,
                    correlationId,
                    contributor,
                    default))
            .Throws <InvalidOperationException>();

            var sut = new SqlEventSourcedRepository <FakeUser>(
                eventStore,
                Mock.Of <ISqlEventPublisher>(),
                mementoStore,
                FakeUser.Factory,
                FakeUser.Factory);

            // Act
            Func <Task> action = () => sut.SaveAndPublish(user, operationId, correlationId, contributor);

            // Assert
            action.ShouldThrow <InvalidOperationException>();
            Mock.Get(mementoStore).Verify(
                x =>
                x.Save <FakeUser>(
                    user.Id,
                    It.IsAny <IMemento>(),
                    It.IsAny <CancellationToken>()),
                Times.Never());
        }
        public SqlEventSourcedRepository(
            ISqlEventStore eventStore,
            ISqlEventPublisher eventPublisher,
            IMementoStore mementoStore,
            Func <Guid, IEnumerable <IDomainEvent>, T> entityFactory,
            Func <Guid, IMemento, IEnumerable <IDomainEvent>, T> mementoEntityFactory)
            : this(eventStore, eventPublisher, entityFactory)
        {
            if (mementoStore == null)
            {
                throw new ArgumentNullException(nameof(mementoStore));
            }

            if (mementoEntityFactory == null)
            {
                throw new ArgumentNullException(nameof(mementoEntityFactory));
            }

            _mementoStore         = mementoStore;
            _mementoEntityFactory = mementoEntityFactory;
        }
示例#9
0
 public CreateSnapshotCommandHandler(IRepository repository, IMementoStore mementoStore, ITypeFinder typeFinder)
 {
     _repository   = repository;
     _mementoStore = mementoStore;
     _typeFinder   = typeFinder;
 }