public async Task WhenPeriodOfLifeCycleStageWasChanged() { var projection = new PublicServiceLifeCycleListProjections(); var resolver = ConcurrentResolve.WhenEqualToHandlerMessageType(projection.Handlers); var stageWasAddedToLifeCycle = _fixture.Create <StageWasAddedToLifeCycle>(); var periodOfLifeCycleStageWasChanged = new PeriodOfLifeCycleStageWasChanged( new PublicServiceId(stageWasAddedToLifeCycle.PublicServiceId), LifeCycleStageId.FromNumber(stageWasAddedToLifeCycle.LifeCycleStageId), _fixture.Create <LifeCycleStagePeriod>()); await new ConnectedProjectionScenario <BackofficeContext>(resolver) .Given( new Envelope <StageWasAddedToLifeCycle>(new Envelope(stageWasAddedToLifeCycle, new Dictionary <string, object>())), new Envelope <PeriodOfLifeCycleStageWasChanged>(new Envelope(periodOfLifeCycleStageWasChanged, new Dictionary <string, object>()))) .Verify(async context => { var publicService = await context.PublicServiceLifeCycleList.FirstAsync(a => a.PublicServiceId == stageWasAddedToLifeCycle.PublicServiceId && a.LifeCycleStageId == stageWasAddedToLifeCycle.LifeCycleStageId); publicService.Should().BeEquivalentTo(new PublicServiceLifeCycleItem() { LifeCycleStageId = stageWasAddedToLifeCycle.LifeCycleStageId, PublicServiceId = stageWasAddedToLifeCycle.PublicServiceId, LifeCycleStageType = stageWasAddedToLifeCycle.LifeCycleStageType, From = periodOfLifeCycleStageWasChanged.From, To = periodOfLifeCycleStageWasChanged.To }); return(VerificationResult.Pass()); }) .Assert(); }
public async Task WhenPublicServiceWasRegistered() { var projection = new PublicServiceListProjections(new ClockProviderStub(DateTime.Now)); var resolver = ConcurrentResolve.WhenEqualToHandlerMessageType(projection.Handlers); var publicServiceId = PublicServiceId.FromNumber(123); var publicServiceWasRegistered = new PublicServiceWasRegistered(publicServiceId, new PublicServiceName("Ophaling huisvuil"), PrivateZoneId.Unregistered); await new ConnectedProjectionScenario <BackofficeContext>(resolver) .Given( new Envelope <PublicServiceWasRegistered>(new Envelope( publicServiceWasRegistered, new Dictionary <string, object>()))) .Verify(async context => { var publicService = await context.PublicServiceList.FirstAsync(a => a.PublicServiceId == publicServiceId); publicService.PublicServiceId.Should().Be("DVR000000123"); publicService.Name.Should().Be("Ophaling huisvuil"); publicService.CompetentAuthorityCode.Should().BeNullOrEmpty(); publicService.CompetentAuthorityName.Should().BeNullOrEmpty(); publicService.ExportToOrafin.Should().Be(false); return(VerificationResult.Pass()); }) .Assert(); }
public static Task ExpectNone(this ProjectionScenario <IAsyncDocumentSession> scenario) { return(scenario .Verify(async session => { using (var streamer = await session.Advanced.StreamAsync <RavenJObject>(Etag.Empty)) { if (await streamer.MoveNextAsync()) { var storedDocumentIdentifiers = new List <string>(); do { storedDocumentIdentifiers.Add(streamer.Current.Key); } while (await streamer.MoveNextAsync()); return VerificationResult.Fail( string.Format("Expected no documents, but found {0} document(s) ({1}).", storedDocumentIdentifiers.Count, string.Join(",", storedDocumentIdentifiers))); } return VerificationResult.Pass(); } }) .Assert()); }
public async Task WhenLifeCycleStageWasRemoved() { var projection = new PublicServiceLifeCycleListProjections(); var resolver = ConcurrentResolve.WhenEqualToHandlerMessageType(projection.Handlers); var stageWasAddedToLifeCycle = _fixture.Create <StageWasAddedToLifeCycle>(); var lifeCycleStageWasRemoved = new LifeCycleStageWasRemoved( new PublicServiceId(stageWasAddedToLifeCycle.PublicServiceId), LifeCycleStageId.FromNumber(stageWasAddedToLifeCycle.LifeCycleStageId)); await new ConnectedProjectionScenario <BackofficeContext>(resolver) .Given( new Envelope <StageWasAddedToLifeCycle>(new Envelope(stageWasAddedToLifeCycle, new Dictionary <string, object>())), new Envelope <LifeCycleStageWasRemoved>(new Envelope(lifeCycleStageWasRemoved, new Dictionary <string, object>()))) .Verify(async context => { var publicService = await context.PublicServiceLifeCycleList.FirstOrDefaultAsync(a => a.PublicServiceId == stageWasAddedToLifeCycle.PublicServiceId && a.LifeCycleStageId == stageWasAddedToLifeCycle.LifeCycleStageId); publicService.Should().BeNull(); return(VerificationResult.Pass()); }) .Assert(); }
public async Task Given_MunicipalityWasRegistered_Then_expected_item_is_added() { var projection = new MunicipalityListProjections(); var resolver = ConcurrentResolve.WhenEqualToHandlerMessageType(projection.Handlers); var municipalityId = Guid.NewGuid(); var nisCode = "123"; var municipalityWasRegistered = new MunicipalityWasRegistered(new MunicipalityId(municipalityId), new NisCode(nisCode)); ((ISetProvenance)municipalityWasRegistered).SetProvenance(_provenance); await new ConnectedProjectionScenario <LegacyContext>(resolver) .Given( new Envelope <MunicipalityWasRegistered>(new Envelope( municipalityWasRegistered, EmptyMetadata))) .Verify(async context => { var municipality = await context.MunicipalityList.FirstAsync(a => a.MunicipalityId == municipalityId); municipality.MunicipalityId .Should() .Be(municipalityId); municipality.NisCode .Should() .Be(nisCode); return(VerificationResult.Pass()); }) .Assert(); }
public static async Task ExpectNone(this ConnectedProjectionScenario <ProductContext> scenario) { var database = Guid.NewGuid().ToString("N"); var specification = scenario.Verify(async context => { var actualRecords = await context.AllRecords(); return(actualRecords.Length == 0 ? VerificationResult.Pass() : VerificationResult.Fail($"Expected 0 records but found {actualRecords.Length}.")); }); using (var context = CreateContextFor(database)) { var projector = new ConnectedProjector <ProductContext>(specification.Resolver); foreach (var message in specification.Messages) { var envelope = new Envelope(message, new Dictionary <string, object>()).ToGenericEnvelope(); await projector.ProjectAsync(context, envelope); } await context.SaveChangesAsync(); } using (var context = CreateContextFor(database)) { var result = await specification.Verification(context, CancellationToken.None); if (result.Failed) { throw specification.CreateFailedScenarioExceptionFor(result); } } }
public void PassReturnsExpectedResult(string message) { var result = VerificationResult.Pass(message); Assert.That(result.Passed, Is.True); Assert.That(result.Failed, Is.False); Assert.That(result.Message, Is.EqualTo(message)); }
public void ResolverReturnsExpectedResult() { var resolver = Resolve.WhenEqualToHandlerMessageType(new ConnectedProjectionHandler <object> [0]); var sut = new ConnectedProjectionTestSpecification <object>( resolver, new object[0], (session, token) => Task.FromResult(VerificationResult.Pass())); var result = sut.Resolver; Assert.That(result, Is.SameAs(resolver)); }
public void MessagesReturnsExpectedResult() { var messages = new [] { new object(), new object() }; var sut = new ConnectedProjectionTestSpecification <object>( Resolve.WhenEqualToHandlerMessageType(new ConnectedProjectionHandler <object> [0]), messages, (session, token) => Task.FromResult(VerificationResult.Pass())); var result = sut.Messages; Assert.That(result, Is.SameAs(messages)); }
public static Task Expect(this ProjectionScenario <MemoryCache> scenario, params CacheItem[] items) { if (items == null) { throw new ArgumentNullException("items"); } if (items.Length == 0) { return(scenario.ExpectNone()); } return(scenario. Verify(cache => { if (cache.GetCount() != items.Length) { if (cache.GetCount() == 0) { return Task.FromResult( VerificationResult.Fail( string.Format("Expected {0} cache item(s), but found 0 cache items.", items.Length))); } return Task.FromResult( VerificationResult.Fail( string.Format("Expected {0} cache item(s), but found {1} cache item(s) ({2}).", items.Length, cache.GetCount(), string.Join(",", cache.Select(pair => pair.Key))))); } if (!cache.Select(pair => cache.GetCacheItem(pair.Key)).SequenceEqual(items, new CacheItemEqualityComparer())) { var builder = new StringBuilder(); builder.AppendLine("Expected the following cache items:"); foreach (var expectedItem in items) { builder.AppendLine(expectedItem.Key + ": " + JToken.FromObject(expectedItem.Value).ToString()); } builder.AppendLine(); builder.AppendLine("But found the following cache items:"); foreach (var actualItem in cache) { builder.AppendLine(actualItem.Key + ": " + JToken.FromObject(actualItem.Value).ToString()); } return Task.FromResult(VerificationResult.Fail(builder.ToString())); } return Task.FromResult(VerificationResult.Pass()); }). Assert()); }
public async Task Given_no_messages_Then_list_is_empty() { var projection = new MunicipalityListProjections(); var resolver = ConcurrentResolve.WhenEqualToHandlerMessageType(projection.Handlers); await new ConnectedProjectionScenario <LegacyContext>(resolver) .Given() .Verify(async context => await context.MunicipalityList.AnyAsync() ? VerificationResult.Fail() : VerificationResult.Pass()) .Assert(); }
public void VerificationReturnsExpectedResult() { Func <object, CancellationToken, Task <VerificationResult> > verification = (session, token) => Task.FromResult(VerificationResult.Pass()); var sut = new ConnectedProjectionTestSpecification <object>( Resolve.WhenEqualToHandlerMessageType(new ConnectedProjectionHandler <object> [0]), new object[0], verification); var result = sut.Verification; Assert.That(result, Is.SameAs(verification)); }
public static async Task Expect( this ConnectedProjectionScenario <ProductContext> scenario, params object[] records) { var database = Guid.NewGuid().ToString("N"); var specification = scenario.Verify(async context => { var comparisonConfig = new ComparisonConfig { MaxDifferences = 5 }; var comparer = new CompareLogic(comparisonConfig); var actualRecords = await context.AllRecords(); var result = comparer.Compare( actualRecords, records ); return(result.AreEqual ? VerificationResult.Pass() : VerificationResult.Fail(result.CreateDifferenceMessage(actualRecords, records))); }); using (var context = CreateContextFor(database)) { var projector = new ConnectedProjector <ProductContext>(specification.Resolver); var position = 0L; foreach (var message in specification.Messages) { var envelope = new Envelope(message, new Dictionary <string, object> { { "Position", position } }).ToGenericEnvelope(); await projector.ProjectAsync(context, envelope); position++; } await context.SaveChangesAsync(); } using (var context = CreateContextFor(database)) { var result = await specification.Verification(context, CancellationToken.None); if (result.Failed) { throw specification.CreateFailedScenarioExceptionFor(result); } } }
public void VerifyWithoutTokenReturnsExpectedResult() { var sut = SutFactory.Create <object>(); var session = new object(); var result = sut. Verify(connection => { Assert.That(connection, Is.EqualTo(session)); return(Task.FromResult(VerificationResult.Pass())); }). Verification(session, CancellationToken.None). Result; Assert.That(result, Is.EqualTo(VerificationResult.Pass())); }
private ConnectedProjectionTestSpecification <TContext> CreateTest(Func <TContext, Task> assertions) { return(_inner.Verify(async context => { try { await assertions(context); return VerificationResult.Pass(); } catch (Exception e) { return VerificationResult.Fail(e.Message); } })); }
public async Task Given_MunicipalityWasNamed_twice_and_municipality_was_registered_Then_expected_item_is_updated() { var projection = new MunicipalityListProjections(); var resolver = ConcurrentResolve.WhenEqualToHandlerMessageType(projection.Handlers); var crabMunicipalityId = new CrabMunicipalityId(1); var municipalityId = MunicipalityId.CreateFor(crabMunicipalityId); var nisCode = "456"; var municipalityWasRegistered = new MunicipalityWasRegistered(municipalityId, new NisCode(nisCode)); ((ISetProvenance)municipalityWasRegistered).SetProvenance(_provenance); var municipalityWasNamed = new MunicipalityWasNamed(municipalityId, new MunicipalityName("test", Language.Dutch)); ((ISetProvenance)municipalityWasNamed).SetProvenance(_provenance); var wasNamed = new MunicipalityWasNamed(municipalityId, new MunicipalityName("test21", Language.French)); ((ISetProvenance)wasNamed).SetProvenance(_provenance); await new ConnectedProjectionScenario <LegacyContext>(resolver) .Given( new Envelope <MunicipalityWasRegistered>(new Envelope( municipalityWasRegistered, EmptyMetadata)), new Envelope <MunicipalityWasNamed>(new Envelope( municipalityWasNamed, EmptyMetadata)), new Envelope <MunicipalityWasNamed>(new Envelope( wasNamed, EmptyMetadata))) .Verify(async context => { var municipality = await context.MunicipalityList.FirstAsync(a => a.MunicipalityId == municipalityId); municipality.MunicipalityId.Should().Be((Guid)municipalityId); municipality.NameDutch.Should().Be("test"); municipality.NameFrench.Should().Be("test21"); return(VerificationResult.Pass()); }) .Assert(); }
public static Task ExpectNone(this ProjectionScenario <MemoryCache> scenario) { return(scenario. Verify(cache => { if (cache.GetCount() != 0) { return Task.FromResult( VerificationResult.Fail( string.Format("Expected no cache items, but found {0} cache item(s) ({1}).", cache.GetCount(), string.Join(",", cache.Select(pair => pair.Key))))); } return Task.FromResult(VerificationResult.Pass()); }). Assert()); }
public async Task StreetNameVersionWasIncreasedCreatesRecordWithCorrectProvenance() { var id = Arrange(Generate.StreetNameId); var streetNameWasRegistered = Arrange(Generate.StreetNameWasRegistered.Select(e => e.WithId(id))); var provenance = new Provenance(Instant.FromDateTimeOffset(DateTimeOffset.Now), Application.CrabWstEditService, Reason.DecentralManagmentCrab, new Operator("test"), Modification.Update, Organisation.Municipality); ((ISetProvenance)streetNameWasRegistered).SetProvenance(provenance); var projection = new StreetNameVersionProjections(); var resolver = ConcurrentResolve.WhenEqualToHandlerMessageType(projection.Handlers); var metadata = new Dictionary <string, object> { [Provenance.ProvenanceMetadataKey] = JsonConvert.SerializeObject(provenance.ToDictionary()), [Envelope.PositionMetadataKey] = 1L }; await new ConnectedProjectionScenario <LegacyContext>(resolver) .Given(new Envelope <StreetNameWasRegistered>(new Envelope(streetNameWasRegistered, metadata))) .Verify(async context => { var streetNameVersion = await context.StreetNameVersions .FirstAsync(a => a.StreetNameId == id); streetNameVersion.Should().NotBeNull(); streetNameVersion.Reason.Should().Be(provenance.Reason); streetNameVersion.Application.Should().Be(provenance.Application); streetNameVersion.VersionTimestamp.Should().Be(provenance.Timestamp); streetNameVersion.Operator.Should().Be(provenance.Operator.ToString()); streetNameVersion.Modification.Should().Be(provenance.Modification); streetNameVersion.Organisation.Should().Be(provenance.Organisation); return(VerificationResult.Pass()); }) .Assert(); }
public async Task Given_MunicipalityDefinedNisCode_Then_nisCode_should_be_changed() { var projection = new MunicipalityListProjections(); var resolver = ConcurrentResolve.WhenEqualToHandlerMessageType(projection.Handlers); var crabMunicipalityId = new CrabMunicipalityId(1); var municipalityId = MunicipalityId.CreateFor(crabMunicipalityId); var nisCode = "456"; var municipalityWasRegistered = new MunicipalityWasRegistered(municipalityId, new NisCode(nisCode)); ((ISetProvenance)municipalityWasRegistered).SetProvenance(_provenance); var municipalityNisCodeWasDefined = new MunicipalityNisCodeWasDefined(municipalityId, new NisCode(nisCode)); ((ISetProvenance)municipalityNisCodeWasDefined).SetProvenance(_provenance); await new ConnectedProjectionScenario <LegacyContext>(resolver) .Given( new Envelope <MunicipalityWasRegistered>(new Envelope( municipalityWasRegistered, EmptyMetadata)), new Envelope <MunicipalityNisCodeWasDefined>(new Envelope( municipalityNisCodeWasDefined, EmptyMetadata))) .Verify(async context => { var municipality = await context.MunicipalityList.FirstAsync(a => a.NisCode == nisCode); municipality.MunicipalityId.Should().Be((Guid)municipalityId); municipality.NisCode.Should().Be(nisCode); return(VerificationResult.Pass()); }) .Assert(); }
public async Task Given_MunicipalityDefinedPrimaryLanguage_Then_expected_item_is_updated() { var projection = new MunicipalityListProjections(); var resolver = ConcurrentResolve.WhenEqualToHandlerMessageType(projection.Handlers); var crabMunicipalityId = new CrabMunicipalityId(1); var municipalityId = MunicipalityId.CreateFor(crabMunicipalityId); var nisCode = "456"; var municipalityWasRegistered = new MunicipalityWasRegistered(municipalityId, new NisCode(nisCode)); ((ISetProvenance)municipalityWasRegistered).SetProvenance(_provenance); var municipalityOfficialLanguageWasAdded = new MunicipalityOfficialLanguageWasAdded(municipalityId, Language.Dutch); ((ISetProvenance)municipalityOfficialLanguageWasAdded).SetProvenance(_provenance); await new ConnectedProjectionScenario <LegacyContext>(resolver) .Given( new Envelope <MunicipalityWasRegistered>(new Envelope( municipalityWasRegistered, EmptyMetadata)), new Envelope <MunicipalityOfficialLanguageWasAdded>(new Envelope( municipalityOfficialLanguageWasAdded, EmptyMetadata))) .Verify(async context => { var municipality = await context.MunicipalityList.FirstAsync(a => a.MunicipalityId == municipalityId); municipality.MunicipalityId.Should().Be((Guid)municipalityId); municipality.OfficialLanguages.Should().AllBeEquivalentTo(Language.Dutch); return(VerificationResult.Pass()); }) .Assert(); }
public static Task Expect(this ProjectionScenario <IAsyncDocumentSession> scenario, params object[] documents) { if (documents == null) { throw new ArgumentNullException("documents"); } if (documents.Length == 0) { return(scenario.ExpectNone()); } return(scenario .Verify(async session => { using (var streamer = await session.Advanced.StreamAsync <object>(Etag.Empty)) { var storedDocuments = new List <object>(); var storedDocumentIdentifiers = new List <string>(); while (await streamer.MoveNextAsync()) { storedDocumentIdentifiers.Add(streamer.Current.Key); storedDocuments.Add(streamer.Current.Document); } if (documents.Length != storedDocumentIdentifiers.Count) { if (storedDocumentIdentifiers.Count == 0) { return VerificationResult.Fail( string.Format("Expected {0} document(s), but found 0 documents.", documents.Length)); } return VerificationResult.Fail( string.Format("Expected {0} document(s), but found {1} document(s) ({2}).", documents.Length, storedDocumentIdentifiers.Count, string.Join(",", storedDocumentIdentifiers))); } var expectedDocuments = documents.Select(JToken.FromObject).ToArray(); var actualDocuments = storedDocuments.Select(JToken.FromObject).ToArray(); if (!expectedDocuments.SequenceEqual(actualDocuments, new JTokenEqualityComparer())) { var builder = new StringBuilder(); builder.AppendLine("Expected the following documents:"); foreach (var expectedDocument in expectedDocuments) { builder.AppendLine(expectedDocument.ToString()); } builder.AppendLine(); builder.AppendLine("But found the following documents:"); foreach (var actualDocument in actualDocuments) { builder.AppendLine(actualDocument.ToString()); } return VerificationResult.Fail(builder.ToString()); } return VerificationResult.Pass(); } }) .Assert()); }
public void PassMessageCanNotBeNull() { Assert.Throws <ArgumentNullException>(() => VerificationResult.Pass(null)); }
private static VerificationResult SutFactory() { return(VerificationResult.Pass()); }
private static VerificationResult SutFactory(VerificationResultState state, string message) { return(state == VerificationResultState.Passed ? VerificationResult.Pass(message) : VerificationResult.Fail(message)); }
public void ResolverCanNotBeNull() { Assert.Throws <ArgumentNullException>(() => new ConnectedProjectionTestSpecification <object>( null, new object[0], (session, token) => Task.FromResult(VerificationResult.Pass())) ); }
public void MessagesCanNotBeNull() { Assert.Throws <ArgumentNullException>(() => new ConnectedProjectionTestSpecification <object>( Resolve.WhenEqualToHandlerMessageType(new ConnectedProjectionHandler <object> [0]), null, (session, token) => Task.FromResult(VerificationResult.Pass())) ); }