using NorthBridgeBusinessLogic.Implementations; using NorthBridgeContract.DataModels; using NorthBridgeContract.Exceptions; using NorthBridgeContract.StoragesContracts; using Microsoft.Extensions.Logging; using Moq; namespace NorthBridgeTest.BusinessLogicsContractsTests; [TestFixture] internal class ManufacturerBusinessLogicContractTests { private ManufacturerBusinessLogicContract _manufacturerBusinessLogicContract; private Mock _manufacturerStorageContract; [OneTimeSetUp] public void OneTimeSetUp() { _manufacturerStorageContract = new Mock(); _manufacturerBusinessLogicContract = new ManufacturerBusinessLogicContract(_manufacturerStorageContract.Object, new Mock().Object); } [SetUp] public void SetUp() { _manufacturerStorageContract.Reset(); } [Test] public void GetAllManufacturers_ReturnListOfRecords_Test() { //Arrange var listOriginal = new List() { new(Guid.NewGuid().ToString(), "name 1", null, null), new(Guid.NewGuid().ToString(), "name 2", null, null), new(Guid.NewGuid().ToString(), "name 3", null, null), }; _manufacturerStorageContract.Setup(x => x.GetList()).Returns(listOriginal); //Act var list = _manufacturerBusinessLogicContract.GetAllManufacturers(); //Assert Assert.That(list, Is.Not.Null); Assert.That(list, Is.EquivalentTo(listOriginal)); } [Test] public void GetAllManufacturers_ReturnEmptyList_Test() { //Arrange _manufacturerStorageContract.Setup(x => x.GetList()).Returns([]); //Act var list = _manufacturerBusinessLogicContract.GetAllManufacturers(); //Assert Assert.That(list, Is.Not.Null); Assert.That(list, Has.Count.EqualTo(0)); _manufacturerStorageContract.Verify(x => x.GetList(), Times.Once); } [Test] public void GetAllManufacturers_ReturnNull_ThrowException_Test() { //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.GetAllManufacturers(), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.GetList(), Times.Once); } [Test] public void GetAllManufacturers_StorageThrowError_ThrowException_Test() { //Arrange _manufacturerStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.GetAllManufacturers(), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.GetList(), Times.Once); } [Test] public void GetManufacturerByData_GetById_ReturnRecord_Test() { //Arrange var id = Guid.NewGuid().ToString(); var record = new ManufacturerDataModel(id, "name", null, null); _manufacturerStorageContract.Setup(x => x.GetElementById(id)).Returns(record); //Act var element = _manufacturerBusinessLogicContract.GetManufacturerByData(id); //Assert Assert.That(element, Is.Not.Null); Assert.That(element.Id, Is.EqualTo(id)); _manufacturerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); } [Test] public void GetManufacturerByData_GetByName_ReturnRecord_Test() { //Arrange var manufacturerName = "name"; var record = new ManufacturerDataModel(Guid.NewGuid().ToString(), manufacturerName, null, null); _manufacturerStorageContract.Setup(x => x.GetElementByName(manufacturerName)).Returns(record); //Act var element = _manufacturerBusinessLogicContract.GetManufacturerByData(manufacturerName); //Assert Assert.That(element, Is.Not.Null); Assert.That(element.ManufacturerName, Is.EqualTo(manufacturerName)); _manufacturerStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); } [Test] public void GetManufacturerByData_GetByOldName_ReturnRecord_Test() { //Arrange var manufacturerOldName = "name before"; var record = new ManufacturerDataModel(Guid.NewGuid().ToString(), "name", manufacturerOldName, null); _manufacturerStorageContract.Setup(x => x.GetElementByOldName(manufacturerOldName)).Returns(record); //Act var element = _manufacturerBusinessLogicContract.GetManufacturerByData(manufacturerOldName); //Assert Assert.That(element, Is.Not.Null); Assert.That(element.PrevManufacturerName, Is.EqualTo(manufacturerOldName)); _manufacturerStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); _manufacturerStorageContract.Verify(x => x.GetElementByOldName(It.IsAny()), Times.Once); } [Test] public void GetManufacturerByData_EmptyData_ThrowException_Test() { //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.GetManufacturerByData(null), Throws.TypeOf()); Assert.That(() => _manufacturerBusinessLogicContract.GetManufacturerByData(string.Empty), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); _manufacturerStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Never); _manufacturerStorageContract.Verify(x => x.GetElementByOldName(It.IsAny()), Times.Never); } [Test] public void GetManufacturerByData__GetById_NotFoundRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.GetManufacturerByData(Guid.NewGuid().ToString()), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); _manufacturerStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Never); _manufacturerStorageContract.Verify(x => x.GetElementByOldName(It.IsAny()), Times.Never); } [Test] public void GetManufacturerByData_GetByNameOrOldName_NotFoundRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.GetManufacturerByData("name"), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); _manufacturerStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); _manufacturerStorageContract.Verify(x => x.GetElementByOldName(It.IsAny()), Times.Once); } [Test] public void GetManufacturerByData_StorageThrowError_ThrowException_Test() { //Arrange _manufacturerStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); _manufacturerStorageContract.Setup(x => x.GetElementByName(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.GetManufacturerByData(Guid.NewGuid().ToString()), Throws.TypeOf()); Assert.That(() => _manufacturerBusinessLogicContract.GetManufacturerByData("name"), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); _manufacturerStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); _manufacturerStorageContract.Verify(x => x.GetElementByOldName(It.IsAny()), Times.Never); } [Test] public void GetManufacturerByData_GetByOldName_StorageThrowError_ThrowException_Test() { //Arrange _manufacturerStorageContract.Setup(x => x.GetElementByOldName(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.GetManufacturerByData("name"), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); _manufacturerStorageContract.Verify(x => x.GetElementByOldName(It.IsAny()), Times.Once); } [Test] public void InsertManufacturer_CorrectRecord_Test() { //Arrange var flag = false; var record = new ManufacturerDataModel(Guid.NewGuid().ToString(), "name", null, null); _manufacturerStorageContract.Setup(x => x.AddElement(It.IsAny())) .Callback((ManufacturerDataModel x) => { flag = x.Id == record.Id && x.ManufacturerName == record.ManufacturerName; }); //Act _manufacturerBusinessLogicContract.InsertManufacturer(record); //Assert _manufacturerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); Assert.That(flag); } [Test] public void InsertManufacturer_RecordWithExistsData_ThrowException_Test() { //Arrange _manufacturerStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.InsertManufacturer(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void InsertManufacturer_NullRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.InsertManufacturer(null), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void InsertManufacturer_InvalidRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.InsertManufacturer(new ManufacturerDataModel("id", "name", null, null)), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void InsertManufacturer_StorageThrowError_ThrowException_Test() { //Arrange _manufacturerStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.InsertManufacturer(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void UpdateManufacturer_CorrectRecord_Test() { //Arrange var flag = false; var record = new ManufacturerDataModel(Guid.NewGuid().ToString(), "name", null, null); _manufacturerStorageContract.Setup(x => x.UpdElement(It.IsAny())) .Callback((ManufacturerDataModel x) => { flag = x.Id == record.Id && x.ManufacturerName == record.ManufacturerName; }); //Act _manufacturerBusinessLogicContract.UpdateManufacturer(record); //Assert _manufacturerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); Assert.That(flag); } [Test] public void UpdateManufacturer_RecordWithIncorrectData_ThrowException_Test() { //Arrange _manufacturerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.UpdateManufacturer(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void UpdateManufacturer_RecordWithExistsData_ThrowException_Test() { //Arrange _manufacturerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.UpdateManufacturer(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void UpdateManufacturer_NullRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.UpdateManufacturer(null), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); } [Test] public void UpdateManufacturer_InvalidRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.UpdateManufacturer(new ManufacturerDataModel("id", "name", null, null)), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); } [Test] public void UpdateManufacturer_StorageThrowError_ThrowException_Test() { //Arrange _manufacturerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.UpdateManufacturer(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void DeleteManufacturer_CorrectRecord_Test() { //Arrange var id = Guid.NewGuid().ToString(); var flag = false; _manufacturerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); //Act _manufacturerBusinessLogicContract.DeleteManufacturer(id); //Assert _manufacturerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); Assert.That(flag); } [Test] public void DeleteManufacturer_RecordWithIncorrectId_ThrowException_Test() { //Arrange var id = Guid.NewGuid().ToString(); _manufacturerStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new ElementNotFoundException(id)); //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.DeleteManufacturer(Guid.NewGuid().ToString()), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); } [Test] public void DeleteManufacturer_IdIsNullOrEmpty_ThrowException_Test() { //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.DeleteManufacturer(null), Throws.TypeOf()); Assert.That(() => _manufacturerBusinessLogicContract.DeleteManufacturer(string.Empty), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); } [Test] public void DeleteManufacturer_IdIsNotGuid_ThrowException_Test() { //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.DeleteManufacturer("id"), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); } [Test] public void DeleteManufacturer_StorageThrowError_ThrowException_Test() { //Arrange _manufacturerStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _manufacturerBusinessLogicContract.DeleteManufacturer(Guid.NewGuid().ToString()), Throws.TypeOf()); _manufacturerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); } }