From 24107caa75085d2ba34498d8d7a1e604d77df9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D1=81=D0=B5=D0=BD=D0=B8=D1=8F?= Date: Fri, 7 Feb 2025 23:50:56 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=201=20-=20=D0=A3=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModels/ProductOnStorageDataModel.cs | 33 +++++++++ .../DataModels/ProductsInSupplyDataModel.cs | 32 +++++++++ .../DataModels/StorageDataModel.cs | 24 +++++++ .../DataModels/SupplyDataModel.cs | 33 +++++++++ .../ProductOnStorageDataModelTests.cs | 71 +++++++++++++++++++ .../ProductsInSupplyDataModelTests.cs | 70 ++++++++++++++++++ .../DataModelsTests/StorageDateModelTests.cs | 51 +++++++++++++ .../DataModelsTests/SupplyDataModelTests.cs | 60 ++++++++++++++++ 8 files changed, 374 insertions(+) create mode 100644 NorthBridge/NorthBridgeContract/DataModels/ProductOnStorageDataModel.cs create mode 100644 NorthBridge/NorthBridgeContract/DataModels/ProductsInSupplyDataModel.cs create mode 100644 NorthBridge/NorthBridgeContract/DataModels/StorageDataModel.cs create mode 100644 NorthBridge/NorthBridgeContract/DataModels/SupplyDataModel.cs create mode 100644 NorthBridge/NorthBridgeTest/DataModelsTests/ProductOnStorageDataModelTests.cs create mode 100644 NorthBridge/NorthBridgeTest/DataModelsTests/ProductsInSupplyDataModelTests.cs create mode 100644 NorthBridge/NorthBridgeTest/DataModelsTests/StorageDateModelTests.cs create mode 100644 NorthBridge/NorthBridgeTest/DataModelsTests/SupplyDataModelTests.cs diff --git a/NorthBridge/NorthBridgeContract/DataModels/ProductOnStorageDataModel.cs b/NorthBridge/NorthBridgeContract/DataModels/ProductOnStorageDataModel.cs new file mode 100644 index 0000000..623dc80 --- /dev/null +++ b/NorthBridge/NorthBridgeContract/DataModels/ProductOnStorageDataModel.cs @@ -0,0 +1,33 @@ +using NorthBridgeContract.Exceptions; +using NorthBridgeContract.Extensions; +using NorthBridgeContract.Infrastructure; + +namespace NorthBridgeContract.DataModels +{ + public class ProductOnStorageDataModel(string storageId, string productId, int count) : IValidation + { + public string StorageId { get; private set; } = storageId; + + public string ProductId { get; private set; } = productId; + + public int Count { get; private set; } = count; + + public void Validate() + { + if (StorageId.IsEmpty()) + throw new ValidationException("Field StorageId is empty"); + + if (!StorageId.IsGuid()) + throw new ValidationException("The value in the field StorageId is not a unique identifier"); + + if (ProductId.IsEmpty()) + throw new ValidationException("Field ProductId is empty"); + + if (!ProductId.IsGuid()) + throw new ValidationException("The value in the field ProductId is not a unique identifier"); + + if (Count <= 0) + throw new ValidationException("Field Count is less than or equal to 0"); + } + } +} diff --git a/NorthBridge/NorthBridgeContract/DataModels/ProductsInSupplyDataModel.cs b/NorthBridge/NorthBridgeContract/DataModels/ProductsInSupplyDataModel.cs new file mode 100644 index 0000000..bbd30be --- /dev/null +++ b/NorthBridge/NorthBridgeContract/DataModels/ProductsInSupplyDataModel.cs @@ -0,0 +1,32 @@ +using NorthBridgeContract.Exceptions; +using NorthBridgeContract.Extensions; + +namespace NorthBridgeContract.DataModels +{ + public class ProductsInSupplyDataModel(string supplyId, string productId, int count) + { + public string SupplyId { get; private set; } = supplyId; + + public string ProductId { get; private set; } = productId; + + public int Count { get; private set; } = count; + + public void Validate() + { + if (SupplyId.IsEmpty()) + throw new ValidationException("Field SupplyId is empty"); + + if (!SupplyId.IsGuid()) + throw new ValidationException("The value in the field SupplyId is not a unique identifier"); + + if (ProductId.IsEmpty()) + throw new ValidationException("Field ProductId is empty"); + + if (!ProductId.IsGuid()) + throw new ValidationException("The value in the field ProductId is not a unique identifier"); + + if (Count <= 0) + throw new ValidationException("Field Count is less than or equal to 0"); + } + } +} diff --git a/NorthBridge/NorthBridgeContract/DataModels/StorageDataModel.cs b/NorthBridge/NorthBridgeContract/DataModels/StorageDataModel.cs new file mode 100644 index 0000000..a92b3f6 --- /dev/null +++ b/NorthBridge/NorthBridgeContract/DataModels/StorageDataModel.cs @@ -0,0 +1,24 @@ +using NorthBridgeContract.Exceptions; +using NorthBridgeContract.Extensions; +using NorthBridgeContract.Infrastructure; + +namespace NorthBridgeContract.DataModels +{ + public class StorageDataModel(string id, string address) : IValidation + { + public string Id { get; private set; } = id; + public string Address { get; private set; } = address; + + 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 (Address.IsEmpty()) + throw new ValidationException("Field Address is empty"); + } + } +} diff --git a/NorthBridge/NorthBridgeContract/DataModels/SupplyDataModel.cs b/NorthBridge/NorthBridgeContract/DataModels/SupplyDataModel.cs new file mode 100644 index 0000000..2405b6b --- /dev/null +++ b/NorthBridge/NorthBridgeContract/DataModels/SupplyDataModel.cs @@ -0,0 +1,33 @@ +using NorthBridgeContract.Exceptions; +using NorthBridgeContract.Extensions; +using NorthBridgeContract.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NorthBridgeContract.DataModels +{ + public class SupplyDataModel(string id, string storageId, DateTime date) : IValidation + { + public string Id { get; private set; } = id; + public string StorageId { get; private set; } = storageId; + public DateTime Date { get; private set; } = date; + + public void Validate() + { + if (StorageId.IsEmpty()) + throw new ValidationException("Field StorageId is empty"); + + if (!StorageId.IsGuid()) + throw new ValidationException("The value in the field StorageId is not a unique identifier"); + + 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"); + } + } +} diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/ProductOnStorageDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/ProductOnStorageDataModelTests.cs new file mode 100644 index 0000000..f314bf0 --- /dev/null +++ b/NorthBridge/NorthBridgeTest/DataModelsTests/ProductOnStorageDataModelTests.cs @@ -0,0 +1,71 @@ +using NorthBridgeContract.DataModels; +using NorthBridgeContract.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NorthBridgeTest.DataModelsTests +{ + [TestFixture] + internal class ProductOnStorageDataModelTests + { + [Test] + public void StorageIdIsNullOrEmptyTest() + { + var productStorage = new ProductOnStorageDataModel(null, Guid.NewGuid().ToString(), 1); + Assert.That(() => productStorage.Validate(), Throws.TypeOf()); + productStorage = new ProductOnStorageDataModel(string.Empty, Guid.NewGuid().ToString(), 1); + Assert.That(() => productStorage.Validate(), Throws.TypeOf()); + } + + [Test] + public void StorageIdIsNotGuidTest() + { + var productStorage = new ProductOnStorageDataModel("id", Guid.NewGuid().ToString(), 1); + Assert.That(() => productStorage.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNullOrEmptyTest() + { + var productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), null, 1); + Assert.That(() => productStorage.Validate(), Throws.TypeOf()); + productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), string.Empty, 1); + Assert.That(() => productStorage.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNotGuidTest() + { + var productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), "id", 1); + Assert.That(() => productStorage.Validate(), Throws.TypeOf()); + } + + [Test] + public void CountIsLessThanOrEqualToZeroTest() + { + var productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0); + Assert.That(() => productStorage.Validate(), Throws.TypeOf()); + productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1); + Assert.That(() => productStorage.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsAreCorrectTest() + { + var storageId = Guid.NewGuid().ToString(); + var productId = Guid.NewGuid().ToString(); + var count = 5; + var productStorage = new ProductOnStorageDataModel(storageId, productId, count); + Assert.That(() => productStorage.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(productStorage.StorageId, Is.EqualTo(storageId)); + Assert.That(productStorage.ProductId, Is.EqualTo(productId)); + Assert.That(productStorage.Count, Is.EqualTo(count)); + }); + } + } +} diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/ProductsInSupplyDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/ProductsInSupplyDataModelTests.cs new file mode 100644 index 0000000..54448e3 --- /dev/null +++ b/NorthBridge/NorthBridgeTest/DataModelsTests/ProductsInSupplyDataModelTests.cs @@ -0,0 +1,70 @@ +using NorthBridgeContract.DataModels; +using NorthBridgeContract.Exceptions; + +namespace NorthBridgeTest.DataModelsTests +{ + [TestFixture] + internal class ProductsInSupplyDataModelTests + { + [Test] + public void SupplyIdIsNullOrEmptyTest() + { + var productSupply = CreateDataModel(null, Guid.NewGuid().ToString(), 1); + Assert.That(() => productSupply.Validate(), Throws.TypeOf()); + productSupply = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1); + Assert.That(() => productSupply.Validate(), Throws.TypeOf()); + } + + [Test] + public void SupplyIdIsNotGuidTest() + { + var productSupply = CreateDataModel("id", Guid.NewGuid().ToString(), 1); + Assert.That(() => productSupply.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNullOrEmptyTest() + { + var productSupply = CreateDataModel(Guid.NewGuid().ToString(), null, 1); + Assert.That(() => productSupply.Validate(), Throws.TypeOf()); + productSupply = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1); + Assert.That(() => productSupply.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNotGuidTest() + { + var productSupply = CreateDataModel(Guid.NewGuid().ToString(), "id", 1); + Assert.That(() => productSupply.Validate(), Throws.TypeOf()); + } + + [Test] + public void CountIsLessThanOrEqualToZeroTest() + { + var productSupply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0); + Assert.That(() => productSupply.Validate(), Throws.TypeOf()); + productSupply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1); + Assert.That(() => productSupply.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsAreCorrectTest() + { + var supplyId = Guid.NewGuid().ToString(); + var productId = Guid.NewGuid().ToString(); + var count = 5; + var productSupply = CreateDataModel(supplyId, productId, count); + Assert.That(() => productSupply.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(productSupply.SupplyId, Is.EqualTo(supplyId)); + Assert.That(productSupply.ProductId, Is.EqualTo(productId)); + Assert.That(productSupply.Count, Is.EqualTo(count)); + }); + } + + private static ProductsInSupplyDataModel CreateDataModel(string? supplyId, string? productId, int count) => + new(supplyId, productId, count); + } + +} diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/StorageDateModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/StorageDateModelTests.cs new file mode 100644 index 0000000..699bb79 --- /dev/null +++ b/NorthBridge/NorthBridgeTest/DataModelsTests/StorageDateModelTests.cs @@ -0,0 +1,51 @@ +using NorthBridgeContract.DataModels; +using NorthBridgeContract.Exceptions; + +namespace NorthBridgeTest.DataModelsTests +{ + [TestFixture] + internal class StorageDataModelTests + { + [Test] + public void IdIsNullOrEmptyTest() + { + var storage = CreateDataModel(null, "ул. Ленина, д. 10"); + Assert.That(() => storage.Validate(), Throws.TypeOf()); + storage = CreateDataModel(string.Empty, "ул. Ленина, д. 10"); + Assert.That(() => storage.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var storage = CreateDataModel("id", "ул. Ленина, д. 10"); + Assert.That(() => storage.Validate(), Throws.TypeOf()); + } + + [Test] + public void AddressIsNullOrEmptyTest() + { + var storage = CreateDataModel(Guid.NewGuid().ToString(), null); + Assert.That(() => storage.Validate(), Throws.TypeOf()); + storage = CreateDataModel(Guid.NewGuid().ToString(), string.Empty); + Assert.That(() => storage.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsAreCorrectTest() + { + var storageId = Guid.NewGuid().ToString(); + var address = "ул. Ленина, д. 10"; + var storage = CreateDataModel(storageId, address); + Assert.That(() => storage.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(storage.Id, Is.EqualTo(storageId)); + Assert.That(storage.Address, Is.EqualTo(address)); + }); + } + + private static StorageDataModel CreateDataModel(string? id, string? address) => + new(id, address); + } +} diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/SupplyDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/SupplyDataModelTests.cs new file mode 100644 index 0000000..8442ab8 --- /dev/null +++ b/NorthBridge/NorthBridgeTest/DataModelsTests/SupplyDataModelTests.cs @@ -0,0 +1,60 @@ +using NorthBridgeContract.DataModels; +using NorthBridgeContract.Exceptions; + +namespace NorthBridgeTest.DataModelsTests +{ + [TestFixture] + internal class SupplyDataModelTests + { + [Test] + public void IdIsNullOrEmptyTest() + { + var supply = CreateDataModel(null, Guid.NewGuid().ToString(), DateTime.Now); + Assert.That(() => supply.Validate(), Throws.TypeOf()); + supply = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), DateTime.Now); + Assert.That(() => supply.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var supply = CreateDataModel("id", Guid.NewGuid().ToString(), DateTime.Now); + Assert.That(() => supply.Validate(), Throws.TypeOf()); + } + + [Test] + public void StorageIdIsNullOrEmptyTest() + { + var supply = CreateDataModel(Guid.NewGuid().ToString(), null, DateTime.Now); + Assert.That(() => supply.Validate(), Throws.TypeOf()); + supply = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, DateTime.Now); + Assert.That(() => supply.Validate(), Throws.TypeOf()); + } + + [Test] + public void StorageIdIsNotGuidTest() + { + var supply = CreateDataModel(Guid.NewGuid().ToString(), "id", DateTime.Now); + Assert.That(() => supply.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsAreCorrectTest() + { + var supplyId = Guid.NewGuid().ToString(); + var storageId = Guid.NewGuid().ToString(); + var date = DateTime.Now; + var supply = CreateDataModel(supplyId, storageId, date); + Assert.That(() => supply.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(supply.Id, Is.EqualTo(supplyId)); + Assert.That(supply.StorageId, Is.EqualTo(storageId)); + Assert.That(supply.Date, Is.EqualTo(date)); + }); + } + + private static SupplyDataModel CreateDataModel(string? id, string? storageId, DateTime date) => + new(id, storageId, date); + } +} -- 2.25.1 From c9488b341a661dac7ba7d5757550a148b7f035aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D1=81=D0=B5=D0=BD=D0=B8=D1=8F?= Date: Sat, 8 Feb 2025 00:38:47 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=201.=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=D1=8B=20=D0=B8=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B4=D1=83=D0=BA=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModels/ComponentDataModel.cs | 45 ++++++ ...aModel.cs => ComponentHistoryDataModel.cs} | 12 +- .../DataModels/ComponentInProductDataModel.cs | 28 ++++ .../DataModels/ProductDataModel.cs | 34 +++-- .../{ProductType.cs => ComponentType.cs} | 2 +- .../ComponentDataModelTests.cs | 91 +++++++++++ .../ComponentHistoryDataModelTests.cs | 52 +++++++ .../ComponentInProductDataModelTests.cs | 63 ++++++++ .../DataModelsTests/ProductDataModelTests.cs | 142 ++++++++---------- .../ProductHistoryDataModelTests.cs | 52 ------- 10 files changed, 367 insertions(+), 154 deletions(-) create mode 100644 NorthBridge/NorthBridgeContract/DataModels/ComponentDataModel.cs rename NorthBridge/NorthBridgeContract/DataModels/{ProductHistoryDataModel.cs => ComponentHistoryDataModel.cs} (53%) create mode 100644 NorthBridge/NorthBridgeContract/DataModels/ComponentInProductDataModel.cs rename NorthBridge/NorthBridgeContract/Enums/{ProductType.cs => ComponentType.cs} (88%) create mode 100644 NorthBridge/NorthBridgeTest/DataModelsTests/ComponentDataModelTests.cs create mode 100644 NorthBridge/NorthBridgeTest/DataModelsTests/ComponentHistoryDataModelTests.cs create mode 100644 NorthBridge/NorthBridgeTest/DataModelsTests/ComponentInProductDataModelTests.cs delete mode 100644 NorthBridge/NorthBridgeTest/DataModelsTests/ProductHistoryDataModelTests.cs diff --git a/NorthBridge/NorthBridgeContract/DataModels/ComponentDataModel.cs b/NorthBridge/NorthBridgeContract/DataModels/ComponentDataModel.cs new file mode 100644 index 0000000..ecd4311 --- /dev/null +++ b/NorthBridge/NorthBridgeContract/DataModels/ComponentDataModel.cs @@ -0,0 +1,45 @@ +using NorthBridgeContract.Enums; +using NorthBridgeContract.Exceptions; +using NorthBridgeContract.Extensions; +using NorthBridgeContract.Infrastructure; + +namespace NorthBridgeContract.DataModels; + +public class ComponentDataModel(string id, string componentName, ComponentType componentType, string manufacturerId, double price, bool isDeleted) : IValidation +{ + public string Id { get; private set; } = id; + + public string ComponentName { get; private set; } = componentName; + + public ComponentType ComponentType { get; private set; } = componentType; + + public string ManufacturerId { get; private set; } = manufacturerId; + + public double Price { get; private set; } = price; + + public bool IsDeleted { get; private set; } = isDeleted; + + 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 (ComponentName.IsEmpty()) + throw new ValidationException("Field ProductName is empty"); + + if (ComponentType == ComponentType.None) + throw new ValidationException("Field ProductType is empty"); + + if (ManufacturerId.IsEmpty()) + throw new ValidationException("Field ManufacturerId is empty"); + + if (!ManufacturerId.IsGuid()) + throw new ValidationException("The value in the field ManufacturerId is not a unique identifier"); + + if (Price <= 0) + throw new ValidationException("Field Price is less than or equal to 0"); + } +} \ No newline at end of file diff --git a/NorthBridge/NorthBridgeContract/DataModels/ProductHistoryDataModel.cs b/NorthBridge/NorthBridgeContract/DataModels/ComponentHistoryDataModel.cs similarity index 53% rename from NorthBridge/NorthBridgeContract/DataModels/ProductHistoryDataModel.cs rename to NorthBridge/NorthBridgeContract/DataModels/ComponentHistoryDataModel.cs index c313f3b..bc0554c 100644 --- a/NorthBridge/NorthBridgeContract/DataModels/ProductHistoryDataModel.cs +++ b/NorthBridge/NorthBridgeContract/DataModels/ComponentHistoryDataModel.cs @@ -4,9 +4,9 @@ using NorthBridgeContract.Infrastructure; namespace NorthBridgeContract.DataModels; -public class ProductHistoryDataModel(string productId, double oldPrice) : IValidation +public class ComponentHistoryDataModel(string componentId, double oldPrice) : IValidation { - public string ProductId { get; private set; } = productId; + public string ComponentId { get; private set; } = componentId; public double OldPrice { get; private set; } = oldPrice; @@ -14,11 +14,11 @@ public class ProductHistoryDataModel(string productId, double oldPrice) : IValid public void Validate() { - if (ProductId.IsEmpty()) - throw new ValidationException("Field ProductId is empty"); + if (ComponentId.IsEmpty()) + throw new ValidationException("Field ComponentId is empty"); - if (!ProductId.IsGuid()) - throw new ValidationException("The value in the field ProductId is not a unique identifier"); + if (!ComponentId.IsGuid()) + throw new ValidationException("The value in the field ComponentId is not a unique identifier"); if (OldPrice <= 0) throw new ValidationException("Field OldPrice is less than or equal to 0"); diff --git a/NorthBridge/NorthBridgeContract/DataModels/ComponentInProductDataModel.cs b/NorthBridge/NorthBridgeContract/DataModels/ComponentInProductDataModel.cs new file mode 100644 index 0000000..8bfe648 --- /dev/null +++ b/NorthBridge/NorthBridgeContract/DataModels/ComponentInProductDataModel.cs @@ -0,0 +1,28 @@ +using NorthBridgeContract.Exceptions; +using NorthBridgeContract.Extensions; +using NorthBridgeContract.Infrastructure; + +namespace NorthBridgeContract.DataModels +{ + public class ComponentInProductDataModel(string componentId, string productId) : IValidation + { + public string ComponentId { get; private set; } = componentId; + public string ProductId { get; private set;} = productId; + + public void Validate() + { + 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 (ProductId.IsEmpty()) + throw new ValidationException("Field ProductId is empty"); + + if (!ProductId.IsGuid()) + throw new ValidationException("The value in the field ProductId is not a unique identifier"); + + } + } +} diff --git a/NorthBridge/NorthBridgeContract/DataModels/ProductDataModel.cs b/NorthBridge/NorthBridgeContract/DataModels/ProductDataModel.cs index f8be277..1f2b9f0 100644 --- a/NorthBridge/NorthBridgeContract/DataModels/ProductDataModel.cs +++ b/NorthBridge/NorthBridgeContract/DataModels/ProductDataModel.cs @@ -1,25 +1,24 @@ -using NorthBridgeContract.Enums; -using NorthBridgeContract.Exceptions; +using NorthBridgeContract.Exceptions; using NorthBridgeContract.Extensions; using NorthBridgeContract.Infrastructure; namespace NorthBridgeContract.DataModels; -public class ProductDataModel(string id, string productName, ProductType productType, string manufacturerId, double price, bool isDeleted) : IValidation +public class ProductDataModel(string id, string productName, string workerId, bool isDeleted, List components) : IValidation { public string Id { get; private set; } = id; public string ProductName { get; private set; } = productName; - public ProductType ProductType { get; private set; } = productType; - - public string ManufacturerId { get; private set; } = manufacturerId; - - public double Price { get; private set; } = price; + public string WorkerId { get; private set; } = workerId; public bool IsDeleted { get; private set; } = isDeleted; - public void Validate() + public List Components { get; private set; } = components; + + public double TotalPrice => Components.Sum(x => x.Price); + + public void Validate() { if (Id.IsEmpty()) throw new ValidationException("Field Id is empty"); @@ -30,16 +29,19 @@ public class ProductDataModel(string id, string productName, ProductType product if (ProductName.IsEmpty()) throw new ValidationException("Field ProductName is empty"); - if (ProductType == ProductType.None) - throw new ValidationException("Field ProductType is empty"); - - if (ManufacturerId.IsEmpty()) + if (WorkerId.IsEmpty()) throw new ValidationException("Field ManufacturerId is empty"); - if (!ManufacturerId.IsGuid()) + if (!WorkerId.IsGuid()) throw new ValidationException("The value in the field ManufacturerId is not a unique identifier"); - if (Price <= 0) + if (TotalPrice <= 0) throw new ValidationException("Field Price is less than or equal to 0"); - } + + if (TotalPrice != Components.Sum(x => x.Price)) + throw new ValidationException("The total price of the components does not match"); + + if (Components.Count == 0) + throw new ValidationException("At least 1 component is needed"); + } } \ No newline at end of file diff --git a/NorthBridge/NorthBridgeContract/Enums/ProductType.cs b/NorthBridge/NorthBridgeContract/Enums/ComponentType.cs similarity index 88% rename from NorthBridge/NorthBridgeContract/Enums/ProductType.cs rename to NorthBridge/NorthBridgeContract/Enums/ComponentType.cs index 004867d..6e683f3 100644 --- a/NorthBridge/NorthBridgeContract/Enums/ProductType.cs +++ b/NorthBridge/NorthBridgeContract/Enums/ComponentType.cs @@ -1,6 +1,6 @@ namespace NorthBridgeContract.Enums; -public enum ProductType +public enum ComponentType { None = 0, GraphicsCard = 1, diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentDataModelTests.cs new file mode 100644 index 0000000..14d0e39 --- /dev/null +++ b/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentDataModelTests.cs @@ -0,0 +1,91 @@ +using NorthBridgeContract.DataModels; +using NorthBridgeContract.Enums; +using NorthBridgeContract.Exceptions; + +namespace NorthBridgeTest.DataModelsTests; + +[TestFixture] +internal class ComponentDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var component = CreateDataModel(null, "name", ComponentType.Processor, Guid.NewGuid().ToString(), 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + component = CreateDataModel(string.Empty, "name", ComponentType.Processor, Guid.NewGuid().ToString(), 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var component = CreateDataModel("id", "name", ComponentType.Processor, Guid.NewGuid().ToString(), 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + } + + [Test] + public void ComponentNameIsEmptyTest() + { + var component = CreateDataModel(Guid.NewGuid().ToString(), null, ComponentType.Processor, Guid.NewGuid().ToString(), 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + component = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ComponentType.Processor, Guid.NewGuid().ToString(), 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + } + + [Test] + public void ComponentTypeIsNoneTest() + { + var component = CreateDataModel(Guid.NewGuid().ToString(), null, ComponentType.None, Guid.NewGuid().ToString(), 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + } + + [Test] + public void ManufacturerIdIsNullOrEmptyTest() + { + var component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Processor, null, 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Processor, string.Empty, 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + } + + [Test] + public void ManufacturerIdIsNotGuidTest() + { + var component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Processor, "manufacturerId", 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + } + + [Test] + public void PriceIsLessOrZeroTest() + { + var component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Processor, Guid.NewGuid().ToString(), 0, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Processor, Guid.NewGuid().ToString(), -10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var componentId = Guid.NewGuid().ToString(); + var componentName = "name"; + var componentType = ComponentType.Processor; + var componentManufacturerId = Guid.NewGuid().ToString(); + var componentPrice = 10; + var componentIsDelete = false; + var component = CreateDataModel(componentId, componentName, componentType, componentManufacturerId, componentPrice, componentIsDelete); + Assert.That(() => component.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(component.Id, Is.EqualTo(componentId)); + Assert.That(component.ComponentName, Is.EqualTo(componentName)); + Assert.That(component.ComponentType, Is.EqualTo(componentType)); + Assert.That(component.ManufacturerId, Is.EqualTo(componentManufacturerId)); + Assert.That(component.Price, Is.EqualTo(componentPrice)); + Assert.That(component.IsDeleted, Is.EqualTo(componentIsDelete)); + }); + } + + private static ComponentDataModel CreateDataModel(string? id, string? componentName, ComponentType componentType, string? manufacturerId, double price, bool isDeleted) => + new(id, componentName, componentType, manufacturerId, price, isDeleted); +} \ No newline at end of file diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentHistoryDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentHistoryDataModelTests.cs new file mode 100644 index 0000000..fb3b2c3 --- /dev/null +++ b/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentHistoryDataModelTests.cs @@ -0,0 +1,52 @@ +using NorthBridgeContract.DataModels; +using NorthBridgeContract.Exceptions; + +namespace NorthBridgeTest.DataModelsTests; + +[TestFixture] +internal class ComponentHistoryDataModelTests +{ + [Test] + public void ComponentIdIsNullOrEmptyTest() + { + var product = CreateDataModel(null, 10); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(string.Empty, 10); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ComponentIdIsNotGuidTest() + { + var product = CreateDataModel("id", 10); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void OldPriceIsLessOrZeroTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), 0); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), -10); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var componentId = Guid.NewGuid().ToString(); + var oldPrice = 10; + var productHistory = CreateDataModel(componentId, oldPrice); + Assert.That(() => productHistory.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(productHistory.ComponentId, Is.EqualTo(componentId)); + Assert.That(productHistory.OldPrice, Is.EqualTo(oldPrice)); + Assert.That(productHistory.ChangeDate, Is.LessThan(DateTime.UtcNow)); + Assert.That(productHistory.ChangeDate, Is.GreaterThan(DateTime.UtcNow.AddMinutes(-1))); + }); + } + + private static ComponentHistoryDataModel CreateDataModel(string? componentId, double oldPrice) => + new(componentId, oldPrice); +} \ No newline at end of file diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentInProductDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentInProductDataModelTests.cs new file mode 100644 index 0000000..2519d2b --- /dev/null +++ b/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentInProductDataModelTests.cs @@ -0,0 +1,63 @@ +using NorthBridgeContract.DataModels; +using NorthBridgeContract.Exceptions; +using NUnit.Framework; +using System; + +namespace NorthBridgeTest.DataModelsTests +{ + [TestFixture] + internal class ComponentInProductDataModelTests + { + [Test] + public void ComponentIdIsNullOrEmptyTest() + { + var componentInProduct = CreateDataModel(null, Guid.NewGuid().ToString()); + Assert.That(() => componentInProduct.Validate(), Throws.TypeOf()); + + componentInProduct = CreateDataModel(string.Empty, Guid.NewGuid().ToString()); + Assert.That(() => componentInProduct.Validate(), Throws.TypeOf()); + } + + [Test] + public void ComponentIdIsNotGuidTest() + { + var componentInProduct = CreateDataModel("invalid_id", Guid.NewGuid().ToString()); + Assert.That(() => componentInProduct.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNullOrEmptyTest() + { + var componentInProduct = CreateDataModel(Guid.NewGuid().ToString(), null); + Assert.That(() => componentInProduct.Validate(), Throws.TypeOf()); + + componentInProduct = CreateDataModel(Guid.NewGuid().ToString(), string.Empty); + Assert.That(() => componentInProduct.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNotGuidTest() + { + var componentInProduct = CreateDataModel(Guid.NewGuid().ToString(), "invalid_id"); + Assert.That(() => componentInProduct.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsAreCorrectTest() + { + var componentId = Guid.NewGuid().ToString(); + var productId = Guid.NewGuid().ToString(); + var componentInProduct = CreateDataModel(componentId, productId); + + Assert.That(() => componentInProduct.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(componentInProduct.ComponentId, Is.EqualTo(componentId)); + Assert.That(componentInProduct.ProductId, Is.EqualTo(productId)); + }); + } + + private static ComponentInProductDataModel CreateDataModel(string? componentId, string? productId) => + new(componentId, productId); + } +} diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/ProductDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/ProductDataModelTests.cs index 9d0085c..f255755 100644 --- a/NorthBridge/NorthBridgeTest/DataModelsTests/ProductDataModelTests.cs +++ b/NorthBridge/NorthBridgeTest/DataModelsTests/ProductDataModelTests.cs @@ -2,90 +2,74 @@ using NorthBridgeContract.Enums; using NorthBridgeContract.Exceptions; -namespace NorthBridgeTest.DataModelsTests; - -[TestFixture] -internal class ProductDataModelTests +namespace NorthBridgeTest.DataModelsTests { - [Test] - public void IdIsNullOrEmptyTest() - { - var product = CreateDataModel(null, "name", ProductType.Processor, Guid.NewGuid().ToString(), 10, false); - Assert.That(() => product.Validate(), Throws.TypeOf()); - product = CreateDataModel(string.Empty, "name", ProductType.Processor, Guid.NewGuid().ToString(), 10, false); - Assert.That(() => product.Validate(), Throws.TypeOf()); - } + [TestFixture] + internal class ProductDataModelTests + { + [Test] + public void IdIsNullOrEmptyTest() + { + var product = CreateDataModel(null, "Processor", Guid.NewGuid().ToString(), false, new List { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) }); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(string.Empty, "Processor", Guid.NewGuid().ToString(), false, new List { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) }); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } - [Test] - public void IdIsNotGuidTest() - { - var product = CreateDataModel("id", "name", ProductType.Processor, Guid.NewGuid().ToString(), 10, false); - Assert.That(() => product.Validate(), Throws.TypeOf()); - } + [Test] + public void IdIsNotGuidTest() + { + var product = CreateDataModel("id", "Processor", Guid.NewGuid().ToString(), false, new List { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) }); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } - [Test] - public void ProductNameIsEmptyTest() - { - var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.Processor, Guid.NewGuid().ToString(), 10, false); - Assert.That(() => product.Validate(), Throws.TypeOf()); - product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ProductType.Processor, Guid.NewGuid().ToString(), 10, false); - Assert.That(() => product.Validate(), Throws.TypeOf()); - } + [Test] + public void ProductNameIsNullOrEmptyTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), null, Guid.NewGuid().ToString(), false, new List { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) }); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), false, new List { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) }); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } - [Test] - public void ProductTypeIsNoneTest() - { - var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.None, Guid.NewGuid().ToString(), 10, false); - Assert.That(() => product.Validate(), Throws.TypeOf()); - } + [Test] + public void WorkerIdIsNullOrEmptyTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "Processor", null, false, new List { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) }); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), "Processor", string.Empty, false, new List { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) }); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } - [Test] - public void ManufacturerIdIsNullOrEmptyTest() - { - var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Processor, null, 10, false); - Assert.That(() => product.Validate(), Throws.TypeOf()); - product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Processor, string.Empty, 10, false); - Assert.That(() => product.Validate(), Throws.TypeOf()); - } + [Test] + public void WorkerIdIsNotGuidTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "Processor", "workerId", false, new List { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) }); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } - [Test] - public void ManufacturerIdIsNotGuidTest() - { - var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Processor, "manufacturerId", 10, false); - Assert.That(() => product.Validate(), Throws.TypeOf()); - } + [Test] + public void ComponentsAreNullOrEmptyTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "Processor", Guid.NewGuid().ToString(), false, null); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), "Processor", Guid.NewGuid().ToString(), false, new List()); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } - [Test] - public void PriceIsLessOrZeroTest() - { - var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Processor, Guid.NewGuid().ToString(), 0, false); - Assert.That(() => product.Validate(), Throws.TypeOf()); - product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Processor, Guid.NewGuid().ToString(), -10, false); - Assert.That(() => product.Validate(), Throws.TypeOf()); - } + [Test] + public void TotalPriceIsIncorrectTest() + { + var components = new List + { + new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false), + new ComponentDataModel(Guid.NewGuid().ToString(), "GPU", ComponentType.GraphicsCard, Guid.NewGuid().ToString(), 300, false) + }; + var product = CreateDataModel(Guid.NewGuid().ToString(), "Processor", Guid.NewGuid().ToString(), false, components); + Assert.That(product.TotalPrice, Is.EqualTo(500)); + } - [Test] - public void AllFieldsIsCorrectTest() - { - var productId = Guid.NewGuid().ToString(); - var productName = "name"; - var productType = ProductType.Processor; - var productManufacturerId = Guid.NewGuid().ToString(); - var productPrice = 10; - var productIsDelete = false; - var product = CreateDataModel(productId, productName, productType, productManufacturerId, productPrice, productIsDelete); - Assert.That(() => product.Validate(), Throws.Nothing); - Assert.Multiple(() => - { - Assert.That(product.Id, Is.EqualTo(productId)); - Assert.That(product.ProductName, Is.EqualTo(productName)); - Assert.That(product.ProductType, Is.EqualTo(productType)); - Assert.That(product.ManufacturerId, Is.EqualTo(productManufacturerId)); - Assert.That(product.Price, Is.EqualTo(productPrice)); - Assert.That(product.IsDeleted, Is.EqualTo(productIsDelete)); - }); - } - - private static ProductDataModel CreateDataModel(string? id, string? productName, ProductType productType, string? manufacturerId, double price, bool isDeleted) => - new(id, productName, productType, manufacturerId, price, isDeleted); -} \ No newline at end of file + private static ProductDataModel CreateDataModel(string? id, string? productName, string? workerId, bool isDeleted, List? components) => + new(id, productName, workerId, isDeleted, components ?? new List()); + } +} diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/ProductHistoryDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/ProductHistoryDataModelTests.cs deleted file mode 100644 index c4b7393..0000000 --- a/NorthBridge/NorthBridgeTest/DataModelsTests/ProductHistoryDataModelTests.cs +++ /dev/null @@ -1,52 +0,0 @@ -using NorthBridgeContract.DataModels; -using NorthBridgeContract.Exceptions; - -namespace NorthBridgeTest.DataModelsTests; - -[TestFixture] -internal class ProductHistoryDataModelTests -{ - [Test] - public void ProductIdIsNullOrEmptyTest() - { - var product = CreateDataModel(null, 10); - Assert.That(() => product.Validate(), Throws.TypeOf()); - product = CreateDataModel(string.Empty, 10); - Assert.That(() => product.Validate(), Throws.TypeOf()); - } - - [Test] - public void ProductIdIsNotGuidTest() - { - var product = CreateDataModel("id", 10); - Assert.That(() => product.Validate(), Throws.TypeOf()); - } - - [Test] - public void OldPriceIsLessOrZeroTest() - { - var product = CreateDataModel(Guid.NewGuid().ToString(), 0); - Assert.That(() => product.Validate(), Throws.TypeOf()); - product = CreateDataModel(Guid.NewGuid().ToString(), -10); - Assert.That(() => product.Validate(), Throws.TypeOf()); - } - - [Test] - public void AllFieldsIsCorrectTest() - { - var productId = Guid.NewGuid().ToString(); - var oldPrice = 10; - var productHistory = CreateDataModel(productId, oldPrice); - Assert.That(() => productHistory.Validate(), Throws.Nothing); - Assert.Multiple(() => - { - Assert.That(productHistory.ProductId, Is.EqualTo(productId)); - Assert.That(productHistory.OldPrice, Is.EqualTo(oldPrice)); - Assert.That(productHistory.ChangeDate, Is.LessThan(DateTime.UtcNow)); - Assert.That(productHistory.ChangeDate, Is.GreaterThan(DateTime.UtcNow.AddMinutes(-1))); - }); - } - - private static ProductHistoryDataModel CreateDataModel(string? productId, double oldPrice) => - new(productId, oldPrice); -} \ No newline at end of file -- 2.25.1