diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestFeedbackReceiverService.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestFeedbackReceiverService.cs index 8cc8643..818b4cb 100644 --- a/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestFeedbackReceiverService.cs +++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestFeedbackReceiverService.cs @@ -1,6 +1,126 @@ -namespace Retroactiune.Tests.Retroactiune.WebAPI.Services +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture.Xunit2; +using MongoDB.Driver; +using Moq; +using Retroactiune.Models; +using Retroactiune.Services; +using Retroactiune.Settings; +using Xunit; + +namespace Retroactiune.Tests.Retroactiune.WebAPI.Services { public class TestFeedbackReceiverService { + [Theory, AutoData] + public async Task Test_CreateManyAsync_Success(IEnumerable items) + { + // Arrange + var mongoDatabaseMock = new Mock(); + var mongoClientMock = new Mock(); + var mongoSettingsMock = new Mock(); + var mongoCollectionMock = new Mock>(); + + mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB"); + mongoSettingsMock.SetupGet(i => i.FeedbackReceiverCollectionName).Returns("feedback_receiver"); + + mongoClientMock + .Setup(stub => stub.GetDatabase(It.IsAny(), It.IsAny())) + .Returns(mongoDatabaseMock.Object); + + mongoDatabaseMock + .Setup(i => i.GetCollection(It.IsAny(), It.IsAny())) + .Returns(mongoCollectionMock.Object); + + // Test + var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object); + await service.CreateManyAsync(items); + + // Assert + mongoClientMock.Verify(i => i.GetDatabase("MyDB", null), Times.Once()); + mongoDatabaseMock.Verify( + i => i.GetCollection("feedback_receiver", It.IsAny()), + Times.Once()); + mongoCollectionMock.Verify( + i => i.InsertManyAsync(items, It.IsAny(), It.IsAny()), + Times.Once()); + } + + [Fact] + public void Test_CreateManyAsync_NullVal() + { + // Arrange + var mongoDatabaseMock = new Mock(); + var mongoClientMock = new Mock(); + var mongoSettingsMock = new Mock(); + var mongoCollectionMock = new Mock>(); + + mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB"); + mongoSettingsMock.SetupGet(i => i.FeedbackReceiverCollectionName).Returns("feedback_receiver"); + + mongoClientMock + .Setup(i => i.GetDatabase(It.IsAny(), It.IsAny())) + .Returns(mongoDatabaseMock.Object); + + mongoDatabaseMock + .Setup(i => i.GetCollection(It.IsAny(), It.IsAny())) + .Returns(mongoCollectionMock.Object); + + // Test + var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object); + var ex = Record.ExceptionAsync(async () => await service.CreateManyAsync(null)); + + + // Assert + Assert.NotNull(ex); + + mongoClientMock + .Verify(i => i.GetDatabase("MyDB", null), Times.Once()); + mongoDatabaseMock + .Verify(i => i.GetCollection("feedback_receiver", It.IsAny()), + Times.Once()); + mongoCollectionMock + .Verify(i => i.InsertManyAsync(It.IsAny>(), It.IsAny(), It.IsAny()), + Times.Never()); + } + + [Fact] + public void Test_CreateManyAsync_NoItems() + { + // Arrange + var mongoDatabaseMock = new Mock(); + var mongoClientMock = new Mock(); + var mongoSettingsMock = new Mock(); + var mongoCollectionMock = new Mock>(); + + mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB"); + mongoSettingsMock.SetupGet(i => i.FeedbackReceiverCollectionName).Returns("feedback_receiver"); + + mongoClientMock + .Setup(i => i.GetDatabase(It.IsAny(), It.IsAny())) + .Returns(mongoDatabaseMock.Object); + + mongoDatabaseMock + .Setup(i => i.GetCollection(It.IsAny(), It.IsAny())) + .Returns(mongoCollectionMock.Object); + + // Test + var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object); + var ex = Record.ExceptionAsync(async () => await service.CreateManyAsync(new List())); + + + // Assert + Assert.NotNull(ex); + + mongoClientMock + .Verify(i => i.GetDatabase("MyDB", null), Times.Once()); + mongoDatabaseMock + .Verify(i => i.GetCollection("feedback_receiver", It.IsAny()), + Times.Once()); + mongoCollectionMock + .Verify(i => i.InsertManyAsync(It.IsAny>(), It.IsAny(), It.IsAny()), + Times.Never()); + } } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs b/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs index 28e9b1e..3337c0f 100644 --- a/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs +++ b/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using MongoDB.Driver; using Retroactiune.Models; @@ -23,6 +24,16 @@ namespace Retroactiune.Services public async Task CreateManyAsync(IEnumerable items) { + if (items == null) + { + throw new ArgumentNullException(nameof(items)); + } + + if (!items.Any()) + { + throw new GenericServiceException("items must contain at least one element"); + } + try { await _collection.InsertManyAsync(items); diff --git a/Retroactiune.WebAPI/Settings/IMongoDbSettings.cs b/Retroactiune.WebAPI/Settings/IMongoDbSettings.cs index 101bb42..512854f 100644 --- a/Retroactiune.WebAPI/Settings/IMongoDbSettings.cs +++ b/Retroactiune.WebAPI/Settings/IMongoDbSettings.cs @@ -1,6 +1,4 @@ -using System; - -namespace Retroactiune.Settings +namespace Retroactiune.Settings { /// /// Interface for repressing the application's MongoDb settings Options.