public async Task <CommandResult> Handle(AddCardCommand request, CancellationToken cancellationToken) { var commandResult = new CommandResult(); if (request.Card != null) { var validationResults = _validator.Validate(request.Card); if (validationResults.IsValid) { var cardModel = _mapper.Map <CardModel>(request.Card); var result = await _cardService.Add(cardModel); if (result != null) { if (request.Card.ImageUrl != null) { var downloadImageCommand = new DownloadImageCommand { RemoteImageUrl = request.Card.ImageUrl, ImageFileName = request.Card.Name, ImageFolderPath = _settings.Value.CardImageFolderPath }; await _mediator.Send(downloadImageCommand, cancellationToken); } commandResult.Data = result.Id; commandResult.IsSuccessful = true; } else { commandResult.Errors = new List <string> { "Card not persisted to data source." }; } } else { commandResult.Errors = validationResults.Errors.Select(err => err.ErrorMessage).ToList(); } } else { commandResult.Errors = new List <string> { "Card must not be null or empty" }; } return(commandResult); }
public void Given_An_Invalid_ImageFileName_Validation_Should_Fail(string imageFileName) { // Arrange var inputModel = new DownloadImageCommand { ImageFileName = imageFileName }; // Act Action act = () => _sut.ShouldHaveValidationErrorFor(d => d.ImageFileName, inputModel); // Assert act.Invoke(); }
public void Given_An_Invalid_RemoteImageUrl_Validation_Should_Fail() { // Arrange var inputModel = new DownloadImageCommand { RemoteImageUrl = null }; // Act Action act = () => _sut.ShouldHaveValidationErrorFor(d => d.RemoteImageUrl, inputModel); // Assert act.Invoke(); }
public async Task Given_An_Invalid_DownloadImageCommand_Error_Should_Not_Be_Null_Or_Empty() { // Arrange var downloadImageCommand = new DownloadImageCommand(); _validator.Validate(Arg.Is(downloadImageCommand)).Returns(new ValidationResult { Errors = { new ValidationFailure("property", "Failed") } }); // Act var result = await _sut.Handle(downloadImageCommand, CancellationToken.None); // Assert result.Errors.Should().NotBeNullOrEmpty(); }
public async Task Given_An_Invalid_DownloadImageCommand_Validation_Should_Fail() { // Arrange var downloadImageCommand = new DownloadImageCommand(); _validator.Validate(Arg.Is(downloadImageCommand)).Returns(new ValidationResult { Errors = { new ValidationFailure("property", "Failed") } }); // Act var result = await _sut.Handle(downloadImageCommand, CancellationToken.None); // Assert result.IsSuccessful.Should().BeFalse(); }
public async Task Given_An_Valid_DownloadImageCommand_If_Publish_Fails_IsSuccessful_Should_Be_False() { // Arrange var downloadImageCommand = new DownloadImageCommand { ImageFolderPath = @"c:\windows", ImageFileName = "Call Of The Haunted", RemoteImageUrl = new Uri("http://filesomewhere/callofthehaunted.png") }; _validator.Validate(Arg.Any <DownloadImageCommand>()).Returns(new DownloadImageCommandValidator().Validate(downloadImageCommand)); _cardImageQueueService.Publish(Arg.Any <DownloadImage>()).Returns(new CardImageCompletion()); // Act var result = await _sut.Handle(downloadImageCommand, CancellationToken.None); // Assert result.IsSuccessful.Should().BeFalse(); }
public async Task Given_An_Valid_DownloadImageCommand_Should_Execute_Publish_Method_Once() { // Arrange var downloadImageCommand = new DownloadImageCommand { ImageFolderPath = @"c:\windows", ImageFileName = "Call Of The Haunted", RemoteImageUrl = new Uri("http://filesomewhere/callofthehaunted.png") }; _validator.Validate(Arg.Any <DownloadImageCommand>()).Returns(new DownloadImageCommandValidator().Validate(downloadImageCommand)); _cardImageQueueService.Publish(Arg.Any <DownloadImage>()).Returns(new CardImageCompletion()); // Act await _sut.Handle(downloadImageCommand, CancellationToken.None); // Assert await _cardImageQueueService.Received(1).Publish(Arg.Any <DownloadImage>()); }
public async Task <CommandResult> Handle(UpdateCardCommand request, CancellationToken cancellationToken) { var commandResult = new CommandResult(); var validationResults = _validator.Validate(request.Card); if (validationResults.IsValid) { var cardModel = _mapper.Map <CardModel>(request.Card); var cardUpdated = await _cardService.Update(cardModel); if (cardUpdated != null) { if (request.Card.ImageUrl != null) { var downloadImageCommand = new DownloadImageCommand { RemoteImageUrl = request.Card.ImageUrl, ImageFileName = request.Card.Name, ImageFolderPath = _settings.Value.CardImageFolderPath }; await _mediator.Send(downloadImageCommand, cancellationToken); } commandResult.Data = CommandMapperHelper.MapCardByCardType(_mapper, request.Card.CardType.GetValueOrDefault(), cardUpdated); commandResult.IsSuccessful = true; } else { commandResult.Errors = new List <string> { "Card not updated in data source." }; } } else { commandResult.Errors = validationResults.Errors.Select(err => err.ErrorMessage).ToList(); } return(commandResult); }
public async Task Given_An_Valid_DownloadImageCommand_Should_Invoke_Download_Method_Once() { // Arrange var downloadImageCommand = new DownloadImageCommand { ImageFileName = "Adreus, Keeper of Armageddon", ImageFolderPath = @"D:\Apps\ygo-api\Images\Cards", RemoteImageUrl = new Uri("https://vignette.wikia.nocookie.net/yugioh/images/f/f6/AdreusKeeperofArmageddon-BP01-EN-R-1E.png") }; _validator.Validate(Arg.Is(downloadImageCommand)).Returns(new ValidationResult()); _fileSystemService.Download(Arg.Any <Uri>(), Arg.Any <string>()).Returns(new DownloadedFile { ContentType = "image/png" }); // Act await _sut.Handle(downloadImageCommand, CancellationToken.None); // Assert await _fileSystemService.Received(1).Download(Arg.Any <Uri>(), Arg.Any <string>()); }
/// <summary> /// This method downloads the image at the specified Uri. /// </summary> /// <param name="uri">The path the image to download.</param> /// <param name="cancellationToken">A cancellation token used to propagate notification that the operation should be canceled.</param> /// <returns>The local machine path to the downloaded image.</returns> public async Task <string> DownloadImageAsync(Uri uri, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); DownloadImageCommand command = new DownloadImageCommand { Uri = uri, }; var response = await InvokeMethodAsync("scheduleCommandXml", new object[] { command.ToString() }, cancellationToken); XmlDocument responseDocument = new XmlDocument(); responseDocument.LoadXml(response.Data.Value <string>()); XmlElement root = responseDocument.SelectSingleNode("//newblue_ext") as XmlElement; if (root != null && root.HasAttribute("path")) { return(root.GetAttribute("path")); } return(null); }
public override void RaiseCommandCanExecuteChanged() { UploadImageCommand.RaiseCanExecuteChanged(); DownloadImageCommand.RaiseCanExecuteChanged(); ResetImageCommand.RaiseCanExecuteChanged(); }