public async Task Create_should_create_events() { var command = new CreateComment { Text = "text1" }; var result = await sut.ExecuteAsync(CreateCommentsCommand(command)); result.ShouldBeEquivalent(EntityCreatedResult.Create(command.CommentId, 0)); sut.GetCommentsAsync(0).Result.Should().BeEquivalentTo(new CommentsResult { Version = 0 }); sut.GetCommentsAsync(-1).Result.Should().BeEquivalentTo(new CommentsResult { CreatedComments = new List <Comment> { new Comment(command.CommentId, LastEvents.ElementAt(0).Headers.Timestamp(), command.Actor, "text1") }, Version = 0 }); LastEvents .ShouldHaveSameEvents( CreateCommentsEvent(new CommentCreated { CommentId = command.CommentId, Text = command.Text }) ); }
public async Task Create_should_create_events() { var command = new CreateComment { Text = "text1", Url = new Uri("http://uri") }; var result = await sut.ExecuteAsync(CreateCommentsCommand(command)); result.ShouldBeEquivalent((object)EntityCreatedResult.Create(command.CommentId, 0)); sut.GetCommentsAsync(0).Result.Should().BeEquivalentTo(new CommentsResult { Version = 0 }); sut.GetCommentsAsync(-1).Result.Should().BeEquivalentTo(new CommentsResult { CreatedComments = new List <Comment> { new Comment(command.CommentId, GetTime(), command.Actor, "text1", command.Url) }, Version = 0 }); LastEvents .ShouldHaveSameEvents( CreateCommentsEvent(new CommentCreated { Text = command.Text, Url = command.Url }) ); }
public async Task Create_should_create_schema_and_create_events() { var properties = new SchemaProperties(); var command = new CreateSchema { Name = SchemaName, SchemaId = SchemaId, Properties = properties, IsSingleton = true }; var result = await sut.ExecuteAsync(CreateCommand(command)); result.ShouldBeEquivalent(EntityCreatedResult.Create(Id, 0)); Assert.Equal(AppId, sut.Snapshot.AppId.Id); Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name); Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name); Assert.True(sut.Snapshot.SchemaDef.IsSingleton); LastEvents .ShouldHaveSameEvents( CreateEvent(new SchemaCreated { Schema = new Schema(command.Name, command.Properties, command.IsSingleton) }) ); }
public async Task Create_should_also_publish() { var command = new CreateContent { Data = data, Publish = true }; var result = await sut.ExecuteAsync(CreateContentCommand(command)); result.ShouldBeEquivalent(EntityCreatedResult.Create(data, 1)); LastEvents .ShouldHaveSameEvents( CreateContentEvent(new ContentCreated { Data = data }), CreateContentEvent(new ContentStatusChanged { Status = Status.Published, Change = StatusChange.Published }) ); A.CallTo(() => scriptEngine.ExecuteAndTransform(A <ScriptContext> .Ignored, "<create-script>")) .MustHaveHappened(); A.CallTo(() => scriptEngine.Execute(A <ScriptContext> .Ignored, "<change-script>")) .MustHaveHappened(); }
protected override Task <object> ExecuteAsync(IAggregateCommand command) { switch (command) { case CreateComment createComment: return(UpsertAsync(createComment, c => { GuardComments.CanCreate(c); Create(c); return EntityCreatedResult.Create(createComment.CommentId, Version); })); case UpdateComment updateComment: return(UpsertAsync(updateComment, c => { GuardComments.CanUpdate(events, c); Update(c); })); case DeleteComment deleteComment: return(UpsertAsync(deleteComment, c => { GuardComments.CanDelete(events, c); Delete(c); })); default: throw new NotSupportedException(); } }
public async Task Create_should_create_events_and_update_state() { var command = new CreateApp { Name = AppName, Actor = User, AppId = AppId }; var result = await sut.ExecuteAsync(CreateCommand(command)); result.ShouldBeEquivalent(EntityCreatedResult.Create(Id, 4)); Assert.Equal(AppName, sut.Snapshot.Name); LastEvents .ShouldHaveSameEvents( CreateEvent(new AppCreated { Name = AppName }), CreateEvent(new AppContributorAssigned { ContributorId = User.Identifier, Permission = AppContributorPermission.Owner }), CreateEvent(new AppLanguageAdded { Language = Language.EN }), CreateEvent(new AppPatternAdded { PatternId = patternId1, Name = "Number", Pattern = "[0-9]" }), CreateEvent(new AppPatternAdded { PatternId = patternId2, Name = "Numbers", Pattern = "[0-9]*" }) ); }
public async Task Create_should_create_schema_with_initial_fields() { var properties = new SchemaProperties(); var fields = new List <CreateSchemaField> { new CreateSchemaField { Name = "field1", Properties = ValidProperties() }, new CreateSchemaField { Name = "field2", Properties = ValidProperties() } }; var command = new CreateSchema { Name = SchemaName, SchemaId = SchemaId, Properties = properties, Fields = fields }; var result = await sut.ExecuteAsync(CreateCommand(command)); result.ShouldBeEquivalent(EntityCreatedResult.Create(Id, 0)); var @event = (SchemaCreated)LastEvents.Single().Payload; Assert.Equal(AppId, sut.Snapshot.AppId.Id); Assert.Equal(SchemaName, sut.Snapshot.Name); Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name); Assert.Equal(2, @event.Fields.Count); }
protected Task On(AddField command, CommandContext context) { return(handler.UpdateAsync <SchemaDomainObject>(context, s => { s.AddField(command); context.Complete(EntityCreatedResult.Create(s.Schema.FieldsById.Values.First(x => x.Name == command.Name).Id, s.Version)); })); }
protected Task On(AttachClient command, CommandContext context) { return(handler.UpdateAsync <AppDomainObject>(context, a => { a.AttachClient(command, keyGenerator.GenerateKey()); context.Succeed(EntityCreatedResult.Create(a.Clients[command.Id], a.Version)); })); }
protected Task On(AddField command, CommandContext context) { return(handler.UpdateSyncedAsync <SchemaDomainObject>(context, s => { GuardSchemaField.CanAdd(s.Snapshot.SchemaDef, command); s.Add(command); context.Complete(EntityCreatedResult.Create(s.Snapshot.SchemaDef.FieldsById.Values.First(x => x.Name == command.Name).Id, s.Version)); })); }
protected Task On(CreateSchema command, CommandContext context) { return(handler.CreateSyncedAsync <SchemaDomainObject>(context, async s => { await GuardSchema.CanCreate(command, appProvider); s.Create(command); context.Complete(EntityCreatedResult.Create(command.SchemaId, s.Version)); })); }
protected async Task On(CreateApp command, CommandContext context) { var app = await handler.CreateSyncedAsync <AppDomainObject>(context, async a => { await GuardApp.CanCreate(command, appProvider); a.Create(command); context.Complete(EntityCreatedResult.Create(command.AppId, a.Version)); }); }
protected async Task On(CreateContent command, CommandContext context) { await ValidateAsync(command, () => "Failed to create content", true); await handler.CreateAsync <ContentDomainObject>(context, c => { c.Create(command); context.Succeed(EntityCreatedResult.Create(command.Data, c.Version)); }); }
public async override Task HandleAsync(CommandContext context, Func <Task> next) { switch (context.Command) { case CreateAsset createAsset: { createAsset.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(createAsset.File.OpenRead()); await assetStore.UploadTemporaryAsync(context.ContextId.ToString(), createAsset.File.OpenRead()); try { var result = await ExecuteCommandAsync(createAsset) as AssetSavedResult; context.Complete(EntityCreatedResult.Create(createAsset.AssetId, result.Version)); await assetStore.CopyTemporaryAsync(context.ContextId.ToString(), createAsset.AssetId.ToString(), result.FileVersion, null); } finally { await assetStore.DeleteTemporaryAsync(context.ContextId.ToString()); } break; } case UpdateAsset updateAsset: { updateAsset.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(updateAsset.File.OpenRead()); await assetStore.UploadTemporaryAsync(context.ContextId.ToString(), updateAsset.File.OpenRead()); try { var result = await ExecuteCommandAsync(updateAsset) as AssetSavedResult; context.Complete(result); await assetStore.CopyTemporaryAsync(context.ContextId.ToString(), updateAsset.AssetId.ToString(), result.FileVersion, null); } finally { await assetStore.DeleteTemporaryAsync(context.ContextId.ToString()); } break; } default: await base.HandleAsync(context, next); break; } }
public static AppCreatedDto FromResult(EntityCreatedResult <Guid> result, IAppPlansProvider apps) { var response = new AppCreatedDto { Id = result.IdOrValue.ToString(), Permission = AppContributorPermission.Owner, PlanName = apps.GetPlan(null)?.Name, PlanUpgrade = apps.GetPlanUpgrade(null)?.Name, Version = result.Version }; return(response); }
public static AppCreatedDto FromResult(string name, EntityCreatedResult <Guid> result, IAppPlansProvider apps) { var response = new AppCreatedDto { Id = result.IdOrValue.ToString(), Permissions = Role.CreateOwner(name).Permissions.ToIds().ToArray(), PlanName = apps.GetPlan(null)?.Name, PlanUpgrade = apps.GetPlanUpgrade(null)?.Name, Version = result.Version }; return(response); }
public async Task Should_add_app_to_index_on_assign_of_contributor() { var context = new CommandContext(new AssignContributor { AppId = appId }, commandBus) .Complete(EntityCreatedResult.Create(userId, 1)); await sut.HandleAsync(context); A.CallTo(() => index.AddAppAsync(appId)) .MustHaveHappened(); }
protected async Task On(CreateAsset command, CommandContext context) { await handler.CreateAsync <AssetDomainObject>(context, async c => { command.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(command.File.OpenRead()); c.Create(command); await assetStore.UploadAsync(c.Id.ToString(), c.FileVersion, null, command.File.OpenRead()); context.Succeed(EntityCreatedResult.Create(c.Id, c.Version)); }); }
public static AssetCreatedDto Create(CreateAsset command, EntityCreatedResult <Guid> result) { var response = new AssetCreatedDto { Id = command.AssetId, FileName = command.File.FileName, FileSize = command.File.FileSize, FileVersion = result.Version, MimeType = command.File.MimeType, IsImage = command.ImageInfo != null, PixelWidth = command.ImageInfo?.PixelWidth, PixelHeight = command.ImageInfo?.PixelHeight, Version = result.Version }; return(response); }
public static ContentDto Create(CreateContent command, EntityCreatedResult <NamedContentData> result) { var now = SystemClock.Instance.GetCurrentInstant(); var response = new ContentDto { Id = command.ContentId, Data = result.IdOrValue, Version = result.Version, Created = now, CreatedBy = command.Actor, LastModified = now, LastModifiedBy = command.Actor }; return(response); }
protected async Task On(CreateApp command, CommandContext context) { if (await appRepository.FindAppAsync(command.Name) != null) { var error = new ValidationError($"An app with name '{command.Name}' already exists", nameof(CreateApp.Name)); throw new ValidationException("Cannot create a new app", error); } await handler.CreateAsync <AppDomainObject>(context, a => { a.Create(command); context.Succeed(EntityCreatedResult.Create(a.Id, a.Version)); }); }
protected async Task On(CreateSchema command, CommandContext context) { if (await schemas.FindSchemaByNameAsync(command.AppId.Id, command.Name) != null) { var error = new ValidationError($"A schema with name '{command.Name}' already exists", "Name", nameof(CreateSchema.Name)); throw new ValidationException("Cannot create a new schema", error); } await handler.CreateAsync <SchemaDomainObject>(context, s => { s.Create(command); context.Complete(EntityCreatedResult.Create(s.Id, s.Version)); }); }
public async Task Create_ReturnsCorrectThreadId_WhenThreadWasCreatedSuccessfully() { // Arrange _uowMock.Setup(x => x.BeginTransaction()).Returns(new Mock <ITransaction>().Object); CreateThreadModel createModel = new CreateThreadModel { InitialPost = new CreatePostModel { Content = "contzntz" } }; _threadsRepositoryMock.Setup(x => x.Add(It.IsAny <ForumThread>())) .Callback <ForumThread>(thread => thread.Id = 12); // Act EntityCreatedResult result = await _threadsService.Create(createModel); Assert.That(result.Id, Is.EqualTo(12)); }
public async Task Create_should_create_events_and_update_state() { var command = MakeCreateCommand(); var result = await sut.ExecuteAsync(CreateRuleCommand(command)); result.ShouldBeEquivalent(EntityCreatedResult.Create(Id, 0)); Assert.Equal(AppId, sut.Snapshot.AppId.Id); Assert.Same(command.Trigger, sut.Snapshot.RuleDef.Trigger); Assert.Same(command.Action, sut.Snapshot.RuleDef.Action); LastEvents .ShouldHaveSameEvents( CreateRuleEvent(new RuleCreated { Trigger = command.Trigger, Action = command.Action }) ); }
public async Task AssignContributor_should_create_events_and_update_state() { var command = new AssignContributor { ContributorId = contributorId, Permission = AppContributorPermission.Editor }; await ExecuteCreateAsync(); var result = await sut.ExecuteAsync(CreateCommand(command)); result.ShouldBeEquivalent(EntityCreatedResult.Create(contributorId, 5)); Assert.Equal(AppContributorPermission.Editor, sut.Snapshot.Contributors[contributorId]); LastEvents .ShouldHaveSameEvents( CreateEvent(new AppContributorAssigned { ContributorId = contributorId, Permission = AppContributorPermission.Editor }) ); }
public async Task Add_should_create_events_and_update_state() { var command = new AddField { Name = fieldName, Properties = ValidProperties() }; await ExecuteCreateAsync(); var result = await sut.ExecuteAsync(CreateCommand(command)); result.ShouldBeEquivalent(EntityCreatedResult.Create(1, 1)); Assert.Equal(command.Properties, sut.Snapshot.SchemaDef.FieldsById[1].RawProperties); LastEvents .ShouldHaveSameEvents( CreateEvent(new FieldAdded { Name = fieldName, FieldId = fieldId, Properties = command.Properties }) ); }
public async Task Should_invite_user_and_not_change_result_if_not_added() { var command = new AssignContributor { ContributorId = "*****@*****.**", IsInviting = true }; var context = new CommandContext(command, commandBus); A.CallTo(() => userResolver.CreateUserIfNotExists("*****@*****.**")) .Returns(false); var result = EntityCreatedResult.Create("13", 13L); context.Complete(result); await sut.HandleAsync(context); Assert.Same(context.Result <EntityCreatedResult <string> >(), result); A.CallTo(() => userResolver.CreateUserIfNotExists("*****@*****.**")) .MustHaveHappened(); }
protected async Task On(CreateContent command, CommandContext context) { await handler.CreateAsync <ContentDomainObject>(context, async content => { var schemaAndApp = await ResolveSchemaAndAppAsync(command); ExecuteScriptAndTransform(command, content, schemaAndApp.SchemaEntity.ScriptCreate, "Create"); if (command.Publish) { ExecuteScript(command, content, schemaAndApp.SchemaEntity.ScriptChange, "Published"); } command.Data.Enrich(schemaAndApp.SchemaEntity.SchemaDef, schemaAndApp.AppEntity.PartitionResolver); await ValidateAsync(schemaAndApp, command, () => "Failed to create content", false); content.Create(command); context.Complete(EntityCreatedResult.Create(command.Data, content.Version)); }); }
public async Task Create_should_create_events_and_update_state() { var command = new CreateContent { Data = data }; var result = await sut.ExecuteAsync(CreateContentCommand(command)); result.ShouldBeEquivalent(EntityCreatedResult.Create(data, 0)); LastEvents .ShouldHaveSameEvents( CreateContentEvent(new ContentCreated { Data = data }) ); A.CallTo(() => scriptEngine.ExecuteAndTransform(A <ScriptContext> .Ignored, "<create-script>")) .MustHaveHappened(); A.CallTo(() => scriptEngine.Execute(A <ScriptContext> .Ignored, "<change-script>")) .MustNotHaveHappened(); }
protected async Task On(CreateContent command, CommandContext context) { await handler.CreateAsync <ContentDomainObject>(context, async content => { GuardContent.CanCreate(command); var operationContext = await CreateContext(command, content, () => "Failed to create content."); if (command.Publish) { await operationContext.ExecuteScriptAsync(x => x.ScriptChange, "Published"); } await operationContext.ExecuteScriptAndTransformAsync(x => x.ScriptCreate, "Create"); await operationContext.EnrichAsync(); await operationContext.ValidateAsync(false); content.Create(command); context.Complete(EntityCreatedResult.Create(command.Data, content.Version)); }); }