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(); } var modules = new ModuleRegistry(new StatisticsModule()); host = new TestHostBuilder().Using(documentStore).Using(eventStore).WithModules(modules).Build(); }
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")); }
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 memoryEventSource = new MemoryEventSource(); using IDocumentStore ravenDbDocumentStore = InMemoryRavenTestDriver.Instance.GetDocumentStore(); var modules = new ModuleRegistry(new StatisticsModule()); var hostBuilder = new HostBuilder().ConfigureWebHostDefaults(webHost => { webHost.UseTestServer(); webHost.ConfigureServices(services => { services .AddServicesFrom(modules) .AddSingleton(ravenDbDocumentStore) .AddSingleton(new Dispatcher(memoryEventSource.Subscribe)) .AddMvcCore().ConfigureMvcUsing(modules); }).Configure(app => { app.UseRouting(); app.UseEndpoints(o => o.MapControllers()); }); }); using var host = await hostBuilder.StartAsync(); Guid countryCode = Guid.NewGuid(); using var session = ravenDbDocumentStore.OpenAsyncSession(); await session.StoreAsync(new CountryLookup { Id = $"CountryLookup/{countryCode}", Name = "Netherlands" }); await session.StoreAsync(new DocumentCountProjection { Id = "DocumentCountProjection/123", Country = countryCode, Kind = "Filming" }); await session.SaveChangesAsync(); // Act await memoryEventSource.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")); } } }