public void Should_validate_command_and_save_new_textModule() { var command = new AddVersion { SiteId = Guid.NewGuid(), ModuleId = Guid.NewGuid(), Id = Guid.NewGuid(), Content = "Content" }; var textModuleRepositoryMock = new Mock <ITextModuleRepository>(); textModuleRepositoryMock.Setup(x => x.Create(It.IsAny <TextModule>())); var validatorMock = new Mock <IValidator <AddVersion> >(); validatorMock.Setup(x => x.Validate(command)).Returns(new ValidationResult()); var addVersionHandler = new AddVersionHandler(textModuleRepositoryMock.Object, validatorMock.Object); addVersionHandler.Handle(command); validatorMock.Verify(x => x.Validate(command)); textModuleRepositoryMock.Verify(x => x.Create(It.IsAny <TextModule>())); }
public IActionResult Save(AddVersion model) { model.SiteId = SiteId; model.VersionId = Guid.NewGuid(); _commandSender.Send <AddVersion, TextModule>(model); return(Ok(string.Empty)); }
public void Should_have_error_when_description_is_too_long() { var description = string.Empty; for (int i = 0; i < 251; i++) { description += i.ToString(); } var command = new AddVersion { SiteId = Guid.NewGuid(), ModuleId = Guid.NewGuid(), Id = Guid.NewGuid(), Content = "", Description = description }; var textModuleRulesMock = new Mock <ITextModuleRules>(); var moduleRulesMock = new Mock <IModuleRules>(); var siteRulesMock = new Mock <ISiteRules>(); var languageRulesMock = new Mock <ILanguageRules>(); var localisationValidator = new Mock <IValidator <AddVersion.VersionLocalisation> >(); var validator = new AddVersionValidator(textModuleRulesMock.Object, moduleRulesMock.Object, siteRulesMock.Object, languageRulesMock.Object, localisationValidator.Object); validator.ShouldHaveValidationErrorFor(x => x.Description, command); }
public void Should_validate_command_and_save_new_textModule() { var textModule = TextModuleFactory.Get(); var command = new AddVersion { SiteId = Guid.NewGuid(), ModuleId = textModule.ModuleId, Id = Guid.NewGuid(), Content = "Content", Status = TextVersionStatus.Draft }; var textModuleRepositoryMock = new Mock <ITextModuleRepository>(); textModuleRepositoryMock.Setup(x => x.GetByModuleId(command.ModuleId)).Returns(textModule); textModuleRepositoryMock.Setup(x => x.Update(It.IsAny <TextModule>())); var validatorMock = new Mock <IValidator <AddVersion> >(); validatorMock.Setup(x => x.Validate(command)).Returns(new ValidationResult()); var addVersionHandler = new AddVersionHandler(textModuleRepositoryMock.Object, validatorMock.Object); addVersionHandler.Handle(command); validatorMock.Verify(x => x.Validate(command)); textModuleRepositoryMock.Verify(x => x.Update(It.IsAny <TextModule>())); }
public IActionResult Put([FromBody] AddVersion model) { model.SiteId = SiteId; model.VersionId = Guid.NewGuid(); _dispatcher.SendAndPublish <AddVersion, TextModule>(model); return(Ok(string.Empty)); }
public void Setup() { _textModule = TextModuleFactory.Get(); _command = new AddVersion { SiteId = Guid.NewGuid(), ModuleId = _textModule.ModuleId, Id = _textModule.Id, VersionId = Guid.NewGuid(), Content = "Content", Description = "Description", Status = TextVersionStatus.Published, VersionLocalisations = new List <AddVersion.VersionLocalisation>() { new AddVersion.VersionLocalisation { LanguageId = Guid.NewGuid(), Content = "Localised content" } } }; _validatorMock = new Mock <IValidator <AddVersion> >(); _validatorMock.Setup(x => x.Validate(_command)).Returns(new ValidationResult()); _textModule.AddVersion(_command, _validatorMock.Object); _newVersion = _textModule.TextVersions.FirstOrDefault(x => x.Id == _command.VersionId); _event = _textModule.Events.OfType <VersionAdded>().SingleOrDefault(); }
public void AddVersion(AddVersion cmd, IValidator <AddVersion> validator) { if (cmd.Status != TextVersionStatus.Published && cmd.Status != TextVersionStatus.Draft) { throw new Exception("Status of a new version should be either Published or Draft."); } validator.ValidateCommand(cmd); var newVersion = new TextVersion(cmd.VersionId, Id, cmd.Content, cmd.Description, cmd.Status, GetVersionLocalisations(cmd)); TextVersions.Add(newVersion); if (newVersion.Status == TextVersionStatus.Published) { var currentPublishedVersion = TextVersions.FirstOrDefault(x => x.Status == TextVersionStatus.Published); currentPublishedVersion?.UnPublish(); } AddEvent(new VersionAdded { SiteId = cmd.SiteId, ModuleId = cmd.ModuleId, AggregateRootId = Id, VersionId = cmd.VersionId, Content = cmd.Content, Description = cmd.Description, Status = cmd.Status }); }
public void Should_have_error_when_module_does_not_exist() { var command = new AddVersion { SiteId = Guid.NewGuid(), ModuleId = Guid.NewGuid(), Id = Guid.NewGuid(), Content = "Content" }; var textModuleRulesMock = new Mock <ITextModuleRules>(); var moduleRulesMock = new Mock <IModuleRules>(); moduleRulesMock.Setup(x => x.DoesModuleExist(command.SiteId, command.ModuleId)).Returns(false); var siteRulesMock = new Mock <ISiteRules>(); var languageRulesMock = new Mock <ILanguageRules>(); var localisationValidator = new Mock <IValidator <AddVersion.VersionLocalisation> >(); var validator = new AddVersionValidator(textModuleRulesMock.Object, moduleRulesMock.Object, siteRulesMock.Object, languageRulesMock.Object, localisationValidator.Object); validator.ShouldHaveValidationErrorFor(x => x.ModuleId, command); }
public async Task <IActionResult> Put([FromBody] AddVersion model) { model.SiteId = SiteId; model.VersionId = Guid.NewGuid(); await Task.Run(() => _commandSender.Send <AddVersion, TextModule>(model)); return(Ok(string.Empty)); }
public AddVersion GetAdminModel(Guid siteId, Guid moduleId, Guid versionId = new Guid()) { var module = _moduleRepository.GetById(siteId, moduleId); if (module == null || module.Status == ModuleStatus.Deleted) { return(new AddVersion()); } var textModule = _textRepository.GetByModuleId(moduleId); if (textModule == null || textModule.Status == TextModuleStatus.Deleted) { return(new AddVersion()); } var version = versionId != Guid.Empty ? textModule.TextVersions.FirstOrDefault(x => x.Id == versionId) : textModule.TextVersions.FirstOrDefault(x => x.Status == TextVersionStatus.Published); if (version == null || version.Status == TextVersionStatus.Deleted) { return(new AddVersion()); } var result = new AddVersion { Id = textModule.Id, ModuleId = textModule.ModuleId, Content = version.Content, Description = version.Description }; var languages = _languageRepository.GetAll(siteId).Where(x => x.Status != LanguageStatus.Deleted); foreach (var language in languages) { var content = string.Empty; var existingLocalisation = version .TextLocalisations .FirstOrDefault(x => x.LanguageId == language.Id); if (existingLocalisation != null) { content = existingLocalisation.Content; } result.VersionLocalisations.Add(new AddVersion.VersionLocalisation { LanguageId = language.Id, LanguageName = language.Name, Content = content }); } return(result); }
public void ShouldAddFirstVersion() { // arrange var processor = new AddVersion(); var args = new AddDbItemArgs(new DbItem("home"), new DataStorage()); // act processor.Process(args); // assert args.DbItem.VersionsCount["en"].Should().Be(1); }
public void ShouldAddFirstVersion() { // arrange var processor = new AddVersion(); var args = new AddDbItemArgs(new DbItem("home"), new DataStorage(Database.GetDatabase("master"))); // act processor.Process(args); // assert args.DbItem.GetVersionCount("en").Should().Be(1); }
public void ShouldAddVersionInTheGivenLanguage() { // arrange var processor = new AddVersion(); var args = new AddDbItemArgs(new DbItem("home"), new DataStorage(Database.GetDatabase("master")), Language.Parse("fr-FR")); // act processor.Process(args); // assert args.DbItem.GetVersionCount("fr-FR").Should().Be(1); }
public void Should_throw_an_exception_if_status_is_not_published_or_draft(TextVersionStatus status) { var textModule = TextModuleFactory.Get(); var command = new AddVersion { Status = status, }; var validatorMock = new Mock <IValidator <AddVersion> >(); validatorMock.Setup(x => x.Validate(command)).Returns(new ValidationResult()); Assert.Throws <Exception>(() => textModule.AddVersion(command, validatorMock.Object)); }
private IList <TextLocalisation> GetVersionLocalisations(AddVersion cmd) { var localisations = new List <TextLocalisation>(); foreach (var localisation in cmd.VersionLocalisations) { localisations.Add(new TextLocalisation { TextVersionId = cmd.VersionId, LanguageId = localisation.LanguageId, Content = localisation.Content }); } return(localisations); }
public static TextModule Get() { var siteId = Guid.NewGuid(); var createCommand = new CreateTextModule { SiteId = siteId, ModuleId = Guid.NewGuid(), Id = Guid.NewGuid(), Content = "Content" }; var createValidatorMock = new Mock <IValidator <CreateTextModule> >(); createValidatorMock.Setup(x => x.Validate(createCommand)).Returns(new ValidationResult()); var textModule = TextModule.CreateNew(createCommand, createValidatorMock.Object); var addVersionCommand = new AddVersion { SiteId = siteId, ModuleId = textModule.ModuleId, Id = textModule.Id, VersionId = Guid.NewGuid(), Content = "Content", Description = "Description", Status = TextVersionStatus.Published, VersionLocalisations = new List <AddVersion.VersionLocalisation>() { new AddVersion.VersionLocalisation { LanguageId = Guid.NewGuid(), Content = "Localised content" } } }; _addVersionValidatorMock = new Mock <IValidator <AddVersion> >(); _addVersionValidatorMock.Setup(x => x.Validate(addVersionCommand)).Returns(new ValidationResult()); textModule.AddVersion(addVersionCommand, _addVersionValidatorMock.Object); textModule.Events.Clear(); return(textModule); }
public static Model.VersionBo AddVersion(Model.VersionBo version) { var query = new AddVersion(); try { if (query.Execute(version.Release, version.Fecha, version.Estado, version.Comentario, version.Usuario, version.IsVersionInicial, version.HasDeploy31) > 0) { return(GetVersiones(null).Last()); } return(null); } catch (Exception ex) { var msg = "Excepcion Controlada: " + ex.Message; throw new Exception(msg, ex); } }
public void Should_not_change_status_of_current_published_version_if_new_version_status_is_draft() { var textModule = TextModuleFactory.Get(); var currentPublishedVersion = textModule.TextVersions.Single(x => x.Status == TextVersionStatus.Published); var command = new AddVersion { Status = TextVersionStatus.Draft, }; var validatorMock = new Mock <IValidator <AddVersion> >(); validatorMock.Setup(x => x.Validate(command)).Returns(new ValidationResult()); textModule.AddVersion(command, validatorMock.Object); Assert.AreEqual(TextVersionStatus.Published, currentPublishedVersion.Status); }
public void AddVersion(AddVersion cmd, IValidator <AddVersion> validator) { if (cmd.Status != TextVersionStatus.Published && cmd.Status != TextVersionStatus.Draft) { throw new Exception("Status of a new version should be either Published or Draft."); } validator.ValidateCommand(cmd); AddEvent(new VersionAdded { SiteId = cmd.SiteId, ModuleId = cmd.ModuleId, AggregateRootId = Id, VersionId = cmd.VersionId, Content = cmd.Content, Description = cmd.Description, Status = cmd.Status, VersionLocalisations = cmd.VersionLocalisations }); }
public void Should_throw_validation_exception_when_validation_fails() { var command = new AddVersion { SiteId = Guid.NewGuid(), ModuleId = Guid.NewGuid(), Id = Guid.Empty, Content = "Content" }; var textModuleRepositoryMock = new Mock <ITextModuleRepository>(); var validatorMock = new Mock <IValidator <AddVersion> >(); validatorMock.Setup(x => x.Validate(command)).Returns(new ValidationResult(new List <ValidationFailure> { new ValidationFailure("Id", "Id Error") })); var addVersionHandler = new AddVersionHandler(textModuleRepositoryMock.Object, validatorMock.Object); Assert.Throws <ValidationException>(() => addVersionHandler.Handle(command)); }
public void Should_have_error_when_content_is_empty() { var command = new AddVersion { SiteId = Guid.NewGuid(), ModuleId = Guid.NewGuid(), Id = Guid.NewGuid(), Content = "" }; var textModuleRulesMock = new Mock <ITextModuleRules>(); var moduleRulesMock = new Mock <IModuleRules>(); var siteRulesMock = new Mock <ISiteRules>(); var languageRulesMock = new Mock <ILanguageRules>(); var localisationValidator = new Mock <IValidator <AddVersion.VersionLocalisation> >(); var validator = new AddVersionValidator(textModuleRulesMock.Object, moduleRulesMock.Object, siteRulesMock.Object, languageRulesMock.Object, localisationValidator.Object); validator.ShouldHaveValidationErrorFor(x => x.Content, command); }