357 lines
15 KiB
C#

using Microsoft.Extensions.Logging;
using Moq;
using TheCyclopsBusinessLogic.Implementations;
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsTests.BusinessLogicsContractsTests;
[TestFixture]
internal class ComponentBusinessLogicContractTests
{
private ComponentBusinessLogicContract _componentBusinessLogicContract;
private Mock<IComponentStorageContract> _componentStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_componentStorageContract = new Mock<IComponentStorageContract>();
_componentBusinessLogicContract = new ComponentBusinessLogicContract(_componentStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_componentStorageContract.Reset();
}
[Test]
public void GetAllComponents_ReturnListOfRecords_Test()
{
// Arrange
var listOriginal = new List<ComponentDataModel>()
{
new(Guid.NewGuid().ToString(), "name 1", ComponentType.Other, 10, false),
new(Guid.NewGuid().ToString(), "name 2", ComponentType.Other, 10, true),
new(Guid.NewGuid().ToString(), "name 3", ComponentType.Other, 10, false),
};
_componentStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
// Act
var listOnlyActive = _componentBusinessLogicContract.GetAllComponents(true);
var list = _componentBusinessLogicContract.GetAllComponents(false);
// Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_componentStorageContract.Verify(x => x.GetList(true), Times.Once);
_componentStorageContract.Verify(x => x.GetList(false), Times.Once);
}
[Test]
public void GetAllComponents_ReturnEmptyList_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(new List<ComponentDataModel>());
// Act
var listOnlyActive = _componentBusinessLogicContract.GetAllComponents(true);
var list = _componentBusinessLogicContract.GetAllComponents(false);
// Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_componentStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
}
[Test]
public void GetAllComponents_ReturnNull_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.GetAllComponents(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_componentStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetAllComponents_StorageThrowError_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.GetAllComponents(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_componentStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetComponentByData_GetById_ReturnRecord_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var record = new ComponentDataModel(id, "name", ComponentType.Other, 10, false);
_componentStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
// Act
var element = _componentBusinessLogicContract.GetComponentByData(id);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_componentStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetComponentByData_GetByName_ReturnRecord_Test()
{
// Arrange
var name = "name";
var record = new ComponentDataModel(Guid.NewGuid().ToString(), name, ComponentType.Other, 10, false);
_componentStorageContract.Setup(x => x.GetElementByName(name)).Returns(record);
// Act
var element = _componentBusinessLogicContract.GetComponentByData(name);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.ComponentName, Is.EqualTo(name));
_componentStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetComponentByData_EmptyData_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.GetComponentByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _componentBusinessLogicContract.GetComponentByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_componentStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
_componentStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetComponentByData_GetById_NotFoundRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_componentStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetComponentByData_GetByName_NotFoundRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.GetComponentByData("name"), Throws.TypeOf<ElementNotFoundException>());
_componentStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetComponentByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_componentStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _componentBusinessLogicContract.GetComponentByData("name"), Throws.TypeOf<StorageException>());
_componentStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_componentStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertComponent_CorrectRecord_Test()
{
// Arrange
var flag = false;
var record = new ComponentDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false);
_componentStorageContract.Setup(x => x.AddElement(It.IsAny<ComponentDataModel>()))
.Callback((ComponentDataModel x) =>
{
flag = x.Id == record.Id && x.ComponentName == record.ComponentName && x.ComponentType == record.ComponentType &&
x.Price == record.Price && x.IsDeleted == record.IsDeleted;
});
// Act
_componentBusinessLogicContract.InsertComponent(record);
// Assert
_componentStorageContract.Verify(x => x.AddElement(It.IsAny<ComponentDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertComponent_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.AddElement(It.IsAny<ComponentDataModel>())).Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false)), Throws.TypeOf<ElementExistsException>());
_componentStorageContract.Verify(x => x.AddElement(It.IsAny<ComponentDataModel>()), Times.Once);
}
[Test]
public void InsertComponent_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.InsertComponent(null), Throws.TypeOf<ArgumentNullException>());
_componentStorageContract.Verify(x => x.AddElement(It.IsAny<ComponentDataModel>()), Times.Never);
}
[Test]
public void InsertComponent_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.InsertComponent(new ComponentDataModel("id", "name", ComponentType.Other, 10, false)), Throws.TypeOf<ValidationException>());
_componentStorageContract.Verify(x => x.AddElement(It.IsAny<ComponentDataModel>()), Times.Never);
}
[Test]
public void InsertComponent_StorageThrowError_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.AddElement(It.IsAny<ComponentDataModel>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false)), Throws.TypeOf<StorageException>());
_componentStorageContract.Verify(x => x.AddElement(It.IsAny<ComponentDataModel>()), Times.Once);
}
[Test]
public void UpdateComponent_CorrectRecord_Test()
{
// Arrange
var flag = false;
var record = new ComponentDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false);
_componentStorageContract.Setup(x => x.UpdElement(It.IsAny<ComponentDataModel>()))
.Callback((ComponentDataModel x) =>
{
flag = x.Id == record.Id && x.ComponentName == record.ComponentName && x.ComponentType == record.ComponentType &&
x.Price == record.Price && x.IsDeleted == record.IsDeleted;
});
// Act
_componentBusinessLogicContract.UpdateComponent(record);
// Assert
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateComponent_RecordWithIncorrectData_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.UpdElement(It.IsAny<ComponentDataModel>())).Throws(new ElementNotFoundException(""));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false)), Throws.TypeOf<ElementNotFoundException>());
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Once);
}
[Test]
public void UpdateComponent_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.UpdElement(It.IsAny<ComponentDataModel>())).Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), "anme", ComponentType.Other, 10, false)), Throws.TypeOf<ElementExistsException>());
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Once);
}
[Test]
public void UpdateComponent_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.UpdateComponent(null), Throws.TypeOf<ArgumentNullException>());
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Never);
}
[Test]
public void UpdateComponent_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.UpdateComponent(new ComponentDataModel("id", "name", ComponentType.Other, 10, false)), Throws.TypeOf<ValidationException>());
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Never);
}
[Test]
public void UpdateComponent_StorageThrowError_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.UpdElement(It.IsAny<ComponentDataModel>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false)), Throws.TypeOf<StorageException>());
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Once);
}
[Test]
public void DeleteComponent_CorrectRecord_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_componentStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
// Act
_componentBusinessLogicContract.DeleteComponent(id);
// Assert
_componentStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteComponent_RecordWithIncorrectId_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_componentStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_componentStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteComponent_IdIsNullOrEmpty_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.DeleteComponent(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _componentBusinessLogicContract.DeleteComponent(string.Empty), Throws.TypeOf<ArgumentNullException>());
_componentStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteComponent_IdIsNotGuid_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.DeleteComponent("id"), Throws.TypeOf<ValidationException>());
_componentStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteComponent_StorageThrowError_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_componentStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}