public void CreateAnnouncementAsync_GivenAnnouncementAndFileValid()
        {
            //Arrange
            int    processed = 0;
            var    bytes     = new byte[2540];
            string urlBlob   = $"http://blobstorage.com/{file.FileName}";

            fileProcesingService.ValidateFileExtension(file).Returns(true);
            fileProcesingService.ValidateFileSize(file).Returns(true);
            fileProcesingService.ValidateImageDimenensions(file).Returns(true);
            fileProcesingService.GetFileName(file).Returns(file.FileName);
            fileProcesingService.GetFileByteArrayFromStreamImage(Arg.Any <MemoryStream>()).Returns(bytes);

            blobStorageService.SaveFileAsync(Arg.Any <UploadFile>()).Returns(urlBlob);
            blobStorageService.When(fx => fx.SaveFileAsync(Arg.Any <UploadFile>())).Do(fx => ++ processed);

            //Act
            var announcementSpectedTask = announcementService.CreateAnnouncementAsync(announcementDto, file);

            announcementSpectedTask.Wait();

            //Assert
            Assert.IsTrue(announcementSpectedTask.Status == TaskStatus.RanToCompletion);
            Assert.AreEqual(2, processed);
        }
Пример #2
0
        /// <summary>
        /// Administra la carga de imagenes al blob
        /// </summary>
        /// <param name="announcement">Objeto que contiene la información del comunicado</param>
        /// <param name="file">Imagen del comunicado</param>
        /// <param name="action"> Define si es creación o actualización de la imagen</param>
        /// <returns></returns>
        private async Task <Announcement> ManageImageBlobAsync(AnnouncementDto announcement, IFormFile file, AnnouncementAction action)
        {
            if (!fileProcesingService.ValidateFileExtension(file))
            {
                throw new InvalidFileExtensionException("La extensión del archivo no está permitida");
            }

            if (!fileProcesingService.ValidateFileSize(file))
            {
                throw new InvalidFileSizeException("El tamaño del archivo excede el máximo permitido");
            }

            if (!fileProcesingService.ValidateImageDimenensions(file))
            {
                throw new InvalidFileDimensionException("Las dimensiones del archivo exceden el máximo permitido");
            }

            string imageName   = fileProcesingService.GetFileName(file);
            var    imageStream = file.OpenReadStream();

            string thumbnailImageName = fileProcesingService.GetFileName(file);

            byte[] thumbnailImageByteArray = fileProcesingService.GetFileByteArrayFromStreamImage(imageStream);

            if (action == AnnouncementAction.Update)
            {
                await DeleteFileBlobAsync(announcement.ImageId).ConfigureAwait(false);
                await DeleteFileBlobAsync(announcement.ThumbnailImageId).ConfigureAwait(false);
            }

            string urlImage = await SaveFileToBlobAsync(imageName, file.OpenReadStream().GetAllBytes()).ConfigureAwait(false);

            string urlThumbnailImage = await SaveFileToBlobAsync(thumbnailImageName, thumbnailImageByteArray).ConfigureAwait(false);

            announcement.UrlImage          = urlImage;
            announcement.UrlThumbnailImage = urlThumbnailImage;
            announcement.ImageId           = imageName;
            announcement.ThumbnailImageId  = thumbnailImageName;

            return(mapper.Map <Announcement>(announcement));
        }