diff --git a/Govor.API.Tests/IntegrationTests/EF/Repositories/MediaAttachmentsTests.cs b/Govor.API.Tests/IntegrationTests/EF/Repositories/MediaAttachmentsTests.cs new file mode 100644 index 0000000..c017e03 --- /dev/null +++ b/Govor.API.Tests/IntegrationTests/EF/Repositories/MediaAttachmentsTests.cs @@ -0,0 +1,103 @@ +using AutoFixture; +using Govor.Core.Infrastructure.Validators; +using Govor.Core.Models; +using Govor.Data; +using Govor.Data.Repositories; +using Govor.Data.Repositories.Exceptions; +using Microsoft.EntityFrameworkCore; + +namespace Govor.API.Tests.IntegrationTests.EF.Repositories; + +[TestFixture] +public class MediaAttachmentsTests +{ + private Fixture _fixture; + private DbContextOptions _options; + private readonly IObjectValidator _validator = new MediaAttachmentsValidator(); + private int _testIteration = 0; + + [SetUp] + public void SetUp() + { + _testIteration += 1; + + _fixture = new Fixture(); + + _fixture.Behaviors + .OfType() + .ToList() + .ForEach(b => _fixture.Behaviors.Remove(b)); + + _fixture.Behaviors.Add(new OmitOnRecursionBehavior()); + + _options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: $"DbGovor_{nameof(MediaAttachmentsTests)}_{_testIteration}") + .Options; + } + + [Test] + public async Task Given_NotEmptySetDb_When_GetAll_Then_ReturnAllAttachments() + { + // Arrange + var random = new Random(); + var attachments = _fixture.CreateMany(random.Next(2, 10)).ToList(); + + await using var context = new GovorDbContext(_options); + var attachmentsRepository = new MediaAttachmentsRepository(context, _validator); + + context.MediaAttachments.AddRange(attachments); + await context.SaveChangesAsync(); + + // Act + + var result = await attachmentsRepository.GetAllAsync(); + + // Assert + Assert.That(result, Is.Not.Null); + Assert.That(result.Count, Is.EqualTo(attachments.Count)); + Assert.That(result, Is.EquivalentTo(attachments)); + } + + [Test] + public void Given_EmptySetDb_When_GetAll_Should_Throw_NotFoundException() + { + // Arrange + using var context = new GovorDbContext(_options); + var mediaAttachments = new MediaAttachmentsRepository(context, _validator); + // Act & Assert + Assert.ThrowsAsync(async () => await mediaAttachments.GetAllAsync()); + } + + [Test] + public async Task Given_ValidMessageId_When_GetAllByMessageId_Then_ReturnAllAttachments() + { + // Arrange + var random = new Random(); + var attachments = _fixture.CreateMany(random.Next(2, 10)).ToList(); + + await using var context = new GovorDbContext(_options); + var attachmentsRepository = new MediaAttachmentsRepository(context, _validator); + + context.MediaAttachments.AddRange(attachments); + await context.SaveChangesAsync(); + + // Act + var result = await attachmentsRepository.GetAllByMessageId(attachments.First().MessageId); + + // Assert + Assert.That(result, Is.Not.Null); + Assert.That(result.Count, Is.EqualTo(1)); + Assert.That(result.First(), Is.EqualTo(attachments.First())); + } + + [Test] + public async Task Given_InvalidMessageId_When_GetAllByMessageId_Should_Throw_NotFoundByKeyException() + { + // Arrange + await using var context = new GovorDbContext(_options); + var attachmentsRepository = new MediaAttachmentsRepository(context, _validator); + + // Act & Assert + Assert.ThrowsAsync>(async () => await attachmentsRepository.GetAllByMessageId(_fixture.Create())); + } +} \ No newline at end of file diff --git a/Govor.Core/Infrastructure/Validators/MediaAttachmentsValidator.cs b/Govor.Core/Infrastructure/Validators/MediaAttachmentsValidator.cs new file mode 100644 index 0000000..fc20e82 --- /dev/null +++ b/Govor.Core/Infrastructure/Validators/MediaAttachmentsValidator.cs @@ -0,0 +1,42 @@ +using Govor.Core.Models; +using Exception = System.Exception; + +namespace Govor.Core.Infrastructure.Validators; + +public class MediaAttachmentsValidator : IObjectValidator +{ + public void Validate(MediaAttachments attachments) + { + try + { + if (attachments is null) + throw new ArgumentNullException(nameof(attachments)); + if(attachments.Id == Guid.Empty) + throw new ArgumentException("Id cannot be empty", nameof(attachments)); + if(attachments.MessageId == Guid.Empty) + throw new ArgumentException("MessageId cannot be empty", nameof(attachments)); + if(string.IsNullOrWhiteSpace(attachments.FilePath)) + throw new ArgumentException("File path cannot be empty", nameof(attachments)); + if(string.IsNullOrWhiteSpace(attachments.EncryptedKey)) + throw new ArgumentException("Encrypted key cannot be empty", nameof(attachments)); + } + catch (Exception ex) + { + throw new InvalidObjectException(ex); + } + + } + + public bool TryValidate(MediaAttachments attachments) + { + try + { + Validate(attachments); + return true; + } + catch (Exception) + { + return false; + } + } +} \ No newline at end of file diff --git a/Govor.Core/Models/MediaAttachments.cs b/Govor.Core/Models/MediaAttachments.cs index df65431..bc5b42c 100644 --- a/Govor.Core/Models/MediaAttachments.cs +++ b/Govor.Core/Models/MediaAttachments.cs @@ -11,6 +11,18 @@ public class MediaAttachments public string? EncryptedKey { get; set; } public Message Message { get; set; } = null!; + + public override bool Equals(object? obj) + { + if (obj is not MediaAttachments other) return false; + + return Id == other.Id && + MessageId == other.MessageId && + EncryptedKey == other.EncryptedKey && + Type == other.Type && + FilePath == other.FilePath && + MimeType == other.MimeType; + } } public enum MediaType diff --git a/Govor.Data/Repositories/MediaAttachmentsRepository.cs b/Govor.Data/Repositories/MediaAttachmentsRepository.cs index 8707fe9..cc18cd8 100644 --- a/Govor.Data/Repositories/MediaAttachmentsRepository.cs +++ b/Govor.Data/Repositories/MediaAttachmentsRepository.cs @@ -26,9 +26,13 @@ public class MediaAttachmentsRepository : IMediaAttachmentsRepository .ToListOrThrowIfEmpty(new NotFoundException("No media attachments found.")); } - public Task> GetAllByMessageId(Guid messageId) + public async Task> GetAllByMessageId(Guid messageId) { - throw new NotImplementedException(); + return await _context.MediaAttachments + .AsNoTracking() + .Include(ma => ma.Message) + .Where(m => m.MessageId == messageId) + .ToListOrThrowIfEmpty(new NotFoundByKeyException(messageId, "No media attachments found by given message Id")); } public Task AddAsync(MediaAttachments mediaAttachments)