201 lines
8.3 KiB
C#

using Moq;
using NorthBridgeBusinessLogic.Implementations;
using NorthBridgeContract.DataModels;
using NorthBridgeContract.StoragesContracts;
using NorthBridgeContract.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NorthBridgeTest.BusinessLogicsContractsTests
{
[TestFixture]
internal class SupplyBusinessLogicContractTests
{
private SupplyBusinessLogicContract _supplyBusinessLogicContract;
private Mock<ISupplyStorageContract> _supplyStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_supplyStorageContract = new Mock<ISupplyStorageContract>();
_supplyBusinessLogicContract = new SupplyBusinessLogicContract(_supplyStorageContract.Object);
}
[TearDown]
public void TearDown()
{
_supplyStorageContract.Reset();
}
[Test]
public void CreateSupply_ValidSupply_CreatesSupply_Test()
{
// Arrange
var supply = new SupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, new List<ComponentInSupplyDataModel>
{
new ComponentInSupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 100)
});
_supplyStorageContract.Setup(x => x.AddSupply(It.IsAny<SupplyDataModel>())).Verifiable();
// Act
_supplyBusinessLogicContract.CreateSupply(supply);
// Assert
_supplyStorageContract.Verify(x => x.AddSupply(It.Is<SupplyDataModel>(s =>
s.Id == supply.Id && s.StorageId == supply.StorageId && s.Date == supply.Date && s.Components == supply.Components)), Times.Once);
}
[Test]
public void CreateSupply_InvalidSupply_ThrowsValidationException_Test()
{
// Arrange
var supply = new SupplyDataModel(Guid.NewGuid().ToString(), "", DateTime.UtcNow, new List<ComponentInSupplyDataModel>());
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.CreateSupply(supply),
Throws.TypeOf<ValidationException>());
_supplyStorageContract.Verify(x => x.AddSupply(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void GetSupplyById_SupplyFound_ReturnsSupply_Test()
{
// Arrange
var supplyId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(supplyId, Guid.NewGuid().ToString(), DateTime.UtcNow, new List<ComponentInSupplyDataModel>());
_supplyStorageContract.Setup(x => x.GetSupplyById(supplyId)).Returns(supply);
// Act
var result = _supplyBusinessLogicContract.GetSupplyById(supplyId);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Id, Is.EqualTo(supplyId));
_supplyStorageContract.Verify(x => x.GetSupplyById(supplyId), Times.Once);
}
[Test]
public void GetSupplyById_SupplyNotFound_ThrowsArgumentException_Test()
{
// Arrange
var supplyId = "non-existent";
_supplyStorageContract.Setup(x => x.GetSupplyById(supplyId)).Returns((SupplyDataModel?)null);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.GetSupplyById(supplyId),
Throws.ArgumentException.With.Message.Contains($"Supply with ID {supplyId} not found."));
_supplyStorageContract.Verify(x => x.GetSupplyById(supplyId), Times.Once);
}
[Test]
public void GetSuppliesByStorageId_ReturnsSupplies_Test()
{
// Arrange
var storageId = Guid.NewGuid().ToString();
var supplies = new List<SupplyDataModel>
{
new SupplyDataModel(Guid.NewGuid().ToString(), storageId, DateTime.UtcNow, new List<ComponentInSupplyDataModel>())
};
_supplyStorageContract.Setup(x => x.GetSuppliesByStorageId(storageId)).Returns(supplies);
// Act
var result = _supplyBusinessLogicContract.GetSuppliesByStorageId(storageId);
// Assert
Assert.That(result, Is.Not.Empty);
Assert.That(result.First().StorageId, Is.EqualTo(storageId));
_supplyStorageContract.Verify(x => x.GetSuppliesByStorageId(storageId), Times.Once);
}
[Test]
public void AddOrUpdateComponentInSupply_ValidComponent_AddsOrUpdatesComponent_Test()
{
// Arrange
var component = new ComponentInSupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 100);
_supplyStorageContract.Setup(x => x.AddOrUpdateComponentInSupply(It.IsAny<ComponentInSupplyDataModel>())).Verifiable();
// Act
_supplyBusinessLogicContract.AddOrUpdateComponentInSupply(component);
// Assert
_supplyStorageContract.Verify(x => x.AddOrUpdateComponentInSupply(It.Is<ComponentInSupplyDataModel>(c =>
c.SupplyId == component.SupplyId && c.ComponentId == component.ComponentId && c.Count == component.Count)), Times.Once);
}
[Test]
public void AddOrUpdateComponentInSupply_InvalidComponent_ThrowsValidationException_Test()
{
// Arrange
var component = new ComponentInSupplyDataModel(Guid.NewGuid().ToString(), "", -10);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.AddOrUpdateComponentInSupply(component),
Throws.TypeOf<ValidationException>());
_supplyStorageContract.Verify(x => x.AddOrUpdateComponentInSupply(It.IsAny<ComponentInSupplyDataModel>()), Times.Never);
}
[Test]
public void UpdateComponentCountInSupply_ValidCount_UpdatesCount_Test()
{
// Arrange
var supplyId = Guid.NewGuid().ToString();
var componentId = Guid.NewGuid().ToString();
var newCount = 200;
_supplyStorageContract.Setup(x => x.UpdateComponentCountInSupply(supplyId, componentId, newCount)).Verifiable();
// Act
_supplyBusinessLogicContract.UpdateComponentCountInSupply(supplyId, componentId, newCount);
// Assert
_supplyStorageContract.Verify(x => x.UpdateComponentCountInSupply(supplyId, componentId, newCount), Times.Once);
}
[Test]
public void UpdateComponentCountInSupply_NegativeCount_ThrowsArgumentException_Test()
{
// Arrange
var supplyId = Guid.NewGuid().ToString();
var componentId = Guid.NewGuid().ToString();
var newCount = -10;
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.UpdateComponentCountInSupply(supplyId, componentId, newCount),
Throws.ArgumentException.With.Message.Contains("Component count must be greater than zero."));
_supplyStorageContract.Verify(x => x.UpdateComponentCountInSupply(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()), Times.Never);
}
[Test]
public void RemoveComponentFromSupply_RemovesComponent_Test()
{
// Arrange
var supplyId = Guid.NewGuid().ToString();
var componentId = Guid.NewGuid().ToString();
_supplyStorageContract.Setup(x => x.RemoveComponentFromSupply(supplyId, componentId)).Verifiable();
// Act
_supplyBusinessLogicContract.RemoveComponentFromSupply(supplyId, componentId);
// Assert
_supplyStorageContract.Verify(x => x.RemoveComponentFromSupply(supplyId, componentId), Times.Once);
}
[Test]
public void RemoveComponentFromSupply_SupplyNotFound_ThrowsArgumentException_Test()
{
// Arrange
var supplyId = "abc";
var componentId = Guid.NewGuid().ToString();
_supplyStorageContract.Setup(x => x.RemoveComponentFromSupply(supplyId, componentId)).Throws(new ArgumentException("Supply not found"));
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.RemoveComponentFromSupply(supplyId, componentId),
Throws.ArgumentException.With.Message.Contains("Supply not found"));
}
}
}