179 lines
7.7 KiB
C#
Raw Normal View History

2025-02-18 01:17:27 +04:00
using Microsoft.Extensions.Logging;
using Moq;
using NorthBridgeBusinessLogic.Implementations;
using NorthBridgeContract.DataModels;
using NorthBridgeContract.Enums;
using NorthBridgeContract.Exceptions;
using NorthBridgeContract.StoragesContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NorthBridgeTest.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(), "component 1", ComponentType.RAM, Guid.NewGuid().ToString(), 20, false),
new(Guid.NewGuid().ToString(), "component 2", ComponentType.CoolingSystem, Guid.NewGuid().ToString(), 30, true),
new(Guid.NewGuid().ToString(), "component 3", ComponentType.PowerSupply, Guid.NewGuid().ToString(), 40, 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([]);
// 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()
{
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()
{
_componentStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
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()
{
var id = Guid.NewGuid().ToString();
var record = new ComponentDataModel(id, "component name", ComponentType.Processor, Guid.NewGuid().ToString(), 100, false);
_componentStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
var element = _componentBusinessLogicContract.GetComponentById(id);
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_EmptyData_ThrowException_Test()
{
Assert.That(() => _componentBusinessLogicContract.GetComponentByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _componentBusinessLogicContract.GetComponentByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_componentStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void InsertComponent_CorrectRecord_Test()
{
var flag = false;
var record = new ComponentDataModel(Guid.NewGuid().ToString(), "component name", ComponentType.Motherboard, Guid.NewGuid().ToString(), 200, 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.ManufacturerId == record.ManufacturerId && x.Price == record.Price && x.IsDeleted == record.IsDeleted;
});
_componentBusinessLogicContract.InsertComponent(record);
_componentStorageContract.Verify(x => x.AddElement(It.IsAny<ComponentDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateComponent_CorrectRecord_Test()
{
var flag = false;
var record = new ComponentDataModel(Guid.NewGuid().ToString(), "component name", ComponentType.Processor, Guid.NewGuid().ToString(), 150, 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.ManufacturerId == record.ManufacturerId && x.Price == record.Price && x.IsDeleted == record.IsDeleted;
});
_componentBusinessLogicContract.UpdateComponent(record);
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteComponent_CorrectRecord_Test()
{
var id = Guid.NewGuid().ToString();
_componentStorageContract.Setup(x => x.DelElement(id)).Verifiable();
_componentBusinessLogicContract.DeleteComponent(id);
_componentStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteComponent_EmptyId_ThrowException_Test()
{
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);
}
}
}