public async Task GivenACommandThenTheAggregateIsRetrievedTheSetOperationInvokedAndTheChangesSavedAsync()
        {
            var identity   = Guid.NewGuid();
            var repository = new Mock <IRepository <SerializableEventCentricAggregateRoot> >();
            var handler    = new TestableCoordinatedOperationHandler <Message>(identity, repository.Object);
            var command    = new SerializableMessage();
            var aggregate  = new SerializableEventCentricAggregateRoot(identity);

            Assert.NotEqual(identity, aggregate.Value);

            _ = repository
                .Setup(repo => repo.GetAsync(
                           It.IsAny <Guid>(),
                           It.IsAny <CancellationToken?>(),
                           It.IsAny <SignedVersion?>()))
                .ReturnsAsync(aggregate);

            await handler.ExecuteAsync(command, CancellationToken.None);

            Assert.Equal(identity, aggregate.Value);

            repository.Verify(
                repo => repo.SaveAsync(
                    It.IsAny <SerializableEventCentricAggregateRoot>(),
                    It.IsAny <CancellationToken?>()),
                Times.Once);

            repository.Verify(
                repo => repo.SaveAsync(
                    It.Is <SerializableEventCentricAggregateRoot>(source => source == aggregate),
                    It.IsAny <CancellationToken?>()),
                Times.Once);
        }
Esempio n. 2
0
        public async Task GivenAPopulatedRepositoryThenAListOfTheMostUpToDateVersionsIsReturnedAsync()
        {
            const int ExpectedTotal = 2;

            var first  = new SerializableEventCentricAggregateRoot();
            var second = new SerializableEventCentricAggregateRoot();

            IRepository <SerializableEventCentricAggregateRoot> repository =
                Create <SerializableEventCentricAggregateRoot>();

            await repository.SaveAsync(first);

            await repository.SaveAsync(second);

            var context = new SerializableMessage();

            second.Set(new SetRequest(context, Guid.NewGuid()));

            await repository.SaveAsync(second);

            IEnumerable <SerializableEventCentricAggregateRoot> results = await repository.GetAllAsync();

            Assert.Equal(ExpectedTotal, results.Count());
            Assert.Contains(results, result => result.Id == first.Id && result.Version == first.Version);
            Assert.Contains(results, result => result.Id == second.Id && result.Version == second.Version);
        }
 public WhenPublishAsyncIsCalled()
 {
     aggregate = new SerializableAggregateRoot();
     context   = new SerializableMessage();
     store     = new Mock <IStore <AtomicUnit, Guid> >();
     bus       = new PersistentBus(store.Object);
 }
Esempio n. 4
0
        public void GivenAContextTheAnInstanceIsReturnedWithTheContextSet()
        {
            var context  = new SerializableMessage();
            var instance = new AggregateDoesNotExistException <AggregateRoot>(context);

            Assert.Equal(context, instance.Context);
        }
Esempio n. 5
0
        public async Task GivenAVersionedReferenceAndARequestForASpecificVersionWhenMoreThanOneVersionExistsThenTheRequestedVersionIsReturnedAsync()
        {
            var           aggregate    = new SerializableAggregateRoot();
            SignedVersion firstVersion = aggregate.Version;
            var           reference    = Reference.Create(aggregate);

            aggregate.MarkChangesAsCommitted();

            var context = new SerializableMessage();

            aggregate.Set();

            _ = repository
                .Setup(repo => repo.GetAsync(
                           It.Is <Guid>(id => id == aggregate.Id),
                           It.IsAny <CancellationToken?>(),
                           It.Is <SignedVersion>(v => v == firstVersion)))
                .ReturnsAsync(aggregate);

            AggregateRoot value = await repository.Object.GetAsync(context, reference, latest : false);

            repository.Verify(
                repo => repo.GetAsync(
                    It.IsAny <Guid>(),
                    It.IsAny <CancellationToken?>(),
                    It.IsAny <SignedVersion>()),
                Times.Once);

            Assert.Equal(aggregate, value);
        }
Esempio n. 6
0
        public async Task GivenAnIdWhenTwoExistingVersionedEntriesExistThenTheMostUpToDateEntryIsReturnedAsync()
        {
            var aggregate = new SerializableEventCentricAggregateRoot();

            IRepository <SerializableEventCentricAggregateRoot> repository =
                Create <SerializableEventCentricAggregateRoot>();

            await repository.SaveAsync(aggregate);

            var context = new SerializableMessage();

            aggregate.Set(new SetRequest(context, Guid.NewGuid()));

            await repository.SaveAsync(aggregate);

            var other = new SerializableEventCentricAggregateRoot();

            await repository.SaveAsync(other);

            SerializableEventCentricAggregateRoot?actual = await repository.GetAsync(aggregate.Id);

            Assert.NotNull(actual);
            Assert.NotSame(aggregate, actual);
            Assert.Equal(aggregate.Id, actual !.Id);
            Assert.Equal(aggregate.Version, actual.Version);
        }
        public void GivenAContextThenTheContextIsPropagated()
        {
            var expectedContext = new SerializableMessage();
            var request         = new TestableRequest(expectedContext);

            Assert.Equal(expectedContext, request.Context);
        }
Esempio n. 8
0
        public async Task GivenAVersionThatIsNotTheCurrentVersionThenANullValueIsReturnedAsync()
        {
            var           aggregate     = new SerializableEventCentricAggregateRoot();
            SignedVersion expectedFirst = aggregate.Version;

            IRepository <SerializableEventCentricAggregateRoot> repository =
                Create <SerializableEventCentricAggregateRoot>();

            await repository.SaveAsync(aggregate);

            var context = new SerializableMessage();

            aggregate.Set(new SetRequest(context, Guid.NewGuid()));

            SignedVersion expectedSecond = aggregate.Version;

            await repository.SaveAsync(aggregate);

            var other = new SerializableEventCentricAggregateRoot();

            await repository.SaveAsync(other);

            SerializableEventCentricAggregateRoot?actualFirst = await repository.GetAsync(aggregate.Id, version : expectedFirst);

            SerializableEventCentricAggregateRoot?actualSecond = await repository.GetAsync(aggregate.Id, version : expectedSecond);

            Assert.Null(actualFirst);

            Assert.NotNull(actualSecond);
            Assert.NotSame(expectedSecond, actualSecond);
            Assert.Equal(aggregate.Id, actualSecond !.Id);
            Assert.Equal(expectedSecond, actualSecond.Version);
        }
Esempio n. 9
0
        public async Task GivenAnAggregateWithChangesWhenAggregateSavedIsRaisedThenTheChangesArePropagatedToTheBusAsync()
        {
            const int ExpectedTotalChanges = 2;

            var context    = new SerializableMessage();
            var aggregate  = new SerializableEventCentricAggregateRoot(context);
            var request    = new SetRequest(context, Guid.NewGuid());
            var bus        = new Mock <IBus>();
            var cloner     = new TestableCloner();
            var repository = new UnversionedMemoryRepository <SerializableEventCentricAggregateRoot>(cloner);
            var propagator = new DomainEventPropagator <SerializableEventCentricAggregateRoot>(bus.Object, repository);
            IEnumerable <DomainEvent> changes = Enumerable.Empty <DomainEvent>();

            _ = bus
                .Setup(b => b.PublishAsync(
                           It.IsAny <IEnumerable <DomainEvent> >(),
                           It.IsAny <CancellationToken?>()))
                .Callback <IEnumerable <DomainEvent>, CancellationToken?>((events, _) => changes = events);

            aggregate.Set(request);

            await repository.SaveAsync(aggregate);

            _ = Assert.Single(changes.OfType <SerializableCreatedDomainEvent>());
            _ = Assert.Single(changes.OfType <SerializableSetDomainEvent>());
            Assert.Equal(ExpectedTotalChanges, changes.Count());
        }
Esempio n. 10
0
        public void Serialize(Message message)
        {
            var serializableMessage = new SerializableMessage(message);
            var serialized          = JsonConvert.SerializeObject(serializableMessage, SerializerSettings);

            exportFile.Value.WriteLine(serialized);
        }
Esempio n. 11
0
        public void GivenAnAggregateAndNoInvariantsThenNoExceptionIsThrown()
        {
            var context   = new SerializableMessage();
            var request   = new TestableRequest(context);
            var aggregate = new SerializableAggregateRoot();

            request.Satisfies(aggregate);
        }
Esempio n. 12
0
        public void GivenTwoEntitiesWithTheSameIdsButDifferentTypesThenAPositiveResponseIsReturned()
        {
            var first  = new SerializableMessage();
            var second = new SerializableEntity <Guid>(first.Id);

            Assert.True(first != second);
            Assert.True(second != first);
        }
Esempio n. 13
0
 private IConnectionMessage ToMessage(byte[] data)
 {
     if (data.Length == 0)
     {
         return(null);
     }
     return(SerializableMessage.FromRawData(data));
 }
Esempio n. 14
0
        public void GivenAnAggregateThenAnInstanceIsCreated()
        {
            var message = new SerializableMessage();
            var @event  = new MessageInvokingAsyncEventArgs(message);

            Assert.Equal(message, @event.Message);
            Assert.Same(message, @event.Message);
        }
        public void GivenAnEventWhenCastToATypedReferenceThenTheTypedReferenceIsReturned()
        {
            var aggregate = new SerializableEventCentricAggregateRoot();
            var message   = new SerializableMessage();
            var @event    = new SerializableDomainEvent <SerializableEventCentricAggregateRoot>(message, aggregate);
            Reference <SerializableEventCentricAggregateRoot> reference = @event;

            Assert.True(reference.IsMatch(aggregate));
        }
        public void GivenTwoEntitiesWithTheSameIdsButDifferentTypesThenANegativeResponseIsReturned()
        {
            var first  = new SerializableMessage();
            var second = new SerializableEntity <Guid>(first.Id);

            Assert.False(first == second);
            Assert.False(first.Equals(second));
            Assert.False(second == first);
        }
        private static DomainEvent[] CreateEvents()
        {
            var aggregate   = new SerializableAggregateRoot();
            var context     = new SerializableMessage();
            var firstEvent  = new SerializableDomainEvent <SerializableAggregateRoot>(context, aggregate);
            var secondEvent = new SerializableDomainEvent <SerializableAggregateRoot>(context, aggregate);

            return(new[] { firstEvent, secondEvent });
        }
        public void GivenAnInstanceThenAllPropertiesAreSerialized()
        {
            var context  = new SerializableMessage();
            var original = new AggregateDoesNotExistException <AggregateRoot>(context);
            AggregateDoesNotExistException <AggregateRoot> deserialized = original.Clone();

            Assert.NotSame(original, deserialized);
            Assert.Equal(original.Context, deserialized.Context);
        }
        protected IEnumerable <DomainEvent> CreateEvents(int count)
        {
            var context   = new SerializableMessage();
            var aggregate = new SerializableAggregateRoot();

            return(Enumerable
                   .Range(0, count)
                   .Select(_ => new SerializableDomainEvent <SerializableAggregateRoot>(context, aggregate))
                   .ToArray());
        }
        public void GivenANonQueryTypedInstanceThenAllPropertiesAreSerialized()
        {
            var context = new SerializableMessage();
            var result  = new SerializablePaginatedResult <int>(context, new Paging(), 100, new[] { 1, 2, 3 });
            SerializablePaginatedResult <int> deserialized = result.Clone();

            Assert.Equal(result, deserialized);
            Assert.NotSame(result, deserialized);
            Assert.Equal(result.GetHashCode(), deserialized.GetHashCode());
        }
Esempio n. 21
0
        public void GivenAContextAndPagingThenThePagingPropertyIsSetToMatch()
        {
            var paging  = new Paging();
            var context = new SerializableMessage();
            var query   = new SerializablePaginatedQuery(context, paging);

            Assert.Equal(context.CorrelationId, query.CorrelationId);
            Assert.Equal(context.Id, query.CausationId);
            Assert.Equal(paging, query.Paging);
        }
Esempio n. 22
0
        public void SerializableAddressToMailAddress()
        {
            var serializableMessage = new SerializableMessage(new SerializableAddress(address, displayName),
                                                              new SerializableAddress(Toaddress));

            serializableMessage.BodyCodePage = displayNameEncoding.CodePage;
            MailMessage mailMessage = serializableMessage;

            Compare(mailMessage, serializableMessage);
        }
Esempio n. 23
0
        public void GivenEventsThenAnInstanceIsReturnedWithTheEventsSet()
        {
            var aggregate = new SerializableEventCentricAggregateRoot();
            var context   = new SerializableMessage();

            SerializableCreatedDomainEvent[] events = new[] { new SerializableCreatedDomainEvent(context, aggregate) };
            var @event = new EventReconciliationAsyncEventArgs(events);

            Assert.Equal(events, @event.Events);
        }
        public void GivenAResultThenTheValueIsReturned(int[] values)
        {
            var context = new SerializableMessage();
            var result  = new SerializableEnumerableResult <int>(context, values);

            int[] actual   = result;
            int[] expected = values ?? Array.Empty <int>();

            Assert.Equal(expected, actual);
        }
        public void GiveEventsFromASingleAggregateVersionButTwoDifferentContextsThenAnArgumentExceptionIsThrown()
        {
            var aggregate     = new SerializableAggregateRoot();
            var firstContext  = new SerializableMessage();
            var secondContext = new SerializableMessage();
            var firstEvent    = new SerializableDomainEvent <SerializableAggregateRoot>(firstContext, aggregate);
            var secondEvent   = new SerializableDomainEvent <SerializableAggregateRoot>(secondContext, aggregate);

            _ = Assert.Throws <ArgumentException>(() => new AtomicUnit(new[] { firstEvent, secondEvent }));
        }
        public void GivenAnEmptyAggregateIdAndAContextThenAnArgumentExceptionIsThrown()
        {
            Guid aggregate = Guid.Empty;
            var  context   = new SerializableMessage();

            ArgumentException exception = Assert.Throws <ArgumentException>(
                () => new AggregateNotFoundException <AggregateRoot>(context, aggregate));

            Assert.Equal(nameof(aggregate), exception.ParamName);
        }
Esempio n. 27
0
        public void GivenAResultThenTheValueIsReturned()
        {
            const int Expected = 9;

            var context = new SerializableMessage();
            var result  = new SerializableResult <int>(context, Expected);
            int actual  = result;

            Assert.Equal(Expected, actual);
        }
        public void GivenANullAggregateAndAContextThenAnArgumentExceptionIsThrown()
        {
            Reference <AggregateRoot>?aggregate = default;
            var context = new SerializableMessage();

            ArgumentException exception = Assert.Throws <ArgumentException>(
                () => new AggregateNotFoundException <AggregateRoot>(context, aggregate !));

            Assert.Equal(nameof(aggregate), exception.ParamName);
        }
Esempio n. 29
0
        public void GivenAContextAndNullPagingThenAnArgumentNullExceptionIsThrown()
        {
            var    context = new SerializableMessage();
            Paging?paging  = default;

            ArgumentNullException exception = Assert.Throws <ArgumentNullException>(
                () => new SerializablePaginatedQuery(context, paging !));

            Assert.Equal(nameof(paging), exception.ParamName);
        }
        public void GiveEventsFromTwoAggregatesThenAnArgumentExceptionIsThrown()
        {
            var firstAggregate  = new SerializableAggregateRoot();
            var secondAggregate = new SerializableAggregateRoot();
            var context         = new SerializableMessage();
            var firstEvent      = new SerializableDomainEvent <SerializableAggregateRoot>(context, firstAggregate);
            var secondEvent     = new SerializableDomainEvent <SerializableAggregateRoot>(context, secondAggregate);

            _ = Assert.Throws <ArgumentException>(() => new AtomicUnit(new[] { firstEvent, secondEvent }));
        }