forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
243 lines
11 KiB
C#
243 lines
11 KiB
C#
using MagicCarpetBusinessLogic.Implementations;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Enums;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.StoragesContracts;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetTests.BusinessLogicContractsTests;
|
|
|
|
[TestFixture]
|
|
internal class SuppliesBusinessLogicContractTests
|
|
{
|
|
private SuppliesBusinessLogicContract _suppliesBusinessLogicContract;
|
|
private Mock<ISuppliesStorageContract> _suppliesStorageContract;
|
|
|
|
[OneTimeSetUp]
|
|
public void OneTimeSetUp()
|
|
{
|
|
_suppliesStorageContract = new Mock<ISuppliesStorageContract>();
|
|
_suppliesBusinessLogicContract = new SuppliesBusinessLogicContract(_suppliesStorageContract.Object, new Mock<ILogger>().Object);
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
_suppliesStorageContract.Reset();
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllSupplies_ReturnListOfRecords_Test()
|
|
{
|
|
// Arrange
|
|
var listOriginal = new List<SuppliesDataModel>()
|
|
{
|
|
new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []),
|
|
new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []),
|
|
new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []),
|
|
};
|
|
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>())).Returns(listOriginal);
|
|
// Act
|
|
var list = _suppliesBusinessLogicContract.GetAllComponents();
|
|
// Assert
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllSupplies_ReturnEmptyList_Test()
|
|
{
|
|
// Arrange
|
|
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>())).Returns([]);
|
|
// Act
|
|
var list = _suppliesBusinessLogicContract.GetAllComponents();
|
|
// Assert
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(0));
|
|
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllSupplies_ReturnNull_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>())).Returns((List<SuppliesDataModel>)null);
|
|
// Act & Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.GetAllComponents(), Throws.TypeOf<NullListException>());
|
|
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllSupplies_StorageThrowError_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
|
// Act & Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.GetAllComponents(), Throws.TypeOf<StorageException>());
|
|
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetSuppliesByData_GetById_ReturnRecord_Test()
|
|
{
|
|
// Arrange
|
|
var id = Guid.NewGuid().ToString();
|
|
var record = new SuppliesDataModel(id,TourType.Beach, DateTime.Now, 1, []);
|
|
_suppliesStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
|
// Act
|
|
var element = _suppliesBusinessLogicContract.GetComponentByData(id);
|
|
// Assert
|
|
Assert.That(element, Is.Not.Null);
|
|
Assert.That(element.Id, Is.EqualTo(id));
|
|
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetComponentsByData_EmptyData_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.GetComponentByData(null), Throws.TypeOf<ArgumentNullException>());
|
|
Assert.That(() => _suppliesBusinessLogicContract.GetComponentByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
|
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void GetSuppliesByData_GetById_NotFoundRecord_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_suppliesStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
|
|
// Act & Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
|
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetSuppliesByData_StorageThrowError_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_suppliesStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
|
// Act & Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
|
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void InsertSupplies_CorrectRecord_Test()
|
|
{
|
|
// Arrange
|
|
var record = new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
|
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
|
_suppliesStorageContract.Setup(x => x.AddElement(It.IsAny<SuppliesDataModel>()));
|
|
// Act
|
|
_suppliesBusinessLogicContract.InsertComponent(record);
|
|
// Assert
|
|
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void InsertSupplies_RecordWithExistsData_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_suppliesStorageContract.Setup(x => x.AddElement(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
|
// Act & Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
|
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
|
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void InsertSupplies_NullRecord_ThrowException_Test()
|
|
{
|
|
// Act & Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.InsertComponent(null), Throws.TypeOf<ArgumentNullException>());
|
|
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void InsertComponents_InvalidRecord_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.InsertComponent(new SuppliesDataModel("id", TourType.Beach, DateTime.UtcNow, 1, [])), Throws.TypeOf<ValidationException>());
|
|
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void InsertSupplies_StorageThrowError_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_suppliesStorageContract.Setup(x => x.AddElement(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
|
// Act & Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
|
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
|
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateSupplies_CorrectRecord_Test()
|
|
{
|
|
// Arrange
|
|
var record = new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
|
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
|
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>()));
|
|
// Act
|
|
_suppliesBusinessLogicContract.UpdateComponent(record);
|
|
// Assert
|
|
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateSupplies_RecordWithIncorrectData_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>())).Throws(new ElementNotFoundException(""));
|
|
// Act & Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
|
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementNotFoundException>());
|
|
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateSupplies_RecordWithExistsData_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
|
// Act & Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
|
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
|
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateSupplies_NullRecord_ThrowException_Test()
|
|
{
|
|
// Act & Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(null), Throws.TypeOf<ArgumentNullException>());
|
|
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateComponents_InvalidRecord_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, [])), Throws.TypeOf<ValidationException>());
|
|
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateSupplies_StorageThrowError_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
|
// Act & Assert
|
|
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
|
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
|
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
|
}
|
|
}
|