Esempio n. 1
0
        public static async Task Main(string[] args)
        {
            var eventStore = new JsonFileEventStore("ExampleEvents.zip", 100);

            IDocumentStore documentStore = BuildDocumentStore();

            var dispatcher = new Dispatcher(eventStore.Subscribe);

            IStartableModule module = null;

            IWebHostBuilder hostBuilder = WebHost
                                          .CreateDefaultBuilder(args)
                                          .UseUrls("http://*:9000")
                                          .ConfigureServices(services =>
            {
            })
                                          .Configure(appBuilder =>
            {
                module = appBuilder.UseDocumentStatisticsModule(documentStore, dispatcher);
            });


            using (var host = hostBuilder.Build())
            {
                host.Start();
                await module.Start();

                Console.WriteLine("The statistics module is running. ");
                Console.WriteLine($"Try http://localhost:9000/Statistics/CountsPerState?country=6df7e2ac-6f06-420a-a0b5-14fb3865e850&kind=permit");
                Console.WriteLine($"Examine the Raven DB at http://localhost:9001");

                Console.ReadLine();
            }
        }
Esempio n. 2
0
            public When_a_document_is_activated()
            {
                Given(async() =>
                {
                    UseThe(new MemoryEventSource());

                    SetThe <IDocumentStore>().To(new RavenDocumentStoreBuilder().Build());

                    countryCode = Guid.NewGuid();

                    using (var session = The <IDocumentStore>().OpenAsyncSession())
                    {
                        await session.StoreAsync(new CountryLookupBuilder()
                                                 .IdentifiedBy(countryCode)
                                                 .Named("Netherlands")
                                                 .Build());

                        await session.StoreAsync(new DocumentCountProjectionBuilder()
                                                 .WithNumber("123")
                                                 .InCountry(countryCode)
                                                 .OfKind("Filming")
                                                 .Build());

                        await session.SaveChangesAsync();
                    }

                    IStartableModule module = null;

                    var webHostBuilder = new WebHostBuilder().Configure(b =>
                    {
                        module = b.UseDocumentStatisticsModule(The <IDocumentStore>(), new Dispatcher(The <MemoryEventSource>().Subscribe));
                    });

                    UseThe(new TestServer(webHostBuilder));
                    UseThe(The <TestServer>().CreateClient());

                    await module.Start();
                });

                When(async() =>
                {
                    await The <MemoryEventSource>().Write(new StateTransitionedEvent
                    {
                        DocumentNumber = "123",
                        State          = "Active"
                    });
                });
            }
Esempio n. 3
0
            protected Given_a_raven_projector_with_an_in_memory_event_source()
            {
                Given(async() =>
                {
                    UseThe(new MemoryEventSource());

                    SetThe <IDocumentStore>().To(new RavenDocumentStoreBuilder().Build());

                    IStartableModule module = null;

                    var webHostBuilder = new WebHostBuilder().Configure(b =>
                    {
                        module = b.UseDocumentStatisticsModule(The <IDocumentStore>(),
                                                               new Dispatcher(The <MemoryEventSource>().Subscribe));
                    });

                    UseThe(new TestServer(webHostBuilder));
                    UseThe(The <TestServer>().CreateClient());

                    await module.Start();
                });
            }
            protected override async Task EstablishContext()
            {
                eventStore = new MemoryEventSource();

                documentStore = new RavenDocumentStoreBuilder().Build();

                countryCode = Guid.NewGuid();

                using (var session = documentStore.OpenAsyncSession())
                {
                    await session.StoreAsync(new CountryLookupBuilder()
                                             .IdentifiedBy(countryCode)
                                             .Named("Netherlands")
                                             .Build());

                    await session.StoreAsync(new DocumentCountProjectionBuilder()
                                             .WithNumber("123")
                                             .InCountry(countryCode)
                                             .OfKind("Filming")
                                             .Build());

                    await session.SaveChangesAsync();
                }

                IStartableModule module = null;

                var webHostBuilder = new WebHostBuilder().Configure(b =>
                {
                    module = b.UseDocumentStatisticsModule(documentStore, new Dispatcher(eventStore.Subscribe));
                });

                testServer = new TestServer(webHostBuilder);
                httpClient = testServer.CreateClient();

                await module.Start();
            }
        public async Task When_a_StateTransitionedEvent_is_applied_to_a_DocumentCountProjection_the_controller_should_return_1_active_document()
        {
            // Arrange
            var memoryEventSource = new MemoryEventSource();

            using (IDocumentStore ravenDbDocumentStore = InMemoryRavenTestDriver.Instance.GetDocumentStore())
            {
                Guid   countryCode    = Guid.NewGuid();
                string documentNumber = "123";
                string countryName    = "Netherlands";
                string kind           = "Filming";
                string newState       = "Active";

                using (var session = ravenDbDocumentStore.OpenAsyncSession())
                {
                    await session.StoreAsync(new CountryLookup
                    {
                        Id   = $"CountryLookup/{countryCode}",
                        Name = countryName
                    });

                    await session.StoreAsync(new DocumentCountProjection
                    {
                        Id      = $"DocumentCountProjection/{documentNumber}",
                        Country = countryCode,
                        Kind    = kind
                    });

                    await session.SaveChangesAsync();
                }

                IStartableModule module = null;

                var webHostBuilder = new WebHostBuilder().Configure(builder =>
                {
                    module = builder.UseDocumentStatisticsModule(ravenDbDocumentStore, new Dispatcher(memoryEventSource.Subscribe));
                });

                using (var testServer = new TestServer(webHostBuilder))
                    using (var httpClient = testServer.CreateClient())
                    {
                        await module.Start();

                        // Act
                        await memoryEventSource.Write(new StateTransitionedEvent
                        {
                            DocumentNumber = documentNumber,
                            State          = newState
                        });

                        // Assert
                        HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(
                            $"/statistics/CountsPerState?country={countryCode}&kind={kind}");

                        string responseBody = await httpResponseMessage.Content.ReadAsStringAsync();

                        Assert.Equal(HttpStatusCode.OK, httpResponseMessage.StatusCode);

                        JToken jtokenElement = JToken.Parse(responseBody).Children().FirstOrDefault();

                        Assert.NotNull(jtokenElement);
                        Assert.Equal(countryCode.ToString(), jtokenElement.Value <string>("Country"));
                        Assert.Equal(countryName, jtokenElement.Value <string>("CountryName"));
                        Assert.Equal(kind, jtokenElement.Value <string>("Kind"));
                        Assert.Equal(newState, jtokenElement.Value <string>("State"));
                        Assert.Equal(1, jtokenElement.Value <int>("Count"));
                    }
            }
        }
        public async Task When_a_document_is_activated_it_should_be_included_in_the_active_count()
        {
            // Arrange
            var eventStore = new MemoryEventSource();

            using (var documentStore = new RavenDocumentStoreBuilder().Build())
            {
                Guid countryCode = Guid.NewGuid();

                using (var session = documentStore.OpenAsyncSession())
                {
                    await session.StoreAsync(new CountryLookupBuilder()
                                             .IdentifiedBy(countryCode)
                                             .Named("Netherlands")
                                             .Build());

                    await session.StoreAsync(new DocumentCountProjectionBuilder()
                                             .WithNumber("123")
                                             .InCountry(countryCode)
                                             .OfKind("Filming")
                                             .Build());

                    await session.SaveChangesAsync();
                }

                IStartableModule module = null;

                var webHostBuilder = new WebHostBuilder().Configure(b =>
                {
                    module = b.UseDocumentStatisticsModule(documentStore, new Dispatcher(eventStore.Subscribe));
                });

                using (var testServer = new TestServer(webHostBuilder))
                    using (var httpClient = testServer.CreateClient())
                    {
                        await module.Start();

                        // Act
                        await eventStore.Write(new StateTransitionedEvent
                        {
                            DocumentNumber = "123",
                            State          = "Active"
                        });

                        // Assert
                        HttpResponseMessage response = await httpClient.GetAsync(
                            $"http://localhost/Statistics/CountsPerState?country={countryCode}&kind=Filming");

                        string body = await response.Content.ReadAsStringAsync();

                        JToken counterElement = JToken.Parse(body).Children().FirstOrDefault();

                        Assert.NotNull(counterElement);
                        Assert.Equal(countryCode.ToString(), counterElement.Value <string>("Country"));
                        Assert.Equal("Netherlands", counterElement.Value <string>("CountryName"));
                        Assert.Equal("Filming", counterElement.Value <string>("Kind"));
                        Assert.Equal("Active", counterElement.Value <string>("State"));
                        Assert.Equal(1, counterElement.Value <int>("Count"));
                    }
            }
        }