Compare commits
10 Commits
LabWork04_
...
LabWork04_
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e059136a60 | ||
|
|
dc11de1d85 | ||
|
|
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,13 @@
|
||||
using FurnitureAssemblyContracts.AdapterContracts.OperationResponses;
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
|
||||
namespace FurnitureAssemblyContracts.AdapterContracts;
|
||||
|
||||
public interface ISuppliesAdapter
|
||||
{
|
||||
SuppliesOperationResponse GetAllComponents();
|
||||
SuppliesOperationResponse GetComponentByData(string data);
|
||||
SuppliesOperationResponse InsertComponent(SuppliesBindingModel componentDataModel);
|
||||
SuppliesOperationResponse UpdateComponent(SuppliesBindingModel componentDataModel);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using FurnitureAssemblyContracts.AdapterContracts.OperationResponses;
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
|
||||
namespace FurnitureAssemblyContracts.AdapterContracts;
|
||||
|
||||
public interface IWarehouseAdapter
|
||||
{
|
||||
WarehouseOperationResponse GetAllComponents();
|
||||
WarehouseOperationResponse GetComponentByData(string data);
|
||||
WarehouseOperationResponse InsertComponent(WarehouseBindingModel warehouseDataModel);
|
||||
WarehouseOperationResponse UpdateComponent(WarehouseBindingModel warehouseDataModel);
|
||||
WarehouseOperationResponse DeleteComponent(string id);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using FurnitureAssemblyContracts.Infrastructure;
|
||||
using FurnitureAssemblyContracts.ViewModels;
|
||||
|
||||
namespace FurnitureAssemblyContracts.AdapterContracts.OperationResponses;
|
||||
|
||||
public class SuppliesOperationResponse : OperationResponse
|
||||
{
|
||||
public static SuppliesOperationResponse OK(List<SuppliesViewModel> data) => OK<SuppliesOperationResponse, List<SuppliesViewModel>>(data);
|
||||
|
||||
public static SuppliesOperationResponse OK(SuppliesViewModel data) => OK<SuppliesOperationResponse, SuppliesViewModel>(data);
|
||||
|
||||
public static SuppliesOperationResponse NoContent() => NoContent<SuppliesOperationResponse>();
|
||||
|
||||
public static SuppliesOperationResponse NotFound(string message) => NotFound<SuppliesOperationResponse>(message);
|
||||
|
||||
public static SuppliesOperationResponse BadRequest(string message) => BadRequest<SuppliesOperationResponse>(message);
|
||||
|
||||
public static SuppliesOperationResponse InternalServerError(string message) => InternalServerError<SuppliesOperationResponse>(message);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using FurnitureAssemblyContracts.Infrastructure;
|
||||
using FurnitureAssemblyContracts.ViewModels;
|
||||
|
||||
namespace FurnitureAssemblyContracts.AdapterContracts.OperationResponses;
|
||||
|
||||
public class WarehouseOperationResponse : OperationResponse
|
||||
{
|
||||
public static WarehouseOperationResponse OK(List<WarehouseViewModel> data) => OK<WarehouseOperationResponse, List<WarehouseViewModel>>(data);
|
||||
|
||||
public static WarehouseOperationResponse OK(WarehouseViewModel data) => OK<WarehouseOperationResponse, WarehouseViewModel>(data);
|
||||
|
||||
public static WarehouseOperationResponse NoContent() => NoContent<WarehouseOperationResponse>();
|
||||
|
||||
public static WarehouseOperationResponse NotFound(string message) => NotFound<WarehouseOperationResponse>(message);
|
||||
|
||||
public static WarehouseOperationResponse BadRequest(string message) => BadRequest<WarehouseOperationResponse>(message);
|
||||
|
||||
public static WarehouseOperationResponse InternalServerError(string message) => InternalServerError<WarehouseOperationResponse>(message);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace FurnitureAssemblyContracts.BindingModels;
|
||||
|
||||
public class ComponentSuppliesBindingModel
|
||||
{
|
||||
public string? SuppliesId { get; set; }
|
||||
public string? ComponentId { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace FurnitureAssemblyContracts.BindingModels;
|
||||
|
||||
public class ComponentWarehouseBindingModel
|
||||
{
|
||||
public string? WarehouseId { get; set; }
|
||||
public string? ComponentId { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
|
||||
namespace FurnitureAssemblyContracts.BindingModels;
|
||||
|
||||
public class SuppliesBindingModel
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public ComponentType? Type { get; set; }
|
||||
public DateTime ProductuionDate { get; set; }
|
||||
public int Count { get; set; }
|
||||
public List<ComponentSuppliesBindingModel>? Components { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
|
||||
namespace FurnitureAssemblyContracts.BindingModels;
|
||||
|
||||
public class WarehouseBindingModel
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public ComponentType? Type { get; set; }
|
||||
public int Count { get; set; }
|
||||
public List<ComponentWarehouseBindingModel>? Components { get; set; }
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
}
|
||||
@@ -6,9 +6,9 @@ namespace FurnitureAssemblyContracts.Infrastructure;
|
||||
|
||||
public class OperationResponse
|
||||
{
|
||||
protected HttpStatusCode StatusCode { get; set; }
|
||||
public HttpStatusCode StatusCode { get; set; }
|
||||
|
||||
protected object? Result { get; set; }
|
||||
public object? Result { get; set; }
|
||||
|
||||
public IActionResult GetResponse(HttpRequest request, HttpResponse response)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace FurnitureAssemblyContracts.ViewModels;
|
||||
|
||||
public class ComponentSuppliesViewModel
|
||||
{
|
||||
public required string SuppliesId { get; set; }
|
||||
public required string ComponentId { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace FurnitureAssemblyContracts.ViewModels;
|
||||
|
||||
public class ComponentWarehouseViewModel
|
||||
{
|
||||
public required string WarehouseId { get; set; }
|
||||
public required string ComponentId { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
|
||||
namespace FurnitureAssemblyContracts.ViewModels;
|
||||
|
||||
public class SuppliesViewModel
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required ComponentType Type { get; set; }
|
||||
public DateTime ProductuionDate { get; set; }
|
||||
public int Count { get; set; }
|
||||
public required List<ComponentSuppliesViewModel> Components { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
|
||||
namespace FurnitureAssemblyContracts.ViewModels;
|
||||
|
||||
public class WarehouseViewModel
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required ComponentType Type { get; set; }
|
||||
public required int Count { get; set; }
|
||||
public required List<ComponentWarehouseViewModel> Components { get; set; }
|
||||
}
|
||||
@@ -40,6 +40,10 @@ public class FurnitureAssemblyDbContext : DbContext
|
||||
.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>()
|
||||
@@ -56,4 +60,8 @@ public class FurnitureAssemblyDbContext : DbContext
|
||||
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,136 @@
|
||||
using AutoMapper;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.StoragesContracts;
|
||||
using FurnitureAssemblyDatebase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
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(ComponentWarehouse));
|
||||
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)];
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
using FurnitureAssemblyDatebase;
|
||||
using FurnitureAssemblyDatebase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
|
||||
namespace FurnitureAssemblyTests.Infrastructure;
|
||||
|
||||
@@ -61,13 +60,45 @@ internal static class FurnitureAssemblyDbContextExtensions
|
||||
return salary;
|
||||
}
|
||||
|
||||
public static Warehouse InsertWarehouseToDatabaseAndReturn(this FurnitureAssemblyDbContext dbContext, string? id = null, ComponentType type = ComponentType.Panel, int count = 20, List<(string, int)>? components = null)
|
||||
{
|
||||
var warehouse = new Warehouse() { Id = id ?? Guid.NewGuid().ToString(), 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 });
|
||||
}
|
||||
}
|
||||
dbContext.Warehouses.Add(warehouse);
|
||||
dbContext.SaveChanges();
|
||||
return warehouse;
|
||||
}
|
||||
|
||||
public static Supplies InsertSuppliesToDatabaseAndReturn(this FurnitureAssemblyDbContext dbContext, string? id = null, ComponentType type = ComponentType.Panel, int count = 20, DateTime? date = null, List<(string, int)>? components = null)
|
||||
{
|
||||
var supply = new Supplies() { Id = id ?? Guid.NewGuid().ToString(), Type = type, Count = count, ProductuionDate = date ?? DateTime.UtcNow, 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 });
|
||||
}
|
||||
}
|
||||
dbContext.Supplieses.Add(supply);
|
||||
dbContext.SaveChanges();
|
||||
return supply;
|
||||
}
|
||||
|
||||
public static Post? GetPostFromDatabaseByPostId(this FurnitureAssemblyDbContext dbContext, string id) => dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual);
|
||||
public static Post[] GetPostsFromDatabaseByPostId(this FurnitureAssemblyDbContext dbContext, string id) => [.. dbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate)];
|
||||
public static Worker? GetWorkerFromDatabaseById(this FurnitureAssemblyDbContext dbContext, string id) => dbContext.Workers.FirstOrDefault(x => x.Id == id);
|
||||
public static Component? GetComponentFromDatabaseById(this FurnitureAssemblyDbContext dbContext, string id) => dbContext.Components.FirstOrDefault(x => x.Id == id);
|
||||
public static Furniture? GetFurnitureFromDatabaseById(this FurnitureAssemblyDbContext dbContext, string id) => dbContext.Furnitures.FirstOrDefault(x => x.Id == id);
|
||||
public static Furniture? GetFurnitureFromDatabaseById(this FurnitureAssemblyDbContext dbContext, string id) => dbContext.Furnitures.Include(x => x.Components).FirstOrDefault(x => x.Id == id);
|
||||
public static ManifacturingFurniture? GetManifacturingFromDatabaseById(this FurnitureAssemblyDbContext dbContext, string id) => dbContext.Manufacturing.FirstOrDefault(x => x.Id == id);
|
||||
public static Salary[] GetSalariesFromDatabaseByWorkerId(this FurnitureAssemblyDbContext dbContext, string id) => [.. dbContext.Salaries.Where(x => x.WorkerId == id)];
|
||||
public static Warehouse? GetWarehouseFromDatabaseById(this FurnitureAssemblyDbContext dbContext, string id) => dbContext.Warehouses.Include(x => x.Components).FirstOrDefault(x => x.Id == id);
|
||||
public static Supplies? GetSuppliesFromDatabaseById(this FurnitureAssemblyDbContext dbContext, string id) => dbContext.Supplieses.Include(x => x.Components).FirstOrDefault(x => x.Id == id);
|
||||
|
||||
public static void RemovePostsFromDatabase(this FurnitureAssemblyDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Posts\" CASCADE;");
|
||||
public static void RemoveWorkersFromDatabase(this FurnitureAssemblyDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE;");
|
||||
@@ -75,6 +106,8 @@ internal static class FurnitureAssemblyDbContextExtensions
|
||||
public static void RemoveFurnituresFromDatabase(this FurnitureAssemblyDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Furnitures\" CASCADE;");
|
||||
public static void RemoveManifacturingFromDatabase(this FurnitureAssemblyDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Manufacturing\" CASCADE;");
|
||||
public static void RemoveSalariesFromDatabase(this FurnitureAssemblyDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;");
|
||||
public static void RemoveWarehousesFromDatabase(this FurnitureAssemblyDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Warehouses\" CASCADE;");
|
||||
public static void RemoveSuppliesFromDatabase(this FurnitureAssemblyDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Supplieses\" CASCADE;");
|
||||
|
||||
private static void ExecuteSqlRaw(this FurnitureAssemblyDbContext dbContext, string command) => dbContext.Database.ExecuteSqlRaw(command);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
using AutoMapper;
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContracts;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.ViewModels;
|
||||
using FurnitureAssemblyWebApi.Adapters;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using System.Net;
|
||||
|
||||
namespace FurnitureAssemblyTests.WebApiAdapterTests;
|
||||
|
||||
|
||||
[TestFixture]
|
||||
internal class SuppliesAdapterTests
|
||||
{
|
||||
private Mock<ISuppliesBusinessLogicContract> _suppliesBusinessLogicContract;
|
||||
private SuppliesAdapter _adapter;
|
||||
private Mock<ILogger<SuppliesAdapter>> _logger;
|
||||
private IMapper _mapper;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_suppliesBusinessLogicContract = new Mock<ISuppliesBusinessLogicContract>();
|
||||
_logger = new Mock<ILogger<SuppliesAdapter>>();
|
||||
_adapter = new SuppliesAdapter(_suppliesBusinessLogicContract.Object, _logger.Object);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllComponents_WhenSuppliesExist_ReturnOk()
|
||||
{
|
||||
// Arrange
|
||||
var supplies = new List<SuppliesDataModel>
|
||||
{
|
||||
new SuppliesDataModel(Guid.NewGuid().ToString(), ComponentType.Panel, DateTime.Now, 5, []),
|
||||
new SuppliesDataModel(Guid.NewGuid().ToString(), ComponentType.Panel, DateTime.Now.AddDays(1), 10, [])
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.GetAllComponents()).Returns(supplies);
|
||||
// Act
|
||||
var result = _adapter.GetAllComponents();
|
||||
var list = (List<SuppliesViewModel>)result.Result!;
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
Assert.That(list.Count, Is.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllComponents_WhenListNull_ReturnNotFound()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesBusinessLogicContract.Setup(x => x.GetAllComponents()).Throws(new NullListException());
|
||||
// Act
|
||||
var result = _adapter.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllComponents_WhenStorageException_ReturnsInternalServerError()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesBusinessLogicContract.Setup(x => x.GetAllComponents()).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act
|
||||
var result = _adapter.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.InternalServerError));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetComponentByData_WhenSupplies_ReturnOK()
|
||||
{
|
||||
var supply = new SuppliesDataModel(Guid.NewGuid().ToString(), ComponentType.Panel, DateTime.Now, 5, []);
|
||||
// Arrange
|
||||
_suppliesBusinessLogicContract.Setup(x => x.GetComponentByData(supply.Id)).Returns(supply);
|
||||
// Act
|
||||
var result = _adapter.GetComponentByData(supply.Id);
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var resultData = (SuppliesViewModel)result.Result!;
|
||||
Assert.That(resultData.Id!, Is.EqualTo(supply.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetComponentByData_WhenComponentNotFound_ReturnsNotFound()
|
||||
{
|
||||
// Arrange
|
||||
var data = "test";
|
||||
_suppliesBusinessLogicContract.Setup(x => x.GetComponentByData(data)).Throws(new ElementNotFoundException(""));
|
||||
// Act
|
||||
var result = _adapter.GetComponentByData(data);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetComponentByData_WhenStorageException_ReturnInternalServerError()
|
||||
{
|
||||
// Arrange
|
||||
var data = "test";
|
||||
_suppliesBusinessLogicContract.Setup(x => x.GetComponentByData(data)).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act
|
||||
var result = _adapter.GetComponentByData(data);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.InternalServerError));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponent_WhenValidData_ReturnsNoContent()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Type = ComponentType.Panel,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Components = [new ComponentSuppliesBindingModel { ComponentId = Guid.NewGuid().ToString(), SuppliesId = Guid.NewGuid().ToString(), Count = 5 }]
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.InsertComponent(It.IsAny<SuppliesDataModel>()));
|
||||
// Act
|
||||
var result = _adapter.InsertComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponent_WhenDataIsNull_ReturnsBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesBusinessLogicContract.Setup(x => x.InsertComponent(null)).Throws(new ArgumentNullException());
|
||||
// Act
|
||||
var result = _adapter.InsertComponent(null);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponent_WhenValidationError_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Type = ComponentType.None,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Components = []
|
||||
};
|
||||
// Act
|
||||
var result = _adapter.InsertComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponent_WhenElementExistException_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply1 = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Type = ComponentType.None,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Components = []
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.InsertComponent(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act
|
||||
var result = _adapter.InsertComponent(supply1);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponent_WhenStorageException_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(), Type = ComponentType.Panel, ProductuionDate = DateTime.Now, Count = 5,
|
||||
Components = new List<ComponentSuppliesBindingModel>()
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.InsertComponent(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act
|
||||
var result = _adapter.InsertComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponent_WhenValidData_ReturnsNoContent()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel { Id = Guid.NewGuid().ToString() };
|
||||
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>()));
|
||||
// Act
|
||||
var result = _adapter.UpdateComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponent_WhenDataIsNull_ReturnsBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var component = new SuppliesBindingModel { Id = Guid.NewGuid().ToString() };
|
||||
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
// Act
|
||||
var result = _adapter.UpdateComponent(component);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponent_WhenValidationError_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Type = ComponentType.None,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Components = []
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new ValidationException(""));
|
||||
// Act
|
||||
var result = _adapter.UpdateComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponent_WhenElementNotFoundException_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Type = ComponentType.None,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Components = []
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
// Act
|
||||
var result = _adapter.UpdateComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponent_WhenElementExistException_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply1 = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Type = ComponentType.None,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Components = []
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act
|
||||
var result = _adapter.UpdateComponent(supply1);
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponent_WhenStorageException_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Type = ComponentType.Panel,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Components = new List<ComponentSuppliesBindingModel>()
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act
|
||||
var result = _adapter.UpdateComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ internal class FurnitureControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
FurnitureAssemblyDbContext.RemoveComponentsFromDatabase();
|
||||
FurnitureAssemblyDbContext.RemoveFurnituresFromDatabase();
|
||||
FurnitureAssemblyDbContext.RemoveWarehousesFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -108,6 +109,7 @@ internal class FurnitureControllerTests : BaseWebApiControllerTest
|
||||
public async Task Post_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||
FurnitureAssemblyDbContext.InsertFurnitureToDatabaseAndReturn();
|
||||
var furniture = CreateModel(name: "name", components: [(_componentId, 5)]);
|
||||
//Act
|
||||
@@ -157,6 +159,7 @@ internal class FurnitureControllerTests : BaseWebApiControllerTest
|
||||
public async Task Put_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||
var furniture = CreateModel(components: [(_componentId, 5)]);
|
||||
FurnitureAssemblyDbContext.InsertFurnitureToDatabaseAndReturn(furniture.Id);
|
||||
//Act
|
||||
|
||||
@@ -0,0 +1,248 @@
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using FurnitureAssemblyContracts.ViewModels;
|
||||
using FurnitureAssemblyDatebase.Models;
|
||||
using FurnitureAssemblyTests.Infrastructure;
|
||||
using System.Net;
|
||||
|
||||
namespace FurnitureAssemblyTests.WebApiControllerTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SuppliesControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
private string _componentId;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_componentId = FurnitureAssemblyDbContext.InsertComponentToDatabaseAndReturn().Id;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
FurnitureAssemblyDbContext.RemoveComponentsFromDatabase();
|
||||
FurnitureAssemblyDbContext.RemoveSuppliesFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAllWarehouses_WhenHaveRecords_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var supply = FurnitureAssemblyDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
FurnitureAssemblyDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
FurnitureAssemblyDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync("/api/supplies");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<SuppliesViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(3));
|
||||
});
|
||||
AssertElement(data.First(x => x.Id == supply.Id), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_WhenNoRecords_ShouldSuccess_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync("/api/supplies");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<WarehouseViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(0));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var supplies = FurnitureAssemblyDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/supplies/{supplies.Id}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<SuppliesViewModel>(response), supplies);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
||||
{
|
||||
//Arrange
|
||||
FurnitureAssemblyDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/supplies/{Guid.NewGuid()}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
FurnitureAssemblyDbContext.InsertSuppliesToDatabaseAndReturn(components: [(_componentId, 5)]);
|
||||
var supplies = CreateModel(components: [(_componentId, 5)]);
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/supplies", MakeContent(supplies));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
AssertElement(FurnitureAssemblyDbContext.GetSuppliesFromDatabaseById(supplies.Id!), supplies);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/supplies", MakeContent(string.Empty));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/supplies", MakeContent(new { Data = "test", Position = 10 }));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var suppliesIdIncorrect = new SuppliesViewModel { Id = "Id", Type = ComponentType.None, Components = [] };
|
||||
var suppliesWithTypeIncorrect = new SuppliesViewModel { Id = Guid.NewGuid().ToString(), Type = ComponentType.None, Components = [] };
|
||||
//Act
|
||||
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/supplies", MakeContent(suppliesIdIncorrect));
|
||||
var responseWithTypeIncorrect = await HttpClient.PostAsync($"/api/supplies", MakeContent(suppliesWithTypeIncorrect));
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
||||
Assert.That(responseWithTypeIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Type is incorrect");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
FurnitureAssemblyDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
var supplies = CreateModel(components: [(_componentId, 5)]);
|
||||
FurnitureAssemblyDbContext.InsertSuppliesToDatabaseAndReturn(supplies.Id);
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/supplies", MakeContent(supplies));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
FurnitureAssemblyDbContext.ChangeTracker.Clear();
|
||||
AssertElement(FurnitureAssemblyDbContext.GetSuppliesFromDatabaseById(supplies.Id!), supplies);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var furniture = CreateModel();
|
||||
FurnitureAssemblyDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/supplies", MakeContent(furniture));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/supplies", MakeContent(string.Empty));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/supplies", MakeContent(new { Data = "test", Position = 10 }));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
|
||||
private static SuppliesBindingModel CreateModel(string? id = null, ComponentType type = ComponentType.Panel, int count = 21, DateTime? date = null, List<(string, int)>? components = null)
|
||||
{
|
||||
var Supply = new SuppliesBindingModel() { Id = id ?? Guid.NewGuid().ToString(), Type = type, Count = count, ProductuionDate = date ?? DateTime.UtcNow, Components = [] };
|
||||
if (components is not null)
|
||||
{
|
||||
foreach (var elem in components)
|
||||
{
|
||||
Supply.Components.Add(new ComponentSuppliesBindingModel { SuppliesId = Supply.Id, ComponentId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
return Supply;
|
||||
}
|
||||
|
||||
private static void AssertElement(SuppliesViewModel? actual, Supplies expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
Assert.That(actual.ProductuionDate.Date, Is.EqualTo(expected.ProductuionDate.Date));
|
||||
});
|
||||
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, SuppliesBindingModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
Assert.That(actual.ProductuionDate.Date, Is.EqualTo(expected.ProductuionDate.Date));
|
||||
});
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using FurnitureAssemblyContracts.Enums;
|
||||
using FurnitureAssemblyContracts.ViewModels;
|
||||
using FurnitureAssemblyDatebase.Models;
|
||||
using FurnitureAssemblyTests.Infrastructure;
|
||||
using System.Net;
|
||||
|
||||
namespace FurnitureAssemblyTests.WebApiControllerTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class WarehouseControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
private string _componentId;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_componentId = FurnitureAssemblyDbContext.InsertComponentToDatabaseAndReturn().Id;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
FurnitureAssemblyDbContext.RemoveComponentsFromDatabase();
|
||||
FurnitureAssemblyDbContext.RemoveWarehousesFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAllWarehouses_WhenHaveRecords_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var warehouse = FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||
FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||
FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync("/api/warehouse");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<WarehouseViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(3));
|
||||
});
|
||||
AssertElement(data.First(x => x.Id == warehouse.Id), warehouse);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_WhenNoRecords_ShouldSuccess_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync("/api/warehouse");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<WarehouseViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(0));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var warehouse = FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/warehouse/{warehouse.Id}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<WarehouseViewModel>(response), warehouse);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
||||
{
|
||||
//Arrange
|
||||
FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/warehouse/{Guid.NewGuid()}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn(components: [(_componentId, 5)]);
|
||||
var warehouse = CreateModel(components: [(_componentId, 5)]);
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/warehouse", MakeContent(warehouse));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
AssertElement(FurnitureAssemblyDbContext.GetWarehouseFromDatabaseById(warehouse.Id!), warehouse);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/warehouse", MakeContent(string.Empty));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/warehouse", MakeContent(new { Data = "test", Position = 10 }));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var furnitureIdIncorrect = new WarehouseBindingModel { Id = "Id" };
|
||||
var furnitureWithNameIncorrect = new WarehouseBindingModel { Id = Guid.NewGuid().ToString(), Type = ComponentType.None };
|
||||
//Act
|
||||
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/warehouse", MakeContent(furnitureIdIncorrect));
|
||||
var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/warehouse", MakeContent(furnitureWithNameIncorrect));
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
||||
Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Type is incorrect");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||
var warehouse = CreateModel(components: [(_componentId, 5)]);
|
||||
FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn(warehouse.Id);
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/warehouse", MakeContent(warehouse));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
FurnitureAssemblyDbContext.ChangeTracker.Clear();
|
||||
AssertElement(FurnitureAssemblyDbContext.GetWarehouseFromDatabaseById(warehouse.Id!), warehouse);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var furniture = CreateModel();
|
||||
FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/warehouse", MakeContent(furniture));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/warehouse", MakeContent(string.Empty));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/warehouse", MakeContent(new { Data = "test", Position = 10 }));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var component = Guid.NewGuid().ToString();
|
||||
FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn(component);
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/warehouse/{component}");
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
Assert.That(FurnitureAssemblyDbContext.GetFurnitureFromDatabaseById(component), Is.Null);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
FurnitureAssemblyDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/warehouse/{Guid.NewGuid()}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/warehouse/id");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
private static WarehouseBindingModel CreateModel(string? id = null, ComponentType type = ComponentType.Panel, int count = 21, List<(string, int)>? components = null)
|
||||
{
|
||||
var warehouse = new WarehouseBindingModel() { Id = id ?? Guid.NewGuid().ToString(), Type = type, Count = count, Components = [] };
|
||||
if (components is not null)
|
||||
{
|
||||
foreach (var elem in components)
|
||||
{
|
||||
warehouse.Components.Add(new ComponentWarehouseBindingModel { WarehouseId = warehouse.Id, ComponentId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
return warehouse;
|
||||
}
|
||||
|
||||
private static void AssertElement(WarehouseViewModel? actual, Warehouse expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
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, WarehouseBindingModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
using AutoMapper;
|
||||
using FurnitureAssemblyContracts.AdapterContracts;
|
||||
using FurnitureAssemblyContracts.AdapterContracts.OperationResponses;
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContracts;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.ViewModels;
|
||||
|
||||
namespace FurnitureAssemblyWebApi.Adapters;
|
||||
|
||||
public class SuppliesAdapter : ISuppliesAdapter
|
||||
{
|
||||
private readonly ISuppliesBusinessLogicContract _suppliesBusinessLogicContract;
|
||||
private readonly ILogger<SuppliesAdapter> _logger;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public SuppliesAdapter(ISuppliesBusinessLogicContract suppliesBusinessLogicContract,
|
||||
ILogger<SuppliesAdapter> logger)
|
||||
{
|
||||
_suppliesBusinessLogicContract = suppliesBusinessLogicContract;
|
||||
_logger = logger;
|
||||
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<SuppliesBindingModel, SuppliesDataModel>();
|
||||
cfg.CreateMap<SuppliesDataModel, SuppliesViewModel>();
|
||||
cfg.CreateMap<ComponentSuppliesBindingModel, ComponentSuppliesDataModel>();
|
||||
cfg.CreateMap<ComponentSuppliesDataModel, ComponentSuppliesViewModel>();
|
||||
});
|
||||
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public SuppliesOperationResponse GetAllComponents()
|
||||
{
|
||||
try
|
||||
{
|
||||
return SuppliesOperationResponse.OK([.. _suppliesBusinessLogicContract.GetAllComponents().Select(x => _mapper.Map<SuppliesViewModel>(x))]);
|
||||
}
|
||||
catch (NullListException)
|
||||
{
|
||||
_logger.LogError("NullListException: The list of supplies is null");
|
||||
return SuppliesOperationResponse.NotFound("The list of supplies is not initialized");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return SuppliesOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return SuppliesOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public SuppliesOperationResponse GetComponentByData(string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
return SuppliesOperationResponse.OK(_mapper.Map<SuppliesViewModel>(_suppliesBusinessLogicContract.GetComponentByData(data)));
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: Data is null or empty");
|
||||
return SuppliesOperationResponse.BadRequest("Data is empty");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException: Component not found");
|
||||
return SuppliesOperationResponse.NotFound($"Component with data '{data}' not found");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return SuppliesOperationResponse.InternalServerError(
|
||||
$"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return SuppliesOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public SuppliesOperationResponse InsertComponent(SuppliesBindingModel componentDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_suppliesBusinessLogicContract.InsertComponent(_mapper.Map<SuppliesDataModel>(componentDataModel));
|
||||
return SuppliesOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: Component data is null");
|
||||
return SuppliesOperationResponse.BadRequest("Component data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException: Invalid component data");
|
||||
return SuppliesOperationResponse.BadRequest($"Invalid component data: {ex.Message}");
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementExistsException: Component already exists");
|
||||
return SuppliesOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return SuppliesOperationResponse.BadRequest(
|
||||
$"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return SuppliesOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public SuppliesOperationResponse UpdateComponent(SuppliesBindingModel componentModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_suppliesBusinessLogicContract.UpdateComponent(_mapper.Map<SuppliesDataModel>(componentModel));
|
||||
return SuppliesOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: Component data is null");
|
||||
return SuppliesOperationResponse.BadRequest("Component data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException: Invalid component data");
|
||||
return SuppliesOperationResponse.BadRequest($"Invalid component data: {ex.Message}");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException: Component not found");
|
||||
return SuppliesOperationResponse.BadRequest($"Component with id '{componentModel.Id}' not found");
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementExistsException: Component already exists");
|
||||
return SuppliesOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return SuppliesOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return SuppliesOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
using AutoMapper;
|
||||
using FurnitureAssemblyContracts.AdapterContracts;
|
||||
using FurnitureAssemblyContracts.AdapterContracts.OperationResponses;
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContracts;
|
||||
using FurnitureAssemblyContracts.DataModels;
|
||||
using FurnitureAssemblyContracts.Exceptions;
|
||||
using FurnitureAssemblyContracts.ViewModels;
|
||||
|
||||
namespace FurnitureAssemblyWebApi.Adapters;
|
||||
|
||||
public class WarehouseAdapter : IWarehouseAdapter
|
||||
{
|
||||
private readonly IWarehouseBusinessLogicContract _warehouseBusinessLogicContract;
|
||||
private readonly ILogger<WarehouseAdapter> _logger;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public WarehouseAdapter(IWarehouseBusinessLogicContract warehouseBusinessLogicContract, ILogger<WarehouseAdapter> logger)
|
||||
{
|
||||
_warehouseBusinessLogicContract = warehouseBusinessLogicContract;
|
||||
_logger = logger;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<WarehouseBindingModel, WarehouseDataModel>();
|
||||
cfg.CreateMap<WarehouseDataModel, WarehouseViewModel>();
|
||||
cfg.CreateMap<ComponentWarehouseBindingModel, ComponentWarehouseDataModel>();
|
||||
cfg.CreateMap<ComponentWarehouseDataModel, ComponentWarehouseViewModel>();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public WarehouseOperationResponse GetAllComponents()
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine(_warehouseBusinessLogicContract.GetAllComponents());
|
||||
return WarehouseOperationResponse.OK([.. _warehouseBusinessLogicContract.GetAllComponents().Select(x => _mapper.Map<WarehouseViewModel>(x))]);
|
||||
}
|
||||
catch (NullListException)
|
||||
{
|
||||
_logger.LogError("NullListException: The list of warehouse is null");
|
||||
return WarehouseOperationResponse.NotFound("The list of warehouse is not initialized");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return WarehouseOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return WarehouseOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public WarehouseOperationResponse GetComponentByData(string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
return WarehouseOperationResponse.OK(_mapper.Map<WarehouseViewModel>(_warehouseBusinessLogicContract.GetComponentByData(data)));
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: Data is null or empty");
|
||||
return WarehouseOperationResponse.BadRequest("Data is empty");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException: warehouse not found");
|
||||
return WarehouseOperationResponse.NotFound($"warehouse with data '{data}' not found");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return WarehouseOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return WarehouseOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public WarehouseOperationResponse InsertComponent(WarehouseBindingModel warehouseDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_warehouseBusinessLogicContract.InsertComponent(_mapper.Map<WarehouseDataModel>(warehouseDataModel));
|
||||
return WarehouseOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: warehouse data is null");
|
||||
return WarehouseOperationResponse.BadRequest("furniture data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException: Invalid warehouse data");
|
||||
return WarehouseOperationResponse.BadRequest($"Invalid warehouse data: {ex.Message}");
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementExistsException: warehouse already exists");
|
||||
return WarehouseOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return WarehouseOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return WarehouseOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public WarehouseOperationResponse UpdateComponent(WarehouseBindingModel warehouseDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_warehouseBusinessLogicContract.UpdateComponent(_mapper.Map<WarehouseDataModel>(warehouseDataModel));
|
||||
return WarehouseOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: warehouse data is null");
|
||||
return WarehouseOperationResponse.BadRequest("warehouse data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException: Invalid warehouse data");
|
||||
return WarehouseOperationResponse.BadRequest($"Invalid warehouse data: {ex.Message}");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException: warehouse not found");
|
||||
return WarehouseOperationResponse.BadRequest($"warehouse with id '{warehouseDataModel.Id}' not found");
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementExistsException: warehouse already exists");
|
||||
return WarehouseOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return WarehouseOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return WarehouseOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public WarehouseOperationResponse DeleteComponent(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
_warehouseBusinessLogicContract.DeleteComponent(id);
|
||||
return WarehouseOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: Id is null or empty");
|
||||
return WarehouseOperationResponse.BadRequest("Id is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException: Invalid id");
|
||||
return WarehouseOperationResponse.BadRequest($"Invalid id: {ex.Message}");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException: warehouse not found");
|
||||
return WarehouseOperationResponse.BadRequest($"furniture with id '{id}' not found");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return WarehouseOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return WarehouseOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using FurnitureAssemblyContracts.AdapterContracts;
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace FurnitureAssemblyWebApi.Controllers;
|
||||
|
||||
[Authorize]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Produces("application/json")]
|
||||
public class SuppliesController(ISuppliesAdapter adapter) : ControllerBase
|
||||
{
|
||||
private readonly ISuppliesAdapter _adapter = adapter;
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult GetAll()
|
||||
{
|
||||
return _adapter.GetAllComponents().GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpGet("{data}")]
|
||||
public IActionResult GetComponentByData(string data)
|
||||
{
|
||||
return _adapter.GetComponentByData(data).GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Register([FromBody] SuppliesBindingModel suppliesBindingModel)
|
||||
{
|
||||
return _adapter.InsertComponent(suppliesBindingModel).GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public IActionResult ChangeInfo([FromBody] SuppliesBindingModel suppliesBindingModel)
|
||||
{
|
||||
return _adapter.UpdateComponent(suppliesBindingModel).GetResponse(Request, Response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using FurnitureAssemblyContracts.AdapterContracts;
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace FurnitureAssemblyWebApi.Controllers;
|
||||
|
||||
[Authorize]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Produces("application/json")]
|
||||
public class WarehouseController(IWarehouseAdapter adapter) : ControllerBase
|
||||
{
|
||||
private readonly IWarehouseAdapter _adapter = adapter;
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult GetAll()
|
||||
{
|
||||
return _adapter.GetAllComponents().GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpGet("{data}")]
|
||||
public IActionResult GetComponentByData(string data)
|
||||
{
|
||||
return _adapter.GetComponentByData(data).GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Register([FromBody] WarehouseBindingModel warehouseBindingModel)
|
||||
{
|
||||
return _adapter.InsertComponent(warehouseBindingModel).GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public IActionResult ChangeInfo([FromBody] WarehouseBindingModel warehouseBindingModel)
|
||||
{
|
||||
return _adapter.UpdateComponent(warehouseBindingModel).GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public IActionResult DeleteComponent(string id)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
return BadRequest("Id is null or empty");
|
||||
|
||||
return _adapter.DeleteComponent(id).GetResponse(Request, Response);
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,8 @@ builder.Services.AddTransient<IManifacturingBusinessLogicContract, Manifacturing
|
||||
builder.Services.AddTransient<IPostBusinessLogicContract, PostBusinessLogicContract>();
|
||||
builder.Services.AddTransient<ISalaryBusinessLogicContract, SalaryBusinessLogicContract>();
|
||||
builder.Services.AddTransient<IWorkerBusinessLogicContract, WorkerBusinessLogicContract>();
|
||||
builder.Services.AddTransient<IWarehouseBusinessLogicContract, WarehouseBusinessLogicContract>();
|
||||
builder.Services.AddTransient<ISuppliesBusinessLogicContract, SuppliesBusinessLogicContract>();
|
||||
|
||||
builder.Services.AddTransient<FurnitureAssemblyDbContext>();
|
||||
builder.Services.AddTransient<IComponentStorageContract, ComponentStorageContract>();
|
||||
@@ -63,6 +65,8 @@ builder.Services.AddTransient<IManifacturingStorageContract, ManifacturingStorag
|
||||
builder.Services.AddTransient<IPostStorageContract, PostStorageContract>();
|
||||
builder.Services.AddTransient<ISalaryStorageContract, SalaryStorageContract>();
|
||||
builder.Services.AddTransient<IWorkerStorageContract, WorkerStorageContract>();
|
||||
builder.Services.AddTransient<IWarehouseStorageContract, WarehouseStorageContract>();
|
||||
builder.Services.AddTransient<ISuppliesStorageContract, SuppliesStorageContract>();
|
||||
|
||||
builder.Services.AddTransient<IPostAdapter, PostAdapter>();
|
||||
builder.Services.AddTransient<IWorkerAdapter, WorkerAdapter>();
|
||||
@@ -70,6 +74,8 @@ builder.Services.AddTransient<IComponentAdapter, ComponentAdapter>();
|
||||
builder.Services.AddTransient<IFurnitureAdapter, FurnitureAdapter>();
|
||||
builder.Services.AddTransient<IManifacturingFurnitureAdapter, ManifacturingFurnitureAdapter>();
|
||||
builder.Services.AddTransient<ISalaryAdapter, SalaryAdapter>();
|
||||
builder.Services.AddTransient<IWarehouseAdapter, WarehouseAdapter>();
|
||||
builder.Services.AddTransient<ISuppliesAdapter, SuppliesAdapter>();
|
||||
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
Reference in New Issue
Block a user