Exemple #1
0
        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();
            }

            var modules = new ModuleRegistry(new StatisticsModule());

            var host = new TestHostBuilder().Using(documentStore).Using(eventStore).WithModules(modules).Build();

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

            // Assert
            using var httpClient = host.GetTestClient();

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

            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("Netherlands", jtokenElement.Value <string>("countryName"));
            Assert.Equal("Filming", jtokenElement.Value <string>("kind"));
            Assert.Equal("Active", 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"));
                    }
            }
        }