public async Task InitialReadModelVersionIsNull()
        {
            // Arrange
            var thingyId = ThingyId.New;

            // Act
            var version = await QueryProcessor.ProcessAsync(new ThingyGetVersionQuery(thingyId)).ConfigureAwait(false);

            // Assert
            version.Should().NotHaveValue();
        }
Esempio n. 2
0
        public async Task OnPostBorrowAsync(string id)
        {
            var 利用者 = (await QueryProcessor.ProcessAsync(new 利用者AllQuery(), CancellationToken.None)).First();

            await CommandBus
            .PublishAsync(
                new 本を借りるCommand(本のID.With(id), 貸出期間.今日から2週間, 利用者.Id),
                CancellationToken.None).ConfigureAwait(false);

            本一覧 = await QueryProcessor.ProcessAsync(new 本DTOQuery(), CancellationToken.None);
        }
        public async Task NonExistingReadModelReturnsNull()
        {
            // Arrange
            var id = ThingyId.New;

            // Act
            var readModel = await QueryProcessor.ProcessAsync(new ThingyGetQuery(id)).ConfigureAwait(false);

            // Assert
            readModel.Should().BeNull();
        }
Esempio n. 4
0
        public async Task <CompanyResponse> Get(string companyId)
        {
            var readModel = await QueryProcessor.ProcessAsync(
                new CompanyGetQuery(new CompanyId(companyId)), CancellationToken.None)
                            .ConfigureAwait(false);

            var response = new CompanyResponse {
                TenantId = readModel.TenantId, Id = readModel.Id.GetGuid().ToString(), Name = readModel.Name, Address = readModel.Address, CreatedDate = readModel.CreatedDate, ModifiedDate = readModel.ModifiedDate, IsDeleted = readModel.IsDeleted
            };

            return(response);
        }
        public async Task <bool> Put(string companyId, CompanyRequest request)
        {
            var oldCompany = await QueryProcessor.ProcessAsync(
                new CompanyGetQuery(new CompanyId(companyId)), CancellationToken.None)
                             .ConfigureAwait(false);

            var newCompanyRecord = new Company(oldCompany.Id, request.Name, request.Address, "", oldCompany.CreatedDate, oldCompany.ModifiedDate, oldCompany.IsDeleted);
            var cmd = new CompanyEditCommand(new CompanyId(companyId), newCompanyRecord);
            await CommandBus.PublishAsync(cmd, CancellationToken.None).ConfigureAwait(false);

            return(true);
        }
        public async Task OnGetAsync(OperativeId operativeId)
        {
            Result = await QueryProcessor.ProcessAsync(
                new GetOperativesCurrentWorkQuery { OperativeId = operativeId },
                CancellationToken.None);

            if (Result.Work != null)
            {
                WorkId      = Result.Work.WorkId;
                OperativeId = Result.Operative.OperativeId;
            }
        }
Esempio n. 7
0
        public async Task <int> GetByAddress(string address, Guid companyId)
        {
            var count = 0;

            if (!string.IsNullOrEmpty(address))
            {
                count = await QueryProcessor.ProcessAsync(
                    new CompanyGetByAddressQuery((address), CompanyId.With(companyId)), CancellationToken.None)
                        .ConfigureAwait(false);
            }
            return(count);
        }
Esempio n. 8
0
        public async Task ReadModelVersionShouldMatchAggregate()
        {
            // Arrange
            var thingyId = ThingyId.New;
            const int expectedVersion = 5;
            await PublishPingCommandsAsync(thingyId, expectedVersion).ConfigureAwait(false);

            // Act
            var version = await QueryProcessor.ProcessAsync(new ThingyGetVersionQuery(thingyId)).ConfigureAwait(false);

            // Assert
            version.Should().Be((long)version);
        }
Esempio n. 9
0
        public async Task ReadModelReceivesEvent()
        {
            // Arrange
            var id = ThingyId.New;
            
            // Act
            await PublishPingCommandsAsync(id, 5).ConfigureAwait(false);
            var readModel = await QueryProcessor.ProcessAsync(new ThingyGetQuery(id)).ConfigureAwait(false);

            // Assert
            readModel.Should().NotBeNull();
            readModel.PingsReceived.Should().Be(5);
        }
Esempio n. 10
0
        public async Task PurgeRemovesReadModels()
        {
            // Arrange
            var id = ThingyId.New;
            await PublishPingCommandsAsync(id).ConfigureAwait(false);

            // Act
            await PurgeTestAggregateReadModelAsync().ConfigureAwait(false);
            var readModel = await QueryProcessor.ProcessAsync(new ThingyGetQuery(id)).ConfigureAwait(false);

            // Assert
            readModel.Should().BeNull();
        }
Esempio n. 11
0
        public async Task AfterAddFlightShouldAddToReadModel()
        {
            //Arrange
            var scenario = new AddFlightScenario(CommandBus);

            //Act
            await scenario.Execute();

            //Assert
            var query = new DepartureStationQuery("MEL");

            var flights = await QueryProcessor.ProcessAsync(query, CancellationToken.None);

            flights.Should().NotBeEmpty();
        }
Esempio n. 12
0
        public async Task CanStoreMultipleMessages()
        {
            // Arrange
            var thingyId = ThingyId.New;
            var otherThingyId = ThingyId.New;
            var thingyMessages = await CreateAndPublishThingyMessagesAsync(thingyId, 5).ConfigureAwait(false);
            await CreateAndPublishThingyMessagesAsync(otherThingyId, 3).ConfigureAwait(false);

            // Act
            var returnedThingyMessages = await QueryProcessor.ProcessAsync(new ThingyGetMessagesQuery(thingyId)).ConfigureAwait(false);

            // Assert
            returnedThingyMessages.Should().HaveCount(thingyMessages.Count);
            returnedThingyMessages.ShouldAllBeEquivalentTo(thingyMessages);
        }
        public async Task PurgeRemovesReadModels()
        {
            // Arrange
            var id = ThingyId.New;

            await PublishPingCommandAsync(id).ConfigureAwait(false);

            // Act
            await ReadModelPopulator.PurgeAsync(ReadModelType, CancellationToken.None).ConfigureAwait(false);

            var readModel = await QueryProcessor.ProcessAsync(new ThingyGetQuery(id)).ConfigureAwait(false);

            // Assert
            readModel.Should().BeNull();
        }
        public async Task CanStoreMessageHistory()
        {
            // Arrange
            var thingyId       = ThingyId.New;
            var thingyMessages = Fixture.CreateMany <ThingyMessage>(5).ToList();
            var command        = new ThingyAddMessageHistoryCommand(thingyId, thingyMessages);
            await CommandBus.PublishAsync(command, CancellationToken.None);

            // Act
            var returnedThingyMessages = await QueryProcessor.ProcessAsync(new ThingyGetMessagesQuery(thingyId)).ConfigureAwait(false);

            // Assert
            returnedThingyMessages.Should().HaveCount(thingyMessages.Count);
            returnedThingyMessages.Should().BeEquivalentTo(thingyMessages);
        }
        public async Task AfterSelectedJourneysShouldAddJourneysToReadModel()
        {
            //Arrange
            var selectJourneysScenario = new SelectJourneysScenario(CommandBus);

            //Act
            await selectJourneysScenario.Execute();

            var bookingId = selectJourneysScenario.BookingId;

            //Assert
            var query   = new BookingIdQuery(bookingId.Value);
            var booking = await QueryProcessor.ProcessAsync(query, CancellationToken.None);

            booking.Journeys.Should().NotBeEmpty();
        }
        public async Task <bool> Delete(string companyId)
        {
            var company = await QueryProcessor.ProcessAsync(
                new CompanyGetQuery(new CompanyId(companyId)), CancellationToken.None)
                          .ConfigureAwait(false);

            if (company == null)
            {
                return(false);
            }

            var cmd = new CompanyDeleteCommand(company.Id, company);
            await CommandBus.PublishAsync(cmd, CancellationToken.None).ConfigureAwait(false);

            return(true);
        }
Esempio n. 17
0
        public async Task AfterAddedPassengerShouldAddPassengerToReadModel()
        {
            //Arrange
            var addPassengerScenario = new AddPassengerScenario(CommandBus);

            //Act
            await addPassengerScenario.Execute();

            var bookingId = addPassengerScenario.BookingId;

            //Assert
            var query   = new BookingIdQuery(bookingId.Value);
            var booking = await QueryProcessor.ProcessAsync(query, CancellationToken.None);

            booking.Passengers.Should().HaveCount(1);
        }
Esempio n. 18
0
        public async Task PopulateCreatesReadModels()
        {
            // Arrange
            var id = ThingyId.New;

            await PublishPingCommandAsync(id, 2).ConfigureAwait(false);
            await PurgeTestAggregateReadModelAsync().ConfigureAwait(false);

            // Act
            await PopulateTestAggregateReadModelAsync().ConfigureAwait(false);

            var readModel = await QueryProcessor.ProcessAsync(new ThingyGetQuery(id)).ConfigureAwait(false);

            // Assert
            readModel.Should().NotBeNull();
            readModel.PingsReceived.Should().Be(2);
        }
Esempio n. 19
0
        public async Task <IList <CompanyResponse> > GetCompanyByName(string name)
        {
            var readModel = await QueryProcessor.ProcessAsync(new CompanySearchByNameQuery(name), CancellationToken.None)
                            .ConfigureAwait(false);

            var response = new List <CompanyResponse>();

            foreach (var item in readModel.Records)
            {
                response.Select(x => new CompanyResponse
                {
                    Name    = item.Name,
                    Address = item.Address,
                }).ToList();
            }
            return(response);
        }
        public async Task when_processing_the_query()
        {
            FakeQueryTypeCollection = new Mock <IQueryTypeCollection>();
            FakeServiceProvider     = new Mock <IServiceProvider>();
            Subject = new QueryProcessor(FakeQueryTypeCollection.Object, FakeServiceProvider.Object);

            async Task should_invoke_the_correct_query_handler_and_return_a_result()
            {
                FakeQuery expectedQuery    = null;
                var       expectedResult   = new FakeResult();
                var       fakeQueryHandler = new FakeQueryHandler(x => { expectedQuery = x; return(expectedResult); });

                FakeServiceProvider.Setup(x => x.GetService(typeof(IEnumerable <IQueryHandler <FakeQuery, FakeResult> >))).Returns(new[] { fakeQueryHandler });

                var query  = new FakeQuery();
                var result = await Subject.ProcessAsync(query);

                query.Should().Be(expectedQuery);
                result.Should().Be(expectedResult);
            }

            void should_throw_exception_if_the_query_handler_is_not_found()
            {
                var query = new Mock <IQuery <FakeResult> >().Object;

                Subject.Awaiting(x => x.ProcessAsync(query)).Should()
                .Throw <QueryProcessorException>()
                .WithMessage($"The query handler for '{query}' could not be found");
            }

            void should_throw_exception_if_multiple_query_handlers_are_found()
            {
                var handlerType    = typeof(IQueryHandler <FakeMultiQuery1, FakeResult>);
                var enumerableType = typeof(IEnumerable <IQueryHandler <FakeMultiQuery1, FakeResult> >);

                FakeServiceProvider.Setup(x => x.GetService(enumerableType)).Returns(new[] { new Mock <IQueryHandler <FakeMultiQuery1, FakeResult> >().Object, new Mock <IQueryHandler <FakeMultiQuery1, FakeResult> >().Object });

                var query = new FakeMultiQuery1();

                Subject.Awaiting(x => x.ProcessAsync(query)).Should()
                .Throw <QueryProcessorException>()
                .WithMessage($"Multiple query handlers for '{handlerType}' was found");
            }
        }
Esempio n. 21
0
        public async Task AfterAddedPassengerTwiceShouldAddTwoPassengersToReadModel()
        {
            //Arrange
            var addPassengerScenario = new AddPassengerScenario(CommandBus);
            await addPassengerScenario.Execute();

            var bookingId = addPassengerScenario.BookingId;

            //Act
            var p = new PassengerBuilder().CreatePassenger(x => { x.Name = "AnotherYang"; });
            var addAnotherPassengerScenario = new AddPassengerScenario(CommandBus, bookingId, p);
            await addAnotherPassengerScenario.Execute();

            //Assert
            var query   = new BookingIdQuery(bookingId.Value);
            var booking = await QueryProcessor.ProcessAsync(query, CancellationToken.None);

            booking.Passengers.Should().HaveCount(2);
        }
Esempio n. 22
0
        public async Task RePopulateHandlesManyAggregates()
        {
            // Arrange
            var id1 = ThingyId.New;
            var id2 = ThingyId.New;
            await PublishPingCommandsAsync(id1, 3).ConfigureAwait(false);
            await PublishPingCommandsAsync(id2, 5).ConfigureAwait(false);

            // Act
            await PurgeTestAggregateReadModelAsync().ConfigureAwait(false);
            await PopulateTestAggregateReadModelAsync().ConfigureAwait(false);

            // Assert
            var readModel1 = await QueryProcessor.ProcessAsync(new ThingyGetQuery(id1)).ConfigureAwait(false);
            var readModel2 = await QueryProcessor.ProcessAsync(new ThingyGetQuery(id2)).ConfigureAwait(false);

            readModel1.PingsReceived.Should().Be(3);
            readModel2.PingsReceived.Should().Be(5);
        }
        public async Task ReadModelContainsPersonNameAfterCreation()
        {
            // Arrange
            var id = PersonId.New;

            // Act
            await CommandBus
            .PublishAsync(new CreatePersonCommand(id, "Bob"), CancellationToken.None)
            .ConfigureAwait(false);

            var readModel = await QueryProcessor
                            .ProcessAsync(new PersonGetQuery(id), CancellationToken.None)
                            .ConfigureAwait(false);

            // Assert
            readModel.Should().NotBeNull();
            readModel.Name.Should().Be("Bob");
            readModel.Addresses.Should().BeNullOrEmpty();
        }
Esempio n. 24
0
        public async Task RePopulateHandlesDeletedAggregate()
        {
            // Arrange
            var id1 = ThingyId.New;
            var id2 = ThingyId.New;

            await PublishPingCommandsAsync(id1, 3).ConfigureAwait(false);
            await PublishPingCommandsAsync(id2, 5).ConfigureAwait(false);

            // Act
            await ReadModelPopulator.DeleteAsync(id2.Value, ReadModelType, CancellationToken.None).ConfigureAwait(false);

            await ReadModelPopulator.PopulateAsync(ReadModelType, CancellationToken.None).ConfigureAwait(false);

            // Assert
            var readModel = await QueryProcessor.ProcessAsync(new ThingyGetQuery(id2)).ConfigureAwait(false);

            readModel.PingsReceived.Should().Be(5);
        }
        public async Task MultipleUpdatesAreHandledCorrect()
        {
            // Arrange
            var id      = ThingyId.New;
            var pingIds = new List <PingId>
            {
                await PublishPingCommandAsync(id).ConfigureAwait(false)
            };

            for (var i = 0; i < 5; i++)
            {
                // Act
                pingIds.Add(await PublishPingCommandAsync(id).ConfigureAwait(false));

                // Assert
                var readModel = await QueryProcessor.ProcessAsync(new ThingyGetQuery(id)).ConfigureAwait(false);

                readModel.PingsReceived.Should().Be(pingIds.Count);
            }
        }
Esempio n. 26
0
        public async Task CanHandleMultipleMessageAtOnce()
        {
            // Arrange
            var thingyId = ThingyId.New;
            var pingIds = Many<PingId>(5);
            var thingyMessages = Many<ThingyMessage>(7);

            // Act
            await CommandBus.PublishAsync(new ThingyImportCommand(
                thingyId,
                pingIds,
                thingyMessages))
                .ConfigureAwait(false);
            var returnedThingyMessages = await QueryProcessor.ProcessAsync(new ThingyGetMessagesQuery(thingyId)).ConfigureAwait(false);
            var thingy = await QueryProcessor.ProcessAsync(new ThingyGetQuery(thingyId)).ConfigureAwait(false);

            // Assert
            thingy.PingsReceived.Should().Be(pingIds.Count);
            returnedThingyMessages.ShouldAllBeEquivalentTo(returnedThingyMessages);
        }
        public async Task AfterUpdatedPassengerNameShouldUpdateNameToReadModel()
        {
            //Arrange
            var newName = "new-name";
            var updatePassengerNameScenario = new UpdatePassengerNameScenario(CommandBus, newName);

            //Act
            await updatePassengerNameScenario.Execute();

            var bookingId    = updatePassengerNameScenario.BookingId;
            var passengerKey = updatePassengerNameScenario.PassengerKey;

            //Assert
            var query   = new BookingIdQuery(bookingId.Value);
            var booking = await QueryProcessor.ProcessAsync(query, CancellationToken.None);

            var passenger = booking.Passengers.Single(p => p.PassengerKey == passengerKey);

            passenger.Name.Should().Be(newName);
        }
Esempio n. 28
0
        public async Task UpdateMultiple(
            bool pingFirst,
            bool injectPing)
        {
            // Arrange
            var readStoreManager = Resolver.Resolve <IReadStoreManager>();
            var thingyId         = ThingyId.New;
            var thingyMessage    = Fixture.Create <ThingyMessage>();
            var pingId           = PingId.New;
            var command          = new ThingyAddMessageAndPingCommand(
                thingyId,
                thingyMessage,
                pingId,
                pingFirst);
            var metadata = new Metadata
            {
                Timestamp = DateTimeOffset.Now,
                AggregateSequenceNumber = 1,
                AggregateName           = typeof(ThingyAggregate).GetAggregateName().Value,
                AggregateId             = thingyId.Value,
                EventId = EventId.New,
            };
            var domainEvent = injectPing
                ? DomainEventFactory.Create(new ThingyPingEvent(pingId), metadata, thingyId.Value, 1)
                : DomainEventFactory.Create(new ThingyMessageAddedEvent(thingyMessage), metadata, thingyId.Value, 1);
            await readStoreManager.UpdateReadStoresAsync(
                new[] { domainEvent },
                CancellationToken.None);

            // Act
            await CommandBus.PublishAsync(command, CancellationToken.None);

            // Assert
            var returnedThingyMessages = await QueryProcessor.ProcessAsync(new ThingyGetMessagesQuery(thingyId)).ConfigureAwait(false);

            returnedThingyMessages.Should().HaveCount(1);
            var readModel = await QueryProcessor.ProcessAsync(new ThingyGetQuery(thingyId)).ConfigureAwait(false);

            readModel.PingsReceived.Should().Be(1);
        }
Esempio n. 29
0
        public async Task OnGet()
        {
            if (this.UserName == null)
            {
                this.UserName = GetString("UserName");
                if (this.UserName == null)
                {
                    this.UserName = "******";
                }
            }

            ConversationId identity;
            var            test = GetString("identity"); //get identity from current conversation in session

            if (test == null)
            {
                identity = ConversationId.New;
                SetString("identity", identity.ToString());                     // save identity to session
                var command = new CreateConversation(identity);
                await CommandBus.PublishAsync(command, CancellationToken.None); //transferring command to its handler
            }
            else
            {
                identity = new ConversationId(test);
            }

            var result = await QueryProcessor.ProcessAsync(new ReadModelByIdQuery <ConversationReadModel>(identity),
                                                           CancellationToken.None); // ReadModelByIDQuery is out of the box in Event Flow for quering read models

            if (result.Messages == null)
            {
                Messages = new List <Message>();
            }
            else
            {
                Messages = result.Messages;
            }
        }
        public async Task <EmployeeResponse> Get(string employeeId)
        {
            var readModel = await QueryProcessor.ProcessAsync(
                new EmployeeGetQuery(new EmployeeId(employeeId)), CancellationToken.None)
                            .ConfigureAwait(false);

            var transactionsReadModel = await QueryProcessor.ProcessAsync(
                new TransactionsGetQuery(new EmployeeId(employeeId)), CancellationToken.None)

                                        .ConfigureAwait(false);

            var transactions = new List <TransactionResponse>();

            if (transactionsReadModel != null)
            {
                transactions.AddRange(transactionsReadModel.Select(transaction => new TransactionResponse(transaction.Date, transaction.Salary)));
            }
            var response = new EmployeeResponse {
                Id = readModel.Id.GetGuid().ToString( ), FirstName = readModel.FirstName, LastName = readModel.LastName, Transactions = transactions
            };

            return(response);
        }