Сделал 2 сложную

This commit is contained in:
2025-03-11 16:17:21 +04:00
parent e212523896
commit 8dfef58c36
8 changed files with 1333 additions and 0 deletions

View File

@@ -0,0 +1,177 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TwoFromTheCasketContracts.BusinessLogicsContracts;
using TwoFromTheCasketContracts.DataModels;
using TwoFromTheCasketContracts.Exceptions;
using TwoFromTheCasketContracts.StorageContracts;
namespace TwoFromTheCasketBusinessLogic.Implementation;
internal class SupplyBusinessLogicContract(ISupplyStorageContract _supplyStorageContract, ILogger _logger) : ISupplyBuisnessLogicContract
{
private ISupplyStorageContract supplyStorageContract = _supplyStorageContract;
private ILogger logger = _logger;
public List<SupplyDataModel> GetSuppliesByPeriod(DateTime fromDate, DateTime toDate)
{
logger.LogInformation("Retrieving supplies from {FromDate} to {ToDate}", fromDate, toDate);
if (fromDate >= toDate)
{
throw new IncorrectDatesException(fromDate, toDate);
}
var supplies = supplyStorageContract.GetList(fromDate, toDate);
if (supplies == null)
{
throw new NullListException();
}
if (supplies.Count == 0)
{
throw new ElementNotFoundException($"No supplies found in the period from {fromDate} to {toDate}.");
}
return supplies;
}
public List<SupplyDataModel> GetSuppliesByWarehouseId(string warehouseId)
{
logger.LogInformation("Retrieving supplies for warehouse: {WarehouseId}", warehouseId);
if (string.IsNullOrWhiteSpace(warehouseId))
{
throw new ArgumentNullException(nameof(warehouseId));
}
if (!Guid.TryParse(warehouseId, out _))
{
throw new ValidationException("Invalid warehouse ID format.");
}
var supplies = supplyStorageContract.GetList(DateTime.MinValue, DateTime.MaxValue, warehouseId);
if (supplies == null)
{
throw new NullListException();
}
if (supplies.Count == 0)
{
throw new ElementNotFoundException($"No supplies found for warehouse ID: {warehouseId}.");
}
return supplies;
}
public SupplyDataModel GetSupplyByData(string data)
{
logger.LogInformation("Retrieving supply by data: {Data}", data);
if (string.IsNullOrWhiteSpace(data))
{
throw new ArgumentNullException(nameof(data));
}
if (!Guid.TryParse(data, out _))
{
throw new ValidationException("The value in the field Id is not a unique identifier.");
}
List<SupplyDataModel>? supplies;
try
{
supplies = supplyStorageContract.GetList(DateTime.MinValue, DateTime.MaxValue);
}
catch (Exception ex)
{
throw new StorageException(ex);
}
if (!supplies.Any())
{
throw new ElementNotFoundException($"No supply found for data: {data}");
}
if (supplies == null)
{
throw new NullListException();
}
var supply = supplies.FirstOrDefault(s => s.Id == data || s.WarehouseId == data);
if (supply == null)
{
throw new ElementNotFoundException($"No supply found for data: {data}");
}
return supply;
}
public void InsertSupply(SupplyDataModel supplyDataModel)
{
logger.LogInformation("Inserting new supply: {@Supply}", supplyDataModel);
if (supplyDataModel == null)
{
throw new ArgumentNullException(nameof(supplyDataModel));
}
supplyDataModel.Validate();
var existingSupply = supplyStorageContract.GetElementById(supplyDataModel.Id);
if (existingSupply != null)
{
throw new ElementExistsException($"Supply", supplyDataModel.Id);
}
try
{
supplyStorageContract.AddElement(supplyDataModel);
logger.LogInformation("Supply {SupplyId} inserted successfully", supplyDataModel.Id);
}
catch (Exception ex)
{
throw new StorageException(ex);
}
}
public void DeleteSupply(string id)
{
logger.LogInformation("Deleting supply with ID: {SupplyId}", id);
if (string.IsNullOrWhiteSpace(id))
{
throw new ArgumentNullException(nameof(id));
}
if (!Guid.TryParse(id, out _))
{
throw new ValidationException("Invalid supply ID format.");
}
var existingSupply = supplyStorageContract.GetElementById(id);
if (existingSupply == null)
{
throw new ElementNotFoundException($"Supply with ID {id} not found.");
}
try
{
supplyStorageContract.DelElement(id);
logger.LogInformation("Supply {SupplyId} deleted successfully", id);
}
catch (Exception ex)
{
throw new StorageException(ex);
}
}
}

View File

@@ -0,0 +1,168 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TwoFromTheCasketContracts.BusinessLogicsContracts;
using TwoFromTheCasketContracts.DataModels;
using TwoFromTheCasketContracts.Exceptions;
using TwoFromTheCasketContracts.Extensions;
using TwoFromTheCasketContracts.StorageContracts;
namespace TwoFromTheCasketBusinessLogic.Implementation;
internal class WarehouseBusinessLogicContract(IWarehouseStorageContract _warehouseStorageContract, ILogger _logger) : IWarehouseBuisnessLogicContract
{
private IWarehouseStorageContract warehouseStorageContract = _warehouseStorageContract;
private ILogger logger = _logger;
public WarehouseDataModel GetWarehouseByData(string warehouseId)
{
logger.LogInformation("Retrieving warehouse data for ID: {WarehouseId}", warehouseId);
if (string.IsNullOrWhiteSpace(warehouseId))
throw new ArgumentNullException(nameof(warehouseId));
if (!warehouseId.IsGuid())
throw new ValidationException($"Invalid warehouse ID format: {warehouseId}");
var warehouse = warehouseStorageContract.GetElementById(warehouseId);
if (warehouse == null)
throw new ElementNotFoundException($"Warehouse with ID {warehouseId} not found.");
logger.LogInformation("Warehouse found: {WarehouseName}, Location: {WarehouseLocation}",
warehouse.Name, warehouse.Location);
return warehouse;
}
public List<WarehouseDataModel> GetWarehousesByLocation(string warehouseLocation)
{
logger.LogInformation("Retrieving warehouses at location: {WarehouseLocation}", warehouseLocation);
if (string.IsNullOrWhiteSpace(warehouseLocation))
throw new ArgumentNullException(nameof(warehouseLocation));
var warehouses = warehouseStorageContract.GetListByLocation(warehouseLocation);
if (warehouses == null)
throw new NullListException();
if (warehouses.Count == 0)
throw new ElementNotFoundException($"No warehouses found at location {warehouseLocation}.");
logger.LogInformation("Found {Count} warehouses at location {WarehouseLocation}", warehouses.Count, warehouseLocation);
return warehouses;
}
public List<WarehouseDataModel> GetWarehousesByName(string warehouseName)
{
logger.LogInformation("Retrieving warehouses with name: {WarehouseName}", warehouseName);
if (string.IsNullOrWhiteSpace(warehouseName))
throw new ArgumentNullException(nameof(warehouseName));
var warehouses = warehouseStorageContract.GetListByName(warehouseName);
if (warehouses == null)
throw new NullListException();
if (warehouses.Count == 0)
throw new ElementNotFoundException($"No warehouses found with name {warehouseName}.");
logger.LogInformation("Found {Count} warehouses with name {WarehouseName}", warehouses.Count, warehouseName);
return warehouses;
}
public void InsertWarehouse(WarehouseDataModel warehouseDataModel)
{
logger.LogInformation("Inserting warehouse: {@Warehouse}", warehouseDataModel);
if (warehouseDataModel == null)
throw new ArgumentNullException(nameof(warehouseDataModel));
if (warehouseStorageContract.GetElementById(warehouseDataModel.Id) != null)
throw new ElementExistsException($"Warehouse", warehouseDataModel.Id);
if (warehouseDataModel.WorkWarehouseDataModels == null || warehouseDataModel.WorkWarehouseDataModels.Count == 0)
throw new ValidationException("The value in the workWarehouseDataModels must include at least one warehouse.");
warehouseDataModel.Validate();
try
{
warehouseStorageContract.AddElement(warehouseDataModel);
logger.LogInformation("Warehouse {WarehouseId} inserted successfully", warehouseDataModel.Id);
}
catch (Exception ex)
{
throw new StorageException(ex);
}
}
public void UpdateWarehouse(WarehouseDataModel warehouseDataModel)
{
logger.LogInformation("Updating warehouse: {@Warehouse}", warehouseDataModel);
if (warehouseDataModel == null)
throw new ArgumentNullException(nameof(warehouseDataModel));
if (!Guid.TryParse(warehouseDataModel.Id, out _))
throw new ValidationException("Invalid warehouse ID format.");
var existingWarehouse = warehouseStorageContract.GetElementById(warehouseDataModel.Id);
if (existingWarehouse == null)
throw new ElementNotFoundException($"Warehouse with ID {warehouseDataModel.Id} not found.");
if (warehouseDataModel.WorkWarehouseDataModels == null || warehouseDataModel.WorkWarehouseDataModels.Count == 0)
throw new ValidationException("The warehouse must have at least one associated product.");
warehouseDataModel.Validate();
try
{
warehouseStorageContract.UpdElement(warehouseDataModel);
logger.LogInformation("Warehouse {WarehouseId} updated successfully", warehouseDataModel.Id);
}
catch (Exception ex)
{
throw new StorageException(ex);
}
}
public void DeleteWarehouse(string warehouseId)
{
logger.LogInformation("Deleting warehouse with ID: {WarehouseId}", warehouseId);
if (string.IsNullOrWhiteSpace(warehouseId))
throw new ArgumentNullException(nameof(warehouseId));
if (!warehouseId.IsGuid())
throw new ValidationException($"Invalid warehouse ID format: {warehouseId}");
var existingWarehouse = warehouseStorageContract.GetElementById(warehouseId);
if (existingWarehouse == null)
throw new ElementNotFoundException($"Warehouse with ID {warehouseId} not found.");
try
{
warehouseStorageContract.DelElement(warehouseId);
logger.LogInformation("Warehouse with ID {WarehouseId} deleted successfully.", warehouseId);
}
catch (Exception ex)
{
throw new StorageException(ex);
}
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TwoFromTheCasketContracts.DataModels;
namespace TwoFromTheCasketContracts.BusinessLogicsContracts;
public interface ISupplyBuisnessLogicContract
{
List<SupplyDataModel> GetSuppliesByPeriod(DateTime fromDate, DateTime toDate);
List<SupplyDataModel> GetSuppliesByWarehouseId(string warehouseId);
SupplyDataModel GetSupplyByData(string data);
void InsertSupply(SupplyDataModel supplyDataModel);
void DeleteSupply(string id);
}

View File

@@ -0,0 +1,14 @@
using TwoFromTheCasketContracts.DataModels;
namespace TwoFromTheCasketContracts.BusinessLogicsContracts;
public interface IWarehouseBuisnessLogicContract
{
List<WarehouseDataModel> GetWarehousesByName(string WarehouseName);
List<WarehouseDataModel> GetWarehousesByLocation(string WarehouseLocation);
WarehouseDataModel GetWarehouseByData(string data);
void InsertWarehouse(WarehouseDataModel warehouseDataModel);
void UpdateWarehouse(WarehouseDataModel warehouseDataModel);
void DeleteWarehouse(string id);
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TwoFromTheCasketContracts.DataModels;
namespace TwoFromTheCasketContracts.StorageContracts;
public interface ISupplyStorageContract
{
List<SupplyDataModel> GetList(DateTime startDate, DateTime endDate, string? warehouseId = null);
SupplyDataModel? GetElementById(string id);
void AddElement(SupplyDataModel supplyDataModel);
void DelElement(string id);
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TwoFromTheCasketContracts.DataModels;
namespace TwoFromTheCasketContracts.StorageContracts;
public interface IWarehouseStorageContract
{
List<WarehouseDataModel> GetListByLocation(string Location);
List<WarehouseDataModel> GetListByName(string Name);
WarehouseDataModel? GetElementById(string id);
void AddElement(WarehouseDataModel warehouseDataModel);
void UpdElement(WarehouseDataModel warehouseDataModel);
void DelElement(string id);
}

View File

@@ -0,0 +1,478 @@
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using TwoFromTheCasketContracts.BusinessLogicsContracts;
using TwoFromTheCasketContracts.DataModels;
using TwoFromTheCasketContracts.Exceptions;
using TwoFromTheCasketBusinessLogic.Implementation;
using TwoFromTheCasketContracts.Enums;
using TwoFromTheCasketContracts.StorageContracts;
using Microsoft.Extensions.Logging;
namespace TwoFromTheCasketTests.BusinessLogicsContractsTests
{
[TestFixture]
public class SupplyBusinessLogicContractTests
{
private SupplyBusinessLogicContract _supplyBusinessLogic;
private Mock<ISupplyStorageContract> _supplyStorageMock;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_supplyStorageMock = new Mock<ISupplyStorageContract>();
_supplyBusinessLogic = new SupplyBusinessLogicContract(
_supplyStorageMock.Object,
new Mock<ILogger>().Object
);
}
[TearDown]
public void TearDown()
{
_supplyStorageMock.Reset();
}
[Test]
public void GetSuppliesByPeriod_ReturnListOfRecords_Test()
{
// Arrange
var fromDate = DateTime.UtcNow.AddMonths(-1);
var toDate = DateTime.UtcNow;
var warehouseId = Guid.NewGuid().ToString();
var suppliesOriginal = new List<SupplyDataModel>
{
new SupplyDataModel(Guid.NewGuid().ToString(), warehouseId, fromDate.AddDays(5),
new List<WorkSupplyDataModel>
{
new WorkSupplyDataModel(Guid.NewGuid().ToString(), warehouseId, 100)
}),
new SupplyDataModel(Guid.NewGuid().ToString(), warehouseId, fromDate.AddDays(10),
new List<WorkSupplyDataModel>
{
new WorkSupplyDataModel(Guid.NewGuid().ToString(), warehouseId, 200)
})
};
_supplyStorageMock.Setup(x => x.GetList(fromDate, toDate, null)).Returns(suppliesOriginal);
var supplies = _supplyBusinessLogic.GetSuppliesByPeriod(fromDate, toDate);
Assert.Multiple(() =>
{
Assert.That(supplies, Is.Not.Null);
Assert.That(supplies, Is.EquivalentTo(suppliesOriginal));
});
_supplyStorageMock.Verify(x => x.GetList(fromDate, toDate, null), Times.Once);
}
[Test]
public void GetSuppliesByPeriod_ReturnEmptyList_Test()
{
var fromDate = DateTime.UtcNow.AddMonths(-1);
var toDate = DateTime.UtcNow;
_supplyStorageMock
.Setup(x => x.GetList(fromDate, toDate, null))
.Returns(new List<SupplyDataModel>());
Assert.That(() => _supplyBusinessLogic.GetSuppliesByPeriod(fromDate, toDate), Throws.TypeOf<ElementNotFoundException>());
_supplyStorageMock.Verify(x => x.GetList(fromDate, toDate, null), Times.Once);
}
[Test]
public void GetSuppliesByPeriod_IncorrectDates_ThrowException_Test()
{
var date = DateTime.UtcNow;
Assert.That(() => _supplyBusinessLogic.GetSuppliesByPeriod(date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _supplyBusinessLogic.GetSuppliesByPeriod(date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_supplyStorageMock.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSuppliesByPeriod_ReturnNull_ThrowException_Test()
{
var fromDate = DateTime.UtcNow.AddMonths(-1);
var toDate = DateTime.UtcNow;
_supplyStorageMock.Setup(x => x.GetList(fromDate, toDate, null)).Returns((List<SupplyDataModel>)null);
Assert.That(() => _supplyBusinessLogic.GetSuppliesByPeriod(fromDate, toDate), Throws.TypeOf<NullListException>());
_supplyStorageMock.Verify(x => x.GetList(fromDate, toDate, null), Times.Once);
}
[Test]
public void GetSuppliesByPeriod_StorageThrowError_ThrowException_Test()
{
var fromDate = DateTime.UtcNow.AddMonths(-1);
var toDate = DateTime.UtcNow;
_supplyStorageMock.Setup(x => x.GetList(fromDate, toDate, null)).Throws(new StorageException(new InvalidOperationException()));
Assert.That(() => _supplyBusinessLogic.GetSuppliesByPeriod(fromDate, toDate), Throws.TypeOf<StorageException>());
_supplyStorageMock.Verify(x => x.GetList(fromDate, toDate, null), Times.Once);
}
[Test]
public void GetSuppliesByWarehouseId_ReturnListOfRecords_Test()
{
var warehouseId = Guid.NewGuid().ToString();
var fromDate = DateTime.UtcNow.AddMonths(-1);
var toDate = DateTime.UtcNow;
var suppliesOriginal = new List<SupplyDataModel>
{
new SupplyDataModel(Guid.NewGuid().ToString(), warehouseId, fromDate.AddDays(5),
new List<WorkSupplyDataModel>
{
new WorkSupplyDataModel(Guid.NewGuid().ToString(), warehouseId, 100)
}),
new SupplyDataModel(Guid.NewGuid().ToString(), warehouseId, fromDate.AddDays(10),
new List<WorkSupplyDataModel>
{
new WorkSupplyDataModel(Guid.NewGuid().ToString(), warehouseId, 200)
})
};
_supplyStorageMock.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), warehouseId)).Returns(suppliesOriginal);
var supplies = _supplyBusinessLogic.GetSuppliesByWarehouseId(warehouseId);
Assert.Multiple(() =>
{
Assert.That(supplies, Is.Not.Null);
Assert.That(supplies, Is.EquivalentTo(suppliesOriginal));
});
_supplyStorageMock.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), warehouseId), Times.Once);
}
[Test]
public void GetSuppliesByWarehouseId_ReturnEmptyList_Test()
{
var warehouseId = Guid.NewGuid().ToString();
_supplyStorageMock
.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), warehouseId))
.Returns(new List<SupplyDataModel>());
Assert.That(() => _supplyBusinessLogic.GetSuppliesByWarehouseId(warehouseId), Throws.TypeOf<ElementNotFoundException>());
_supplyStorageMock.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), warehouseId), Times.Once);
}
[Test]
public void GetSuppliesByWarehouseId_WarehouseIdIsNullOrEmpty_ThrowException_Test()
{
var warehouseId = string.Empty;
Assert.That(() => _supplyBusinessLogic.GetSuppliesByWarehouseId(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _supplyBusinessLogic.GetSuppliesByWarehouseId(warehouseId), Throws.TypeOf<ArgumentNullException>());
_supplyStorageMock.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSuppliesByWarehouseId_WarehouseIdIsNotGuid_ThrowException_Test()
{
var warehouseId = "invalid-guid";
Assert.That(() => _supplyBusinessLogic.GetSuppliesByWarehouseId(warehouseId), Throws.TypeOf<ValidationException>());
_supplyStorageMock.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSuppliesByWarehouseId_ReturnNull_ThrowException_Test()
{
var warehouseId = Guid.NewGuid().ToString();
_supplyStorageMock.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), warehouseId)).Returns((List<SupplyDataModel>)null);
Assert.That(() => _supplyBusinessLogic.GetSuppliesByWarehouseId(warehouseId), Throws.TypeOf<NullListException>());
_supplyStorageMock.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), warehouseId), Times.Once);
}
[Test]
public void GetSuppliesByWarehouseId_StorageThrowError_ThrowException_Test()
{
var warehouseId = Guid.NewGuid().ToString();
_supplyStorageMock.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), warehouseId))
.Throws(new StorageException(new InvalidOperationException()));
Assert.That(() => _supplyBusinessLogic.GetSuppliesByWarehouseId(warehouseId), Throws.TypeOf<StorageException>());
_supplyStorageMock.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), warehouseId), Times.Once);
}
[Test]
public void GetSupplyByData_ReturnRecord_Test()
{
var supplyId = Guid.NewGuid().ToString();
var warehouseId = Guid.NewGuid().ToString();
var supplyRecord = new SupplyDataModel(
supplyId,
warehouseId,
DateTime.UtcNow,
new List<WorkSupplyDataModel> { new WorkSupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10) });
_supplyStorageMock
.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()))
.Returns(new List<SupplyDataModel> { supplyRecord });
var result = _supplyBusinessLogic.GetSupplyByData(supplyId);
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EqualTo(supplyRecord));
_supplyStorageMock.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSupplyByData_SupplyIdIsNullOrEmpty_ThrowException_Test()
{
Assert.That(() => _supplyBusinessLogic.GetSupplyByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _supplyBusinessLogic.GetSupplyByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_supplyStorageMock.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSupplyByData_SupplyIdIsNotGuid_ThrowException_Test()
{
var invalidSupplyId = "invalid-guid";
Assert.That(() => _supplyBusinessLogic.GetSupplyByData(invalidSupplyId), Throws.TypeOf<ValidationException>());
_supplyStorageMock.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSupplyByData_StorageThrowsException_ThrowException_Test()
{
var supplyId = Guid.NewGuid().ToString();
_supplyStorageMock
.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
Assert.That(() => _supplyBusinessLogic.GetSupplyByData(supplyId), Throws.TypeOf<StorageException>());
_supplyStorageMock.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertSupply_CorrectRecord_Test()
{
var supplyId = Guid.NewGuid().ToString();
var warehouseId = Guid.NewGuid().ToString();
var supplyDate = DateTime.UtcNow.AddDays(-10);
var products = new List<WorkSupplyDataModel>
{
new WorkSupplyDataModel(Guid.NewGuid().ToString(), warehouseId, 100)
};
var supply = new SupplyDataModel(supplyId, warehouseId, supplyDate, products);
_supplyStorageMock.Setup(x => x.GetElementById(supply.Id)).Returns((SupplyDataModel)null);
_supplyStorageMock.Setup(x => x.AddElement(It.IsAny<SupplyDataModel>()));
_supplyBusinessLogic.InsertSupply(supply);
_supplyStorageMock.Verify(x => x.GetElementById(supply.Id), Times.Once);
_supplyStorageMock.Verify(x => x.AddElement(supply), Times.Once);
}
[Test]
public void InsertSupply_NullRecord_ThrowException_Test()
{
Assert.That(() => _supplyBusinessLogic.InsertSupply(null), Throws.TypeOf<ArgumentNullException>());
_supplyStorageMock.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void InsertSupply_RecordWithExistingId_ThrowException_Test()
{
var supplyId = Guid.NewGuid().ToString();
var warehouseId = Guid.NewGuid().ToString();
var supplyDate = DateTime.UtcNow.AddDays(-10);
var products = new List<WorkSupplyDataModel>
{
new WorkSupplyDataModel(Guid.NewGuid().ToString(), warehouseId, 100)
};
var supply = new SupplyDataModel(supplyId, warehouseId, supplyDate, products);
_supplyStorageMock.Setup(x => x.GetElementById(supply.Id)).Returns(supply);
Assert.That(() => _supplyBusinessLogic.InsertSupply(supply), Throws.TypeOf<ElementExistsException>());
_supplyStorageMock.Verify(x => x.GetElementById(supply.Id), Times.Once);
_supplyStorageMock.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void InsertSupply_WarehouseIdIsNullOrEmpty_ThrowException_Test()
{
var supplyId = Guid.NewGuid().ToString();
var supplyDate = DateTime.UtcNow.AddDays(-10);
var products = new List<WorkSupplyDataModel>
{
new WorkSupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 100)
};
var invalidSupply = new SupplyDataModel(supplyId, "", supplyDate, products);
Assert.That(() => _supplyBusinessLogic.InsertSupply(invalidSupply), Throws.TypeOf<ValidationException>());
_supplyStorageMock.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void InsertSupply_WarehouseIdIsNotGuid_ThrowException_Test()
{
var supplyId = Guid.NewGuid().ToString();
var invalidWarehouseId = "invalid-guid";
var supplyDate = DateTime.UtcNow.AddDays(-10);
var products = new List<WorkSupplyDataModel>
{
new WorkSupplyDataModel(Guid.NewGuid().ToString(), invalidWarehouseId, 100)
};
var invalidSupply = new SupplyDataModel(supplyId, invalidWarehouseId, supplyDate, products);
Assert.That(() => _supplyBusinessLogic.InsertSupply(invalidSupply), Throws.TypeOf<ValidationException>());
_supplyStorageMock.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void InsertSupply_StorageThrowsError_ThrowException_Test()
{
var supplyId = Guid.NewGuid().ToString();
var warehouseId = Guid.NewGuid().ToString();
var supplyDate = DateTime.UtcNow.AddDays(-10);
var products = new List<WorkSupplyDataModel>
{
new WorkSupplyDataModel(Guid.NewGuid().ToString(), warehouseId, 100)
};
var supply = new SupplyDataModel(supplyId, warehouseId, supplyDate, products);
_supplyStorageMock.Setup(x => x.GetElementById(supply.Id)).Returns((SupplyDataModel)null);
_supplyStorageMock.Setup(x => x.AddElement(It.IsAny<SupplyDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
Assert.That(() => _supplyBusinessLogic.InsertSupply(supply), Throws.TypeOf<StorageException>());
_supplyStorageMock.Verify(x => x.GetElementById(supply.Id), Times.Once);
_supplyStorageMock.Verify(x => x.AddElement(supply), Times.Once);
}
[Test]
public void DeleteSupply_CorrectRecord_Test()
{
var supplyId = Guid.NewGuid().ToString();
var warehouseId = Guid.NewGuid().ToString();
var products = new List<WorkSupplyDataModel>
{
new WorkSupplyDataModel(Guid.NewGuid().ToString(), supplyId, 100)
};
_supplyStorageMock.Setup(x => x.GetElementById(supplyId))
.Returns(new SupplyDataModel(supplyId, warehouseId, DateTime.UtcNow, products));
_supplyBusinessLogic.DeleteSupply(supplyId);
_supplyStorageMock.Verify(x => x.DelElement(supplyId), Times.Once);
}
[Test]
public void DeleteSupply_NullOrEmptyId_ThrowException_Test()
{
Assert.That(() => _supplyBusinessLogic.DeleteSupply(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _supplyBusinessLogic.DeleteSupply(""), Throws.TypeOf<ArgumentNullException>());
_supplyStorageMock.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteSupply_InvalidSupplyId_ThrowException_Test()
{
var supplyId = "invalid-guid";
Assert.That(() => _supplyBusinessLogic.DeleteSupply(supplyId), Throws.TypeOf<ValidationException>());
_supplyStorageMock.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteSupply_NotFound_ThrowException_Test()
{
var supplyId = Guid.NewGuid().ToString();
_supplyStorageMock.Setup(x => x.GetElementById(supplyId)).Returns((SupplyDataModel)null);
Assert.That(() => _supplyBusinessLogic.DeleteSupply(supplyId), Throws.TypeOf<ElementNotFoundException>());
_supplyStorageMock.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteSupply_StorageThrowsError_ThrowException_Test()
{
var supplyId = Guid.NewGuid().ToString();
var warehouseId = Guid.NewGuid().ToString();
var products = new List<WorkSupplyDataModel>
{
new WorkSupplyDataModel(Guid.NewGuid().ToString(), supplyId, 100)
};
_supplyStorageMock.Setup(x => x.GetElementById(supplyId))
.Returns(new SupplyDataModel(supplyId, warehouseId, DateTime.UtcNow, products));
_supplyStorageMock.Setup(x => x.DelElement(supplyId))
.Throws(new StorageException(new InvalidOperationException()));
Assert.That(() => _supplyBusinessLogic.DeleteSupply(supplyId), Throws.TypeOf<StorageException>());
_supplyStorageMock.Verify(x => x.DelElement(supplyId), Times.Once);
}
}
}

View File

@@ -0,0 +1,444 @@
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using TwoFromTheCasketContracts.BusinessLogicsContracts;
using TwoFromTheCasketContracts.DataModels;
using TwoFromTheCasketContracts.Exceptions;
using TwoFromTheCasketBusinessLogic.Implementation;
using TwoFromTheCasketContracts.Enums;
using TwoFromTheCasketContracts.StorageContracts;
using Microsoft.Extensions.Logging;
namespace TwoFromTheCasketTests.BusinessLogicsContractsTests
{
[TestFixture]
public class WarehouseBusinessLogicContractTests
{
private WarehouseBusinessLogicContract _warehouseBusinessLogic;
private Mock<IWarehouseStorageContract> _warehouseStorageMock;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_warehouseStorageMock = new Mock<IWarehouseStorageContract>();
_warehouseBusinessLogic = new WarehouseBusinessLogicContract(_warehouseStorageMock.Object, new Mock<ILogger>().Object);
}
[TearDown]
public void TearDown()
{
_warehouseStorageMock.Reset();
}
[Test]
public void GetWarehouseByData_CorrectId_ReturnsRecord_Test()
{
var warehouseId = Guid.NewGuid().ToString();
var warehouse = new WarehouseDataModel(warehouseId, "Main Warehouse", "New York", []);
_warehouseStorageMock.Setup(x => x.GetElementById(warehouseId))
.Returns(warehouse);
var result = _warehouseBusinessLogic.GetWarehouseByData(warehouseId);
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EqualTo(warehouse));
_warehouseStorageMock.Verify(x => x.GetElementById(warehouseId), Times.Once);
}
[Test]
public void GetWarehouseByData_NullOrEmptyId_ThrowException_Test()
{
Assert.That(() => _warehouseBusinessLogic.GetWarehouseByData(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _warehouseBusinessLogic.GetWarehouseByData(""),
Throws.TypeOf<ArgumentNullException>());
_warehouseStorageMock.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetWarehouseByData_InvalidIdFormat_ThrowException_Test()
{
var invalidId = "invalid-guid";
Assert.That(() => _warehouseBusinessLogic.GetWarehouseByData(invalidId),
Throws.TypeOf<ValidationException>());
_warehouseStorageMock.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetWarehouseByData_NotFound_ThrowException_Test()
{
var warehouseId = Guid.NewGuid().ToString();
_warehouseStorageMock.Setup(x => x.GetElementById(warehouseId))
.Returns((WarehouseDataModel)null);
Assert.That(() => _warehouseBusinessLogic.GetWarehouseByData(warehouseId),
Throws.TypeOf<ElementNotFoundException>());
_warehouseStorageMock.Verify(x => x.GetElementById(warehouseId), Times.Once);
}
[Test]
public void GetWarehouseByData_StorageThrowsException_ThrowException_Test()
{
var warehouseId = Guid.NewGuid().ToString();
_warehouseStorageMock.Setup(x => x.GetElementById(warehouseId))
.Throws(new StorageException(new InvalidOperationException()));
Assert.That(() => _warehouseBusinessLogic.GetWarehouseByData(warehouseId),
Throws.TypeOf<StorageException>());
_warehouseStorageMock.Verify(x => x.GetElementById(warehouseId), Times.Once);
}
[Test]
public void GetWarehousesByLocation_CorrectLocation_ReturnsList_Test()
{
var location = "New York";
var warehouses = new List<WarehouseDataModel>
{
new(Guid.NewGuid().ToString(), "Warehouse A", location, []),
new(Guid.NewGuid().ToString(), "Warehouse B", location, [])
};
_warehouseStorageMock.Setup(x => x.GetListByLocation(location)).Returns(warehouses);
var result = _warehouseBusinessLogic.GetWarehousesByLocation(location);
Assert.Multiple(() =>
{
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EquivalentTo(warehouses));
});
_warehouseStorageMock.Verify(x => x.GetListByLocation(location), Times.Once);
}
[Test]
public void GetWarehousesByLocation_LocationIsNullOrEmpty_ThrowException_Test()
{
Assert.That(() => _warehouseBusinessLogic.GetWarehousesByLocation(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _warehouseBusinessLogic.GetWarehousesByLocation(""),
Throws.TypeOf<ArgumentNullException>());
_warehouseStorageMock.Verify(x => x.GetListByLocation(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetWarehousesByLocation_NoWarehousesFound_ThrowException_Test()
{
var location = "Unknown City";
_warehouseStorageMock.Setup(x => x.GetListByLocation(location))
.Returns(new List<WarehouseDataModel>());
Assert.That(() => _warehouseBusinessLogic.GetWarehousesByLocation(location),
Throws.TypeOf<ElementNotFoundException>());
_warehouseStorageMock.Verify(x => x.GetListByLocation(location), Times.Once);
}
[Test]
public void GetWarehousesByLocation_StorageReturnsNull_ThrowException_Test()
{
var location = "Los Angeles";
_warehouseStorageMock.Setup(x => x.GetListByLocation(location))
.Returns((List<WarehouseDataModel>)null);
Assert.That(() => _warehouseBusinessLogic.GetWarehousesByLocation(location),
Throws.TypeOf<NullListException>());
_warehouseStorageMock.Verify(x => x.GetListByLocation(location), Times.Once);
}
[Test]
public void GetWarehousesByLocation_StorageThrowsException_ThrowException_Test()
{
var location = "Chicago";
_warehouseStorageMock.Setup(x => x.GetListByLocation(location))
.Throws(new StorageException(new InvalidOperationException()));
Assert.That(() => _warehouseBusinessLogic.GetWarehousesByLocation(location),
Throws.TypeOf<StorageException>());
_warehouseStorageMock.Verify(x => x.GetListByLocation(location), Times.Once);
}
[Test]
public void GetWarehousesByName_CorrectName_ReturnsList_Test()
{
var warehouseName = "Central Warehouse";
var warehouses = new List<WarehouseDataModel>
{
new(Guid.NewGuid().ToString(), warehouseName, "New York", []),
new(Guid.NewGuid().ToString(), warehouseName, "Los Angeles", [])
};
_warehouseStorageMock.Setup(x => x.GetListByName(warehouseName)).Returns(warehouses);
var result = _warehouseBusinessLogic.GetWarehousesByName(warehouseName);
Assert.Multiple(() =>
{
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EquivalentTo(warehouses));
});
_warehouseStorageMock.Verify(x => x.GetListByName(warehouseName), Times.Once);
}
[Test]
public void GetWarehousesByName_NameIsNullOrEmpty_ThrowException_Test()
{
Assert.That(() => _warehouseBusinessLogic.GetWarehousesByName(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _warehouseBusinessLogic.GetWarehousesByName(""),
Throws.TypeOf<ArgumentNullException>());
_warehouseStorageMock.Verify(x => x.GetListByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetWarehousesByName_NoWarehousesFound_ThrowException_Test()
{
var warehouseName = "Unknown Warehouse";
_warehouseStorageMock.Setup(x => x.GetListByName(warehouseName))
.Returns(new List<WarehouseDataModel>());
Assert.That(() => _warehouseBusinessLogic.GetWarehousesByName(warehouseName),
Throws.TypeOf<ElementNotFoundException>());
_warehouseStorageMock.Verify(x => x.GetListByName(warehouseName), Times.Once);
}
[Test]
public void GetWarehousesByName_StorageReturnsNull_ThrowException_Test()
{
var warehouseName = "Central Warehouse";
_warehouseStorageMock.Setup(x => x.GetListByName(warehouseName))
.Returns((List<WarehouseDataModel>)null);
Assert.That(() => _warehouseBusinessLogic.GetWarehousesByName(warehouseName),
Throws.TypeOf<NullListException>());
_warehouseStorageMock.Verify(x => x.GetListByName(warehouseName), Times.Once);
}
[Test]
public void GetWarehousesByName_StorageThrowsException_ThrowException_Test()
{
var warehouseName = "East Warehouse";
_warehouseStorageMock.Setup(x => x.GetListByName(warehouseName))
.Throws(new StorageException(new InvalidOperationException()));
Assert.That(() => _warehouseBusinessLogic.GetWarehousesByName(warehouseName),
Throws.TypeOf<StorageException>());
_warehouseStorageMock.Verify(x => x.GetListByName(warehouseName), Times.Once);
}
[Test]
public void InsertWarehouse_CorrectRecord_Test()
{
var warehouseId = Guid.NewGuid().ToString();
var workWarehouseDataModels = new List<WorkWarehouseDataModel>
{
new WorkWarehouseDataModel(Guid.NewGuid().ToString(), warehouseId, 10)
};
var warehouse = new WarehouseDataModel(warehouseId, "Main Warehouse", "New York", workWarehouseDataModels);
_warehouseStorageMock.Setup(x => x.GetElementById(warehouseId)).Returns((WarehouseDataModel?)null);
_warehouseStorageMock.Setup(x => x.AddElement(warehouse));
_warehouseBusinessLogic.InsertWarehouse(warehouse);
_warehouseStorageMock.Verify(x => x.AddElement(warehouse), Times.Once);
}
[Test]
public void InsertWarehouse_NullRecord_ThrowException_Test()
{
Assert.That(() => _warehouseBusinessLogic.InsertWarehouse(null),
Throws.TypeOf<ArgumentNullException>());
_warehouseStorageMock.Verify(x => x.AddElement(It.IsAny<WarehouseDataModel>()), Times.Never);
}
[Test]
public void InsertWarehouse_StorageThrowsException_ThrowException_Test()
{
var warehouseId = Guid.NewGuid().ToString();
var workWarehouseDataModels = new List<WorkWarehouseDataModel>
{
new WorkWarehouseDataModel(Guid.NewGuid().ToString(), warehouseId, 10)
};
var warehouse = new WarehouseDataModel(warehouseId, "Warehouse A", "Los Angeles", workWarehouseDataModels);
_warehouseStorageMock
.Setup(x => x.AddElement(It.IsAny<WarehouseDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
Assert.That(() => _warehouseBusinessLogic.InsertWarehouse(warehouse), Throws.TypeOf<StorageException>());
_warehouseStorageMock.Verify(x => x.AddElement(It.IsAny<WarehouseDataModel>()), Times.Once);
}
[Test]
public void UpdateWarehouse_CorrectRecord_Test()
{
var warehouseId = Guid.NewGuid().ToString();
var workWarehouseDataModels = new List<WorkWarehouseDataModel>
{
new WorkWarehouseDataModel(Guid.NewGuid().ToString(), warehouseId, 10)
};
var warehouse = new WarehouseDataModel(warehouseId, "Updated Warehouse", "New York", workWarehouseDataModels);
_warehouseStorageMock
.Setup(x => x.GetElementById(warehouseId))
.Returns(warehouse);
_warehouseStorageMock
.Setup(x => x.UpdElement(It.IsAny<WarehouseDataModel>()))
.Verifiable();
_warehouseBusinessLogic.UpdateWarehouse(warehouse);
_warehouseStorageMock.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Once);
}
[Test]
public void UpdateWarehouse_NullRecord_ThrowException_Test()
{
Assert.That(() => _warehouseBusinessLogic.UpdateWarehouse(null),
Throws.TypeOf<ArgumentNullException>());
_warehouseStorageMock.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Never);
}
[Test]
public void UpdateWarehouse_WarehouseNotFound_ThrowException_Test()
{
var warehouseId = Guid.NewGuid().ToString();
var updatedWarehouse = new WarehouseDataModel(warehouseId, "Updated Warehouse", "San Francisco", []);
_warehouseStorageMock.Setup(x => x.GetElementById(warehouseId)).Returns((WarehouseDataModel)null);
Assert.That(() => _warehouseBusinessLogic.UpdateWarehouse(updatedWarehouse),
Throws.TypeOf<ElementNotFoundException>());
_warehouseStorageMock.Verify(x => x.GetElementById(warehouseId), Times.Once);
_warehouseStorageMock.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Never);
}
[Test]
public void UpdateWarehouse_StorageThrowsException_ThrowException_Test()
{
var warehouseId = Guid.NewGuid().ToString();
var workWarehouseDataModels = new List<WorkWarehouseDataModel>
{
new WorkWarehouseDataModel(Guid.NewGuid().ToString(), warehouseId, 10)
};
var updatedWarehouse = new WarehouseDataModel(warehouseId, "Updated Warehouse", "New Location", workWarehouseDataModels);
_warehouseStorageMock
.Setup(x => x.GetElementById(warehouseId))
.Returns(updatedWarehouse);
_warehouseStorageMock
.Setup(x => x.UpdElement(It.IsAny<WarehouseDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
Assert.That(() => _warehouseBusinessLogic.UpdateWarehouse(updatedWarehouse), Throws.TypeOf<StorageException>());
_warehouseStorageMock.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Once);
}
[Test]
public void DeleteWarehouse_CorrectRecord_Test()
{
var warehouseId = Guid.NewGuid().ToString();
var existingWarehouse = new WarehouseDataModel(warehouseId, "Warehouse 1", "New York", []);
_warehouseStorageMock.Setup(x => x.GetElementById(warehouseId)).Returns(existingWarehouse);
_warehouseStorageMock.Setup(x => x.DelElement(warehouseId));
_warehouseBusinessLogic.DeleteWarehouse(warehouseId);
_warehouseStorageMock.Verify(x => x.GetElementById(warehouseId), Times.Once);
_warehouseStorageMock.Verify(x => x.DelElement(warehouseId), Times.Once);
}
[Test]
public void DeleteWarehouse_IdIsNullOrEmpty_ThrowException_Test()
{
Assert.That(() => _warehouseBusinessLogic.DeleteWarehouse(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _warehouseBusinessLogic.DeleteWarehouse(string.Empty), Throws.TypeOf<ArgumentNullException>());
_warehouseStorageMock.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteWarehouse_IdIsNotGuid_ThrowException_Test()
{
var invalidId = "invalid-id";
Assert.That(() => _warehouseBusinessLogic.DeleteWarehouse(invalidId), Throws.TypeOf<ValidationException>());
_warehouseStorageMock.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteWarehouse_WarehouseNotFound_ThrowException_Test()
{
var warehouseId = Guid.NewGuid().ToString();
_warehouseStorageMock.Setup(x => x.GetElementById(warehouseId)).Returns((WarehouseDataModel)null);
Assert.That(() => _warehouseBusinessLogic.DeleteWarehouse(warehouseId), Throws.TypeOf<ElementNotFoundException>());
_warehouseStorageMock.Verify(x => x.GetElementById(warehouseId), Times.Once);
_warehouseStorageMock.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteWarehouse_StorageThrowsError_ThrowException_Test()
{
var warehouseId = Guid.NewGuid().ToString();
var existingWarehouse = new WarehouseDataModel(warehouseId, "Warehouse 1", "New York", []);
_warehouseStorageMock.Setup(x => x.GetElementById(warehouseId)).Returns(existingWarehouse);
_warehouseStorageMock.Setup(x => x.DelElement(warehouseId)).Throws(new StorageException(new InvalidOperationException()));
Assert.That(() => _warehouseBusinessLogic.DeleteWarehouse(warehouseId), Throws.TypeOf<StorageException>());
_warehouseStorageMock.Verify(x => x.GetElementById(warehouseId), Times.Once);
_warehouseStorageMock.Verify(x => x.DelElement(warehouseId), Times.Once);
}
}
}