using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using AutoFixture.Xunit2; using MongoDB.Driver; using Moq; using Retroactiune.Database; using Retroactiune.Models; using Retroactiune.Services; 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); var feedbackReceivers = items as FeedbackReceiver[] ?? items.ToArray(); await service.CreateManyAsync(feedbackReceivers); // 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(feedbackReceivers, 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()); } [Fact] public async Task Test_DeleteManyAsync_Ok() { // 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.DeleteManyAsync(new []{"insert_guid_here"}); // 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.DeleteManyAsync(It.IsAny>(), It.IsAny()), Times.Once()); } [Fact] public async Task Test_FindAsync_Ok() { // Arrange var mongoDatabaseMock = new Mock(); var mongoClientMock = new Mock(); var mongoSettingsMock = new Mock(); var mongoCollectionMock = new Mock>(); var mongoCursorMock = 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); mongoCollectionMock .Setup(i => i.FindAsync(It.IsAny>(), It.IsAny>(), It.IsAny())) .ReturnsAsync(mongoCursorMock.Object); // Test var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object); var guids = new[] {"insert_guid_here"}; await service.FindAsync(guids, 0, 15); // 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.FindAsync(It.IsAny>(), It.IsAny>(), It.IsAny()), Times.Once); } } }