Inheritance: AggregateRoot
Beispiel #1
0
        public void ApplyingNoEvents_GivenNewAggregateObject_ShouldNotModifyState()
        {
            // Arrange
            var user = new User();

            // Act
            user.ApplyAll(Enumerable.Empty<object>());

            // Assert
            AssertAreJsonEqual((Guid?)null, user.BaseVersion);
        }
Beispiel #2
0
        public void ApplyingEventsThatHaveBaseType_GivenAnyAggregateObject_ShouldCallAppropriateApplyMethodForEachTypeInEventTypeHierarchy()
        {
            // Arrange
            var user = new User();
            var events = new object[] { new UserRegistered(user.Id), new UserChangedPassword("newpassword"), new UserChangedPassword("newerpassword") };

            // Act
            user.ApplyAll(events);

            // Assert
            Assert.AreEqual(3, user.ChangeCount);
        }
Beispiel #3
0
        public void GivenAggregateWithUncommittedEvents_WhenSaving_ThenUncommittedEventsShouldBeAccepted()
        {
            // Arrange
            var user = new User();
            user.Register();
            var repository = new EventSourcingRepository<User>(new InMemoryEventStore(), new Mock<IConcurrencyMonitor>().Object);

            // Act
            repository.Save(user);

            // Assert
            CollectionAssert.IsEmpty(user.GetUncommittedEvents());
        }
Beispiel #4
0
        public void ApplyingPreviouslyGeneratedEvents_GivenNewAggregateObject_ShouldBringAggregateBackToPreviousState()
        {
            // Arrange
            var user = new User();
            user.Register();
            var events = new object[] {new UserRegistered(user.Id), new UserChangedPassword("newpassword"), new UserChangedPassword("newerpassword") };

            // Act
            user.ApplyAll(events);

            // Assert
            Assert.Throws<InvalidOperationException>(() => user.ChangePassword("newerpassword"), "Expected exception stating the new password must be different the the previous one, indicating that previous events have replayed successfully.");
        }
Beispiel #5
0
        public void ApplyingPreviousEvents_GivenEventsThatWouldNotSatisfyCurrentInvariantLogic_ShouldNotFail()
        {
            // Arrange
            var userId = Guid.Parse("{42B90234-926D-4AA6-A960-F610D52F8F88}");
            var user = new User();
            var events = new object[] {new UserRegistered(userId), new UserChangedPassword("newpassword"), new UserChangedPassword("newpassword") };

            // Act
            user.ApplyAll(events);

            // Assert
            Assert.Throws<InvalidOperationException>(() => user.ChangePassword("newpassword"), "Expected exception stating the new password must be different the the previous one, indicating that previous events have replayed successfully.");
        }
Beispiel #6
0
        public void GivenAggregateWithNoUncommittedEvents_WhenSaved_ThenEventStoreShouldContainNoAdditionalEvents()
        {
            // Arrange
            var eventStore = new InMemoryEventStore();
            var repository = new EventSourcingRepository<User>(eventStore, new Mock<IConcurrencyMonitor>().Object);
            var user = new User();

            var expectedEvents = Enumerable.Empty<object>();

            // Act
            repository.Save(user);

            // Assert
            CollectionAssertAreJsonEqual(expectedEvents, eventStore.Events);
        }
        public void GivenEmptyEventStore_WhenAddingEventsInBulk_ThenStoreShouldContainThoseEvents()
        {
            // Arrange
            IEventStore store = new InMemoryEventStore();
            var user = new User();
            user.Register();
            user.ChangePassword("newpassword");

            // Act
            store.Store(user.Id, user.GetUncommittedEvents());

            // Assert
            CollectionAssert.IsNotEmpty(((InMemoryEventStore)store).Events);
            CollectionAssert.AreEqual(user.GetUncommittedEvents(), ((InMemoryEventStore)store).Events);
        }
Beispiel #8
0
        public void GivenAggregateWithUncommittedEvents_WhenSaved_ThenEventStoreShouldContainThoseEvents()
        {
            // Arrange
            var eventStore = new InMemoryEventStore();
            IRepository<User> repository = new EventSourcingRepository<User>(eventStore, new Mock<IConcurrencyMonitor>().Object);
            var user = new User();
            user.Register();

            var expectedEvents = new object[] { new UserRegistered(user.Id) };

            // Act
            repository.Save(user);

            // Assert
            CollectionAssertAreJsonEqual(expectedEvents, eventStore.Events);
        }
Beispiel #9
0
        public void AcceptingEvents_GivenAggregateWithUncommittedEvents_ShouldClearUncommittedEvents()
        {
            // Arrange
            var user = new User();
            user.Register();
            user.ChangePassword("newpassword");

            // Act
            var userRegisteredEvent = new UserRegistered(user.Id);
            var userChangedPasswordEvent = new UserChangedPassword("newpassword") { ParentVersion = userRegisteredEvent.Version };
            IEnumerable<object> expectedBefore = new object[] { userRegisteredEvent, userChangedPasswordEvent };
            IEnumerable<object> expectedAfter = new object[0];

            IEnumerable<object> before = user.GetUncommittedEvents();
            user.AcceptUncommittedEvents();
            IEnumerable<object> after = user.GetUncommittedEvents();

            // Assert
            CollectionAssertAreJsonEqual(expectedBefore, before);
            CollectionAssertAreJsonEqual(expectedAfter, after);
        }
        public void GivenEventStorePopulatedWithEventsForMultipleAggregates_WhenLoadingEventsForAnAggregate_ThenShouldReturnEventsForThatAggregate()
        {
            // Arrange
            IEventStore store = new InMemoryEventStore();

            var user1 = new User();
            user1.Register();
            user1.ChangePassword("user1pwd1");
            user1.ChangePassword("user1pwd2");

            var user2 = new User();
            user2.Register();
            user2.ChangePassword("user2pwd1");
            user2.ChangePassword("user2pwd2");

            store.Store(user1.Id, user1.GetUncommittedEvents());
            store.Store(user2.Id, user2.GetUncommittedEvents());

            // Act
            IEnumerable<object> eventsForUser1 = store.Load(user1.Id);

            // Assert
            CollectionAssert.AreEqual(user1.GetUncommittedEvents(), eventsForUser1, "Store didn't return user1's events properly.");
        }
Beispiel #11
0
        public void GivenAnyAggregateRoot_WhenBehaviourIsInvokedAndEventsRaised_ThenBaseVersionShouldNotChange()
        {
            // Arrange
            var user = new User();
            user.Register();
            var baseVersion = user.BaseVersion;

            // Act
            user.ChangePassword("newpassword");

            // Assert
            Assert.AreEqual(baseVersion, user.BaseVersion, "User's base version is not correct.");
        }
Beispiel #12
0
        public void GivenNewAggreateWithNoEvents_WhenSaving_ThenShouldNotBotherCheckingConcurrency()
        {
            // Arrange
            var user = new User();
            var concurrencyMonitor = new Mock<IConcurrencyMonitor>();
            var repository = new EventSourcingRepository<User>(new InMemoryEventStore(), concurrencyMonitor.Object);

            // Act
            repository.Save(user);

            // Assert
            concurrencyMonitor.Verify(monitor => monitor.CheckForConflicts(It.IsAny<IEnumerable<object>>(), It.IsAny<IEnumerable<object>>()), Times.Never());
        }
Beispiel #13
0
        public void InvokingBehaviourThatDoesntSetId_GivenNewObject_ShouldFail()
        {
            var user = new User();

            Assert.Throws<IdNotSetException>(() => user.ChangePassword("newpassword"));
        }
Beispiel #14
0
        public void InvokingBehaviourOnObjectWithNoIdThatDoesntSetTheId_ShouldFail()
        {
            var user = new User();
            var events = new object[] { /*new UserRegistered(user.Id), */new UserChangedPassword("newpassword"), new UserChangedPassword("newpassword") };
            user.ApplyAll(events);

            Assert.Throws<IdNotSetException>(() => user.ChangePassword("newnewpassword"));
        }
Beispiel #15
0
        public void InvokingBehaviour_GivenSimpleAggregateRoot_ShouldRecordEvents()
        {
            // Arrange
            var user = new User();
            user.Register();

            // Act
            user.ChangePassword("newpassword");
            IEnumerable<object> actual = user.GetUncommittedEvents();
            var userRegisteredEvent = new UserRegistered(user.Id);
            var userChangedPasswordEvent = new UserChangedPassword("newpassword") { ParentVersion = userRegisteredEvent.Version };
            IEnumerable<object> expected = new object[] { userRegisteredEvent, userChangedPasswordEvent };

            // Assert
            CollectionAssertAreJsonEqual(expected, actual);
        }
Beispiel #16
0
        public void InvokingBehaviour_GivenAggregateWithInvariantLogic_ShouldFailIfInvariantIsNotSatisfied()
        {
            // Arrange
            var user = new User();
            user.Register();
            user.ChangePassword("newpassword");

            // Act / Assert
            Assert.Throws<InvalidOperationException>(() => user.ChangePassword("newpassword"), "Expected exception stating the new password must be different the the previous one.");
        }