Compare commits
8 Commits
LabWork05_
...
LabWork03_
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0bd6c0910c | ||
|
|
b9f01be873 | ||
|
|
12844dc03b | ||
|
|
6d0e92cd35 | ||
|
|
ecb621bbbc | ||
|
|
814b878d67 | ||
|
|
1b10b08b94 | ||
|
|
21e7134953 |
@@ -8,9 +8,10 @@ using System.Text.Json;
|
||||
|
||||
namespace FurnitureAssemblyBusinessLogic.Implementations;
|
||||
|
||||
internal class FurnitureBusinessLogicContract(IFurnitureStorageContract furnitureStorageContract, ILogger logger) : IFurnitureBusinessLogicContract
|
||||
internal class FurnitureBusinessLogicContract(IFurnitureStorageContract furnitureStorageContract, IWarehouseStorageContract warehouseStorageContract, ILogger logger) : IFurnitureBusinessLogicContract
|
||||
{
|
||||
IFurnitureStorageContract _furnitureStorageContract = furnitureStorageContract;
|
||||
IWarehouseStorageContract _warehouseStorageContract = warehouseStorageContract;
|
||||
private readonly ILogger _logger = logger;
|
||||
|
||||
public List<FurnitureDataModel> GetAllFurnitures()
|
||||
@@ -38,6 +39,10 @@ internal class FurnitureBusinessLogicContract(IFurnitureStorageContract furnitur
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(furnitureDataModel));
|
||||
ArgumentNullException.ThrowIfNull(furnitureDataModel);
|
||||
furnitureDataModel.Validate();
|
||||
if (!_warehouseStorageContract.CheckComponents(furnitureDataModel))
|
||||
{
|
||||
throw new InsufficientException("Dont have component in warehouse");
|
||||
}
|
||||
_furnitureStorageContract.AddElement(furnitureDataModel);
|
||||
}
|
||||
|
||||
@@ -46,6 +51,10 @@ internal class FurnitureBusinessLogicContract(IFurnitureStorageContract furnitur
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(furnitureDataModel));
|
||||
ArgumentNullException.ThrowIfNull(furnitureDataModel);
|
||||
furnitureDataModel.Validate();
|
||||
if (!_warehouseStorageContract.CheckComponents(furnitureDataModel))
|
||||
{
|
||||
throw new InsufficientException("Dont have component in warehouse");
|
||||
}
|
||||
_furnitureStorageContract.UpdElement(furnitureDataModel);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContracts;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.Extentions;
|
||||
using FurnitureAssemblyContracts.StoragesContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace FurnitureAssemblyBusinessLogic.Implementations;
|
||||
|
||||
public class SuppliesBusinessLogicContract(ISuppliesStorageContract suppliesStorageContract, ILogger logger) : ISuppliesBusinessLogicContract
|
||||
{
|
||||
private readonly ISuppliesStorageContract _suppliesStorageContract = suppliesStorageContract;
|
||||
private readonly ILogger _logger = logger;
|
||||
|
||||
public List<SuppliesDataModel> GetAllComponents()
|
||||
{
|
||||
_logger.LogInformation("GetAllComponents");
|
||||
return _suppliesStorageContract.GetList() ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public SuppliesDataModel GetComponentByData(string data)
|
||||
{
|
||||
_logger.LogInformation("Get element by data: {data}", data);
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
if (!data.IsGuid())
|
||||
{
|
||||
throw new ElementNotFoundException(data);
|
||||
}
|
||||
return _suppliesStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
|
||||
public void InsertComponent(SuppliesDataModel suppliesDataModel)
|
||||
{
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(suppliesDataModel));
|
||||
ArgumentNullException.ThrowIfNull(suppliesDataModel);
|
||||
suppliesDataModel.Validate();
|
||||
_suppliesStorageContract.AddElement(suppliesDataModel);
|
||||
}
|
||||
|
||||
public void UpdateComponent(SuppliesDataModel suppliesDataModel)
|
||||
{
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(suppliesDataModel));
|
||||
ArgumentNullException.ThrowIfNull(suppliesDataModel);
|
||||
suppliesDataModel.Validate();
|
||||
_suppliesStorageContract.UpdElement(suppliesDataModel);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContracts;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.Extentions;
|
||||
using FurnitureAssemblyContracts.StoragesContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace FurnitureAssemblyBusinessLogic.Implementations;
|
||||
|
||||
public class WarehouseBusinessLogicContract(IWarehouseStorageContract warehouseStorageContract, ILogger logger) : IWarehouseBusinessLogicContract
|
||||
{
|
||||
private readonly IWarehouseStorageContract _warehouseStorageContract = warehouseStorageContract;
|
||||
private readonly ILogger _logger = logger;
|
||||
public List<WarehouseDataModel> GetAllComponents()
|
||||
{
|
||||
_logger.LogInformation("GetAllComponents");
|
||||
return _warehouseStorageContract.GetList() ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public WarehouseDataModel GetComponentByData(string data)
|
||||
{
|
||||
_logger.LogInformation("Get element by data: {data}", data);
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
if (!data.IsGuid())
|
||||
{
|
||||
throw new ElementNotFoundException(data);
|
||||
}
|
||||
return _warehouseStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
|
||||
public void InsertComponent(WarehouseDataModel warehouseDataModel)
|
||||
{
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(warehouseDataModel));
|
||||
ArgumentNullException.ThrowIfNull(warehouseDataModel);
|
||||
warehouseDataModel.Validate();
|
||||
_warehouseStorageContract.AddElement(warehouseDataModel);
|
||||
}
|
||||
|
||||
public void UpdateComponent(WarehouseDataModel warehouseDataModel)
|
||||
{
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(warehouseDataModel));
|
||||
ArgumentNullException.ThrowIfNull(warehouseDataModel);
|
||||
warehouseDataModel.Validate();
|
||||
_warehouseStorageContract.UpdElement(warehouseDataModel);
|
||||
}
|
||||
|
||||
public void DeleteComponent(string id)
|
||||
{
|
||||
logger.LogInformation("Delete by id: {id}", id);
|
||||
if (id.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
}
|
||||
if (!id.IsGuid())
|
||||
{
|
||||
throw new ValidationException("Id is not a unique identifier");
|
||||
}
|
||||
_warehouseStorageContract.DelElement(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
|
||||
namespace FurnitureAssemblyContracts.BusinessLogicsContracts;
|
||||
|
||||
public interface ISuppliesBusinessLogicContract
|
||||
{
|
||||
List<SuppliesDataModel> GetAllComponents();
|
||||
SuppliesDataModel GetComponentByData(string data);
|
||||
void InsertComponent(SuppliesDataModel componentDataModel);
|
||||
void UpdateComponent(SuppliesDataModel componentDataModel);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
|
||||
namespace FurnitureAssemblyContracts.BusinessLogicsContracts;
|
||||
|
||||
public interface IWarehouseBusinessLogicContract
|
||||
{
|
||||
List<WarehouseDataModel> GetAllComponents();
|
||||
WarehouseDataModel GetComponentByData(string data);
|
||||
void InsertComponent(WarehouseDataModel warehouseDataModel);
|
||||
void UpdateComponent(WarehouseDataModel warehouseDataModel);
|
||||
void DeleteComponent(string id);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.Extentions;
|
||||
using FurnitureAssemblyContracts.Infrastructure;
|
||||
|
||||
namespace FurnitureAssemblyContracts.DataModels;
|
||||
|
||||
public class ComponentSuppliesDataModel(string suppliesId, string componentId, int count) : IValidation
|
||||
{
|
||||
public string SuppliesId { get; private set; } = suppliesId;
|
||||
public string ComponentId { get; private set; } = componentId;
|
||||
public int Count { get; private set; } = count;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (SuppliesId.IsEmpty())
|
||||
throw new ValidationException("Field FurnitureId is empty");
|
||||
if (!SuppliesId.IsGuid())
|
||||
throw new ValidationException("The value in the field FurnitureId is not a unique identifier");
|
||||
if (ComponentId.IsEmpty())
|
||||
throw new ValidationException("Field ComponentId is empty");
|
||||
if (!ComponentId.IsGuid())
|
||||
throw new ValidationException("The value in the field BlandId is not a unique identifier");
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.Extentions;
|
||||
using FurnitureAssemblyContracts.Infrastructure;
|
||||
|
||||
namespace FurnitureAssemblyContracts.DataModels;
|
||||
|
||||
public class ComponentWarehouseDataModel(string warehouseId, string componentId, int count) : IValidation
|
||||
{
|
||||
public string WarehouseId { get; private set; } = warehouseId;
|
||||
public string ComponentId { get; private set; } = componentId;
|
||||
public int Count { get; private set; } = count;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (WarehouseId.IsEmpty())
|
||||
throw new ValidationException("Field WarehouseId is empty");
|
||||
if (!WarehouseId.IsGuid())
|
||||
throw new ValidationException("The value in the field WarehouseId is not a unique identifier");
|
||||
if (ComponentId.IsEmpty())
|
||||
throw new ValidationException("Field ComponentId is empty");
|
||||
if (!ComponentId.IsGuid())
|
||||
throw new ValidationException("The value in the field ComponentId is not a unique identifier");
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.Extentions;
|
||||
using FurnitureAssemblyContracts.Infrastructure;
|
||||
|
||||
namespace FurnitureAssemblyContracts.DataModels;
|
||||
|
||||
public class SuppliesDataModel(string id, ComponentType type, DateTime productuionDate, int count, List<ComponentSuppliesDataModel> components) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public ComponentType Type { get; private set; } = type;
|
||||
public DateTime ProductuionDate { get; private set; } = productuionDate;
|
||||
public int Count { get; private set; } = count;
|
||||
public List<ComponentSuppliesDataModel> Components { get; private set; } = components;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||
if (Type == ComponentType.None)
|
||||
throw new ValidationException("Field Type is empty");
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
if ((Components?.Count ?? 0) == 0)
|
||||
throw new ValidationException("The component must include supplies");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.Extentions;
|
||||
using FurnitureAssemblyContracts.Infrastructure;
|
||||
|
||||
namespace FurnitureAssemblyContracts.DataModels;
|
||||
|
||||
public class WarehouseDataModel(string id, ComponentType type, int count, List<ComponentWarehouseDataModel> components) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public ComponentType Type { get; private set; } = type;
|
||||
public int Count { get; private set; } = count;
|
||||
public List<ComponentWarehouseDataModel> Components { get; private set; } = components;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||
if (Type == ComponentType.None)
|
||||
throw new ValidationException("Field Type is empty");
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
if ((Components?.Count ?? 0) == 0)
|
||||
throw new ValidationException("The component must include supplies");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace FurnitureAssemblyContracts.Exceptions;
|
||||
|
||||
public class InsufficientException(string message) : Exception(message)
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
|
||||
namespace FurnitureAssemblyContracts.StoragesContracts;
|
||||
|
||||
public interface ISuppliesStorageContract
|
||||
{
|
||||
List<SuppliesDataModel> GetList(DateTime? startDate = null);
|
||||
SuppliesDataModel GetElementById(string id);
|
||||
void AddElement(SuppliesDataModel suppliesDataModel);
|
||||
void UpdElement(SuppliesDataModel suppliesDataModel);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
|
||||
namespace FurnitureAssemblyContracts.StoragesContracts;
|
||||
|
||||
public interface IWarehouseStorageContract
|
||||
{
|
||||
List<WarehouseDataModel> GetList();
|
||||
WarehouseDataModel GetElementById(string id);
|
||||
void AddElement(WarehouseDataModel warehouseDataModel);
|
||||
void UpdElement(WarehouseDataModel warehouseDataModel);
|
||||
void DelElement(string id);
|
||||
bool CheckComponents(FurnitureDataModel furnitureDataModel);
|
||||
}
|
||||
@@ -33,6 +33,10 @@ public class FurnitureAssemblyDbContext(IConfigurationDatabase configurationData
|
||||
.IsUnique()
|
||||
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
||||
|
||||
modelBuilder.Entity<ComponentSupplies>().HasKey(x => new { x.SuppliesId, x.ComponentId });
|
||||
|
||||
modelBuilder.Entity<ComponentWarehouse>().HasKey(x => new { x.WarehouseId, x.ComponentId });
|
||||
|
||||
modelBuilder.Entity<FurnitureComponent>().HasKey(x => new { x.FurnitureId, x.ComponentId });
|
||||
|
||||
modelBuilder.Entity<FurnitureComponent>()
|
||||
@@ -49,4 +53,8 @@ public class FurnitureAssemblyDbContext(IConfigurationDatabase configurationData
|
||||
public DbSet<Furniture> Furnitures { get; set; }
|
||||
public DbSet<FurnitureComponent> FurnitureComponents { get; set; }
|
||||
public DbSet<Worker> Workers { get; set; }
|
||||
public DbSet<Supplies> Supplieses { get; set; }
|
||||
public DbSet<Warehouse> Warehouses { get; set; }
|
||||
public DbSet<ComponentSupplies> ComponentSupplieses { get; set; }
|
||||
public DbSet<ComponentWarehouse> ComponentWarehouses { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
using AutoMapper;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.StoragesContracts;
|
||||
using FurnitureAssemblyDatebase.Models;
|
||||
namespace FurnitureAssemblyDatebase.Implementations;
|
||||
|
||||
public class SuppliesStorageContract : ISuppliesStorageContract
|
||||
{
|
||||
private readonly FurnitureAssemblyDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public SuppliesStorageContract(FurnitureAssemblyDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.AddMaps(typeof(Supplies));
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<SuppliesDataModel> GetList(DateTime? startDate = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Supplieses.Select(x => _mapper.Map<SuppliesDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public SuppliesDataModel GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<SuppliesDataModel>(GetSuppliesById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(SuppliesDataModel suppliesDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Supplieses.Add(_mapper.Map<Supplies>(suppliesDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(SuppliesDataModel suppliesDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetSuppliesById(suppliesDataModel.Id) ?? throw new ElementNotFoundException(suppliesDataModel.Id);
|
||||
_dbContext.Supplieses.Update(_mapper.Map(suppliesDataModel, element));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Supplies? GetSuppliesById(string id) => _dbContext.Supplieses.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
using AutoMapper;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.StoragesContracts;
|
||||
using FurnitureAssemblyDatebase.Models;
|
||||
|
||||
namespace FurnitureAssemblyDatebase.Implementations;
|
||||
|
||||
public class WarehouseStorageContract : IWarehouseStorageContract
|
||||
{
|
||||
private readonly FurnitureAssemblyDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public WarehouseStorageContract(FurnitureAssemblyDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.AddMaps(typeof(Warehouse));
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<WarehouseDataModel> GetList()
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Warehouses.Select(x => _mapper.Map<WarehouseDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public WarehouseDataModel GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<WarehouseDataModel>(GetWarehouseById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(WarehouseDataModel warehouseDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Warehouses.Add(_mapper.Map<Warehouse>(warehouseDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(WarehouseDataModel warehouseDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetWarehouseById(warehouseDataModel.Id) ?? throw new ElementNotFoundException(warehouseDataModel.Id);
|
||||
_dbContext.Warehouses.Update(_mapper.Map(warehouseDataModel, element));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetWarehouseById(id) ?? throw new ElementNotFoundException(id);
|
||||
_dbContext.Warehouses.Remove(element);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public bool CheckComponents(FurnitureDataModel furnitureDataModel)
|
||||
{
|
||||
using (var transaction = _dbContext.Database.BeginTransaction())
|
||||
{
|
||||
foreach (FurnitureComponentDataModel furniture_component in furnitureDataModel.Components)
|
||||
{
|
||||
var component = _dbContext.Components.FirstOrDefault(x => x.Id == furniture_component.ComponentId);
|
||||
var warehouse = _dbContext.Warehouses.FirstOrDefault(x => x.Type == component.Type && x.Count >= furniture_component.Count);
|
||||
|
||||
if (warehouse == null)
|
||||
{
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
if (warehouse.Count - furniture_component.Count == 0)
|
||||
{
|
||||
DelElement(warehouse.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
warehouse.Count -= furniture_component.Count;
|
||||
}
|
||||
}
|
||||
transaction.Commit();
|
||||
_dbContext.SaveChanges();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private Warehouse? GetWarehouseById(string id) => _dbContext.Warehouses.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
@@ -15,4 +15,8 @@ public class Component
|
||||
public required ComponentType Type { get; set; }
|
||||
[ForeignKey("ComponentId")]
|
||||
public List<FurnitureComponent>? FurnitureComponents { get; set; }
|
||||
[ForeignKey("SuppliesesId")]
|
||||
public List<ComponentSupplies>? ComponentSupplies { get; set; }
|
||||
[ForeignKey("WarehousesId")]
|
||||
public List<ComponentWarehouse>? ComponentWarehouse { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
using AutoMapper;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
|
||||
namespace FurnitureAssemblyDatebase.Models;
|
||||
|
||||
[AutoMap(typeof(ComponentSuppliesDataModel), ReverseMap = true)]
|
||||
public class ComponentSupplies
|
||||
{
|
||||
public required string SuppliesId { get; set; }
|
||||
public required string ComponentId { get; set; }
|
||||
public int Count { get; set; }
|
||||
public Supplies? Supplies { get; set; }
|
||||
public Component? Components { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using AutoMapper;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
|
||||
namespace FurnitureAssemblyDatebase.Models;
|
||||
|
||||
[AutoMap(typeof(ComponentWarehouseDataModel), ReverseMap = true)]
|
||||
public class ComponentWarehouse
|
||||
{
|
||||
public required string WarehouseId { get; set; }
|
||||
public required string ComponentId { get; set; }
|
||||
public int Count { get; set; }
|
||||
public Warehouse? Warehouses { get; set; }
|
||||
public Component? Components { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using AutoMapper;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace FurnitureAssemblyDatebase.Models;
|
||||
|
||||
[AutoMap(typeof(SuppliesDataModel), ReverseMap = true)]
|
||||
public class Supplies
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required ComponentType Type { get; set; }
|
||||
public DateTime ProductuionDate { get; set; }
|
||||
public required int Count { get; set; }
|
||||
|
||||
[ForeignKey("SuppliesId")]
|
||||
public List<ComponentSupplies>? Components { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using AutoMapper;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace FurnitureAssemblyDatebase.Models;
|
||||
|
||||
[AutoMap(typeof(WarehouseDataModel), ReverseMap = true)]
|
||||
public class Warehouse
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required ComponentType Type { get; set; }
|
||||
public required int Count { get; set; }
|
||||
|
||||
[ForeignKey("WarehouseId")]
|
||||
public List<ComponentWarehouse>? Components { get; set; }
|
||||
}
|
||||
@@ -11,18 +11,21 @@ namespace FurnitureAssemblyTests.BusinessLogicContractsTests;
|
||||
public class FurnitureBusinessLogicContractTests
|
||||
{
|
||||
private IFurnitureBusinessLogicContract _furnitureBusinessLogicContract;
|
||||
private Mock<IWarehouseStorageContract> _warehouseStorageContract;
|
||||
private Mock<IFurnitureStorageContract> _furnitureStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_furnitureStorageContract = new Mock<IFurnitureStorageContract>();
|
||||
_furnitureBusinessLogicContract = new FurnitureBusinessLogicContract(_furnitureStorageContract.Object, new Mock<ILogger>().Object);
|
||||
_warehouseStorageContract = new Mock<IWarehouseStorageContract>();
|
||||
_furnitureBusinessLogicContract = new FurnitureBusinessLogicContract(_furnitureStorageContract.Object, _warehouseStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_warehouseStorageContract.Reset();
|
||||
_furnitureStorageContract.Reset();
|
||||
}
|
||||
|
||||
@@ -154,6 +157,7 @@ public class FurnitureBusinessLogicContractTests
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new FurnitureDataModel(Guid.NewGuid().ToString(), "name", 10, [new FurnitureComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_warehouseStorageContract.Setup(x => x.CheckComponents(It.IsAny<FurnitureDataModel>())).Returns(true);
|
||||
_furnitureStorageContract.Setup(x => x.AddElement(It.IsAny<FurnitureDataModel>())).Callback((FurnitureDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.Name == record.Name;
|
||||
@@ -161,6 +165,7 @@ public class FurnitureBusinessLogicContractTests
|
||||
//Act
|
||||
_furnitureBusinessLogicContract.InsertFurniture(record);
|
||||
//Assert
|
||||
_warehouseStorageContract.Verify(x => x.CheckComponents(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
_furnitureStorageContract.Verify(x => x.AddElement(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
@@ -169,10 +174,12 @@ public class FurnitureBusinessLogicContractTests
|
||||
public void InsertFurnitures_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_warehouseStorageContract.Setup(x => x.CheckComponents(It.IsAny<FurnitureDataModel>())).Returns(true);
|
||||
_furnitureStorageContract.Setup(x => x.AddElement(It.IsAny<FurnitureDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() => _furnitureBusinessLogicContract.InsertFurniture(new(Guid.NewGuid().ToString(), "name", 10,
|
||||
[new FurnitureComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_warehouseStorageContract.Verify(x => x.CheckComponents(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
_furnitureStorageContract.Verify(x => x.AddElement(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
@@ -197,19 +204,34 @@ public class FurnitureBusinessLogicContractTests
|
||||
public void InsertFurnitures_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_warehouseStorageContract.Setup(x => x.CheckComponents(It.IsAny<FurnitureDataModel>())).Returns(true);
|
||||
_furnitureStorageContract.Setup(x => x.AddElement(It.IsAny<FurnitureDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _furnitureBusinessLogicContract.InsertFurniture(new(Guid.NewGuid().ToString(), "name", 10,
|
||||
[new FurnitureComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.CheckComponents(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
_furnitureStorageContract.Verify(x => x.AddElement(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertFurniture_InsufficientError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_warehouseStorageContract.Setup(x => x.CheckComponents(It.IsAny<FurnitureDataModel>())).Returns(false);
|
||||
Assert.That(() => _furnitureBusinessLogicContract.InsertFurniture(new FurnitureDataModel(Guid.NewGuid().ToString(), "name", 10,
|
||||
[new FurnitureComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<InsufficientException>());
|
||||
//Act&Assert
|
||||
_warehouseStorageContract.Verify(x => x.CheckComponents(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
_furnitureStorageContract.Verify(x => x.UpdElement(It.IsAny<FurnitureDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateFurnitures_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new FurnitureDataModel(Guid.NewGuid().ToString(), "name", 10, [new FurnitureComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_warehouseStorageContract.Setup(x => x.CheckComponents(It.IsAny<FurnitureDataModel>())).Returns(true);
|
||||
_furnitureStorageContract.Setup(x => x.UpdElement(It.IsAny<FurnitureDataModel>())).Callback((FurnitureDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.Name == record.Name;
|
||||
@@ -217,6 +239,7 @@ public class FurnitureBusinessLogicContractTests
|
||||
//Act
|
||||
_furnitureBusinessLogicContract.UpdateFurniture(record);
|
||||
//Assert
|
||||
_warehouseStorageContract.Verify(x => x.CheckComponents(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
_furnitureStorageContract.Verify(x => x.UpdElement(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
@@ -225,10 +248,12 @@ public class FurnitureBusinessLogicContractTests
|
||||
public void UpdateFurnitures_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_warehouseStorageContract.Setup(x => x.CheckComponents(It.IsAny<FurnitureDataModel>())).Returns(true);
|
||||
_furnitureStorageContract.Setup(x => x.UpdElement(It.IsAny<FurnitureDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
//Act&Assert
|
||||
Assert.That(() => _furnitureBusinessLogicContract.UpdateFurniture(new(Guid.NewGuid().ToString(), "name", 10,
|
||||
[new FurnitureComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementNotFoundException>());
|
||||
_warehouseStorageContract.Verify(x => x.CheckComponents(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
_furnitureStorageContract.Verify(x => x.UpdElement(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
@@ -236,10 +261,12 @@ public class FurnitureBusinessLogicContractTests
|
||||
public void UpdateFurnitures_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_warehouseStorageContract.Setup(x => x.CheckComponents(It.IsAny<FurnitureDataModel>())).Returns(true);
|
||||
_furnitureStorageContract.Setup(x => x.UpdElement(It.IsAny<FurnitureDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() => _furnitureBusinessLogicContract.UpdateFurniture(new(Guid.NewGuid().ToString(), "name", 10,
|
||||
[new FurnitureComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_warehouseStorageContract.Verify(x => x.CheckComponents(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
_furnitureStorageContract.Verify(x => x.UpdElement(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
@@ -264,13 +291,27 @@ public class FurnitureBusinessLogicContractTests
|
||||
public void UpdateFurnitures_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_warehouseStorageContract.Setup(x => x.CheckComponents(It.IsAny<FurnitureDataModel>())).Returns(true);
|
||||
_furnitureStorageContract.Setup(x => x.UpdElement(It.IsAny<FurnitureDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _furnitureBusinessLogicContract.UpdateFurniture(new(Guid.NewGuid().ToString(), "name", 10,
|
||||
[new FurnitureComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),5)])), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.CheckComponents(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
_furnitureStorageContract.Verify(x => x.UpdElement(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateFurniture_InsufficientError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_warehouseStorageContract.Setup(x => x.CheckComponents(It.IsAny<FurnitureDataModel>())).Returns(false);
|
||||
//Act&Assert
|
||||
Assert.That(() => _furnitureBusinessLogicContract.UpdateFurniture(new(Guid.NewGuid().ToString(), "name", 10,
|
||||
[new FurnitureComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<InsufficientException>());
|
||||
_warehouseStorageContract.Verify(x => x.CheckComponents(It.IsAny<FurnitureDataModel>()), Times.Once);
|
||||
_furnitureStorageContract.Verify(x => x.UpdElement(It.IsAny<FurnitureDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteFurnitures_CorrectRecord_Test()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
using FurnitureAssemblyBusinessLogic.Implementations;
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContracts;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.StoragesContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
|
||||
namespace FurnitureAssemblyTests.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(), ComponentType.Panel, DateTime.Now, 1, []),
|
||||
new(Guid.NewGuid().ToString(), ComponentType.Panel, DateTime.Now, 1, []),
|
||||
new(Guid.NewGuid().ToString(), ComponentType.Panel, 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, ComponentType.Panel, 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(), ComponentType.Panel, DateTime.Now, 1,
|
||||
[new ComponentSuppliesDataModel(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(), ComponentType.Panel, DateTime.Now, 1,
|
||||
[new ComponentSuppliesDataModel(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", ComponentType.Panel, 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(), ComponentType.Panel, DateTime.Now, 1,
|
||||
[new ComponentSuppliesDataModel(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(), ComponentType.Panel, DateTime.Now, 1,
|
||||
[new ComponentSuppliesDataModel(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(), ComponentType.Panel, DateTime.Now, 1,
|
||||
[new ComponentSuppliesDataModel(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(), ComponentType.Panel, DateTime.Now, 1,
|
||||
[new ComponentSuppliesDataModel(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(), ComponentType.Panel, 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(), ComponentType.Panel, DateTime.Now, 1,
|
||||
[new ComponentSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
using FurnitureAssemblyBusinessLogic.Implementations;
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContracts;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.StoragesContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
|
||||
namespace FurnitureAssemblyTests.BusinessLogicContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class WarehouseBusinessLogicContractTests
|
||||
{
|
||||
private IWarehouseBusinessLogicContract _warehouseBusinessLogicContract;
|
||||
private Mock<IWarehouseStorageContract> _warehouseStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_warehouseStorageContract = new Mock<IWarehouseStorageContract>();
|
||||
_warehouseBusinessLogicContract = new WarehouseBusinessLogicContract(_warehouseStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_warehouseStorageContract.Reset();
|
||||
}
|
||||
|
||||
public void GetAllSupplies_ReturnListOfRecords_Test()
|
||||
{
|
||||
// Arrange
|
||||
var listOriginal = new List<WarehouseDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), ComponentType.Panel, 1, []),
|
||||
new(Guid.NewGuid().ToString(), ComponentType.Panel, 1, []),
|
||||
new(Guid.NewGuid().ToString(), ComponentType.Panel, 1, []),
|
||||
};
|
||||
_warehouseStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
|
||||
// Act
|
||||
var list = _warehouseBusinessLogicContract.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_ReturnEmptyList_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetList()).Returns([]);
|
||||
// Act
|
||||
var list = _warehouseBusinessLogicContract.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_warehouseStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetList()).Returns((List<WarehouseDataModel>)null);
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetAllComponents(), Throws.TypeOf<NullListException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetAllComponents(), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new WarehouseDataModel(id, ComponentType.Panel, 1, []);
|
||||
_warehouseStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
// Act
|
||||
var element = _warehouseBusinessLogicContract.GetComponentByData(id);
|
||||
// Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetComponentsByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_CorrectRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var record = new WarehouseDataModel(Guid.NewGuid().ToString(), ComponentType.Panel, 1,
|
||||
[new ComponentWarehouseDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_warehouseStorageContract.Setup(x => x.AddElement(It.IsAny<WarehouseDataModel>()));
|
||||
// Act
|
||||
_warehouseBusinessLogicContract.InsertComponent(record);
|
||||
// Assert
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.AddElement(It.IsAny<WarehouseDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), ComponentType.Panel, 1,
|
||||
[new ComponentWarehouseDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_NullRecord_ThrowException_Test()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<WarehouseDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponents_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(new WarehouseDataModel("id", ComponentType.Panel, 1, [])), Throws.TypeOf<ValidationException>());
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<WarehouseDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.AddElement(It.IsAny<WarehouseDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), ComponentType.Panel, 1,
|
||||
[new ComponentWarehouseDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_CorrectRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var record = new WarehouseDataModel(Guid.NewGuid().ToString(), ComponentType.Panel, 1,
|
||||
[new ComponentWarehouseDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<WarehouseDataModel>()));
|
||||
// Act
|
||||
_warehouseBusinessLogicContract.UpdateComponent(record);
|
||||
// Assert
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<WarehouseDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), ComponentType.Panel, 1,
|
||||
[new ComponentWarehouseDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementNotFoundException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<WarehouseDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), ComponentType.Panel, 1,
|
||||
[new ComponentWarehouseDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_NullRecord_ThrowException_Test()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponents_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new WarehouseDataModel(Guid.NewGuid().ToString(), ComponentType.Panel, 1, [])), Throws.TypeOf<ValidationException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<WarehouseDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), ComponentType.Panel, 1,
|
||||
[new ComponentWarehouseDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_warehouseStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
//Act
|
||||
_warehouseBusinessLogicContract.DeleteComponent(id);
|
||||
//Assert
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once); Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_warehouseStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent("id"),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_warehouseStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ internal class ComponentDataModelTests
|
||||
{
|
||||
var component = CreateDataModel(Guid.NewGuid().ToString(), null, ComponentType.Box);
|
||||
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
|
||||
component = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ComponentType.Box);
|
||||
component = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ComponentType.Box);
|
||||
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ internal class ComponentDataModelTests
|
||||
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
|
||||
namespace FurnitureAssemblyTests.DataModelsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class ComponentSuppliesDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void SuppliesIdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(null, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SuppliesIdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel("id", Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ComponentIdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), null, 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ComponentIdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), "id", 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var suppliesId = Guid.NewGuid().ToString();
|
||||
var componentId = Guid.NewGuid().ToString();
|
||||
var count = 1;
|
||||
var model = CreateDataModel(suppliesId, componentId, count);
|
||||
Assert.That(() => model.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.SuppliesId, Is.EqualTo(suppliesId));
|
||||
Assert.That(model.ComponentId, Is.EqualTo(componentId));
|
||||
Assert.That(model.Count, Is.EqualTo(count));
|
||||
});
|
||||
}
|
||||
|
||||
private static ComponentSuppliesDataModel CreateDataModel(string? suppliesId, string? componentId, int count)
|
||||
=> new(suppliesId, componentId, count);
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
|
||||
namespace FurnitureAssemblyTests.DataModelsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class ComponentWarehouseDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void WarehouseIdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(null, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WarehouseIdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel("id", Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ComponentIdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), null, 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ComponentIdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), "id", 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var warehouseId = Guid.NewGuid().ToString();
|
||||
var componentId = Guid.NewGuid().ToString();
|
||||
var count = 1;
|
||||
var model = CreateDataModel(componentId, componentId, count);
|
||||
Assert.That(() => model.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.WarehouseId, Is.EqualTo(componentId));
|
||||
Assert.That(model.ComponentId, Is.EqualTo(componentId));
|
||||
Assert.That(model.Count, Is.EqualTo(count));
|
||||
});
|
||||
}
|
||||
|
||||
private static ComponentWarehouseDataModel CreateDataModel(string? warehouseId, string? componentId, int count)
|
||||
=> new(warehouseId, componentId, count);
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
|
||||
namespace FurnitureAssemblyTests.DataModelsTests;
|
||||
|
||||
[TestFixture]
|
||||
public class SuppliesDataModelTest
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(null, ComponentType.Handle, DateTime.Now, 1, CreateSuppliesDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
model = CreateDataModel(string.Empty, ComponentType.Handle, DateTime.Now, 1, CreateSuppliesDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel("id", ComponentType.Handle, DateTime.Now, 1, CreateSuppliesDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TypeIsNoneTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), ComponentType.None, DateTime.Now, 1, CreateSuppliesDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), ComponentType.None, DateTime.Now, 0, CreateSuppliesDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), ComponentType.None, DateTime.Now, -1, CreateSuppliesDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SuppliesIsEmptyOrNullTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), ComponentType.None, DateTime.Now, 0, []);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), ComponentType.None, DateTime.Now, 0, null);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var type = ComponentType.Handle;
|
||||
var date = DateTime.Now;
|
||||
var count = 1;
|
||||
var supplies = CreateSuppliesDataModel();
|
||||
var model = CreateDataModel(id, type, date, count, supplies);
|
||||
Assert.DoesNotThrow(() => model.Validate());
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.Id, Is.EqualTo(id));
|
||||
Assert.That(model.Type, Is.EqualTo(type));
|
||||
Assert.That(model.ProductuionDate, Is.EqualTo(date));
|
||||
Assert.That(model.Count, Is.EqualTo(count));
|
||||
Assert.That(model.Components, Is.EqualTo(supplies));
|
||||
});
|
||||
}
|
||||
|
||||
private static SuppliesDataModel CreateDataModel(string? id, ComponentType type, DateTime date, int count, List<ComponentSuppliesDataModel> supplies)
|
||||
=> new(id, type, date, count, supplies);
|
||||
private static List<ComponentSuppliesDataModel> CreateSuppliesDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
|
||||
namespace FurnitureAssemblyTests.DataModelsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class WorkhouseDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(null, ComponentType.Handle, 1, CreateWarehouseDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
model = CreateDataModel(string.Empty, ComponentType.Handle, 1, CreateWarehouseDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel("id", ComponentType.Handle, 1, CreateWarehouseDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TypeIsNoneTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), ComponentType.None, 1, CreateWarehouseDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), ComponentType.None, 0, CreateWarehouseDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), ComponentType.None, -1, CreateWarehouseDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WorkhouseIsEmptyOrNullTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), ComponentType.None, 0, []);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), ComponentType.None, 0, null);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var type = ComponentType.Handle;
|
||||
var count = 1;
|
||||
var warehouse = CreateWarehouseDataModel();
|
||||
var model = CreateDataModel(id, type, count, warehouse);
|
||||
Assert.DoesNotThrow(() => model.Validate());
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.Id, Is.EqualTo(id));
|
||||
Assert.That(model.Type, Is.EqualTo(type));
|
||||
Assert.That(model.Count, Is.EqualTo(count));
|
||||
Assert.That(model.Components, Is.EqualTo(warehouse));
|
||||
});
|
||||
}
|
||||
|
||||
private static WarehouseDataModel CreateDataModel(string? id, ComponentType type, int count, List<ComponentWarehouseDataModel> warehouse)
|
||||
=> new WarehouseDataModel(id, type, count, warehouse);
|
||||
private static List<ComponentWarehouseDataModel> CreateWarehouseDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
}
|
||||
@@ -23,7 +23,6 @@ internal class ComponentStorageContractTests : BaseStorageContractTest
|
||||
public void TearDown()
|
||||
{
|
||||
FurnitureAssemblyDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Components\" CASCADE; ");
|
||||
//FurnitureAssemblyDbContext.Database.ExecuteSqlRaw("TRUNCATE\"Manufacturers\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyDatebase.Implementations;
|
||||
using FurnitureAssemblyDatebase.Models;
|
||||
using FurnitureAssemblyDatebase;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
|
||||
namespace FurnitureAssemblyTests.StorageContractTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SuppliesStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private SuppliesStorageContract _suppliesStorageContract;
|
||||
private Component _component;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_suppliesStorageContract = new SuppliesStorageContract(FurnitureAssemblyDbContext);
|
||||
_component = InsertComponentToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
FurnitureAssemblyDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Supplieses\" CASCADE;");
|
||||
FurnitureAssemblyDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Components\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var supply = InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), ComponentType.Handle, 5);
|
||||
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), ComponentType.Handle, 5);
|
||||
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), ComponentType.Handle, 5);
|
||||
var list = _suppliesStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _suppliesStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var supply = InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), ComponentType.Handle, 5);
|
||||
AssertElement(_suppliesStorageContract.GetElementById(supply.Id), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _suppliesStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var supply = CreateModel(Guid.NewGuid().ToString(), ComponentType.Handle, 5, [_component.Id]);
|
||||
_suppliesStorageContract.AddElement(supply);
|
||||
AssertElement(GetSupplyFromDatabaseById(supply.Id), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var supply = CreateModel(Guid.NewGuid().ToString(), ComponentType.Handle, 5, [_component.Id]);
|
||||
InsertSupplyToDatabaseAndReturn(supply.Id, ComponentType.Handle, 5, [(_component.Id, 3)]);
|
||||
Assert.That(() => _suppliesStorageContract.AddElement(supply), Throws.TypeOf<StorageException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var supply = CreateModel(Guid.NewGuid().ToString(), ComponentType.Handle, 5, [_component.Id]);
|
||||
InsertSupplyToDatabaseAndReturn(supply.Id, ComponentType.Legs, 5, null);
|
||||
_suppliesStorageContract.UpdElement(supply);
|
||||
AssertElement(GetSupplyFromDatabaseById(supply.Id), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _suppliesStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString(), ComponentType.Handle, 5, [_component.Id])), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Component InsertComponentToDatabaseAndReturn()
|
||||
{
|
||||
var component = new Component { Id = Guid.NewGuid().ToString(), Name = "Test Component", Type = ComponentType.Panel };
|
||||
FurnitureAssemblyDbContext.Components.Add(component);
|
||||
FurnitureAssemblyDbContext.SaveChanges();
|
||||
return component;
|
||||
}
|
||||
|
||||
private Supplies InsertSupplyToDatabaseAndReturn(string id, ComponentType type, int count, List<(string, int)>? components = null)
|
||||
{
|
||||
var supply = new Supplies { Id = id, Type = type, ProductuionDate = DateTime.UtcNow, Count = count, Components = [] };
|
||||
if (components is not null)
|
||||
{
|
||||
foreach (var elem in components)
|
||||
{
|
||||
supply.Components.Add(new ComponentSupplies { SuppliesId = supply.Id, ComponentId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
FurnitureAssemblyDbContext.Supplieses.Add(supply);
|
||||
FurnitureAssemblyDbContext.SaveChanges();
|
||||
return supply;
|
||||
}
|
||||
|
||||
private static void AssertElement(SuppliesDataModel? actual, Supplies expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
||||
Assert.That(actual.ProductuionDate, Is.EqualTo(expected.ProductuionDate));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Components is not null)
|
||||
{
|
||||
Assert.That(actual.Components, Is.Not.Null);
|
||||
Assert.That(actual.Components, Has.Count.EqualTo(expected.Components.Count));
|
||||
for (int i = 0; i < actual.Components.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Components[i].ComponentId, Is.EqualTo(expected.Components[i].ComponentId));
|
||||
Assert.That(actual.Components[i].Count, Is.EqualTo(expected.Components[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Components, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertElement(Supplies? actual, SuppliesDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
||||
Assert.That(actual.ProductuionDate, Is.EqualTo(expected.ProductuionDate));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Components is not null)
|
||||
{
|
||||
Assert.That(actual.Components, Is.Not.Null);
|
||||
Assert.That(actual.Components, Has.Count.EqualTo(expected.Components.Count));
|
||||
for (int i = 0; i < actual.Components.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Components[i].ComponentId, Is.EqualTo(expected.Components[i].ComponentId));
|
||||
Assert.That(actual.Components[i].Count, Is.EqualTo(expected.Components[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Components, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static SuppliesDataModel CreateModel(string id, ComponentType type, int count, List<string> componentsIds)
|
||||
{
|
||||
var components = componentsIds.Select(x => new ComponentSuppliesDataModel(id, x, 1)).ToList();
|
||||
return new(id, type, DateTime.UtcNow, count, components);
|
||||
}
|
||||
|
||||
|
||||
private Supplies? GetSupplyFromDatabaseById(string id)
|
||||
{
|
||||
return FurnitureAssemblyDbContext.Supplieses.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyDatebase.Implementations;
|
||||
using FurnitureAssemblyDatebase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FurnitureAssemblyTests.StorageContractTests;
|
||||
|
||||
internal class WarehouseStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private WarehouseStorageContract _warehouseStorageContract;
|
||||
private Component _component;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_warehouseStorageContract = new WarehouseStorageContract(FurnitureAssemblyDbContext);
|
||||
_component = InsertComponentToDatabaseAndReturn(Guid.NewGuid().ToString(), "name", ComponentType.Panel);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
FurnitureAssemblyDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Warehouses\" CASCADE;");
|
||||
FurnitureAssemblyDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Components\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var warehouse = InsertWarehouseToDatabaseAndReturn(Guid.NewGuid().ToString(), ComponentType.Handle, 5);
|
||||
InsertWarehouseToDatabaseAndReturn(Guid.NewGuid().ToString(), ComponentType.Handle, 5);
|
||||
InsertWarehouseToDatabaseAndReturn(Guid.NewGuid().ToString(), ComponentType.Handle, 5);
|
||||
var list = _warehouseStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(), warehouse);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _warehouseStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var warehouse = InsertWarehouseToDatabaseAndReturn(Guid.NewGuid().ToString(), ComponentType.Handle, 5);
|
||||
var result = _warehouseStorageContract.GetElementById(warehouse.Id);
|
||||
AssertElement(result, warehouse);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _warehouseStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenValidData_Test()
|
||||
{
|
||||
var warehouse = CreateModel(Guid.NewGuid().ToString(), ComponentType.Handle, 5, [_component.Id]);
|
||||
_warehouseStorageContract.AddElement(warehouse);
|
||||
var result = GetWarehouseFromDatabaseById(warehouse.Id);
|
||||
AssertElement(result, warehouse);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var warehouse = CreateModel(Guid.NewGuid().ToString(), ComponentType.Handle, 5, [_component.Id]);
|
||||
InsertWarehouseToDatabaseAndReturn(warehouse.Id, ComponentType.Handle, 5, [(_component.Id, 3)]);
|
||||
Assert.That(() => _warehouseStorageContract.AddElement(warehouse), Throws.TypeOf<StorageException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenNoHaveComponent_Test()
|
||||
{
|
||||
var warehouse = CreateModel(Guid.NewGuid().ToString(), ComponentType.Handle, 0, [_component.Id]);
|
||||
InsertWarehouseToDatabaseAndReturn(warehouse.Id, ComponentType.Handle, 5, [(_component.Id, 3)]);
|
||||
Assert.That(() => _warehouseStorageContract.AddElement(warehouse), Throws.TypeOf<StorageException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenValidData_Test()
|
||||
{
|
||||
var warehouse = CreateModel(Guid.NewGuid().ToString(), ComponentType.Handle, 5, [_component.Id]);
|
||||
InsertWarehouseToDatabaseAndReturn(warehouse.Id, ComponentType.Legs, 10, null);
|
||||
_warehouseStorageContract.UpdElement(warehouse);
|
||||
var result = GetWarehouseFromDatabaseById(warehouse.Id);
|
||||
AssertElement(result, warehouse);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
var warehouse = CreateModel(Guid.NewGuid().ToString(), ComponentType.Handle, 5, [_component.Id]);
|
||||
Assert.That(() => _warehouseStorageContract.UpdElement(warehouse), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordExists_Test()
|
||||
{
|
||||
var warehouse = InsertWarehouseToDatabaseAndReturn(Guid.NewGuid().ToString(), ComponentType.Handle, 5);
|
||||
_warehouseStorageContract.DelElement(warehouse.Id);
|
||||
var result = GetWarehouseFromDatabaseById(warehouse.Id);
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordDoesNotExist_ThrowsElementNotFoundException()
|
||||
{
|
||||
Assert.That(() => _warehouseStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Component InsertComponentToDatabaseAndReturn(string id, string name, ComponentType type)
|
||||
{
|
||||
var component = new Component { Id = id, Name = name, Type = type };
|
||||
FurnitureAssemblyDbContext.Components.Add(component);
|
||||
FurnitureAssemblyDbContext.SaveChanges();
|
||||
return component;
|
||||
}
|
||||
|
||||
private Warehouse InsertWarehouseToDatabaseAndReturn(string id, ComponentType type, int count, List<(string, int)>? components = null)
|
||||
{
|
||||
var warehouse = new Warehouse { Id = id, Type = type, Count = count, Components = [] };
|
||||
if (components is not null)
|
||||
{
|
||||
foreach (var elem in components)
|
||||
{
|
||||
warehouse.Components.Add(new ComponentWarehouse { WarehouseId = warehouse.Id, ComponentId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
FurnitureAssemblyDbContext.Warehouses.Add(warehouse);
|
||||
FurnitureAssemblyDbContext.SaveChanges();
|
||||
return warehouse;
|
||||
}
|
||||
|
||||
private static void AssertElement(WarehouseDataModel? actual, Warehouse expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Components is not null)
|
||||
{
|
||||
Assert.That(actual.Components, Is.Not.Null);
|
||||
Assert.That(actual.Components, Has.Count.EqualTo(expected.Components.Count));
|
||||
for (int i = 0; i < actual.Components.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Components[i].ComponentId, Is.EqualTo(expected.Components[i].ComponentId));
|
||||
Assert.That(actual.Components[i].Count, Is.EqualTo(expected.Components[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Components, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertElement(Warehouse? actual, WarehouseDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Components is not null)
|
||||
{
|
||||
Assert.That(actual.Components, Is.Not.Null);
|
||||
Assert.That(actual.Components, Has.Count.EqualTo(expected.Components.Count));
|
||||
for (int i = 0; i < actual.Components.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Components[i].ComponentId, Is.EqualTo(expected.Components[i].ComponentId));
|
||||
Assert.That(actual.Components[i].Count, Is.EqualTo(expected.Components[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Components, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static WarehouseDataModel CreateModel(string id, ComponentType type, int count, List<string> componentsIds)
|
||||
{
|
||||
var components = componentsIds.Select(x => new ComponentWarehouseDataModel(id, x, 1)).ToList();
|
||||
return new(id, type, count, components);
|
||||
}
|
||||
|
||||
private Warehouse? GetWarehouseFromDatabaseById(string id)
|
||||
{
|
||||
return FurnitureAssemblyDbContext.Warehouses.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user