using Moq; using NUnit.Framework; using System; using System.Collections.Generic; using CandyHouseBase.DataModels; using CandyHouseBase.Exceptions; using CandyHouseBase.Interfaces.StoragesContracts; using Microsoft.Extensions.Logging; using CandyHouseBase.Implementations; namespace CandyHouseTests.BusinessLogicsContractsTests { [TestFixture] internal class IngredientBusinessLogicContractTests { private IngredientBusinessLogicContract _ingredientBusinessLogicContract; private Mock _ingredientStorageContact; [OneTimeSetUp] public void OneTimeSetUp() { _ingredientStorageContact = new Mock(); _ingredientBusinessLogicContract = new IngredientBusinessLogicContract(_ingredientStorageContact.Object, new Mock().Object); } [SetUp] public void SetUp() { _ingredientStorageContact.Reset(); } [Test] public void GetAllIngredients_ReturnsListOfRecords_Test() { // Arrange var ingredients = new List { new(Guid.NewGuid().ToString(), "Sugar", "kg", 100), new(Guid.NewGuid().ToString(), "Flour", "kg", 200), new(Guid.NewGuid().ToString(), "Cocoa", "kg", 50) }; _ingredientStorageContact.Setup(x => x.GetList()).Returns(ingredients); // Act var list = _ingredientBusinessLogicContract.GetAllIngredients(); // Assert Assert.That(list, Is.Not.Null); Assert.That(list, Is.EquivalentTo(ingredients)); } [Test] public void GetAllIngredients_ReturnsEmptyList_Test() { // Arrange _ingredientStorageContact.Setup(x => x.GetList()).Returns(new List()); // Act var list = _ingredientBusinessLogicContract.GetAllIngredients(); // Assert Assert.That(list, Is.Not.Null); Assert.That(list, Has.Count.EqualTo(0)); _ingredientStorageContact.Verify(x => x.GetList(), Times.Once); } [Test] public void GetAllIngredients_ReturnsNull_ThrowException_Test() { // Arrange _ingredientStorageContact.Setup(x => x.GetList()).Returns((List)null); // Act & Assert Assert.That(() => _ingredientBusinessLogicContract.GetAllIngredients(), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.GetList(), Times.Once); } [Test] public void GetAllIngredients_StorageThrowError_ThrowException_Test() { // Arrange _ingredientStorageContact.Setup(x => x.GetList()) .Throws(new StorageException(new InvalidOperationException())); // Act & Assert Assert.That(() => _ingredientBusinessLogicContract.GetAllIngredients(), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.GetList(), Times.Once); } [Test] public void GetIngredientById_ReturnsRecord_Test() { // Arrange var id = Guid.NewGuid().ToString(); var ingredient = new IngredientDataModel(id, "Sugar", "kg", 100); _ingredientStorageContact.Setup(x => x.GetElementById(id)).Returns(ingredient); // Act var element = _ingredientBusinessLogicContract.GetIngredientByData(id); // Assert Assert.That(element, Is.Not.Null); Assert.That(element.Id, Is.EqualTo(id)); _ingredientStorageContact.Verify(x => x.GetElementById(It.IsAny()), Times.Once); } [Test] public void GetIngredientByName_ReturnsRecord_Test() { // Arrange var name = "Sugar"; var ingredient = new IngredientDataModel(Guid.NewGuid().ToString(), name, "kg", 100); _ingredientStorageContact.Setup(x => x.GetElementByName(name)).Returns(ingredient); // Act var element = _ingredientBusinessLogicContract.GetIngredientByData(name); // Assert Assert.That(element, Is.Not.Null); Assert.That(element.Name, Is.EqualTo(name)); _ingredientStorageContact.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); } [Test] public void GetIngredientById_NotFoundRecord_ThrowException_Test() { // Arrange var id = "nonexistent"; _ingredientStorageContact.Setup(x => x.GetElementById(id)).Returns((IngredientDataModel)null); // Act & Assert Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(id), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.GetElementById(It.IsAny()), Times.Once); } [Test] public void GetIngredientByName_NotFoundRecord_ThrowException_Test() { // Arrange var name = "Nonexistent"; _ingredientStorageContact.Setup(x => x.GetElementByName(name)).Returns((IngredientDataModel)null); // Act & Assert Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(name), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); } [Test] public void GetIngredientById_StorageThrowError_ThrowException_Test() { // Arrange _ingredientStorageContact.Setup(x => x.GetElementById(It.IsAny())) .Throws(new StorageException(new InvalidOperationException())); // Act & Assert Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(Guid.NewGuid().ToString()), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.GetElementById(It.IsAny()), Times.Once); } [Test] public void GetIngredientByName_StorageThrowError_ThrowException_Test() { // Arrange _ingredientStorageContact.Setup(x => x.GetElementByName(It.IsAny())) .Throws(new StorageException(new InvalidOperationException())); // Act & Assert Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData("Sugar"), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); } [Test] public void AddIngredient_CorrectRecord_Test() { // Arrange var flag = false; var ingredient = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100); _ingredientStorageContact.Setup(x => x.AddElement(It.IsAny())) .Callback((IngredientDataModel x) => { flag = x.Id == ingredient.Id && x.Name == ingredient.Name && x.Cost == ingredient.Cost && x.Unit == ingredient.Unit; }); // Act _ingredientBusinessLogicContract.InsertIngredient(ingredient); // Assert _ingredientStorageContact.Verify(x => x.AddElement(It.IsAny()), Times.Once); Assert.That(flag); } [Test] public void AddIngredient_RecordWithExistsData_ThrowException_Test() { var ingredient = It.IsAny(); // Arrange _ingredientStorageContact.Setup(x => x.AddElement(ingredient)) .Throws(new ElementExistsException("ID", ingredient.Id)); // Act & Assert Assert.That( () => _ingredientBusinessLogicContract.InsertIngredient( new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void AddIngredient_NullRecord_ThrowException_Test() { // Act & Assert Assert.That(() => _ingredientBusinessLogicContract.InsertIngredient(null), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void AddIngredient_InvalidRecord_ThrowException_Test() { // Act & Assert Assert.That( () => _ingredientBusinessLogicContract.InsertIngredient(new IngredientDataModel("", null, "", -1)), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void AddIngredient_StorageThrowError_ThrowException_Test() { // Arrange _ingredientStorageContact.Setup(x => x.AddElement(It.IsAny())) .Throws(new StorageException(new InvalidOperationException())); // Act & Assert Assert.That( () => _ingredientBusinessLogicContract.InsertIngredient( new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void UpdateIngredient_CorrectRecord_Test() { // Arrange var flag = false; var ingredient = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100); _ingredientStorageContact.Setup(x => x.UpdateElement(It.IsAny())) .Callback((IngredientDataModel x) => { flag = x.Id == ingredient.Id && x.Name == ingredient.Name && x.Cost == ingredient.Cost && x.Unit == ingredient.Unit; }); // Act _ingredientBusinessLogicContract.UpdateIngredient(ingredient); // Assert _ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny()), Times.Once); Assert.That(flag); } [Test] public void UpdateIngredient_RecordNotFound_ThrowException_Test() { var ingredient = It.IsAny(); // Arrange _ingredientStorageContact.Setup(x => x.UpdateElement(ingredient)) .Throws(new ElementNotFoundException(ingredient.ToString())); // Act & Assert Assert.That( () => _ingredientBusinessLogicContract.UpdateIngredient( new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny()), Times.Once); } [Test] public void UpdateIngredient_NullRecord_ThrowException_Test() { // Act & Assert Assert.That(() => _ingredientBusinessLogicContract.UpdateIngredient(null), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny()), Times.Never); } [Test] public void UpdateIngredient_InvalidRecord_ThrowException_Test() { // Act & Assert Assert.That( () => _ingredientBusinessLogicContract.UpdateIngredient(new IngredientDataModel("", null, "", -1)), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny()), Times.Never); } [Test] public void UpdateIngredient_StorageThrowError_ThrowException_Test() { // Arrange _ingredientStorageContact.Setup(x => x.UpdateElement(It.IsAny())) .Throws(new StorageException(new InvalidOperationException())); // Act & Assert Assert.That( () => _ingredientBusinessLogicContract.UpdateIngredient( new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny()), Times.Once); } [Test] public void DeleteIngredient_CorrectId_Test() { // Arrange var id = "1"; var flag = false; _ingredientStorageContact.Setup(x => x.DeleteElement(It.Is((string x) => x == id))) .Callback(() => { flag = true; }); // Act _ingredientBusinessLogicContract.DeleteIngredient(id); // Assert _ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny()), Times.Once); Assert.That(flag); } [Test] public void DeleteIngredient_RecordNotFound_ThrowException_Test() { var ingredient = It.IsAny(); // Arrange _ingredientStorageContact.Setup(x => x.UpdateElement(ingredient)) .Throws(new ElementNotFoundException(ingredient.ToString())); // Act & Assert Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient("nonexistent"), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny()), Times.Once); } [Test] public void DeleteIngredient_NullOrEmptyId_ThrowException_Test() { // Act & Assert Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(null), Throws.TypeOf()); Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(string.Empty), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny()), Times.Never); } [Test] public void DeleteIngredient_InvalidId_ThrowException_Test() { // Act & Assert Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient("invalid"), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny()), Times.Never); } [Test] public void DeleteIngredient_StorageThrowError_ThrowException_Test() { // Arrange _ingredientStorageContact.Setup(x => x.DeleteElement(It.IsAny())) .Throws(new StorageException(new InvalidOperationException())); // Act & Assert Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(Guid.NewGuid().ToString()), Throws.TypeOf()); _ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny()), Times.Once); } } }